Section 7.18. Working with Specific Dates (Pre-epoch)


7.17. Computing the Difference in Two Date/Time Values

We 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.





The Ruby Way(c) Solutions and Techniques in Ruby Programming
The Ruby Way, Second Edition: Solutions and Techniques in Ruby Programming (2nd Edition)
ISBN: 0672328844
EAN: 2147483647
Year: 2004
Pages: 269
Authors: Hal Fulton

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net