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

Get The Ruby Way: Solutions and Techniques in Ruby Programming, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.