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

Hi,

This is certainly odd behavior as I would expect this to work..

===============================
#!/usr/bin/perl use strict; use warnings; my ($day, $month, $year) = (localtime)[3..5]; $year += 1900; my $date = "$year-$month-$day"; print "$date\n"; $date = localtime; print "$date\n";
===============================

produces:
2004-4-16

Sun May 16 22:27:20 2004

$ date
Sun May 16 22:19:52 CDT 2004

Now, according to Perl 5.8.0 localtime, this should be printing "2004-5-16". Perl 5.8.3 on Mandrake Linux 10.0.

Any ideas?

Jason L. Froebe

No one has seen what you have seen, and until that happens, we're all going to think that you're nuts. - Jack O'Neil, Stargate SG-1

Replies are listed 'Best First'.
Re: Weird Time: localtime shows wrong month
by broquaint (Abbot) on May 17, 2004 at 03:43 UTC
    Look more closely at the docs with regards to $month
    $mon is the month itself, in the range 0..11 with 0 indicating January and 11 indicating December
    So your code should look like this
    my ($day, $month, $year) = (localtime)[3..5]; $year += 1900; ## increase $month to get desired output $month++; my $date = "$year-$month-$day"; print "$date\n"; $date = localtime; print "$date\n"; __output__ 2004-5-17 Mon May 17 10:41:08 2004
    HTH

    _________
    broquaint

      THANKS!

      I knew it was something horribly simple... never get pulled into cleaning the house in the middle of coding.. your brain gets clean slated.

      Jason L. Froebe

      No one has seen what you have seen, and until that happens, we're all going to think that you're nuts. - Jack O'Neil, Stargate SG-1

Re: Weird Time: localtime shows wrong month
by chiburashka (Initiate) on May 17, 2004 at 03:41 UTC
    appearently $month is a part of an array of month names and starts with the index of '0', so add this string before line nomber 8 :  $month++;