Class: Timing::TimeInZone
- Inherits:
-
Object
- Object
- Timing::TimeInZone
- Extended by:
- Forwardable
- Defined in:
- lib/timing/time_in_zone.rb
Constant Summary collapse
- REGEXP =
/[+-]\d\d:?\d\d$/
Instance Attribute Summary collapse
-
#zone_offset ⇒ Object
(also: #utc_offset, #gmt_offset, #gmtoff)
Returns the value of attribute zone_offset.
Class Method Summary collapse
Instance Method Summary collapse
- #+(seconds) ⇒ Object
- #-(seconds) ⇒ Object
- #as_json(*args) ⇒ Object
-
#initialize(time, zone_offset = nil) ⇒ TimeInZone
constructor
A new instance of TimeInZone.
- #iso8601 ⇒ Object
- #months_after(count) ⇒ Object
- #months_ago(count) ⇒ Object
- #strftime(format) ⇒ Object
- #to_json(*args) ⇒ Object
- #to_s ⇒ Object (also: #inspect)
- #to_time ⇒ Object
- #to_utc ⇒ Object (also: #getutc)
- #to_zone(zone_offset) ⇒ Object
- #utc? ⇒ Boolean (also: #gmt?)
- #years_after(count) ⇒ Object
- #years_ago(count) ⇒ Object
Constructor Details
#initialize(time, zone_offset = nil) ⇒ 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 |
Instance Attribute Details
#zone_offset ⇒ Object 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
115 116 117 |
# File 'lib/timing/time_in_zone.rb', line 115 def self.at(seconds, zone_offset=nil) new Time.at(seconds), zone_offset end |
.now(zone_offset = nil) ⇒ Object
111 112 113 |
# File 'lib/timing/time_in_zone.rb', line 111 def self.now(zone_offset=nil) new Time.now, zone_offset end |
.parse(text) ⇒ Object
119 120 121 122 123 |
# File 'lib/timing/time_in_zone.rb', line 119 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
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
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
75 76 77 |
# File 'lib/timing/time_in_zone.rb', line 75 def as_json(*args) iso8601 end |
#iso8601 ⇒ Object
71 72 73 |
# File 'lib/timing/time_in_zone.rb', line 71 def iso8601 strftime "%FT%T#{zone_offset.iso8601}" end |
#months_after(count) ⇒ Object
99 100 101 |
# File 'lib/timing/time_in_zone.rb', line 99 def months_after(count) Timing.months_after self, count end |
#months_ago(count) ⇒ Object
95 96 97 |
# File 'lib/timing/time_in_zone.rb', line 95 def months_ago(count) Timing.months_ago self, count end |
#strftime(format) ⇒ Object
62 63 64 65 66 67 68 69 |
# File 'lib/timing/time_in_zone.rb', line 62 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
79 80 81 |
# File 'lib/timing/time_in_zone.rb', line 79 def to_json(*args) "\"#{as_json(*args)}\"" end |
#to_s ⇒ Object Also known as: inspect
57 58 59 |
# File 'lib/timing/time_in_zone.rb', line 57 def to_s strftime '%F %T %z' end |
#to_time ⇒ Object
53 54 55 |
# File 'lib/timing/time_in_zone.rb', line 53 def to_time time end |
#to_utc ⇒ Object Also known as: getutc
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
49 50 51 |
# File 'lib/timing/time_in_zone.rb', line 49 def to_zone(zone_offset) self.class.new time, zone_offset end |
#utc? ⇒ Boolean Also known as: gmt?
39 40 41 |
# File 'lib/timing/time_in_zone.rb', line 39 def utc? zone_offset == 0 end |
#years_after(count) ⇒ Object
107 108 109 |
# File 'lib/timing/time_in_zone.rb', line 107 def years_after(count) Timing.years_after self, count end |