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

I'm working with SQL, and I need to get the date I parsed a given into a yyyy-mm-dd format into the database. I can get the date into the format I wish using the following subroutine (snipped from my code). Unfortunately, the month seems to be off by one. If I parse a file on February 13, 2001, It gets returned as "2001-01-13" which would be incorrect. I checked my system time, and everything is okay. Does anyone have any ideas of what I missed?
#!/usr/bin/perl use strict; my $date_parsed = get_date(); print "Date Parsed: $date_parsed\n"; # gets parse-time date for release date sub get_date { my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtim +e(time); $year += 1900; ($mon, $mday) = (sprintf('%02d', $mon), sprintf('%02d', $mday)); return $year."-".$mon."-".$mday; };
As I said, the script returns "2001-01-13" if I ran the script on February 13th. I guess I could always add one to the month -- but I want to make sure it isn't something I did wrong otherwise.

--Coplan

Replies are listed 'Best First'.
Re: month off by one?
by lemming (Priest) on Feb 14, 2001 at 01:28 UTC
    Month is counted from zero:
    January is month 0
    December is month 11
    This works well for arrays
    Readup on localtime to see details on what else may bite you later
Re: month off by one?
by japhy (Canon) on Feb 14, 2001 at 01:28 UTC
    The obvious answer would be to add one to the month, because as the Perl documentation for localtime() dictates, the numerical value of the month is from 0 to 11, not 1 to 12. How did you know to add 1900 to the year?

    japhy -- Perl and Regex Hacker
Re: month off by one?
by goldclaw (Scribe) on Feb 14, 2001 at 01:33 UTC
    Basically, anything that you might want to replace with a textual description(month, day of week, is there anyone else?) is 0 based, all the others are what they are supposed to be...

    This is so that you can say:

    my @days=qw(Mon Tue Wed Thu Fri Sat Sun); my $day=$days[(localtime())[6];

    GoldClaw

Re: month off by one?
by myocom (Deacon) on Feb 14, 2001 at 01:29 UTC

    I think perldoc -f localtime might be helpful here. The month returned from localtime comes from a 0-based array. Therefore, 0 = January, 1 = February, etc.

Re: month off by one?
by KM (Priest) on Feb 14, 2001 at 02:09 UTC
    Others have pointed you to the docs, which is were you should have gone. First, you could check what DB functions may be available to you to have it create your time string. Another alternative is to use POSIX (I like it, some don't).

    #!/usr/bin/perl -w use strict; use POSIX; my $date = POSIX::strftime('%Y-%m-%d', localtime);

    Cheers,
    KM

Re: month off by one?
by Anonymous Monk on Feb 14, 2001 at 03:23 UTC
    theres a great module called astro:time (or similar) it can do some cool stuff wiht dates and times, like validate dates, work out what tomorrow is (leap years etc.). It may be handy if your playing with dates.