From: Charles Comstock Date: 2004-04-02T03:59:23+09:00 Subject: Re: How to get internal state of DateTime in seconds Ara.T.Howard wrote: > 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 Utc doesn't start out at a completely zeroed out date, so you automatically get a starting value, therfore you can't format nicely just using utc, strftime and the difference in seconds. I mean it is possible that something similar could be used but I'm not sure the best approach there. At the very least it would seem like a nice TimeSpan object that at least allowed manipulation of weeks/days/hours/minutes/seconds/milliseconds as well as some sort of string format method would be a nice thing to have in the standard library. Charles Comstock