Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options

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.

zz zZ Z Z #!perl

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://542119]
[karlgoethebier]: james28909: See also Class::Tiny and Role::Tiny

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (7)
As of 2017-05-25 16:54 GMT
Find Nodes?
    Voting Booth?