Class: Reality::TZOffset
- Inherits:
-
Object
- Object
- Reality::TZOffset
- Includes:
- Comparable
- Defined in:
- lib/reality/tz_offset.rb
Overview
Simple class representing timezone offset (in minutes). Knows nothing about timezone name, DST or other complications, but useful when ONLY offset is known for some Entity.
Usage:
# As entity property:
Reality::Entity('Beijing').tz_offset
# => #<Reality::TZOffset(UTC+08:00)>
# On itself:
o = Reality::TZOffset.parse('UTC+3')
# => #<Reality::TZOffset(UTC+03:00)>
o.now
# => 2016-04-16 19:01:40 +0300
o.local(2016, 4, 1, 20, 30)
# => 2016-04-01 20:30:00 +0300
o.convert(Time.now)
# => 2016-04-16 19:02:22 +0300
Instance Attribute Summary collapse
-
#minutes ⇒ Fixnum
readonly
Number of minutes in offset.
Class Method Summary collapse
-
.parse(text) ⇒ TZOffset
Parses TZOffset from string.
Instance Method Summary collapse
- #<=>(other) ⇒ Boolean
-
#convert(tm) ⇒ Time
Converts
tminto correct offset. -
#initialize(minutes) ⇒ TZOffset
constructor
Constructs offset from number of minutes.
- #inspect ⇒ String
-
#local(*values) ⇒ Time
Like Ruby's
Time.local, but in desired offset. -
#now ⇒ Time
Like Ruby's
Time.now, but in desired offset. - #to_s ⇒ String
Constructor Details
Instance Attribute Details
#minutes ⇒ Fixnum (readonly)
Number of minutes in offset.
31 32 33 |
# File 'lib/reality/tz_offset.rb', line 31 def minutes @minutes end |
Class Method Details
.parse(text) ⇒ TZOffset
Parses TZOffset from string. Understands several options like:
GMT(not all TZ names, only those Ruby itself knows about);UTC+3(orGMT+3);+03:30;- ..and several combinations.
44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/reality/tz_offset.rb', line 44 def self.parse(text) text = text.gsub(MINUSES, '-') case text when /^[A-Z]{3}$/ Time.zone_offset(text) when /^(?:UTC|GMT)?([+-]\d{1,2}:?\d{2})$/ offset = $1 Time.zone_offset(offset.sub(/^([+-])(\d):/, '\10\2:')) when /^(?:UTC|GMT)?([+-]\d{1,2})/ $1.to_i * 3600 end.derp{|sec| sec && new(sec / 60)} end |
Instance Method Details
#<=>(other) ⇒ Boolean
77 78 79 80 |
# File 'lib/reality/tz_offset.rb', line 77 def <=>(other) other.is_a?(TZOffset) or fail ArgumentError, "Can't compare TZOffset with #{other.class}" minutes <=> other.minutes end |
#convert(tm) ⇒ Time
Converts tm into correct offset.
103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/reality/tz_offset.rb', line 103 def convert(tm) pattern = tm.utc + minutes * 60 # FIXME: usec are lost Time.new( pattern.year, pattern.month, pattern.day, pattern.hour, pattern.min, pattern.sec, to_s ) end |
#inspect ⇒ String
67 68 69 |
# File 'lib/reality/tz_offset.rb', line 67 def inspect '#<%s(UTC%+03i:%02i)>' % [self.class.name, *minutes.divmod(60)] end |
#local(*values) ⇒ Time
Like Ruby's Time.local, but in desired offset.
94 95 96 97 |
# File 'lib/reality/tz_offset.rb', line 94 def local(*values) values << 0 until values.count == 6 Time.new(*values, to_s) end |
#now ⇒ Time
Like Ruby's Time.now, but in desired offset.
87 88 89 |
# File 'lib/reality/tz_offset.rb', line 87 def now convert(Time.now) end |
#to_s ⇒ String
72 73 74 |
# File 'lib/reality/tz_offset.rb', line 72 def to_s '%+03i:%02i' % minutes.divmod(60) end |