In the following, $time is the date in seconds since the UNIX epoch. I'm converting it back to a string (in the print statement) for testing purposes.
use Time::Local; %MONTHS_LOOKUP = ( Jan => 0, Feb => 1, Mar => 2, Apr => 3, #... ); my $date = 'Apr 29 13:54:10'; $date =~ /^(.{3}) (.{2}) (.{2}):(.{2}):(.{2})$/ or die("Bad date\n"); my $time = timelocal($5, $4, $3, $2, $MONTHS_LOOKUP{$1}, 2005); print(scalar(localtime($time)), "\n");
The following variation might be faster, because it doesn't check the validity of the inputs:
use Time::Local qw( timelocal_nocheck ); %MONTHS_LOOKUP = ( Jan => 0, Feb => 1, Mar => 2, Apr => 3, #... ); my $date = 'Apr 29 13:54:10'; $date =~ /^(.{3}) (.{2}) (.{2}):(.{2}):(.{2})$/ or die("Bad date\n"); my $time = timelocal_nocheck($5, $4, $3, $2, $MONTHS_LOOKUP{$1}, 2005); print(scalar(localtime($time)), "\n");
In reply to Re: Fast date parsing
by ikegami
in thread Fast date parsing
by jettero
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |