For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > April 2004 > Date problem









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Date problem
Alok Bhatt

2004-04-27, 12:23 am

Hi All,

I am facing this strange problem related to dates.
When I print the date using the system's date command,
it prints correctly. But when I do the same using
localtime, it shows the month as wrong (1 month
previous.

bash-2.03$ perl -e '@a=localtime; print "@a\n"'
18 4 8 26 3 104 1 116 0
^^^
bash-2.03$ perl -e '@a=localtime(time); print "@a\n"'
20 4 8 26 3 104 1 116 0
^^^
bash-2.03$ date
Mon Apr 26 08:04:23 GMT 2004
bash-2.03$

It shows the month as March (3) instead of April. What
could I be doing wrong?

Thanks in Advance




__________________________________
Do you Yahoo!?
Yahoo! Photos: High-quality 4x6 digital prints for 25¢
http://photos.yahoo.com/ph/print_splash
Beau E. Cox

2004-04-27, 12:23 am

On Monday 26 April 2004 03:07 am, Alok Bhatt wrote:
> Hi All,
>
> I am facing this strange problem related to dates.
> When I print the date using the system's date command,
> it prints correctly. But when I do the same using
> localtime, it shows the month as wrong (1 month
> previous.
>
> bash-2.03$ perl -e '@a=localtime; print "@a\n"'
> 18 4 8 26 3 104 1 116 0
> ^^^
> bash-2.03$ perl -e '@a=localtime(time); print "@a\n"'
> 20 4 8 26 3 104 1 116 0
> ^^^
> bash-2.03$ date
> Mon Apr 26 08:04:23 GMT 2004
> bash-2.03$
>
> It shows the month as March (3) instead of April. What
> could I be doing wrong?
>
> Thanks in Advance


Check the docs: the months go from 0..11 in localtime.

Aloha => Beau;

John W . Krahn

2004-04-27, 12:23 am

On Monday 26 April 2004 06:07, Alok Bhatt wrote:
>
> Hi All,


Hello,

> I am facing this strange problem related to dates.
> When I print the date using the system's date command,
> it prints correctly. But when I do the same using
> localtime, it shows the month as wrong (1 month
> previous.
>
> bash-2.03$ perl -e '@a=localtime; print "@a\n"'
> 18 4 8 26 3 104 1 116 0
> ^^^
> bash-2.03$ perl -e '@a=localtime(time); print "@a\n"'
> 20 4 8 26 3 104 1 116 0
> ^^^
> bash-2.03$ date
> Mon Apr 26 08:04:23 GMT 2004
> bash-2.03$
>
> It shows the month as March (3) instead of April. What
> could I be doing wrong?


localtime() and gmtime() return months in the range 0 to 11 so 3 is
April.

perldoc -f localtime
perldoc -f gmtime


John
--
use Perl;
program
fulfillment

Patrick Heffernan

2004-04-27, 12:23 am


On Mon, 26 Apr 2004 06:07:03 -0700 (PDT)
Alok Bhatt hammered out:

> I am facing this strange problem related to dates.
> When I print the date using the system's date command,
> it prints correctly. But when I do the same using
> localtime, it shows the month as wrong (1 month
> previous.
>
> bash-2.03$ perl -e '@a=localtime; print "@a\n"'
> 18 4 8 26 3 104 1 116 0
> ^^^

[Snip]
> bash-2.03$ date
> Mon Apr 26 08:04:23 GMT 2004
> bash-2.03$
>
> It shows the month as March (3) instead of April. What
> could I be doing wrong?


I'm only a raw beginner in Perl, so hopefully I'm giving you the right
information. I'm sure others in the list will point out my mistakes :)

localtime returns the following:

($sec,$min,$hour,$mday,$mon,$year,$wday,
$yday,$isdst)

with $mon, the month, being in the range (0 .. 11). So January is 0 to
December being 11. You aren't doing anything wrong, just misinterpreting
the information.

HTH

--
Patrick Heffernan * Over the years, I've developed my sense of
Maryvale Computer Service * deja vu so acutely that now I can remember
Warwick - QLD * things that *have* happened before ...
07 4667 3807 *

Alok Bhatt

2004-04-27, 12:23 am

Hi,

Thanks everyone for the answer.

So silly of me. Didnt check the docs :$


--- Patrick Heffernan <pheff@hypermax.net.au> wrote:
>
> On Mon, 26 Apr 2004 06:07:03 -0700 (PDT)
> Alok Bhatt hammered out:
>
> command,
> [Snip]
> What
>
> I'm only a raw beginner in Perl, so hopefully I'm
> giving you the right
> information. I'm sure others in the list will point
> out my mistakes :)
>
> localtime returns the following:
>
>

($sec,$min,$hour,$mday,$mon,$year,$wday,
$yday,$isdst)
>
> with $mon, the month, being in the range (0 .. 11).
> So January is 0 to
> December being 11. You aren't doing anything wrong,
> just misinterpreting
> the information.
>
> HTH
>
> --
> Patrick Heffernan * Over the years, I've
> developed my sense of
> Maryvale Computer Service * deja vu so acutely that
> now I can remember
> Warwick - QLD * things that *have*
> happened before ...
> 07 4667 3807 *
>
>
> --
> To unsubscribe, e-mail:
> beginners-unsubscribe@perl.org
> For additional commands, e-mail:
> beginners-help@perl.org
> <http://learn.perl.org/>
> <http://learn.perl.org/first-response>
>
>






__________________________________
Do you Yahoo!?
Yahoo! Photos: High-quality 4x6 digital prints for 25¢
http://photos.yahoo.com/ph/print_splash
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com