[sf-perl] Date manipulation question

Joe Brenner doom at kzsu.stanford.edu
Sat Jul 5 11:33:45 PDT 2008



Bill Moseley <moseley at hank.org>:

> Richard Reina wrote:

>> I have a date that is a string such as: Thu Jul 3 17:43:45 2008

>> and need to convert it to epoch seconds.  I am scowering my perl books
>> and the web but cannot find a function to help me do this.  Does
>> anyone know how this can be done?


The Perl Cookbook (Christiansen & Torkinkton) has an entire chapter on
"Dates and Times".

Recipe 3.7 (2nd ed) "Parsing Dates and Times from Strings" shows how to
do it with Time::Local (in the core library), and with a ParseDate
routine from Date::Manip.

> perldoc Time::Local is one way.
>
> But, I like to have all my dates in DateTime objects.  There's more than
> one parser that will work.
>
> $ perl -MDateTime::Format::DateParse -le 'print
> DateTime::Format::DateParse->parse_datetime( "Thu Jul 3 17:43:45
> 2008")->epoch'
> 1215132225
>
> $ perl -MDateTime::Format::HTTP -le 'print
> DateTime::Format::HTTP->parse_datetime( "Thu Jul 3 17:43:45 2008")->epoch'
> 1215107025

The "DateTime" system is a worthy attempt at doing saner, unified
handling, but when last I looked at it (a few years ago), it seemed
like it was going to have a hard time unseating the babel of existing
modules. The somewhat clunky system of routines supplied by Date::Calc
(implemented internally in C) are too popular.


Date::Parse from the TimeDate cpan distribution looks like it's made a
good attempt at understanding dates in different European languages.



More information about the SanFrancisco-pm mailing list