Class: Timing::TimeInZone

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/timing/time_in_zone.rb

Constant Summary collapse

REGEXP =
/[+-]\d\d:?\d\d$/

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(time, zone_offset = nil) ⇒ TimeInZone

Returns a new instance of TimeInZone.



13
14
15
16
# File 'lib/timing/time_in_zone.rb', line 13

def initialize(time, zone_offset=nil)
  @time = time
  @zone_offset = build_zone_offset(zone_offset || time.utc_offset)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object (private)



142
143
144
# File 'lib/timing/time_in_zone.rb', line 142

def method_missing(method, *args, &block)
  time.public_send method, *args, &block
end

Instance Attribute Details

#zone_offsetObject Also known as: utc_offset, gmt_offset, gmtoff

Returns the value of attribute zone_offset.



11
12
13
# File 'lib/timing/time_in_zone.rb', line 11

def zone_offset
  @zone_offset
end

Class Method Details

.at(seconds, zone_offset = nil) ⇒ Object



116
117
118
# File 'lib/timing/time_in_zone.rb', line 116

def self.at(seconds, zone_offset=nil)
  new Time.at(seconds), zone_offset
end

.now(zone_offset = nil) ⇒ Object



112
113
114
# File 'lib/timing/time_in_zone.rb', line 112

def self.now(zone_offset=nil)
  new Time.now, zone_offset
end

.parse(text) ⇒ Object



120
121
122
123
124
# File 'lib/timing/time_in_zone.rb', line 120

def self.parse(text)
  match = text.length > 10 ? REGEXP.match(text) : nil
  zone_offset = match ? match.to_s : nil
  new Time.parse(text), zone_offset
end

Instance Method Details

#+(seconds) ⇒ Object

Raises:

  • (ArgumentError)


26
27
28
29
# File 'lib/timing/time_in_zone.rb', line 26

def +(seconds)
  raise ArgumentError, "#{seconds} must be a valid seconds count" unless seconds.is_a? Numeric
  self.class.new (time + seconds), zone_offset
end

#-(seconds) ⇒ Object

Raises:

  • (ArgumentError)


31
32
33
34
35
# File 'lib/timing/time_in_zone.rb', line 31

def -(seconds)
  raise ArgumentError, "#{seconds} must be a time or a valid seconds count" unless seconds.respond_to? :to_f
  result = self.class.at (time.to_f - seconds.to_f), zone_offset
  seconds.is_a?(Numeric) ? result : result.to_f
end

#as_json(*args) ⇒ Object



76
77
78
# File 'lib/timing/time_in_zone.rb', line 76

def as_json(*args)
  iso8601
end

#iso8601Object



72
73
74
# File 'lib/timing/time_in_zone.rb', line 72

def iso8601
  strftime "%FT%T#{zone_offset.iso8601}"
end

#months_after(count) ⇒ Object



100
101
102
# File 'lib/timing/time_in_zone.rb', line 100

def months_after(count)
  Timing.months_after self, count
end

#months_ago(count) ⇒ Object



96
97
98
# File 'lib/timing/time_in_zone.rb', line 96

def months_ago(count)
  Timing.months_ago self, count
end

#strftime(format) ⇒ Object



63
64
65
66
67
68
69
70
# File 'lib/timing/time_in_zone.rb', line 63

def strftime(format)
  sanitized_format = format.gsub('%Z', '')
                           .gsub('%z', zone_offset.to_s)
                           .gsub('%:z', zone_offset.to_s(':'))
                           .gsub('%::z', "#{zone_offset.to_s(':')}:00")

  time_with_offset.strftime sanitized_format
end

#to_json(*args) ⇒ Object



80
81
82
# File 'lib/timing/time_in_zone.rb', line 80

def to_json(*args)
  "\"#{as_json(*args)}\""
end

#to_sObject Also known as: inspect



58
59
60
# File 'lib/timing/time_in_zone.rb', line 58

def to_s
  strftime '%F %T %z'
end

#to_timeObject



54
55
56
# File 'lib/timing/time_in_zone.rb', line 54

def to_time
  time
end

#to_utcObject Also known as: getutc, utc



44
45
46
# File 'lib/timing/time_in_zone.rb', line 44

def to_utc
  self.class.new time, 0
end

#to_zone(zone_offset) ⇒ Object



50
51
52
# File 'lib/timing/time_in_zone.rb', line 50

def to_zone(zone_offset)
  self.class.new time, zone_offset
end

#utc?Boolean Also known as: gmt?

Returns:

  • (Boolean)


39
40
41
# File 'lib/timing/time_in_zone.rb', line 39

def utc?
  zone_offset == 0
end

#years_after(count) ⇒ Object



108
109
110
# File 'lib/timing/time_in_zone.rb', line 108

def years_after(count)
  Timing.years_after self, count
end

#years_ago(count) ⇒ Object



104
105
106
# File 'lib/timing/time_in_zone.rb', line 104

def years_ago(count)
  Timing.years_ago self, count
end