Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Date to epoch

by akagrawal3 (Beadle)
on Jul 19, 2012 at 07:17 UTC ( [id://982596]=perlquestion: print w/replies, xml ) Need Help??

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

Hi Monks, I am facing a little difficulty in converting a date of a specific format to epoch time I want to convert date of this format 2106-02-27 06:28:15 to epoch time. And then from epoch time to date in same format as shown above. Your help is highly appreciated. Thanks

Replies are listed 'Best First'.
Re: Date to epoch
by Utilitarian (Vicar) on Jul 19, 2012 at 07:38 UTC
    the mktime function in the POSIX module is your friend here.
    ~/$ perl -MPOSIX -E 'say POSIX::mktime(15, 28, 06, 27, 02, 206);' 4299114495 ~/$ perl -E 'say scalar localtime (4299114495);' Sat Mar 27 06:28:15 2106
    The important thing to note is that the year value is $year - 1900 (but of course you are going to read the POSIX pod now anyway in order to see how strftime works to create ISO date strings ;) )

    print "Good ",qw(night morning afternoon evening)[(localtime)[2]/6]," fellow monks."
Re: Date to epoch
by Marshall (Canon) on Jul 19, 2012 at 10:48 UTC
    The normal Perl functions cannot handle this year of 2106 because:

    On perl versions older than 5.12.0, the range of dates that can be actually be handled depends on the size of time_t (usually a signed 32 bit integer) on the given platform. Currently, this is 32 bits for most systems, yielding an approximate range from Dec 1901 to Jan 2038. This integer is the number of seconds from the "epoch" or arbitrary start time/date (plus or minus).

    There are a number of modules on CPAN that can handle dates greater or less than the normal Unix epoch. These are good for internal program calculations.
    You might find: UNIX Time interesting.

    Since the "epoch" date is arbitrary, I would not use this integer for date/time interchange between systems and I would not assume that the other system uses 64 bits vs 32 bits.

      Oh... Thanks for this info... This might help me in making my concept clear.
        Instead of a 32 bit int describing the combined year and time, you could make two ints: $year and $time. Or perhaps better: look in CPAN for time/date modules that can describe this concept with a more complex data structures.

        Doing "date/time" math can be more complex than you might think. For a small example, this year (2012) - we had a "leap second" - WOW - what the heck does that mean?.

        I didn't mean to make a pun. But think about this and what might happen in 2106?

      So here we go again ... welcome to the Y2038 problem. That’s only 26 years away now.   Plenty of time to say the sky is falling... again.

      Plenty of time to write the books ... and to make the tin-foil hats.

      P.S.:   I actually collected “Y2K books” at that time, knowing that they’d be gone in an instant ...

        from local Time

        Strictly speaking, the year should be specified in a form consistent with localtime(), i.e. the offset from 1900. In order to make the interpretation of the year easier for humans, however, who are more accustomed to seeing years as two-digit or four-digit values, the following conventions are followed

        Years greater than 999 are interpreted as being the actual year, rather than the offset from 1900. Thus, 1964 would indicate the year Martin Luther King won the Nobel prize, not the year 3864.

        Years in the range 100..999 are interpreted as offset from 1900, so that 112 indicates 2012. This rule also applies to years less than zero (but see note below regarding date range).

        Years in the range 0..99 are interpreted as shorthand for years in the rolling "current century," defined as 50 years on either side of the current year. Thus, today, in 1999, 0 would refer to 2000, and 45 to 2045, but 55 would refer to 1955. Twenty years from now, 55 would instead refer to 2055. This is messy, but matches the way people currently think about two digit dates. Whenever possible, use an absolute four digit year instead.

        The scheme above allows interpretation of a wide range of dates, particularly if 4-digit years are used.

        I have a 6 Mhz, pre-production PC with a Quantum "Hard Card" => This is free to "good home".
        This is a part of the history of the PC. Contact me if you are interested.

        Computer are faster and have more storage than the Y2K models.
        Most folks are aware of this issue and use 4 digits for the year and DB's can handle that.

Re: Date to epoch
by Anonymous Monk on Jul 19, 2012 at 07:56 UTC

Log In?
Username:
Password:

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

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

    No recent polls found