Class: TZInfo::TimezoneOffset

Inherits:
Object
  • Object
show all
Defined in:
lib/tzinfo/timezone_offset.rb

Overview

Represents an offset defined in a Timezone data file.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(utc_offset, std_offset, abbreviation) ⇒ TimezoneOffset

Constructs a new TimezoneOffset. utc_offset and std_offset are specified in seconds.



45
46
47
48
49
50
51
# File 'lib/tzinfo/timezone_offset.rb', line 45

def initialize(utc_offset, std_offset, abbreviation)
  @utc_offset = utc_offset
  @std_offset = std_offset      
  @abbreviation = abbreviation
  
  @utc_total_offset = @utc_offset + @std_offset
end

Instance Attribute Details

#abbreviationObject (readonly)

The abbreviation that identifies this observance, e.g. “GMT” (Greenwich Mean Time) or “BST” (British Summer Time) for “Europe/London”. The returned identifier is a symbol.



41
42
43
# File 'lib/tzinfo/timezone_offset.rb', line 41

def abbreviation
  @abbreviation
end

#std_offsetObject (readonly)

The offset from the time zone’s standard time in seconds. Zero when daylight savings time is not in effect. Non-zero (usually 3600 = 1 hour) if daylight savings is being observed.

Note that zoneinfo files only include the value of utc_total_offset and a DST flag. When using DataSources::ZoneinfoDataSource, the std_offset will be derived from changes to the UTC total offset and the DST flag. As a consequence, utc_total_offset will always be correct, but std_offset may be inaccurate.

If you require std_offset to be accurate, install the tzinfo-data gem and set RubyDataSource as the DataSource.



32
33
34
# File 'lib/tzinfo/timezone_offset.rb', line 32

def std_offset
  @std_offset
end

#utc_offsetObject (readonly)

The base offset of the timezone from UTC in seconds. This does not include any adjustment made for daylight savings time and will typically remain constant throughout the year.

To obtain the currently observed offset from UTC, including the effect of daylight savings time, use utc_total_offset instead.

Note that zoneinfo files only include the value of utc_total_offset and a DST flag. When using ZoneinfoDataSource, the utc_offset will be derived from changes to the UTC total offset and the DST flag. As a consequence, utc_total_offset will always be correct, but utc_offset may be inaccurate.

If you require utc_offset to be accurate, install the tzinfo-data gem and set RubyDataSource as the DataSource.



18
19
20
# File 'lib/tzinfo/timezone_offset.rb', line 18

def utc_offset
  @utc_offset
end

#utc_total_offsetObject (readonly)

The total offset of this observance from UTC in seconds (utc_offset + std_offset).



36
37
38
# File 'lib/tzinfo/timezone_offset.rb', line 36

def utc_total_offset
  @utc_total_offset
end

Instance Method Details

#==(toi) ⇒ Object

Returns true if and only if toi has the same utc_offset, std_offset and abbreviation as this TimezoneOffset.



80
81
82
83
# File 'lib/tzinfo/timezone_offset.rb', line 80

def ==(toi)
  toi.kind_of?(TimezoneOffset) &&
    utc_offset == toi.utc_offset && std_offset == toi.std_offset && abbreviation == toi.abbreviation
end

#dst?Boolean

True if std_offset is non-zero.

Returns:

  • (Boolean)


54
55
56
# File 'lib/tzinfo/timezone_offset.rb', line 54

def dst?
  @std_offset != 0
end

#eql?(toi) ⇒ Boolean

Returns true if and only if toi has the same utc_offset, std_offset and abbreviation as this TimezoneOffset.

Returns:

  • (Boolean)


87
88
89
# File 'lib/tzinfo/timezone_offset.rb', line 87

def eql?(toi)
  self == toi
end

#hashObject

Returns a hash of this TimezoneOffset.



92
93
94
# File 'lib/tzinfo/timezone_offset.rb', line 92

def hash
  utc_offset.hash ^ std_offset.hash ^ abbreviation.hash
end

#inspectObject

Returns internal object state as a programmer-readable string.



97
98
99
# File 'lib/tzinfo/timezone_offset.rb', line 97

def inspect
  "#<#{self.class}: #@utc_offset,#@std_offset,#@abbreviation>"
end

#to_local(utc) ⇒ Object

Converts a UTC Time, DateTime or integer timestamp to local time, based on the offset of this period.

Deprecation warning: this method will be removed in TZInfo version 2.0.0.



62
63
64
65
66
# File 'lib/tzinfo/timezone_offset.rb', line 62

def to_local(utc)
  TimeOrDateTime.wrap(utc) {|wrapped|
    wrapped + @utc_total_offset
  }
end

#to_utc(local) ⇒ Object

Converts a local Time, DateTime or integer timestamp to UTC, based on the offset of this period.

Deprecation warning: this method will be removed in TZInfo version 2.0.0.



72
73
74
75
76
# File 'lib/tzinfo/timezone_offset.rb', line 72

def to_utc(local)
  TimeOrDateTime.wrap(local) {|wrapped|
    wrapped - @utc_total_offset
  }
end