Method: When::Parts::Timezone#initialize

Defined in:
lib/when_exe/parts/timezone.rb

#initialize(identifier) ⇒ Timezone

オブジェクト生成

Parameters:

  • identifier (String)

    識別名 ( “America/New_York” など)


148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/when_exe/parts/timezone.rb', line 148

def initialize(identifier)
  @identifier = identifier
  id, query   = identifier.split('?', 2)
  @timezone   = TZInfo::Timezone.get(id.sub(/\(.+?\)\z/,''))
  unless TZInfo::TimeOrDateTime.method_defined?(:_to_datetime)
    if TZInfo::RubyCoreSupport.respond_to?(:datetime_new)
      TZInfo::TimeOrDateTime.class_eval %Q{
        alias :_to_datetime :to_datetime
        ::Rational
        def to_datetime
          unless @datetime
            u = usec
            s = u == 0 ? sec : Rational(sec * 1000000 + u, 1000000)
            @datetime = TZInfo::RubyCoreSupport.datetime_new(year, mon, mday, hour, min, s, 0, Date::GREGORIAN)
          end
          @datetime
        end
      }
    else
      TZInfo::TimeOrDateTime.class_eval %Q{
        alias :_to_datetime :to_datetime
        def to_datetime
          @datetime ||= DateTime.new(year, mon, mday, hour, min, sec, 0, Date::GREGORIAN)
        end
      }
    end
  end
  dst, std  = _offsets(Time.now.to_i)
  options   = query ? Hash[*(query.split('&').map {|item| item.split('=',2)}.flatten)] : {}
  @standard = When::TM::Clock.new(options.merge({:zone=>std, :tz_prop=>self}))
  if std == dst
    @daylight      = @standard
    @tz_difference = 0
  else
    @daylight      = When::TM::Clock.new(options.merge({:zone=>dst, :tz_prop=>self}))
    @tz_difference = @standard.universal_time - @daylight.universal_time
  end
  @indices = When::Coordinates::DefaultTimeIndices
end