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 = 1113837449Seeing as how my $ENV{TZ} = US/Eastern
Thanks in advance,
John
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Parsing Dates
by crashtest (Curate) on May 07, 2005 at 00:07 UTC | |
Re: Parsing Dates
by Tanktalus (Canon) on May 06, 2005 at 23:42 UTC |