From: Charles Comstock Date: 2004-04-02T01:49:27+09:00 Subject: Re: How to get internal state of DateTime in seconds > iff > - your times are a common format > - your times are within the ranges allowable by Time > > both of which appear to be true why not simply parse the time yourself? this > is plenty clean, especially if it's in instance method of a LogEntry class or > something: > > > file: logtm.rb > > def logtm s > #{{{ > pat = > %r|^\s*(\d\d)/(\w\w\w)/(\d\d\d\d):(\d\d):(\d\d):(\d\d)\s+\+(\d\d)(\d\d)\s*$|io > > s = s.to_s > m = pat.match s > raise ArgumentError, "<#{ s.inspect }>" unless m > > parts = m.to_a > raise ArgumentError, "<#{ s.inspect }>" unless parts.size == 9 > > parts.shift > day, month, year, hour, min, sec, zhour, zmin = parts > > months = %w(jan feb mar apr may jun jul aug sep oct nov dec) > i = months.index month.downcase > raise ArgumentError, "<#{ s.inspect }>" unless i > month = i + 1 > > day, month, year, hour, min, sec, zhour, zmin = > [day, month, year, hour, min, sec, zhour, zmin].map{|x| x.to_i} > > utc = Time.utc year, month, day, hour, min, sec, usec = 0 > > t = utc + ((zhour * 60 * 60) + (zmin * 60)) > #}}} > end > > str1 = '20/Nov/2003:08:08:09 +0200' > str2 = '20/Nov/2003:08:04:09 +0200' > t1 = logtm str1 > t2 = logtm str2 > > #what is difference in minutes between these 2 times? > p((t1 - t2).abs / 60.0) # => 4.0 > > > -a Umm I don't understand how this relates, I can construct Time objects from strings just fine (parsedate), I want a library to take the time difference in seconds from two Time or Date objects, and format output of that into something meaningful, ie %H:%M:%S would result in "00:04:00" for your example difference. But it should scale all the way up to differences in Months/Years. It seems like a perfectly useful library, and I was suprised it hadn't been written. Yes it's just division up to months, and then months cares what the actual start/end times were, but still it seems like a useful object. Seconds is all fine and good when your just recording short ranges, but for longer... Charles Comstock