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



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/when_exe/parts/timezone.rb', line 96

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::DefaultTimeIndex
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) に委譲する


178
179
180
# File 'lib/when_exe/parts/timezone.rb', line 178

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

Instance Attribute Details

#daylightWhen::TM::Clock (readonly)

夏時間帯の時計

Returns:



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

def daylight
  @daylight
end

#differenceWhen::TM:IntervalLength (readonly)

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

Returns:

  • (When::TM:IntervalLength)


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

def difference
  @difference
end

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

時分秒のインデクス



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

def indices
  @indices
end

#standardWhen::TM::Clock (readonly)

標準時間帯の時計

Returns:



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

def standard
  @standard
end

#timezoneTZInfo::Timezone (readonly)

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

Returns:

  • (TZInfo::Timezone)


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

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

def 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

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

Instance Method Details

#_daylight(zdate = nil) ⇒ Object



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/when_exe/parts/timezone.rb', line 134

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? }
    return nil if flags[0] && !flags[1]
    (flags[0] || flags[1]) ? clocks[1] : clocks[0]
  else
    now      = zdate.to_time.to_i
    period   = @timezone.period_for_utc(now)
    unless period.dst?
      offset = _offsets(now)
      period = @timezone.period_for_utc(now-(offset[1]-offset[0]))
    end
    When::TM::Clock.new({:zone=>period.utc_total_offset, :tz_prop=>self})
  end
end

#_need_validateObject



154
155
156
# File 'lib/when_exe/parts/timezone.rb', line 154

def _need_validate
  true
end

#labelString

ユニーク識別名

Returns:



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

def label
  @timezone.identifier
end