Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re^2: epoch time

by johngg (Canon)
on Jan 31, 2007 at 17:10 UTC ( [id://597570]=note: print w/replies, xml ) Need Help??


in reply to Re: epoch time
in thread epoch time

I think timelocal() wants the year to be years since 1900 so code shoud perhaps be

my $time = timelocal(0, 0, 0, $d, $m-1, $y - 1900);

Also, for this particular application, why not set the hash up with the subtraction by one already done?

my %month_num_by_name = ( JAN => 0, FEB => 1, ... );

Cheers,

JohnGG

Update: davorg and ikegami have pointed out that timelocal() does accept dates like 2007 without first subtracting 1900, although it does the right thing if you do the subtraction as well. ikegami also explained his excellent reason for doing the month the way he does.

Replies are listed 'Best First'.
Re^3: epoch time
by davorg (Chancellor) on Jan 31, 2007 at 17:13 UTC
    I think timelocal() wants the year to be years since 1900

    That would make sense. But the documentation says:

    Strictly speaking, the year should also be specified in a form consistent with localtime(), i.e. the offset from 1900. In order to make the interpretation of the year easier for humans, however, who are more accustomed to seeing years as two-digit or four-digit values, the following conventions are followed:

    • Years greater than 999 are interpreted as being the actual year, rather than the offset from 1900. Thus, 1964 would indicate the year Martin Luther King won the Nobel prize, not the year 3864.

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

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

    The scheme above allows interpretation of a wide range of dates, particularly if 4-digit years are used.

      Looks like I have been coding under a false assumption all these years. I thought the year had to be consistent with localtime() and didn't know there was a human-friendly set of rules.

      Thank you for educating me.

      JohnGG

Re^3: epoch time
by ikegami (Patriarch) on Jan 31, 2007 at 17:26 UTC

    I think timelocal() wants the year to be years since 1900

    It accepts both.

    why not set the hash up with the subtraction by one already done?

    I like $m having a natural value like $d (1..~31) and $y (19xx) do. I always follow localtime and gmtime with $m++; $y += 1900;, and I always specify $m-1 for timelocal and timegm. Not seeing $m-1 raises alarms in my head.

      It accepts both.

      Yes, davorg also pointed out my error. I had always thought that localtime() and timelocal() were essentially mirrors of each other. Now I know better. Re. your habitual $m-1, that's very sound reasoning; perhaps I should do the same.

      I saw davorg's reply to my post just as I was leaving work so I didn't have time to correct my error. I will do so now.

      Cheers,

      JohnGG

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://597570]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (4)
As of 2024-03-28 14:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found