Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options

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?


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

Replies are listed 'Best First'.
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.
Re: Why does localtime() return 1900-$YEAR?
by marto (Archbishop) on Jan 31, 2011 at 11:59 UTC

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://885251]
Approved by marto
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (4)
As of 2018-06-18 03:05 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (107 votes). Check out past polls.