Home > Archive > PERL Beginners > August 2006 > Time::Local let me faint
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 |
Time::Local let me faint
|
|
| Practical Perl 2006-08-29, 9:57 pm |
| Hello,lists,
Please see these two lines' output:
[$ perl -Mstrict -MTime::Local -le 'print timelocal(0,0,0,31,8,2006)'
Day '31' out of range 1..30 at -e line 1
$ perl -Mstrict -MTime::Local -le 'print timelocal(0,0,0,31,7,2006)'
1156953600
I translate the time of '2006-7-31 00:00:00' to unix timestamp,it's
successful.
But when I translate the time of '2006-8-31 00:00:00' to unix timestamp,it
said '31 out of range'.
I'm so faint that August doesn't have 31th day?Please tell me why this
happen and how to resolve it.
Thank you very much.
| |
| Lawrence Statton XE1/N1GAK 2006-08-29, 9:57 pm |
|
Because SEPTEMBER only has thirty days.
0 - January; 1 - February; 2 - March; ...
--L
| |
| Adriano Ferreira 2006-08-29, 9:57 pm |
| On 8/29/06, Practical Perl <practicalperl@gmail.com> wrote:
> But when I translate the time of '2006-8-31 00:00:00' to unix timestamp,it
> said '31 out of range'.
> I'm so faint that August doesn't have 31th day?Please tell me why this
> happen and how to resolve it.
from "perldoc Time::Local"
It is worth drawing particular attention to the expected ranges for the
values provided. The value for the day of the month is the actual day
(ie 1..31), while the month is the number of months since January
(0..11). This is consistent with the values returned from localtime()
and gmtime().
That means
$ perl -Mstrict -MTime::Local -le 'print timelocal(0,0,0,31,8,2006)'
Day '31' out of range 1..30 at -e line 1
is trying to get "31/Sep/2006" which does not exist
$ perl -Mstrict -MTime::Local -le 'print timelocal(0,0,0,31,7,2006)'
1156953600
is getting "31/Aug/2006" which is alright.
Regards,
Adriano Ferreira.
| |
| Practical Perl 2006-08-29, 9:57 pm |
| Thank you,:-)
For the intuition I treated '8' as August in Time::Local's method...A
low-level mistake.
2006/8/30, Lawrence Statton XE1/N1GAK <lawrence@cluon.com>:
>
>
> Because SEPTEMBER only has thirty days.
>
> 0 - January; 1 - February; 2 - March; ...
>
> --L
>
>
> --
> 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>
>
>
>
| |
| Flemming Greve Skovengaard 2006-08-29, 9:57 pm |
| Practical Perl wrote:
> Hello,lists,
>
> Please see these two lines' output:
>
> [$ perl -Mstrict -MTime::Local -le 'print timelocal(0,0,0,31,8,2006)'
> Day '31' out of range 1..30 at -e line 1
>
> $ perl -Mstrict -MTime::Local -le 'print timelocal(0,0,0,31,7,2006)'
> 1156953600
>
>
> I translate the time of '2006-7-31 00:00:00' to unix timestamp,it's
> successful.
> But when I translate the time of '2006-8-31 00:00:00' to unix timestamp,it
> said '31 out of range'.
> I'm so faint that August doesn't have 31th day?Please tell me why this
> happen and how to resolve it.
> Thank you very much.
>
I believe it is because the months are 0-indexed (0-11), so
timelocal(0,0,0,31,8,2006) is 2006-7-31 *not* 2006-8-31.
It also say so in the documentation.
perldoc Time::Local
Hope it helps.
--
Flemming Greve Skovengaard The killer's breed or the Demon's seed,
a.k.a Greven, TuxPower The glamour, the fortune, the pain,
<dsl58893@vip.cybercity.dk> Go to war again, blood is freedom's stain,
4011.74 BogoMIPS Don't you pray for my soul anymore.
| |
| John W. Krahn 2006-08-29, 9:57 pm |
| Practical Perl wrote:
> Hello,lists,
Hello,
> Please see these two lines' output:
>
> [$ perl -Mstrict -MTime::Local -le 'print timelocal(0,0,0,31,8,2006)'
> Day '31' out of range 1..30 at -e line 1
>
> $ perl -Mstrict -MTime::Local -le 'print timelocal(0,0,0,31,7,2006)'
> 1156953600
>
>
> I translate the time of '2006-7-31 00:00:00' to unix timestamp,it's
> successful.
> But when I translate the time of '2006-8-31 00:00:00' to unix timestamp,it
> said '31 out of range'.
> I'm so faint that August doesn't have 31th day?Please tell me why this
> happen and how to resolve it.
localtime EXPR
Converts a time as returned by the time function to a 9-element
list with the time analyzed for the local time zone. Typically
used as follows:
# 0 1 2 3 4 5 6 7 8
($sec,$min,$hour,$mday,$mon,$year,$wday,
$yday,$isdst) =
localtime(time);
All list elements are numeric, and come straight out of the C
‘struct tm’. $sec, $min, and $hour are the seconds, minutes, and
hours of the specified time. $mday is the day of the month, and
$mon is the month itself, in the range 0..11 with 0 indicating
January and 11 indicating December. $year is the number of years
since 1900. That is, $year is 123 in year 2023. $wday is the day
of the w , with 0 indicating Sunday and 3 indicating Wednesday.
$yday is the day of the year, in the range 0..364 (or 0..365 in
leap years.) $isdst is true if the specified time occurs during
daylight savings time, false otherwise.
So if you want to translate 31 August 2006 you have to subtract one from the
month and 1900 from the year:
$ perl -Mstrict -MTime::Local -le 'print timelocal(0,0,0,31,7,106)'
1157007600
John
--
use Perl;
program
fulfillment
| |
| Jeff Pang 2006-08-29, 9:57 pm |
|
>
>So if you want to translate 31 August 2006 you have to subtract one from the
>month and 1900 from the year:
>
>$ perl -Mstrict -MTime::Local -le 'print timelocal(0,0,0,31,7,106)'
>1157007600
John,
For the instance described by you,both '2006' and '106' are right.
$ perl -Mstrict -MTime::Local -le 'print timelocal(0,0,0,31,7,106)'
1156953600
$ perl -Mstrict -MTime::Local -le 'print timelocal(0,0,0,31,7,2006)'
1156953600
--
Jeff Pang
NetEase AntiSpam Team
http://corp.netease.com
| |
| John W. Krahn 2006-08-29, 9:57 pm |
| Jeff Pang wrote:
>
> John,
>
> For the instance described by you,both '2006' and '106' are right.
>
> $ perl -Mstrict -MTime::Local -le 'print timelocal(0,0,0,31,7,106)'
> 1156953600
> $ perl -Mstrict -MTime::Local -le 'print timelocal(0,0,0,31,7,2006)'
> 1156953600
The reason that '2006' works is because Time::Local makes a guess that '2006'
actually means '106'.
John
--
use Perl;
program
fulfillment
| |
| Paul Lalli 2006-08-30, 7:57 am |
| John W. Krahn wrote:
> Jeff Pang wrote:
[color=darkred]
>
> The reason that '2006' works is because Time::Local makes a guess that '2006'
> actually means '106'.
There is no "guesswork" involved. Time::Local has a very well
documented specific set of rules:
o Years greater than 999 are interpreted as being the
actual year, rather than the offset from 1900. Thus,
1963 would indicate the year Martin Luther King won the
Nobel prize, not the year 2863.
o Years in the range 100..999 are interpreted as offset
from 1900, so that 112 indicates 2012. This rule also
applies to years less than zero (but see note below
regarding date range).
o Years in the range 0..99 are interpreted as shorthand
for years in the rolling "current century," defined as
50 years on either side of the current year. Thus,
today, in 1999, 0 would refer to 2000, and 45 to 2045,
but 55 would refer to 1955. Twenty years from now, 55
would instead refer to 2055. This is messy, but matches
the way people currently think about two digit dates.
Whenever possible, use an absolute four digit year
instead.
Paul Lalli
|
|
|
|
|