Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Bug in Time::Local?

by spacewarp (Pilgrim)
on Jul 02, 2004 at 18:45 UTC ( #371479=perlquestion: print w/ replies, xml ) Need Help??
spacewarp has asked for the wisdom of the Perl Monks concerning the following question:

Can someone please explain to me what's happening here? (Running perl 5.6.1)
{9} % perl use Time::Local; $epoch = timelocal(0, 0, 0, 25, 11, 48); print "$epoch\n"; Can't handle date (0, 0, 0, 25, 11, 148) at - line 2 {10} %
I'm trying to convert December 25, 1948 to epoch seconds. The man page claims that Time::Local can handle dates back to 1900, but it doesn't seem so in this case. And why on earth is it converting "48" to "148"?? Any help would be greatly appreciated.

Spacewarp

DISCLAIMER:
Use of this advanced computing technology does not imply an endorsement
of Western industrial civilization.

Comment on Bug in Time::Local?
Download Code
Re: Bug in Time::Local?
by Happy-the-monk (Monsignor) on Jul 02, 2004 at 18:56 UTC

    The man page claims that Time::Local can handle dates back to 1900

    It also claims to be buggy, you see that when reading on. And what you experienced is a bug, see this:

    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.   ...   Whenever possible, use an absolute four digit year instead.

    So however I try, 48 must be 2048 (or 148 in the counting of the   localtime()   builtin function).

    Update:
    Using the   "absolute four digit year instead"   works though.
    Update2:
    (except for dates > ~2038 ... uh!)

    Cheers, Sören

      I admit it, I only read the 'known bugs' section. Thanks.

      Spacewarp

      DISCLAIMER:
      Use of this advanced computing technology does not imply an endorsement
      of Western industrial civilization.
Re: Bug in Time::Local?
by eserte (Deacon) on Jul 02, 2004 at 18:58 UTC
    perl5.8.x has a different error message:
    Cannot handle date (0, 0, 0, 25, 11, 2048) at - line 2
    So you have probably to use "1948" instead of "48".
Re: Bug in Time::Local?
by idsfa (Vicar) on Jul 02, 2004 at 18:59 UTC

    From the docs:

    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.

    Last time I checked, 2004 - 1948 > 50, so it is assuming that you mean 2048 (stored internally to perl as year 148). As the docs also note:

    Please note, however, that the range of dates that can be actually be handled depends on the size of an integer (time_t) on a given platform. Currently, this is 32 bits for most systems, yielding an approximate range from Dec 1901 to Jan 2038.

    And 2048 > 2038. So you might want to do as the docs suggest and stick with a four digit year.


    If anyone needs me I'll be in the Angry Dome.
Re: Bug in Time::Local?
by Zaxo (Archbishop) on Jul 02, 2004 at 19:03 UTC

    Use the full year,

    $ perl -MTime::Local -e'print timelocal(0, 0, 0, 25, 11, 1948),$/' -663274800 $
    Note that the Time::Local docs warn that not all OS support negative epoch times.

    Update: fglock, I'm in EDT TZ 5 hours behind GMT in November.

    After Compline,
    Zaxo

      I've got different results - there is a difference of 5 hours from yours. I'm trying to figure out why.

      perl -MDateTime -e ' print DateTime->new(year=>1970)->epoch, $/ ' 0 perl -MDateTime -e ' print DateTime->new(year=>1948,month=>12,day=>25) +->epoch, $/ ' -663292800 perl -e 'print +( 663292800 - 663274800 )/3600 , $/ ' 5
Re: Bug in Time::Local?
by bart (Canon) on Jul 03, 2004 at 13:34 UTC
    I'm trying to convert December 25, 1948 to epoch seconds.
    Unless you have a Mac, epoch time is usually around Jan 1st 1970. You generally can't use negative epoch seconds, so dates before 1970 are out. A pre-OSX Mac can go down to 1904 — because that is its epoch.

    Try one of the wider range date related modules, such as one using the Julian date. You can find a nice comprehensive list here.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (11)
As of 2014-08-29 20:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (288 votes), past polls