Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

The function localtime(time) returns wrong month . How to overcome this problem ??

by sugarkannan (Novice)
on Feb 09, 2006 at 09:53 UTC ( [id://529034]=perlquestion: print w/replies, xml ) Need Help??

sugarkannan has asked for the wisdom of the Perl Monks concerning the following question:

This node falls below the community's threshold of quality. You may see it by logging in.
  • Comment on The function localtime(time) returns wrong month . How to overcome this problem ??

Replies are listed 'Best First'.
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?

      Change it from...

      $mon is the month itself, in the range 0..11 with 0 indicating January and 11 indicating December.

      ...to...

      $mon is the month offset, in the range 0..11 with 0 indicating January and 11 indicating December.

      ...as the former implies that $mon is the number of the month which people might assume starts at 1 (and stop reading). But my guess is that anyone who has a problem hasn't read the current docs. (but, hey at least they don't just assume tm_year is the last two digits nowadays).

      --
      James Antill
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++;
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

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.
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

        if(! $careless_with_power_tools) { scalar(@fingers) == 10; } else { scalar(@fingers) < 10; }

        emc

        " When in doubt, use brute force." — Ken Thompson
      and we can count up to 1024 with them :)
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
    Use Date::Calc, it is very good at dealing with dates!
    Read: http://www.perl.com/doc/manual/html/pod/perlfunc/localtime.html
    Excerpt: All array elements are numeric, and come straight out of a struct tm. In particular this means that $mon has the range 0..11 and $wday has the range 0..6 with sunday as day 0. Also, $year is the number of years since 1900, that is, $year is 123 in year 2023, and not simply the last two digits of the year...
    Danny M. Not a Perl monk but definitely a Perl advocate!
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

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!
      Avoiding confusion between mildly silly little-endian dates and utterly ridiculous middle-endian dates is easy. Use the ISO 8601 standard - YYYY-MM-DD.
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.

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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://529034]
Approved by GrandFather
Front-paged by grinder
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (5)
As of 2024-04-25 09:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found