Class: When::Parts::Timezone

Inherits:
Object
  • Object
show all
Extended by:
Resource::Pool
Includes:
Base
Defined in:
lib/when_exe/parts/timezone.rb,
lib/when_exe/inspect.rb

Overview

TZInfo::Timezone クラスを本ライブラリから使用するためのラッパークラス

Defined Under Namespace

Modules: Base

Instance Attribute Summary collapse

Attributes included from Base

#daylight, #standard, #tz_difference

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Resource::Pool

[], []=, _pool, _setup_, pool_keys

Methods included from Resource::Synchronize

#synchronize

Methods included from Base

#^

Constructor Details

#initialize(identifier) ⇒ Timezone

オブジェクト生成

Parameters:

  • identifier (String)

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



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
# File 'lib/when_exe/parts/timezone.rb', line 151

def initialize(identifier)
  @identifier = identifier
  id, query   = identifier.split('?', 2)
  @timezone   = TZInfo::Timezone.get(id.sub(/\(.+?\)\z/,''))
  unless TZInfo::Timezone.method_defined?(:period_for)
     TZInfo::Timezone.class_eval %Q{
      def period_for(time)
        period_for_utc(Time.at(time).getutc)
      end
    }
    TZInfo::TimezonePeriod.class_eval %Q{
      alias :starts_at :utc_start
      alias :ends_at   :utc_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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object (private)

その他のメソッド

When::Parts::GeometricComplex で定義されていないメソッドは
処理を first (type: When::TM::(Temporal)Position) に委譲する


228
229
230
231
232
233
234
235
# File 'lib/when_exe/parts/timezone.rb', line 228

def method_missing(name, *args, &block)
  self.class.module_eval %Q{
    def #{name}(*args, &block)
      timezone.send("#{name}", *args, &block)
    end
  } unless When::Parts::MethodCash.escape(name)
  timezone.send(name, *args, &block)
end

Instance Attribute Details

#identifierString (readonly) Also known as: label, inspect

ユニーク識別名

Returns:



111
112
113
# File 'lib/when_exe/parts/timezone.rb', line 111

def identifier
  @identifier
end

#indicesArray<When::Coordinates::Index> (readonly)

時分秒のインデクス



145
146
147
# File 'lib/when_exe/parts/timezone.rb', line 145

def indices
  @indices
end

#timezoneTZInfo::Timezone (readonly)

ラップしている TZInfo::Timezone インスタンス

Returns:

  • (TZInfo::Timezone)


107
108
109
# File 'lib/when_exe/parts/timezone.rb', line 107

def timezone
  @timezone
end

Class Method Details

.[](label) ⇒ When::Parts::Timezone Also known as: get

オブジェクト参照

Parameters:

  • label (String)

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

Returns:



64
65
66
67
68
69
70
71
# File 'lib/when_exe/parts/timezone.rb', line 64

def [](label)
  ref = _get(label)
  return ref if ref
  return nil unless label =~ /\A[A-Z]/i
  self[label] = self.new(label)
rescue NameError
  raise NameError, 'Prease install TZInfo - gem install tzinfo'
end

._getObject



56
# File 'lib/when_exe/parts/timezone.rb', line 56

alias :_get :[]

.tz_infoHash

TZInfo でサポートしている Timezone の連想配列

Returns:

  • (Hash)

    { String => TZInfo::CountryTimezone }

    String - 時間帯ID
    TZInfo::CountryTimezone - 時間帯オブジェクト(proxy for autoload)


80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/when_exe/parts/timezone.rb', line 80

def tz_info
  return @tz_info if @tz_info
  zones = {}
  TZInfo::Country.all.each do |c|
    c.zone_info.each do |z|
      zones[z.identifier]       ||= {}
      zones[z.identifier][c.code] = z
    end
  end

  @tz_info = {}
  zones.each_pair do |id, hash|
    @tz_info[id] = hash[hash.keys[0]]
    unless hash.keys.size == 1
      hash.each_pair do |c, z|
        @tz_info["#{id}(#{c})"] = z
      end
    end
  end
  @tz_info
end

Instance Method Details

#_daylight(time) ⇒ Object



180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'lib/when_exe/parts/timezone.rb', line 180

def _daylight(time)
  frame, cal_date, clk_time = time
  clocks  = {}
  options = {}
  %w(border location).each do |attr|
    value = @standard.instance_variable_get("@#{attr}")
    next unless value
    value = value.dup unless value.kind_of?(When::Parts::Resource) && value.registered?
    options[attr] = value
  end
  if clk_time
    time    = frame.to_universal_time(cal_date, clk_time, @standard)
    offsets = _offsets((time/When::TM::Duration::SECOND).floor)
    offsets.each do |offset|
      clocks[offset] ||= When::TM::Clock.new(options.merge({:zone=>offset, :tz_prop=>self}))
      time_for_offset = frame.to_universal_time(cal_date, clk_time, clocks[offset])
      return clocks[offsets[0]] if @timezone.period_for(Time.at((time_for_offset/When::TM::Duration::SECOND).floor)).dst?
    end
  end
  offset = @timezone.period_for(Time.at((time/When::TM::Duration::SECOND).floor)).utc_total_offset
  clocks[offset] || When::TM::Clock.new(options.merge({:zone=>offset, :tz_prop=>self}))
end

#_need_validateObject



204
205
206
# File 'lib/when_exe/parts/timezone.rb', line 204

def _need_validate
  true
end

#latitudeRational

時間帯を代表する都市の緯度 / 度

Returns:

  • (Rational)


123
124
125
# File 'lib/when_exe/parts/timezone.rb', line 123

def latitude
  self.class.tz_info[label].latitude
end

#locationWhen::Coordinates::Spatial

時間帯を代表する都市の空間位置



129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/when_exe/parts/timezone.rb', line 129

def location
  return @location if @location
  city_tz = @timezone
  unless self.class.tz_info[label]
    while city_tz.kind_of?(TZInfo::LinkedTimezone)
      city_tz = TZInfo::Timezone.get(city_tz.send(:info).link_to_identifier)
    end
  end
  tzinfo = self.class.tz_info[city_tz.identifier]
  longitude = When::Coordinates.to_dms(tzinfo.longitude, 'EW')
  latitude  = When::Coordinates.to_dms(tzinfo.latitude,  'NS')
  @location = When.Resource("_l:long=#{longitude}&lat=#{latitude}&label=#{label}")
end

#longitudeRational

時間帯を代表する都市の経度 / 度

Returns:

  • (Rational)


117
118
119
# File 'lib/when_exe/parts/timezone.rb', line 117

def longitude
  self.class.tz_info[label].longitude
end