Class: When::Parts::Timezone

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

Overview

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

Defined Under Namespace

Modules: Base

Instance Attribute Summary collapse

Attributes included from Base

#daylight, #difference, #standard

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” など)



137
138
139
140
141
142
143
144
145
146
147
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
# File 'lib/when_exe/parts/timezone.rb', line 137

def initialize(identifier)
  @timezone   = TZInfo::Timezone.get(identifier)
  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)
  @standard = When::TM::Clock.new({:zone=>std, :tz_prop=>self})
  if std == dst
    @daylight   = @standard
    @difference = 0
  else
    @daylight   = When::TM::Clock.new({:zone=>dst, :tz_prop=>self})
    @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) に委譲する


216
217
218
219
220
221
222
223
# File 'lib/when_exe/parts/timezone.rb', line 216

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

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

時分秒のインデクス



131
132
133
# File 'lib/when_exe/parts/timezone.rb', line 131

def indices
  @indices
end

#timezoneTZInfo::Timezone (readonly)

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

Returns:

  • (TZInfo::Timezone)


101
102
103
# File 'lib/when_exe/parts/timezone.rb', line 101

def timezone
  @timezone
end

Class Method Details

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

オブジェクト参照

Parameters:

  • label (String)

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

Returns:



60
61
62
63
64
# File 'lib/when_exe/parts/timezone.rb', line 60

def [](label)
  ref = _get(label)
  return ref if ref
  self[label] = self.new(label)
end

._getObject



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

alias :_get :[]

.tz_infoHash

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

Returns:

  • (Hash)

    { String => TZInfo::CountryTimezone }

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


73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/when_exe/parts/timezone.rb', line 73

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|
    if hash.keys.size == 1
      @tz_info[id] = hash[hash.keys[0]]
    else
      hash.each_pair do |c, z|
        @tz_info["#{id}(#{c})"] = z
      end
    end
  end
  @tz_info
end

Instance Method Details

#_daylight(time) ⇒ Object



175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/when_exe/parts/timezone.rb', line 175

def _daylight(time)
  frame, cal_date, clk_time = time
  clocks = {}
  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({:zone=>offset, :tz_prop=>self})
      return clocks[offsets[0]] if @timezone.period_for_utc(
        (frame.to_universal_time(cal_date, clk_time, clocks[offset])/When::TM::Duration::SECOND).floor).dst?
    end
  end
  offset = @timezone.period_for_utc((time/When::TM::Duration::SECOND).floor).utc_total_offset
  clocks[offset] || When::TM::Clock.new({:zone=>offset, :tz_prop=>self})
end

#_need_validateObject



192
193
194
# File 'lib/when_exe/parts/timezone.rb', line 192

def _need_validate
  true
end

#labelString

ユニーク識別名

Returns:



105
106
107
# File 'lib/when_exe/parts/timezone.rb', line 105

def label
  @timezone.identifier
end

#latitudeRational

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

Returns:

  • (Rational)


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

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

#locationWhen::Coordinates::Spatial

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



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

def location
  return @location if @location
  tzinfo = self.class.tz_info[label]
  @location = When.Resource("_l:long=#{tzinfo.longitude.to_f}&lat=#{tzinfo.latitude.to_f}&label=#{label}")
end

#longitudeRational

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

Returns:

  • (Rational)


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

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