Class: Teasy::TimeWithZone
- Inherits:
-
Object
- Object
- Teasy::TimeWithZone
- Extended by:
- Forwardable
- Includes:
- Comparable
- Defined in:
- lib/teasy/time_with_zone.rb
Overview
rubocop:disable Metrics/ClassLength
Class Method Summary collapse
-
.from_time(time, zone = Teasy.default_zone) ⇒ Object
rubocop:enable Metrics/ParameterLists.
- .from_utc(utc_time, zone = Teasy.default_zone) ⇒ Object
- .iso8601(string, zone = Teasy.default_zone) ⇒ Object
- .parse(string, zone = Teasy.default_zone) ⇒ Object
- .strptime(string, format, zone = Teasy.default_zone) ⇒ Object
Instance Method Summary collapse
- #+(other) ⇒ Object
- #-(other) ⇒ Object
- #<=>(other) ⇒ Object
- #asctime ⇒ Object (also: #ctime)
- #eql?(other) ⇒ Boolean
- #hash ⇒ Object
- #in_time_zone(zone = Teasy.default_zone) ⇒ Object
- #in_time_zone!(zone = Teasy.default_zone) ⇒ Object
-
#initialize(year, month = nil, day = nil, hour = nil, minute = nil, second = nil, usec_with_frac = nil, zone = Teasy.default_zone) ⇒ TimeWithZone
constructor
rubocop:disable Metrics/ParameterLists.
- #inspect ⇒ Object (also: #to_s)
- #round(*args) ⇒ Object
- #round!(*args) ⇒ Object
- #strftime(format) ⇒ Object
- #to_a ⇒ Object
- #to_time ⇒ Object
- #utc ⇒ Object
- #utc! ⇒ Object
- #utc? ⇒ Boolean
- #zone ⇒ Object
Constructor Details
#initialize(year, month = nil, day = nil, hour = nil, minute = nil, second = nil, usec_with_frac = nil, zone = Teasy.default_zone) ⇒ TimeWithZone
rubocop:disable Metrics/ParameterLists
22 23 24 25 26 27 28 |
# File 'lib/teasy/time_with_zone.rb', line 22 def initialize(year, month = nil, day = nil, hour = nil, minute = nil, second = nil, usec_with_frac = nil, zone = Teasy.default_zone) @zone = tzinfo_time_zone(zone) @time = Time.utc(year, month, day, hour, minute, second, usec_with_frac) @period = determine_period(@time, @zone) end |
Class Method Details
.from_time(time, zone = Teasy.default_zone) ⇒ Object
rubocop:enable Metrics/ParameterLists
31 32 33 34 |
# File 'lib/teasy/time_with_zone.rb', line 31 def self.from_time(time, zone = Teasy.default_zone) new(time.year, time.mon, time.day, time.hour, time.min, time.sec, time.nsec / 1_000.0, zone) end |
.from_utc(utc_time, zone = Teasy.default_zone) ⇒ Object
36 37 38 |
# File 'lib/teasy/time_with_zone.rb', line 36 def self.from_utc(utc_time, zone = Teasy.default_zone) from_time(utc_time, 'UTC').in_time_zone!(zone) end |
.iso8601(string, zone = Teasy.default_zone) ⇒ Object
44 45 46 |
# File 'lib/teasy/time_with_zone.rb', line 44 def self.iso8601(string, zone = Teasy.default_zone) from_utc(Time.iso8601(string).utc, zone) end |
.parse(string, zone = Teasy.default_zone) ⇒ Object
40 41 42 |
# File 'lib/teasy/time_with_zone.rb', line 40 def self.parse(string, zone = Teasy.default_zone) from_utc(Time.parse(string).utc, zone) end |
.strptime(string, format, zone = Teasy.default_zone) ⇒ Object
48 49 50 |
# File 'lib/teasy/time_with_zone.rb', line 48 def self.strptime(string, format, zone = Teasy.default_zone) new(*DateTime._strptime(string, format).values, 'UTC').in_time_zone!(zone) end |
Instance Method Details
#+(other) ⇒ Object
111 112 113 |
# File 'lib/teasy/time_with_zone.rb', line 111 def +(other) TimeWithZone.from_utc(utc_time + other, @zone.identifier) end |
#-(other) ⇒ Object
115 116 117 118 119 120 121 122 123 |
# File 'lib/teasy/time_with_zone.rb', line 115 def -(other) if other.is_a? Numeric TimeWithZone.from_utc(utc_time - other, @zone.identifier) elsif other.respond_to? :to_time to_time - other.to_time else raise TypeError, "#{other.class} can't be coerced into TimeWithZone" end end |
#<=>(other) ⇒ Object
125 126 127 128 129 |
# File 'lib/teasy/time_with_zone.rb', line 125 def <=>(other) return nil unless other.respond_to? :to_time to_time <=> other.to_time end |
#asctime ⇒ Object Also known as: ctime
105 106 107 |
# File 'lib/teasy/time_with_zone.rb', line 105 def asctime strftime('%a %b %e %T %Y') end |
#eql?(other) ⇒ Boolean
131 132 133 |
# File 'lib/teasy/time_with_zone.rb', line 131 def eql?(other) hash == other.hash end |
#hash ⇒ Object
135 136 137 |
# File 'lib/teasy/time_with_zone.rb', line 135 def hash (utc.to_a << self.class).hash end |
#in_time_zone(zone = Teasy.default_zone) ⇒ Object
61 62 63 |
# File 'lib/teasy/time_with_zone.rb', line 61 def in_time_zone(zone = Teasy.default_zone) dup.in_time_zone!(zone) end |
#in_time_zone!(zone = Teasy.default_zone) ⇒ Object
52 53 54 55 56 57 58 59 |
# File 'lib/teasy/time_with_zone.rb', line 52 def in_time_zone!(zone = Teasy.default_zone) time = utc_time @zone = tzinfo_time_zone(zone) @time = @zone.utc_to_local(time) @period = @zone.period_for_utc(time) remove_instance_variable(:@local_time) unless @local_time.nil? self end |
#inspect ⇒ Object Also known as: to_s
93 94 95 96 |
# File 'lib/teasy/time_with_zone.rb', line 93 def inspect format = utc? ? '%Y-%m-%d %H:%M:%S %Z' : '%Y-%m-%d %H:%M:%S %z' strftime(format) end |
#round(*args) ⇒ Object
89 90 91 |
# File 'lib/teasy/time_with_zone.rb', line 89 def round(*args) dup.round!(*args) end |
#round!(*args) ⇒ Object
84 85 86 87 |
# File 'lib/teasy/time_with_zone.rb', line 84 def round!(*args) @time = @time.round(*args) self end |
#strftime(format) ⇒ Object
100 101 102 103 |
# File 'lib/teasy/time_with_zone.rb', line 100 def strftime(format) format = replace_zone_info(format) if includes_zone_directive?(format) time.strftime(format) end |
#to_a ⇒ Object
139 140 141 |
# File 'lib/teasy/time_with_zone.rb', line 139 def to_a time.to_a[0..7] + [dst?, period.abbreviation.to_s] end |
#to_time ⇒ Object
143 144 145 146 147 148 149 |
# File 'lib/teasy/time_with_zone.rb', line 143 def to_time return @local_time unless @local_time.nil? params = i[year mon day hour min].map! { |m| @time.send(m) } params << @time.sec + @time.subsec @local_time = utc? ? Time.utc(*params) : Time.new(*params, utc_offset) end |
#utc ⇒ Object
80 81 82 |
# File 'lib/teasy/time_with_zone.rb', line 80 def utc dup.utc! end |
#utc! ⇒ Object
73 74 75 76 77 78 |
# File 'lib/teasy/time_with_zone.rb', line 73 def utc! @time = @zone.local_to_utc(@time, @period.dst?) @zone = tzinfo_time_zone('UTC') @period = @zone.period_for_local(@time) self end |
#utc? ⇒ Boolean
69 70 71 |
# File 'lib/teasy/time_with_zone.rb', line 69 def utc? @zone.identifier == 'UTC' end |
#zone ⇒ Object
65 66 67 |
# File 'lib/teasy/time_with_zone.rb', line 65 def zone @zone.identifier end |