Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Year of day in localtime and timelocal don't match?

Reply
Thread Tools

Year of day in localtime and timelocal don't match?

 
 
J Moreno
Guest
Posts: n/a
 
      03-08-2006
As I read the documentation, the day of the year in both should be the
same, but when I run the script below, they differ by a day (I get the
day before the today's day as returned by localtime).

Is this a problem with the documentation, my reading of the
documentation, or a bug (i.e. do I have to check to see if the behavior
changes based upon version or something)?

###
use warnings;
use strict;

use Time::Local 'timelocal_nocheck';

$\ = "\n";

my $b;
my $day_of_year;

($b,$b,$b,$b,$b,$b,$b,$day_of_year) = localtime;

print "From localtime: " . $day_of_year;

($b,$b,$b,$b,$b,$b,$b,$day_of_year) = localtime timelocal_nocheck
(0,0,0,$day_of_year, 0, 0);

print "From localtime via timelocal_nocheck: " . $day_of_year;

###

Outputs:

From localtime: 66
From localtime via timelocal_nocheck 65



--
John Moreno
"Everything is futile." -- Marvin of Borg
 
Reply With Quote
 
 
 
 
Brian Wakem
Guest
Posts: n/a
 
      03-08-2006
J Moreno wrote:

> As I read the documentation, the day of the year in both should be the
> same, but when I run the script below, they differ by a day (I get the
> day before the today's day as returned by localtime).
>
> Is this a problem with the documentation, my reading of the
> documentation, or a bug (i.e. do I have to check to see if the behavior
> changes based upon version or something)?
>
> ###
> use warnings;
> use strict;
>
> use Time::Local 'timelocal_nocheck';
>
> $\ = "\n";
>
> my $b;
> my $day_of_year;
>
> ($b,$b,$b,$b,$b,$b,$b,$day_of_year) = localtime;
>
> print "From localtime: " . $day_of_year;
>
> ($b,$b,$b,$b,$b,$b,$b,$day_of_year) = localtime timelocal_nocheck
> (0,0,0,$day_of_year, 0, 0);


^^ year 0 is equivalent to 2000


> print "From localtime via timelocal_nocheck: " . $day_of_year;
>
> ###
>
> Outputs:
>
> From localtime: 66
> From localtime via timelocal_nocheck 65



2000 was a leap year.


--
Brian Wakem
Email: http://homepage.ntlworld.com/b.wakem/myemail.png
 
Reply With Quote
 
 
 
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      03-08-2006
Brian Wakem wrote:
> J Moreno wrote:
>>
>>($b,$b,$b,$b,$b,$b,$b,$day_of_year) = localtime timelocal_nocheck
>>(0,0,0,$day_of_year, 0, 0);

>
> ^^ year 0 is equivalent to 2000


True.

> 2000 was a leap year.


Not true.

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
 
Reply With Quote
 
Brian Wakem
Guest
Posts: n/a
 
      03-08-2006
Gunnar Hjalmarsson wrote:

> Brian Wakem wrote:
>> J Moreno wrote:
>>>
>>>($b,$b,$b,$b,$b,$b,$b,$day_of_year) = localtime timelocal_nocheck
>>>(0,0,0,$day_of_year, 0, 0);

>>
>> ^^ year 0 is equivalent to 2000

>
> True.
>
>> 2000 was a leap year.

>
> Not true.
>



According to the Gregorian calendar, which is the civil calendar in use
today, years evenly divisible by 4 are leap years, with the exception of
centurial years that are not evenly divisible by 400. Therefore, the years
1700, 1800, 1900 and 2100 are not leap years, but 1600, 2000, and 2400 are
leap years.



--
Brian Wakem
Email: http://homepage.ntlworld.com/b.wakem/myemail.png
 
Reply With Quote
 
Ch Lamprecht
Guest
Posts: n/a
 
      03-08-2006
Brian Wakem wrote:
> J Moreno wrote:
>
>
>>As I read the documentation, the day of the year in both should be the
>>same, but when I run the script below, they differ by a day (I get the
>>day before the today's day as returned by localtime).


>>###
>>use warnings;
>>use strict;
>>
>>use Time::Local 'timelocal_nocheck';
>>
>>$\ = "\n";
>>
>>my $b;
>>my $day_of_year;
>>
>>($b,$b,$b,$b,$b,$b,$b,$day_of_year) = localtime;
>>
>>print "From localtime: " . $day_of_year;
>>
>>($b,$b,$b,$b,$b,$b,$b,$day_of_year) = localtime timelocal_nocheck
>>(0,0,0,$day_of_year, 0, 0);


>
>>print "From localtime via timelocal_nocheck: " . $day_of_year;
>>


>
> 2000 was a leap year.
>

Hi,
did you check with 2001 ??

timelocal_nocheck expects a day_of_month as 4th param ranging from 1 to
something .
Localtime returns day_of_year ranging from 0 to 364.

Christoph


--

perl -e "print scalar reverse q/(E-Mail Removed)/"
 
Reply With Quote
 
A. Sinan Unur
Guest
Posts: n/a
 
      03-08-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) (J Moreno) wrote in news:1hbw3qt.mwmbb31c2hn32N%
(E-Mail Removed):

> As I read the documentation, the day of the year in both should be the
> same, but when I run the script below, they differ by a day (I get the
> day before the today's day as returned by localtime).
>
> Is this a problem with the documentation, my reading of the
> documentation, or a bug (i.e. do I have to check to see if the
> behavior changes based upon version or something)?
>

....

> ($b,$b,$b,$b,$b,$b,$b,$day_of_year) = localtime timelocal_nocheck
> (0,0,0,$day_of_year, 0, 0);


$time = timelocal($sec,$min,$hour,$mday,$mon,$year);

You are passing to this function:

$sec = 0
$min = 0
$hour = 0
$mday = $day_of_year
$mon = 0
$year = 0

$day_of_year is out of range for $mday. While the docs show examples of
such usage, it also warns:

Your mileage may vary when trying these with minutes and hours, and it
doesn't work at all for months.

I guess this falls under "your mileage may vary".


D:\Home\asu1\UseNet\clpmisc> cat time.pl
use warnings;
use strict;

use Time::Local 'timelocal';

my $d1 = (localtime)[7];

print "From localtime: $d1\n";

my $d2 = (localtime timelocal(localtime))[7];

print "From localtime via timelocal: $d2\n";


D:\Home\asu1\UseNet\clpmisc> time.pl
From localtime: 66
From localtime via timelocal: 66

--
A. Sinan Unur <(E-Mail Removed)>
(reverse each component and remove .invalid for email address)

comp.lang.perl.misc guidelines on the WWW:
http://mail.augustmail.com/~tadmc/cl...uidelines.html

 
Reply With Quote
 
usenet@DavidFilmer.com
Guest
Posts: n/a
 
      03-08-2006
J Moreno wrote:
> Is this a problem with...


FWIW, you can (and should) use more Perl-ish notation, such as:

my $day_of_year = (localtime)[7];

instead of

($b,$b,$b,$b,$b,$b,$b,$day_of_year) = localtime;

--
http://DavidFilmer.com

 
Reply With Quote
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      03-08-2006
J Moreno wrote:
> As I read the documentation, the day of the year in both should be the
> same, but when I run the script below, they differ by a day (I get the
> day before the today's day as returned by localtime).
>
> Is this a problem with the documentation, my reading of the
> documentation, or a bug (i.e. do I have to check to see if the behavior
> changes based upon version or something)?


<snip>

> ($b,$b,$b,$b,$b,$b,$b,$day_of_year) = localtime;
>
> print "From localtime: " . $day_of_year;


From "perldoc -f localtime":
"$yday is the day of the year, in the range 0..364 (or 0..365 in leap
years.)"

Hence you need to pass $day_of_year+1 to timelocal_nocheck() to make the
8:th element returned by localtime() equal $day_of_year.

$day_of_year =
(localtime timelocal_nocheck 0,0,0,$day_of_year+1,0,0)[7];
-----------------------------------------------------^^

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
 
Reply With Quote
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      03-08-2006
Brian Wakem wrote:
> Gunnar Hjalmarsson wrote:
>>Brian Wakem wrote:
>>>2000 was a leap year.

>>
>>Not true.

>
> According to the Gregorian calendar, which is the civil calendar in use
> today, years evenly divisible by 4 are leap years, with the exception of
> centurial years that are not evenly divisible by 400. Therefore, the years
> 1700, 1800, 1900 and 2100 are not leap years, but 1600, 2000, and 2400 are
> leap years.


I was wrong; sorry.

Nevertheless, that fact is unrelated to the OP's observation. See my
other post in this thread.

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
 
Reply With Quote
 
Brian Wakem
Guest
Posts: n/a
 
      03-08-2006
Gunnar Hjalmarsson wrote:

> Brian Wakem wrote:
>> Gunnar Hjalmarsson wrote:
>>>Brian Wakem wrote:
>>>>2000 was a leap year.
>>>
>>>Not true.

>>
>> According to the Gregorian calendar, which is the civil calendar in use
>> today, years evenly divisible by 4 are leap years, with the exception of
>> centurial years that are not evenly divisible by 400. Therefore, the
>> years 1700, 1800, 1900 and 2100 are not leap years, but 1600, 2000, and
>> 2400 are leap years.

>
> I was wrong; sorry.
>
> Nevertheless, that fact is unrelated to the OP's observation. See my
> other post in this thread.



No need to apologise.

I saw it was 1 day out, noticed the leap year and assumed that was the
answer, so I was wrong too.


--
Brian Wakem
Email: http://homepage.ntlworld.com/b.wakem/myemail.png
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Convert day of year to month, day Rob Redmon Ruby 7 04-04-2008 04:32 PM
problem using timelocal, localtime to discern d-o-w. John Perl Misc 2 03-22-2007 02:12 PM
Daylight Savings using timelocal() and localtime() Datamon Perl Misc 1 03-14-2007 10:06 PM
Timelocal input, post good Date filters (from - to) robic0@yahoo.com Perl Misc 16 11-16-2005 06:50 AM
Bug in timelocal? yocoyote Perl Misc 14 04-07-2005 06:58 PM



Advertisments