Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: localtime hour count

by nothingmuch (Priest)
on Apr 09, 2006 at 10:09 UTC ( #542119=note: print w/ replies, xml ) Need Help??


in reply to localtime hour count

The problem is that when you subtract $t1 from $t2 you wind up with a number, that is incidentially both the duration that passed, in seconds, *and* a valid epoch number representing a certain date, in this case 5 seconds after the "epoch", which is January 1st, 1970 GMT on UNIX systems. An epoch date is the number of seconds that passed since the epoch (essentially also a form of a duration, but with a well defined base value).

localtime accepts an epoch number representing a date and then calculates that date, converts it to the local time zone, and provides the separate fields (something like the Jan 1, 1970, 1:00:05, that is 5 seconds after the epoch, converted to your timezone). Even if you use gmtime this would still not make sense, because you are talking about a duration, and not a date.

Use DateTime to subtract the two dates, play with duration sanely, and all that, or use simple modular arithmetics:

my ( $h, $m, $s ) = do { use integer; ( ($tdelta / (60 * 60)) % 24, ($ +tdelta / 60) % 60, $tdelta % 60 ) };
but note that this conversion is lossy.

You can also use Time::Duration::Object, which is convenient for formatting durations for users as '2 hours, 5 minutes' or '2 minutes, 10 seconds', that is - rounded to a certain number of units.

-nuffin
zz zZ Z Z #!perl


Comment on Re: localtime hour count
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (8)
As of 2015-07-02 23:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (47 votes), past polls