Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

How to convert time stamp to unix time ?

by bh_perl (Monk)
on Feb 03, 2010 at 02:17 UTC ( [id://821065]=perlquestion: print w/replies, xml ) Need Help??

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

Hi..
Could some body help me how to convert time stamo to unix time using perl.

Below are some example of the time stamp format:-
100117104030 (YYMMDDHHMISS format)

Replies are listed 'Best First'.
Re: How to convert time stamp to unix time ?
by jettero (Monsignor) on Feb 03, 2010 at 02:27 UTC
    If you know the format, grind away on it a bit and feed it to Time::Local.

    -Paul

Re: How to convert time stamp to unix time ?
by gmargo (Hermit) on Feb 03, 2010 at 03:41 UTC

      The regex
          /^(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})/
      could surely be shortened to
          m{ \d{2} }xmsg

      >perl -wMstrict -le "my $stamp = '100117104030'; my ($YY, $MM, $DD, $hh, $mm, $ss) = $stamp =~ m{ \d{2} }xmsg; print $stamp; print qq{$YY $MM $DD $hh $mm $ss}; " 100117104030 10 01 17 10 40 30

        What are the "m" and the "s" for. :)

        And in this case,
           m{ \d{2} }xmsg

        could be
           /(\d\d)/g

Re: How to convert time stamp to unix time ?
by CountZero (Bishop) on Feb 03, 2010 at 07:20 UTC
    In the FAQ from Date::Time: How Do I Convert between Epoch Times and DateTime Objects?

    It can be as easy as:

    use DateTime; $dt = DateTime->new( year => 2010, month => 2, day => 3, hour => 8, minute => 15, second => 0, nanosecond => 0, time_zone => 'UTC', ); print $dt->epoch;
    Output: 1265184900

    Parsing your input string is left as an exercise for the reader.

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

Re: How to convert time stamp to unix time ?
by Anonymous Monk on Feb 03, 2010 at 04:07 UTC

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (1)
As of 2024-04-25 00:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found