PDA

View Full Version : Get the difference between two dates


erum
02-07-2007, 09:27 PM
Hi,

What I need to do is take a date from an RSS feed (this part is done), which is in this format:

Sat, 01 Jul 2006 10:46:09 +0000

and match it against today's date and see if the difference is less than 24 hours.

I've researched a lot and this solution makes sense:
convert this date to epoch, get today's date in epoch and just find the diff for 24 hours in seconds.

Now the question is how do I get these two dates in epoch?

Newbie at Perl especially where this dates bit is concerned. So any and all help is appreciated.

Thanx!
Erum

KevinADC
02-08-2007, 07:23 AM
look at using the Time::Local module to conver the date to epoch time:

http://perldoc.perl.org/Time/Local.html

its a core module so should be installed already.

erum
02-08-2007, 09:21 AM
Sadly not much detail there on how to convert a string in this format:
Sat, 01 Jul 2006 10:46:09 +0000
to get it to work with the time::local function.

Also how do I know I got the correct result?

KevinADC
02-08-2007, 09:41 AM
how to convert the string is up to you to know. Modules can't teach you all there is to know in order to use them. But you can convert your string into variables using a regular expression.

my $date = 'Sat, 01 Jul 2006 10:46:09 +0000';
$date =~ tr/,:+//d;
my @date = split(/\s+/,$date);

The month needs to be in (0..11) format as explained in the module:


while the month is the number of months since January (0..11). This is consistent with the values returned from localtime() and gmtime().


where july = 6

besides that, @date has all the elements you need to plug into the timelocal() function to get the epoch time.

So see if you can figure out how to convert the named month into its corresponding number value. I'll check back in the morning, good-night.

KevinADC
02-09-2007, 07:40 AM
still working on it or....?

erum
02-18-2007, 08:43 PM
Thanx for all your help. Turns out this module does the trick:
Date::Parse