From: "Ara.T.Howard" Date: 2004-04-02T03:14:27+09:00 Subject: Re: How to get internal state of DateTime in seconds On Thu, 1 Apr 2004, Charles Comstock wrote: > > 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... i think i would be suprisingly difficult to do this for anything other than seconds when you consider leap years/seconds and diffs which span multiple leap years. consider span = TimeSpan.new t0, t1 span.years # => 3 t0 + (span.years * YEAR_SEC) # but what if one of those years was a leap year! ok so you could span = TimeSpan.new t0, t1 t2 = span.add_years t0 but this will really just use seconds under the hood right? the hard part is - how t you represent an 'absolute' time span in anything other than one unit (seconds is only one) AND give a methods for breaking this unit up into chunks (days, years) such that the chunks retain their leapness? i suppose you could class TimeSpan class Year end class Day end end and extend Time/Data to use those... but surely this is far more error prone than using utc and seconds? -a -- =============================================================================== | EMAIL :: Ara [dot] T [dot] Howard [at] noaa [dot] gov | PHONE :: 303.497.6469 | ADDRESS :: E/GC2 325 Broadway, Boulder, CO 80305-3328 | URL :: http://www.ngdc.noaa.gov/stp/ | TRY :: for l in ruby perl;do $l -e "print \"\x3a\x2d\x29\x0a\"";done ===============================================================================