Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

Re: Date and time difference

by roboticus (Chancellor)
on Nov 17, 2010 at 13:30 UTC ( #871993=note: print w/replies, xml ) Need Help??

in reply to Date and time difference

While there are many date & time packages available on CPAN, for my purposes, I generally just store times as returned by time() (seconds since 1970/01/01 00:00:00 IIRC). Then taking the difference is a simple subtraction. Of course, you'll need to format the results when you're done. But I typically do something like:

my $start=time; # ... long processing job goes here... my $dur = time - $start; # Print out in appropriate format: print "Job took $dur seconds\n"; # For longer jobs, I often use one of: printf "Job took %5.2 minutes\n", $dur/60.0; printf "Job took %5.2 hours\n", $dur/(60*60.0); printf "Job took %5.2 days\n", $dur/(24*60*60.0); printf "Job took %5.2 weeks\n", $dur/(7*24*60*60.0); # etc.

Pretty simple and adequate for what I do. If I cared for more accuracy or features, I'd check out one of the Date modules on CPAN.


I have a fancier formatting module to show in format "Days HH:MM:SS", but the margin is too small for me to note it here. (Actually, it's on my server at home, and I haven't brought into work with me yet. It's on my TODO list.)

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://871993]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (4)
As of 2017-10-17 23:20 GMT
Find Nodes?
    Voting Booth?
    My fridge is mostly full of:

    Results (239 votes). Check out past polls.