7.22. Time Zone ConversionsIt 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. |