Re: The function localtime(time) returns wrong month . How to overcome this problem ??
by Corion (Patriarch) on Feb 09, 2006 at 09:56 UTC
|
I wonder if you'd care to suggest how we could improve the documentation for localtime to make it more obvious how to solve your problem. Currently the docs say this:
...
$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.
If you're formatting dates for outputting to the user, let me recommend a proper date formatting tool, like POSIX::strftime or DateTime or Date::Calc.
If all you need is the number of the month, how about just adding 1 to the number?
| [reply] [d/l] [select] |
|
| [reply] |
Re: The function localtime(time) returns wrong month . How to overcome this problem ??
by bart (Canon) on Feb 09, 2006 at 10:36 UTC
|
The reason why month indexes were originally made to start at 0, not 1, is because it's primarily intended to be used as an array index. So you can do this:
@month = qw(january february march april may june july august septembe
+r october november december);
$m = 1;
print $month[$m]; # prints "february"
(No, I lied, actually the primary reason is backward compatibility with C. But I digress.)
So perhaps you can do this?
$m = 1;
print +(1 .. 12)[$m]; # prints "2"
Now, it might be easier still, to just increment the month number.
$m++;
| [reply] [d/l] [select] |
Re: The function localtime(time) returns wrong month . How to overcome this problem ??
by mickeyn (Priest) on Feb 09, 2006 at 09:58 UTC
|
wrong,
localtime returns the month in 0..11 range, thus, February is '1' (January is '0').
Enjoy,
Mickey | [reply] |
Re: The function localtime(time) returns wrong month . How to overcome this problem ??
by johnnywang (Priest) on Feb 09, 2006 at 18:48 UTC
|
Zero is a very difficult concept: when my daughter sees a baby and asks "how old is the baby"? my answer is "0". In my native China, a baby is one year old as soon as it's born. In other words, at least one fifth of the world population has difficulty with the 0 index. | [reply] |
Re: The function localtime(time) returns wrong month . How to overcome this problem ??
by zentara (Archbishop) on Feb 09, 2006 at 11:21 UTC
|
A programmer should know that we all have 9 fingers. :-)
I'm not really a human, but I play one on earth.
flash japh
| [reply] |
|
| [reply] |
|
if(! $careless_with_power_tools)
{
scalar(@fingers) == 10;
}
else
{
scalar(@fingers) < 10;
}
emc
" When in doubt, use brute force." — Ken Thompson
| [reply] [d/l] |
|
and we can count up to 1024 with them :)
| [reply] |
Re: The function localtime(time) returns wrong month . How to overcome this problem ??
by danny_mk (Novice) on Feb 09, 2006 at 16:35 UTC
|
| [reply] |
Re: The function localtime(time) returns wrong month . How to overcome this problem ??
by cormanaz (Deacon) on Feb 09, 2006 at 19:42 UTC
|
Months are numbered from 0 so you have to add 1. There is other strangeness in the output of that function so you better read its docs to avoid further confusion. If you're doing a lot of work with dates you might want to have a look at the module Date::Manip, which is very handy and lets you avoid messing with localtime. Steve | [reply] |
Re: The function localtime(time) returns wrong month . How to overcome this problem ??
by herby1620 (Monk) on Feb 10, 2006 at 00:13 UTC
|
Months are zero based. January is therefore 0 (zero). Originally this value was used as an index to a text array in Unix. The routine that used the value was written in C, thus the zero based value. In addition the year value is commonly the year less 1900. Around 2000, some people assumed that it was the last two digits of the year (WRONG!). They then proceded to write the year as 19%2d (printf format). If you see the year "19100" where you should see "2000" this is the problem. There is no fix, you just need to know that the months go from 0-11 and if you desire to display the month as a numeric value, you need to add 1. Since there is confusion between which should be displayed first: the month (USA), or the day (europe), is is usually better to write out an abbreviation. It makes it less confusing.
Good luck! | [reply] |
|
Avoiding confusion between mildly silly little-endian dates and utterly ridiculous middle-endian dates is easy. Use the ISO 8601 standard - YYYY-MM-DD.
| [reply] |
Re: The function localtime(time) returns wrong month . How to overcome this problem ??
by spiritway (Vicar) on Feb 10, 2006 at 00:26 UTC
|
Add 1 to the number returned for month. If you notice, the year is also "wrong". You'd need to add 1900 to it, to get the current year. I'm not sure whether this is a "feature" or a bug, but it's what we've got - so we have to deal with it.
When you run into problems, the first thing to do is to review the documentation about the function, module, operator, etc. that you're trying to use. Usually this will explain the quirks and idiosyncrasies that appear. A good place to start is by typing 'perldoc perldoc' from the command line. Another place is right here on PM, using the Search or Super Search features.
PM has a whole bunch of helpful, cheerful monks who will try to help you - but there are occasional monks who are impatient with people who appear unwilling to make an effort first. It's better to try to find the answer yourself, then ask - and tell people what you've tried so far.
| [reply] |
Re: The function localtime(time) returns wrong month . How to overcome this problem ??
by DrHyde (Prior) on Feb 13, 2006 at 10:01 UTC
|
Downvoted because you were too lazy to bother reading the documentation. | [reply] |