Section 7.23. Determining the Number of Days in a Month


7.22. Time Zone Conversions

It is only convenient to work with two time zones: GMT (or UTC) is one, and the other is whatever time zone you happen to be in.

The gmtime method converts a time to GMT (changing the receiver in place). There is an alias named utc.

You might expect that it would be possible to convert a time to an array, tweak the time zone, and convert it back. The trouble with this is that all the class methods such as local and gm (or their aliases mktime and utc) want to create a Time object using either your local time zone or GMT.

There is a workaround to get time zone conversions. This does require that you know the time difference in advance. See the following code fragment:

mississippi = Time.local(2000,11,13,9,35) # 9:35 am CST california  = mississippi - 2*3600        # Minus two hours time1 = mississippi.strftime("%X CST")    # 09:35:00 CST time2 = california.strftime("%X PST")     # 07:35:00 PST


The %X directive to strftime that we see here simply uses the hh:mm:ss format as shown.




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

Similar book on Amazon

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