njcodewarrior has asked for the wisdom of the Perl Monks concerning the following question:

First off, I'd like to say that I'm relatively new to the world of Perl (~4 months) and find the open and sharing atmosphere of perlmonks.org extremely conducive to learning. Thanks to everyone who contributes to this wonderful site.

Now for the business at hand:

I'm trying to parse a date of the form:
Mon Apr 18 15:17:29 2005
and converting it to unix time (epoch seconds) using Time::Local. Here is the code I'm using:

#!/usr/bin/perl -w use strict; use Time::Local; my %months = ( 'Jan' => 0, 'Feb' => 1, 'Mar' => 2, 'Apr' => 3, 'May' => 4, 'Jun' => 5, 'Jul' => 6, 'Aug' => 7, 'Sep' => 8, 'Oct' => 9, 'Nov' => 10, 'Dec' => 11, ); my ($wday, $mon, $mday, $hr, $min, $sec, $year); my $date = 'Mon Apr 18 15:17:29 2005'; if (($wday, $mon, $mday, $hr, $min, $sec, $year) = $date =~ /(\w+)\s+( +\w+)\s+(\d+)\s(\d+):(\d+):(\d+)\s+(\d+)/) { foreach my $key (keys %months) { $mon =~ s/$key/$months{$key}/i; } my $local_epoch = timelocal($sec, $min, $hr, $mday, $mon, $year); my $gmt_epoch = timegm($sec, $min, $hr, $mday, $mon, $year); }

Here's what is returned:

$local_epoch = 1113851849
$gmt_epoch = 1113837449
Seeing as how my $ENV{TZ} = US/Eastern
I'm wondering why the number of seconds returned for $local_epoch is larger than that returned for $gmt_epoch? During daylight saving time (EDT), the difference between GMT and local time is -4 hours (ie: we're 4 hours behind GMT time) on the East Coast.

Shouldn't the value $gmt_epoch be larger than that of $local_epoch?

Thanks in advance,
John

Replies are listed 'Best First'.
Re: Parsing Dates
by crashtest (Curate) on May 07, 2005 at 00:07 UTC
    Instead of trying to parse dates by hand, which can get to be really tedious (believe me, I've tried), a better idea is to make use of one of the date-parsing modules available on CPAN. I regularly use Date::Parse:
    #!/usr/bin/perl -w use Date::Parse; my $time = str2time("Mon Apr 18 15:17:29 2005 EDT"); print "That date is ", localtime($time). " as PDT.\n"; print "That date is ", gmtime($time). " as GMT.\n"; __END__ That date is Mon Apr 18 12:17:29 2005 as PDT. That date is Mon Apr 18 19:17:29 2005 as GMT.
    Another alternative is Date::Manip, which I haven't used but is fairly popular (AFAIK). There is also the DateTime Project. Unless performance is critical and you know your data well, you should probably stick to one of these solutions, and not reinvent the date-parsing wheel.

Re: Parsing Dates
by Tanktalus (Canon) on May 06, 2005 at 23:42 UTC

    You're looking at the whole thing a bit backwards. First off, you're feading in "local" times into both timelocal and timegm. That is, 15:17:29 local time for the local time zone, and 15:17:29 local time for the GMT time zone. And the former is definitely later than the latter since your time zone is later than GMT - you get to 15:17:29 much later than GMT does.

    Then you also have to take into account that the conversion to epoch is always using GMT time zone for the epoch. So the numbers are relative to the GMT time zone's beginning time.

    Hope that helps.