Class: Reality::TZOffset

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(minutes) ⇒ TZOffset

Constructs offset from number of minutes. In most cases, you don't want to use it, but rather parse.

Parameters:

  • minutes (Fixnum)

    Number of minutes in offset.



62
63
64
# File 'lib/reality/tz_offset.rb', line 62

def initialize(minutes)
  @minutes = minutes
end

Instance Attribute Details

#minutesFixnum (readonly)

Number of minutes in offset.

Returns:

  • (Fixnum)


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 (or GMT+3);
  • +03:30;
  • ..and several combinations.

Returns:



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

Returns:

  • (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.

Parameters:

  • tm (Time)

    Time object to convert (with any offset);

Returns:

  • (Time)

    Converted object.



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

#inspectString

Returns:

  • (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.

Returns:

  • (Time)

    Constructed time in that 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

#nowTime

Like Ruby's Time.now, but in desired offset.

Returns:

  • (Time)

    Current time in that offset.



87
88
89
# File 'lib/reality/tz_offset.rb', line 87

def now
  convert(Time.now)
end

#to_sString

Returns:

  • (String)


72
73
74
# File 'lib/reality/tz_offset.rb', line 72

def to_s
  '%+03i:%02i' % minutes.divmod(60)
end