Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Why does localtime() return $YEAR-1900?

by szabgab (Priest)
on Jan 31, 2011 at 11:40 UTC ( #885251=perlquestion: print w/ replies, xml ) Need Help??
szabgab has asked for the wisdom of the Perl Monks concerning the following question:

field 5 returned by localtime() in list context is the year. In order to get back the date one needs to write $year + 1900.
   ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);

Does anyone know why did Larry make it that way?

Update

Title corrected thanks to Re: Why does localtime() return 1900-$YEAR?

Comment on Why does localtime() return $YEAR-1900?
Re: Why does localtime() return 1900-$YEAR?
by marto (Chancellor) on Jan 31, 2011 at 11:59 UTC
Re: Why does localtime() return 1900-$YEAR?
by bart (Canon) on Jan 31, 2011 at 12:00 UTC
    It's $year-1900, not 1900-$year.

    And it's a fix for an old custom that started a long time ago (70s - 80s) when they thought "2-digit years should be enough for any date".

    So when 2000 came along, they had a problem. Replacing 2 digit years with years since 1900 was the easiest, backward compatible fix.

      Further, you need 11 bits to specify 1999, while only 7 are necessary for 99. Believe it or not, being able to to save 4 bits a record (or structure) was a big deal back in the '70s...

      Yes, I'm a dinosaur, as I've been programming since 1972...

        Perl was first released in 1987. The memory issue was much less important by that time and I am quite sure people were already aware of the bug 2000.

        Though that's an interesting other question. When did people start to talk about "bug 2000"?

Re: Why does localtime() return $YEAR-1900?
by cdarke (Prior) on Jan 31, 2011 at 13:29 UTC
    This is often called Broken-down time and is used in a C structure called struct tm. Perl is written in C, so it is not surprising that it uses it.
    Neither is Perl alone in that, many other languages use this format.
    I guess Larry used it because everyone would expect that format and is familiar with it.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (10)
As of 2014-11-21 19:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My preferred Perl binaries come from:














    Results (114 votes), past polls