7.17. Computing the Difference in Two Date/Time ValuesWe can find the interval of time between two points in time. Subtracting one Time object from another gives us a number of seconds: today = Time.local(2000,11,10) yesterday = Time.local(2000,11,9) diff = today - yesterday # 86400 seconds Once again, the function sec2dhms comes in handy. (This is defined in section 7.6 "Converting Between Seconds and Larger Units" earlier in the chapter.) past = Time.local(1998,9,13,4,15) now = Time.local(2000,11,10,22,42) diff = now - past unit = sec2dhms(diff) puts "#{unit[0]} days," # 789 days, puts "#{unit[1]} hours," # 18 hours, puts "#{unit[2]} minutes," # 27 minutes, puts "and #{unit[3]} seconds." # and 0 seconds. |