Class: Vremya::Timezone

Inherits:
Object
  • Object
show all
Defined in:
lib/vremya/timezone.rb

Constant Summary collapse

STR =
/([+-])(\d{2}):(\d{2})/

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value, stamp = nil) ⇒ Timezone

Returns a new instance of Timezone.



17
18
19
20
21
22
23
# File 'lib/vremya/timezone.rb', line 17

def initialize(value, stamp = nil)
  @value = value
  @stamp = stamp
  if block_given?
    yield self
  end
end

Instance Attribute Details

#valueObject (readonly)

Returns the value of attribute value.



10
11
12
# File 'lib/vremya/timezone.rb', line 10

def value
  @value
end

Class Method Details

.allObject



4
5
6
# File 'lib/vremya/timezone.rb', line 4

def self.all
  TZInfo::Timezone.all_identifiers
end

.at(value, year, mon = nil, day = nil, hour = nil, min = nil, sec = nil) ⇒ Object



12
13
14
15
# File 'lib/vremya/timezone.rb', line 12

def self.at(value, year, mon = nil, day = nil, hour = nil, min = nil, sec = nil)
  stamp = Time.gm(year, mon, day, hour, min, sec)
  new(value, stamp.to_i - new(value, stamp).offset)
end

Instance Method Details

#dst?Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/vremya/timezone.rb', line 58

def dst?
  tz.nil? ? false : tz.dst?
end

#nameObject



29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/vremya/timezone.rb', line 29

def name
  if value.nil?
    return 'GMT'
  end
  unless value.is_a?(String)
    return nil
  end
  if self.class.all.include?(value)
    return value
  end
  nil
end

#name_or_offsetObject



25
26
27
# File 'lib/vremya/timezone.rb', line 25

def name_or_offset
  name || offset
end

#offsetObject



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/vremya/timezone.rb', line 42

def offset
  if value.nil?
    return 0
  end
  if value.is_a?(Numeric)
    return value
  end
  if value.is_a?(String) && value =~ STR
    return ($2.to_i * 3600 + $3.to_i * 60) * ($1 == '-' ? -1 : 1)
  end
  unless name.nil?
    return period.utc_offset
  end
  0
end

#to_sObject



62
63
64
# File 'lib/vremya/timezone.rb', line 62

def to_s
  "%s%s" % [offset < 0 ? '-' : '+', "%02d:%02d" % (offset / 60).divmod(60)]
end