Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: Date and time difference

by roboticus (Canon)
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.

...roboticus

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 MY::Utils.pm into work with me yet. It's on my TODO list.)


Comment on Re: Date and time difference
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (18)
As of 2014-07-31 14:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (249 votes), past polls