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

This is a question re perl v. 5.6.1, compiled for PARISC1.1 on an HP9000. Using localtime in a scalar context returns the proper month (December). However, in a list context, the year and day are correct, but the month shows '11' for November? (The year returned is 101 which when added to 1900 is correct). Is this a bug or is there something the books are not telling me?
#! /usr/contrib/bin/perl use Time::Local; @tm = localtime; print "$tm[5] $tm[4] $tm[3]";

Replies are listed 'Best First'.
Re: A month behind the times...
by AidanLee (Chaplain) on Dec 22, 2001 at 00:18 UTC

    localtime returns months as 0..11 not 1..12. Check the localtime for the full spec. It also lists days of the week as 0..6

Re: A month behind the times...
by derby (Abbot) on Dec 22, 2001 at 00:42 UTC
    pppaulll,

    It sounds like you might be rolling your own date routines to create simple strings for use as filenames, log entries, etc. After having done that for years, I finally dropped it for Date::Manip. This is just one nice module - need a date string for today:

    #!/usr/bin/perl use Date::Manip; print UnixDate( ParseDate( "today" ), "%Y %m %d" ), "\n";

    no need to remember to add 100 or 1.
    correction: no need to remember to add 1900 or 1 (guess I've gotten use to Date::Manip all ready)

    -derby

Re: A month behind the times...
by bmcatt (Friar) on Dec 22, 2001 at 00:19 UTC
    Direct from perldoc -f localtime:

    All array elements are numeric, and come straight out of a struct tm. In particular this means that $mon has the range 0..11.

Re: A month behind the times...
by Chmrr (Vicar) on Dec 22, 2001 at 00:20 UTC

    A perusal of perldoc perlfunc might be in order. It comments on this issue explicitly.

    As a side note, you don't need to use Time::Local to use the localtime function. Time::Local provides the inverse of localtinme (that is, produces UNIX datestamp from a an array).

    perl -pe '"I lo*`+$^X$\"$]!$/"=~m%(.*)%s;$_=$1;y^`+*^e v^#$&V"+@( NO CARRIER'

Re: A month behind the times...
by Beatnik (Parson) on Dec 22, 2001 at 02:19 UTC
    All the above nodes seems to miss the fact that the 11th element on your platform is November. Anyway, it could be a bug... looking at your description, it most likely is but probably only on your platform. Check P5P and read up on perlbug. If necessary, submit the bugreport.

    Greetz
    Beatnik
    ... Quidquid perl dictum sit, altum viditur.
Re: A month behind the times...
by Sweeper (Pilgrim) on Dec 22, 2001 at 12:15 UTC
    This is not a bug, this is a wart inherited from C. (There are few of them, but they exist nevertheless.) The reason is that arrays indexes are 0-based, both in C and in Perl, and it was thought that the main use of the month as a number was to index an array of strings. But more often than not, the month as a number is used as a number like "2001-12-21" or "21/12/2001" (or "12/21/2001" in the US).

    Fortunately, with Perl 6, hopefully, we get rid of this hideous wart, as well as the Y19.1K pseudo-bug. See http://dev.perl.org/rfc/48.html.

Re: A month behind the times...
by davorg (Chancellor) on Dec 28, 2001 at 14:47 UTC

    I'd be interested to know which books you're looking in - as a the description of the return values from localtime is one of the tests I use to rate Perl books. Perhaps your books should be added to the list of Perl books to avoid :)

    --
    <http://www.dave.org.uk>

    "The first rule of Perl club is you do not talk about Perl club."
    -- Chip Salzenberg

Re: A month behind the times...
by Steve_p (Priest) on Dec 22, 2001 at 01:11 UTC
    localtime is behaving correctly. The values of month are 0-11 with January being 0 and 11 being December. As you noted, year is the number of years since 1900. Also, day of week has values 0-6 with 0 for Sunday and 6 for Saturday.