Class: SDL4R::RelativeTimezone

Inherits:
TZInfo::Timezone
  • Object
show all
Defined in:
lib/sdl4r/relative_timezone.rb

Overview

Represents a Timezone which is distant from a standard timezone by a fixed offset.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#identifierObject (readonly)

Returns the value of attribute identifier.



125
126
127
# File 'lib/sdl4r/relative_timezone.rb', line 125

def identifier
  @identifier
end

#relative_offsetObject (readonly)

Returns the value of attribute relative_offset.



125
126
127
# File 'lib/sdl4r/relative_timezone.rb', line 125

def relative_offset
  @relative_offset
end

Class Method Details

.get(identifier) ⇒ Object

Returns a timezone by its identifier (e.g. “Europe/London”, “America/Chicago” or “UTC”).

Supports relative timezones in the following formats: “UTC+2”, “GMT+01:30”, “Europe/Paris-10:00”.

Raises InvalidTimezoneIdentifier if the timezone couldn’t be found.



38
39
40
41
42
43
44
45
46
# File 'lib/sdl4r/relative_timezone.rb', line 38

def self.get(identifier)
  base_identifier, offset_text, offset = RelativeTimezone::parse_relative_identifier(identifier)

  tz = TZAbbreviationDB.get_timezone(base_identifier)

  offset ?
    RelativeTimezone.new(base_identifier + offset_text.to_s, offset_text, offset, tz) :
    tz
end

.get_proxy(identifier) ⇒ Object

Returns a proxy for the Timezone with the given identifier. The proxy will cause the real timezone to be loaded when an attempt is made to find a period or convert a time. get_proxy will not validate the identifier. If an invalid identifier is specified, no exception will be raised until the proxy is used.

Supports relative timezones in the following format: “GMT+01:30”, “CET-10:00”.



56
57
58
59
60
61
62
63
64
# File 'lib/sdl4r/relative_timezone.rb', line 56

def self.get_proxy(identifier)
  base_identifier, offset_text, offset = RelativeTimezone::parse_relative_identifier(identifier)

  proxy = TZAbbreviationDB.get_timezone_proxy(base_identifier)

  offset ?
    RelativeTimezone.new(base_identifier + offset_text.to_s, offset_text, offset, proxy) :
    proxy
end

.new(identifier, offset_text, offset, base_timezone) ⇒ Object



103
104
105
106
107
# File 'lib/sdl4r/relative_timezone.rb', line 103

def self.new(identifier, offset_text, offset, base_timezone)
  o = super()
  o.send(:_initialize, identifier, offset_text, offset, base_timezone)
  o
end

.parse_relative_identifier(identifier) ⇒ Object

Parses the specified identifier of the shape “GMT” or “CET+7” or “Asia/Shanghai-03:30” and returns an array containing respectively the simple zone identifier (e.g. “CET”, “Asia/Shanghai”), the offset part (e.g. “-6”, “+08:30”) and the offset (seconds).



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/sdl4r/relative_timezone.rb', line 70

def self.parse_relative_identifier(identifier) # :nodoc:
  offset = nil

  if identifier =~ /^([a-zA-Z0-9\/_]+)(([+\-])(\d+)(?::(\d+))?)?$/
    identifier, offset_text, sign_part, hour_part, minute_part = $1, $2, $3, $4, $5

    if sign_part # relative offset
      offset = 0
      offset += 3600 * hour_part.to_i if hour_part
      offset += 60 * minute_part.to_i if minute_part
      offset = -offset if sign_part == '-'

      offset = nil if offset == 0

      # We regenerate/normalize the offset text "+7:3" ==> "+07:03".
      offset_text = ""
      if offset
        offset_text << (offset >= 0 ? '+' : '-')

        hours = offset.abs / 3600
        minutes = (offset.abs % 3600) / 60
        if minutes == 0
          offset_text << hours.to_s
        else
          offset_text << sprintf("%02d:%02d", hours, minutes)
        end
      end
    end
  end

  return identifier, offset_text, offset
end

Instance Method Details

#period_for_utc(utc) ⇒ Object



127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/sdl4r/relative_timezone.rb', line 127

def period_for_utc(utc)
  period = @base_timezone.period_for_utc(utc)

  translated_offset =
    period.offset ?
      TZInfo::TimezoneOffsetInfo.new(
        period.offset.utc_offset + @relative_offset,
        period.offset.std_offset,
        (period.offset.abbreviation.to_s + @relative_offset_text).to_sym) :
      nil

  return TZInfo::TimezonePeriod.new(nil, nil, translated_offset)
end

#periods_for_local(local) ⇒ Object



141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/sdl4r/relative_timezone.rb', line 141

def periods_for_local(local)
  periods = @base_timezone.periods_for_local(local)

  return periods.collect { |period|
    translated_offset =
      period.offset ?
        TZInfo::TimezoneOffsetInfo.new(
          period.offset.utc_offset + @relative_offset,
          period.offset.std_offset,
          (period.offset.abbreviation.to_s + @relative_offset_text).to_sym) :
        nil
    TZInfo::TimezonePeriod.new(nil, nil, translated_offset)
  }
end