Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Can Date::Manip parse a unix timestamp ?

by intuited (Novice)
on May 05, 2010 at 01:47 UTC ( [id://838427]=perlquestion: print w/replies, xml ) Need Help??

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

I've been using this for a while, I find it useful for generating RFC-3339-compliant timestamps.

However I'm at a bit of a loss for a way to get it to parse dates like

Thu Jan 21 17:13:27 2010 -0400

These can be parsed by the str2time() function in Date::Parse, which returns a Unix timestamp (ie seconds since epoch). However there's no readily apparent way to enter a Unix timestamp into Date::Manip's function set. This in itself is a pretty serious flaw in the module.. unless I'm missing something?

Replies are listed 'Best First'.
Re: Can Date::Manip parse a unix timestamp?
by ambrus (Abbot) on May 05, 2010 at 08:37 UTC

    For unix timestamps, just prepend "epoch ". This is well documented.

    $ perl -wE 'use Date::Manip; say UnixDate("epoch 1264108407", "%O\n")' + 2010-01-21T22:13:27

    If you have Date::Manip 6, you can also parse that format with Date::Manip directly, though not with the default parsing rules, you have to give the format explicitly.

    $ perl -wE 'use Date::Manip 6.0; my $d = Date::Manip::Date->new; $d->p +arse_format("%a %b %d %T %Y %z", "Thu Jan 21 17:13:27 2010 -0400"); s +ay $d->printf("%O");' 2010-01-21T17:13:27

      Just to put in my opinion... I'd like to see Date::Manip able to parse the date "Thu Jan 21 17:13:27 2010 -0400" directly without having to resort to the parse_format function... so I'll add it to my todo list. The parse_format function is good for catching formats I hadn't anticipated, but this one is common enough I should be able to parse it directly.

      It's an unusual format in that the time portion and date portion are intermingled. Date::Manip 6 parses the time and date portions separately, so this format is not a trivial date format to add unfortunately.

        For reference, the date format "%a %b %d %T %Y" (without timezone) came up again in a later question Perl date calculation and formatting. Date::Manip can parse such dates since version 6.31 (2012-03-01).

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://838427]
Approved by GrandFather
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (4)
As of 2024-04-19 06:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found