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

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.

...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 having an uproarious good time at the Monastery: (9)
As of 2015-07-04 19:55 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 (60 votes), past polls