Section 7.8. Working with Leap Seconds: Don t


7.7. Converting To and From the Epoch

For various reasons we may want to convert back and forth between the internal (or traditional) measure and the standard date form. Internally, dates are stored as a number of seconds since the epoch.

The Time.at class method creates a new time given the number of seconds since the epoch:

epoch = Time.at(0)          # Find the epoch (1 Jan 1970 GMT) newmil = Time.at(978307200) # Happy New Millennium! (1 Jan 2001) The inverse is the instance method to_i which converts to an integer. now = Time.now              # 16 Nov 2000 17:24:28 sec = now.to_i              # 974424268


If you need microseconds, and your system supports that resolution, you can use to_f to convert to a floating point number.




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