[LRUG] Finding the duration between two DateTimes

Timothy Cowlishaw timcowlishaw at gmail.com
Sun May 27 06:02:09 PDT 2007


Hi all,

I'm new to this list (hi!), and to LRUG, despite having lurked on  
here for a while...

I was wondering if anyone would be able to help with a small problem  
- I'm working on a little rails app for time management, and need to  
work out the duration of a 'task' object, that has both a start_time  
and an end_time (both values are DateTimes).

I've got a  method in my 'Task' class that does this:

def duration
return self.end_time - self.start_time	
end

but if  I do something like this

task1 = Task.new(:name => 'foo')
task1.start_time = Time.now
task1.end_time = Time.now + 1.hour

...then task1.duration returns an integer (3613.217825 for a duration  
of one hour).

Firstly, I'm not sure what this duration refers to - presumably it  
isn't seconds, as there are 3600 seconds in an hour, so this figure  
should be < 3600, in that case, allowing for a few seconds to pass  
between setting the start_time and the end_time. Would anyone be able  
to enlighten me on this?

Secondly, is there a class similar to C#'s TimeSpan, that can be used  
to express durations such as this? presumably | can't cast the  
duration to DateTime, as that class models  a time-of-day rather than  
a length of time.)

Any advice would be greatly appreciated!

Many thanks,

Tim



More information about the Chat mailing list