Class: When::Parts::Timezone

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

Overview

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Resource::Pool

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

Methods included from Resource::Synchronize

#synchronize

Constructor Details

#initialize(identifier) ⇒ Timezone

オブジェクト生成

Parameters:

  • identifier (String)

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



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/when_exe/parts/timezone.rb', line 109

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
  std, dst    = _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) に委譲する


184
185
186
# File 'lib/when_exe/parts/timezone.rb', line 184

def method_missing(name, *args, &block)
  timezone.send(name.to_sym, *args, &block)
end

Instance Attribute Details

#daylightWhen::TM::Clock (readonly)

夏時間帯の時計

Returns:



77
78
79
# File 'lib/when_exe/parts/timezone.rb', line 77

def daylight
  @daylight
end

#differenceWhen::TM:IntervalLength (readonly)

夏時間帯と標準時間帯の時間差

Returns:

  • (When::TM:IntervalLength)


81
82
83
# File 'lib/when_exe/parts/timezone.rb', line 81

def difference
  @difference
end

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

時分秒のインデクス



103
104
105
# File 'lib/when_exe/parts/timezone.rb', line 103

def indices
  @indices
end

#standardWhen::TM::Clock (readonly)

標準時間帯の時計

Returns:



73
74
75
# File 'lib/when_exe/parts/timezone.rb', line 73

def standard
  @standard
end

#timezoneTZInfo::Timezone (readonly)

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

Returns:

  • (TZInfo::Timezone)


69
70
71
# File 'lib/when_exe/parts/timezone.rb', line 69

def timezone
  @timezone
end

Class Method Details

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

オブジェクト参照

Parameters:

  • label (String)

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

Returns:



30
31
32
33
34
# File 'lib/when_exe/parts/timezone.rb', line 30

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

._getObject



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

alias :_get :[]

.tz_infoHash

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

Returns:

  • (Hash)

    { String => TZInfo::CountryTimezone }

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


43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/when_exe/parts/timezone.rb', line 43

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(zdate = nil) ⇒ Object



147
148
149
150
151
152
153
154
155
156
157
# File 'lib/when_exe/parts/timezone.rb', line 147

def _daylight(zdate=nil)
  if block_given?
    zdate  = yield(@standard.dup.tap{|clock| clock.tz_prop = nil})
    now    = zdate.to_time.to_i
    clocks = _offsets(now).map {|clock| When::TM::Clock.new({:zone=>clock, :tz_prop=>self})}
    flags  = clocks.map {|z| @timezone.period_for_utc(yield(z.dup.tap{|clock| clock.tz_prop = nil}).to_time.to_i).dst? }
    (flags[0] || flags[1]) ? clocks[1] : clocks[0]
  else
    When::TM::Clock.new({:zone=>@timezone.period_for_utc(zdate.to_time.to_i).utc_total_offset, :tz_prop=>self})
  end
end

#_need_validateObject



160
161
162
# File 'lib/when_exe/parts/timezone.rb', line 160

def _need_validate
  true
end

#labelString

ユニーク識別名

Returns:



85
86
87
# File 'lib/when_exe/parts/timezone.rb', line 85

def label
  @timezone.identifier
end

#latitudeRational

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

Returns:

  • (Rational)


97
98
99
# File 'lib/when_exe/parts/timezone.rb', line 97

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

#longitudeRational

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

Returns:

  • (Rational)


91
92
93
# File 'lib/when_exe/parts/timezone.rb', line 91

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