Brad Tilley wrote: > Say I have two time objects represented as floats like so: > > x = 64299600.0 > y = 1157489583.2798 > > I want to subtract x from y and then represent the difference as years, > months, days, hours, minutes and seconds. > > I don't see how the Time library would do this. Has anyone done > something like this? If so, how? This may not be exactly what you had in mind, but it's better than the other proposed "solutions" when it comes to displaying the number of months or years between two dates: (uses rails activesupport) class Time def time_span(other = nil) other ||= self.utc? ? Time.now.utc : Time.now other.time_spent(self) end def time_spent(other = nil) other ||= self.utc? ? Time.now.utc : Time.now n = other - self case n.abs when 0...60 #1.minute "%d seconds" % n when 0...3600 #1.hour "%.1f minutes" % (n / 60) when 0...86400 #1.day "%.1f hours" % (n / 3600) else sign = "-" if self > other t1,t2 = [other,self].sort if t2.year != t1.year and t2 >= t1.advance(:years=>1) nb_years = t2.year - t1.year t = t1.advance(:years => nb_years) t = t1.advance(:years => (nb_years -= 1)) if t > t2 "#{sign}%.1f years" % (nb_years + (t2 - t).to_f / (t.advance(:years=>1) - t)) elsif t2 >= t1.advance(:months=>1) nb_months = (t1.year==t2.year ? 0 : 12) + t2.month - t1.month t = t1.advance(:months => nb_months) t = t1.advance(:months => (nb_months -= 1)) if t > t2 "#{sign}%.1f months" % (nb_months + (t2 - t).to_f / (t.advance(:months=>1) - t)) else "%.1f days" % (n / 1.0.day) end end end end >> t.time_span(t - 26.seconds) => "26 seconds" >> t.time_span(t - 26.hours) => "1.1 days" >> t.time_span(t - 26.days) => "26.0 days" >> t.time_span(t - 26.months) => "2.1 years"