Re: Is this a bug in localtime
by Abigail other posts by this author
Aug 29 2008 3:56AM messages near this date
Is this a bug in localtime
|
Smoke [5.11.0] 34234 FAIL(F) MSWin32 WinXP/.Net SP3 (x86/2 cpu)
On Fri, Aug 29, 2008 at 04:02:33PM +0530, Pankaj Singh wrote:
> Hi All,
>
> I was trying to form timeStamp from localtime function in perl.
>
> Perl version : v5.8.8 built for i386-linux-thread-multi
>
> Output of date command
> --------------------------------------
>
> Fri Aug 29 15:56:14 IST 2008
>
> The code I wrote in perl
> ------------------------------------
>
> my ($sec, $min, $hour, $day, $month, $year ) = ( localtime ) [ 0, 1, 2,
> 3, 4, 5 ];
>
> print "$year $month $day $hour $min \n";
>
> printf("%04d-%02d-%02d %02d:%02d\n",$year+1900,$month,$day,$hour, $min);
>
> print "\n";
> @timeData = localtime(time);
> print join(' ', @timeData);
>
>
> The output was
> ----------------------
> 108 7 29 15 53
> 2008-07-29 15:53
> 10 53 15 29 7 108 5 241 0
>
> But i was expecting
> Something like
> 2008-08-29 15:53
This is not a bug, and it's documented behaviour.
Just as localtime() returns 1900 based years, it returns months 0 based.
So, January is month 0, February is month 1, etc. Thus, where you add
1900 to the year, you should add 1 to the month.
Abigail
Thread:
Pankaj Singh
Abigail
|