# Localtime returning wrong month

#### Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

•  Subject
• Author
• Posted on
(\$sec,\$min,\$hour,\$mday,\$mon,\$year,\$wday,\$yday,\$isdst)
= localtime(time);

print "\n\$sec,\$min,\$hour,\$mday,\$mon,\$year,\$wday,\$yday,\$isdst \n";

When I run this today. It gives me 9 for the month (\$mon) instead of 10.
Why is that?

Output : 56,0,15,17,9,104,0,290,1

Date command on the Unix commandline returns Sun Oct 17 14:58:58 EDT 2004

## Re: Localtime returning wrong month

>> On Sun, 17 Oct 2004 15:01:07 -0400,

> (\$sec,\$min,\$hour,\$mday,\$mon,\$year,\$wday,\$yday,\$isdst) =
> localtime(time);

> print "\n\$sec,\$min,\$hour,\$mday,\$mon,\$year,\$wday,\$yday,\$isdst
> \n";

> When I run this today. It gives me 9 for the month (\$mon)
> instead of 10.

perldoc -f localtime.

What's the range of the month value?

Note there are much friendlier ways of doing date
manipulation.  What were you trying to do?

hth
t

## Re: Localtime returning wrong month

> (\$sec,\$min,\$hour,\$mday,\$mon,\$year,\$wday,\$yday,\$isdst)
>                                              = localtime(time);
>
> print "\n\$sec,\$min,\$hour,\$mday,\$mon,\$year,\$wday,\$yday,\$isdst \n";
>
> When I run this today. It gives me 9 for the month (\$mon) instead of 10.
> Why is that?

If you were expecting to get a value for October, that is the correct
return value for month. According to the man page for the function,
month ranges from 0 to 11.  Think of the index to an array of months .
.. .

>
> Output : 56,0,15,17,9,104,0,290,1
>
> Date command on the Unix commandline returns Sun Oct 17 14:58:58 EDT 2004

## Re: Localtime returning wrong month

> (\$sec,\$min,\$hour,\$mday,\$mon,\$year,\$wday,\$yday,\$isdst)
>                                              = localtime(time);
>
> print "\n\$sec,\$min,\$hour,\$mday,\$mon,\$year,\$wday,\$yday,\$isdst \n";
>
> When I run this today. It gives me 9 for the month (\$mon) instead of 10.
> Why is that?
>
> Output : 56,0,15,17,9,104,0,290,1
>
> Date command on the Unix commandline returns Sun Oct 17 14:58:58 EDT 2004

Well, this is actually how the documentation says it should be.
In programming the 0th is often the 1st... If you read the
documentation carefully (perldoc perlfunc) is says:

(\$sec,\$min,\$hour,\$mday,\$mon,\$year,\$wday,\$yday,\$isdst)
= localtime(time);

.....and \$mon is the month itself, in the range 0..11
with 0 indicating January and 11 indicating December

Probably not ideal, but its not Perl's fault, i think, because it just
uses an underlaying operation system call. However, it facilitates:

print "Month: ".[qw/Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov
Dec/]->[(localtime())[4]]."\n";