123456789_123456789_123456789_123456789_123456789_

Timezone Specifiers

Certain Time methods accept arguments that specify timezones:

The value given with any of these must be one of the following (each detailed below):

Hours/Minutes Offsets

The zone value may be a string offset from UTC in the form '+HH:MM' or '-HH:MM', where:

Examples:

t = Time.utc(2000, 1, 1, 20, 15, 1) # => 2000-01-01 20:15:01 UTC
Time.at(t, in: '-23:59')            # => 1999-12-31 20:16:01 -2359
Time.at(t, in: '+23:59')            # => 2000-01-02 20:14:01 +2359

Single-Letter Offsets

The zone value may be a letter in the range 'A'..'I' or 'K'..'Z'; see List of military time zones:

t = Time.utc(2000, 1, 1, 20, 15, 1) # => 2000-01-01 20:15:01 UTC
Time.at(t, in: 'A')                 # => 2000-01-01 21:15:01 +0100
Time.at(t, in: 'I')                 # => 2000-01-02 05:15:01 +0900
Time.at(t, in: 'K')                 # => 2000-01-02 06:15:01 +1000
Time.at(t, in: 'Y')                 # => 2000-01-01 08:15:01 -1200
Time.at(t, in: 'Z')                 # => 2000-01-01 20:15:01 UTC

Integer Offsets

The zone value may be an integer number of seconds in the range -86399..86399:

t = Time.utc(2000, 1, 1, 20, 15, 1) # => 2000-01-01 20:15:01 UTC
Time.at(t, in: -86399)              # => 1999-12-31 20:15:02 -235959
Time.at(t, in: 86399)               # => 2000-01-02 20:15:00 +235959

Timezone Objects

The zone value may be an object responding to certain timezone methods, an instance of Timezone and TZInfo for example.

The timezone methods are:

A custom timezone class may have these instance methods, which will be called if defined:

Time-Like Objects

A Time-like object is a container object capable of interfacing with timezone libraries for timezone conversion.

The argument to the timezone conversion methods above will have attributes similar to Time, except that timezone related attributes are meaningless.

The objects returned by local_to_utc and utc_to_local methods of the timezone object may be of the same class as their arguments, of arbitrary object classes, or of class Integer.

For a returned class other than Integer, the class must have the following methods:

For a returned Integer, its components, decomposed in UTC, are interpreted as times in the specified timezone.

Timezone Names

If the class (the receiver of class methods, or the class of the receiver of instance methods) has find_timezone singleton method, this method is called to achieve the corresponding timezone object from a timezone name.

For example, using Timezone:

class TimeWithTimezone < Time
  require 'timezone'
  def self.find_timezone(z) = Timezone[z]
end

TimeWithTimezone.now(in: "America/New_York")        #=> 2023-12-25 00:00:00 -0500
TimeWithTimezone.new("2023-12-25 America/New_York") #=> 2023-12-25 00:00:00 -0500

Or, using TZInfo:

class TimeWithTZInfo < Time
  require 'tzinfo'
  def self.find_timezone(z) = TZInfo::Timezone.get(z)
end

TimeWithTZInfo.now(in: "America/New_York")          #=> 2023-12-25 00:00:00 -0500
TimeWithTZInfo.new("2023-12-25 America/New_York")   #=> 2023-12-25 00:00:00 -0500

You can define this method per subclasses, or on the toplevel Time class.