http://www.perlmonks.org?node_id=885251

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?

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 (Cardinal) on Jan 31, 2011 at 11:59 UTC