views:

190

answers:

2

I am parsing a log and would like an elegant way of calculating the difference between these time stamps shown below:

[Mar 2, 2010 1:54:40 AM] [Mar 4, 2010 10:54:40 PM]

I've looked at DateTime however I wasnt sure if it was ncessary to seperate the Date from the actual time part and create a Time object. If there is an easier way please fire away.

Regards. Snappy

A: 

Use chronic gem to parse the two times to obtain two time objects then substract

clyfe
Thanks that does work - wish it could interpret the Mar into March so that it could add the difference of days to the time difference..
A: 

Using only DateTime class:

require 'date'
d1, d2 = 'Mar 2, 2010 1:54:40 AM', 'Mar 4, 2010 10:54:40 PM'
diff = DateTime.parse(d2) - DateTime.parse(d1)
hours,minutes,seconds,frac = Date.send(:day_fraction_to_time, diff)
# => [69, 0, 0, 01]
Mladen Jablanović