Class: TimeCrisis::TZInfo::TimeOrDateTime
- Includes:
- Comparable
- Defined in:
- lib/time_crisis/tzinfo/time_or_datetime.rb
Overview
Used by TZInfo internally to represent either a Time, DateTime or integer timestamp (seconds since 1970-01-01 00:00:00).
Class Method Summary collapse
-
.wrap(timeOrDateTime) ⇒ Object
If no block is given, returns a TimeOrDateTime wrapping the given timeOrDateTime.
Instance Method Summary collapse
-
#+(seconds) ⇒ Object
Adds a number of seconds to the TimeOrDateTime.
-
#-(seconds) ⇒ Object
Subtracts a number of seconds from the TimeOrDateTime.
-
#<=>(timeOrDateTime) ⇒ Object
Compares this TimeOrDateTime with another Time, DateTime, integer timestamp or TimeOrDateTime.
-
#add_with_convert(seconds) ⇒ Object
Similar to the + operator, but for cases where adding would cause a timestamp or time to go out of the allowed range, converts to a DateTime based TimeOrDateTime.
-
#eql?(todt) ⇒ Boolean
Returns true if todt represents the same time and was originally constructed with the same type (DateTime, Time or timestamp) as this TimeOrDateTime.
-
#hash ⇒ Object
Returns a hash of this TimeOrDateTime.
-
#hour ⇒ Object
Returns the hour of the day (0..23).
-
#initialize(timeOrDateTime) ⇒ TimeOrDateTime
constructor
Constructs a new TimeOrDateTime.
-
#inspect ⇒ Object
Returns internal object state as a programmer-readable string.
-
#mday ⇒ Object
(also: #day)
Returns the day of the month (1..n).
-
#min ⇒ Object
Returns the minute of the hour (0..59).
-
#mon ⇒ Object
(also: #month)
Returns the month of the year (1..12).
-
#sec ⇒ Object
Returns the second of the minute (0..60).
-
#to_datetime ⇒ Object
Returns the time as a DateTime.
-
#to_i ⇒ Object
Returns the time as an integer timestamp.
-
#to_orig ⇒ Object
Returns the time as the original time passed to new.
-
#to_s ⇒ Object
Returns a string representation of the TimeOrDateTime.
-
#to_tc_datetime ⇒ Object
returns the time as a TimeCrisis::DateTime.
- #to_tc_time ⇒ Object
-
#to_time ⇒ Object
Returns the time as a Time.
-
#year ⇒ Object
Returns the year.
Constructor Details
#initialize(timeOrDateTime) ⇒ TimeOrDateTime
Constructs a new TimeOrDateTime. timeOrDateTime can be a Time, DateTime or an integer. If using a Time or DateTime, any time zone information is ignored.
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/time_crisis/tzinfo/time_or_datetime.rb', line 13 def initialize(timeOrDateTime) @time = nil @tctime = nil @datetime = nil @tcdatetime = nil @timestamp = nil if timeOrDateTime.is_a?(::TimeCrisis::Time) @tctime = timeOrDateTime @tctime = ::TimeCrisis::Time.utc(@tctime.year, @tctime.mon, @tctime.mday, @tctime.hour, @tctime.min, @tctime.sec) unless @tctime.zone == 'UTC' @orig = @tctime elsif timeOrDateTime.is_a?(::Time) @time = timeOrDateTime @time = ::Time.utc(@time.year, @time.mon, @time.mday, @time.hour, @time.min, @time.sec) unless @time.zone == 'UTC' @orig = @time elsif timeOrDateTime.is_a?(::TimeCrisis::DateTime) @tcdatetime = timeOrDateTime @tcdatetime = @tcdatetime.new_offset(0) unless @tcdatetime.offset == 0 @orig = @tcdatetime elsif timeOrDateTime.is_a?(::DateTime) @datetime = timeOrDateTime @datetime = @datetime.new_offset(0) unless @datetime.offset == 0 @orig = @datetime else @timestamp = timeOrDateTime.to_i @orig = @timestamp end end |
Class Method Details
.wrap(timeOrDateTime) ⇒ Object
If no block is given, returns a TimeOrDateTime wrapping the given timeOrDateTime. If a block is specified, a TimeOrDateTime is constructed and passed to the block. The result of the block must be a TimeOrDateTime. to_orig will be called on the result and the result of to_orig will be returned.
timeOrDateTime can be a Time, DateTime, integer timestamp or TimeOrDateTime. If a TimeOrDateTime is passed in, no new TimeOrDateTime will be constructed, the passed in value will be used.
322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 |
# File 'lib/time_crisis/tzinfo/time_or_datetime.rb', line 322 def self.wrap(timeOrDateTime) t = timeOrDateTime.is_a?(TimeOrDateTime) ? timeOrDateTime : TimeOrDateTime.new(timeOrDateTime) if block_given? t = yield t if timeOrDateTime.is_a?(TimeOrDateTime) t elsif timeOrDateTime.is_a?(::TimeCrisis::Time) t.to_tc_time elsif timeOrDateTime.is_a?(::Time) t.to_time elsif timeOrDateTime.is_a?(::TimeCrisis::DateTime) t.to_tc_datetime elsif timeOrDateTime.is_a?(::DateTime) t.to_datetime else t.to_i end else t end end |
Instance Method Details
#+(seconds) ⇒ Object
Adds a number of seconds to the TimeOrDateTime. Returns a new TimeOrDateTime, preserving what the original constructed type was. If the original type is a Time and the resulting calculation goes out of range for Times, then an exception will be raised by the Time class.
254 255 256 257 258 259 260 261 262 263 264 265 266 267 |
# File 'lib/time_crisis/tzinfo/time_or_datetime.rb', line 254 def +(seconds) if seconds == 0 self else if @orig.is_a?(::TimeCrisis::DateTime) TimeOrDateTime.new(@orig + OffsetRationals.rational_for_offset(seconds).to_f) elsif @orig.is_a?(::DateTime) TimeOrDateTime.new(@orig + OffsetRationals.rational_for_offset(seconds)) else # + defined for Time and integer timestamps TimeOrDateTime.new(@orig + seconds) end end end |
#-(seconds) ⇒ Object
Subtracts a number of seconds from the TimeOrDateTime. Returns a new TimeOrDateTime, preserving what the original constructed type was. If the original type is a Time and the resulting calculation goes out of range for Times, then an exception will be raised by the Time class.
273 274 275 |
# File 'lib/time_crisis/tzinfo/time_or_datetime.rb', line 273 def -(seconds) self + (-seconds) end |
#<=>(timeOrDateTime) ⇒ Object
Compares this TimeOrDateTime with another Time, DateTime, integer timestamp or TimeOrDateTime. Returns -1, 0 or +1 depending whether the receiver is less than, equal to, or greater than timeOrDateTime.
Milliseconds and smaller units are ignored in the comparison.
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 |
# File 'lib/time_crisis/tzinfo/time_or_datetime.rb', line 218 def <=>(timeOrDateTime) if timeOrDateTime.is_a?(TimeOrDateTime) orig = timeOrDateTime.to_orig if @orig.is_a?(::TimeCrisis::DateTime) || orig.is_a?(::TimeCrisis::DateTime) to_tc_datetime <=> timeOrDateTime.to_tc_datetime elsif @orig.is_a?(::DateTime) || orig.is_a?(::DateTime) # If either is a DateTime, assume it is there for a reason # (i.e. for range). to_datetime <=> timeOrDateTime.to_datetime elsif orig.is_a?(::TimeCrisis::Time) to_tc_time <=> timeOrDateTime.to_tc_time elsif orig.is_a?(::Time) to_time <=> timeOrDateTime.to_time else to_i <=> timeOrDateTime.to_i end elsif @orig.is_a?(::TimeCrisis::DateTime) || timeOrDateTime.is_a?(::TimeCrisis::DateTime) to_tc_datetime <=> TimeOrDateTime.wrap(timeOrDateTime).to_tc_datetime elsif @orig.is_a?(::DateTime) || timeOrDateTime.is_a?(::DateTime) # If either is a DateTime, assume it is there for a reason # (i.e. for range). to_datetime <=> TimeOrDateTime.wrap(timeOrDateTime).to_datetime elsif timeOrDateTime.is_a?(::TimeCrisis::Time) to_tc_time <=> timeOrDateTime elsif timeOrDateTime.is_a?(::Time) to_time <=> timeOrDateTime else to_i <=> timeOrDateTime.to_i end end |
#add_with_convert(seconds) ⇒ Object
Similar to the + operator, but for cases where adding would cause a timestamp or time to go out of the allowed range, converts to a DateTime based TimeOrDateTime.
280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 |
# File 'lib/time_crisis/tzinfo/time_or_datetime.rb', line 280 def add_with_convert(seconds) if seconds == 0 self else if @orig.is_a?(::TimeCrisis::DateTime) TimeOrDateTime.new(@orig + OffsetRationals.rational_for_offset(seconds).to_f) elsif @orig.is_a?(::DateTime) TimeOrDateTime.new(@orig + OffsetRationals.rational_for_offset(seconds)) else # A Time or timestamp. result = to_i + seconds if result < 0 || result > 2147483647 result = TimeOrDateTime.new(to_tc_datetime + OffsetRationals.rational_for_offset(seconds).to_f) else result = TimeOrDateTime.new(@orig + seconds) end end end end |
#eql?(todt) ⇒ Boolean
Returns true if todt represents the same time and was originally constructed with the same type (DateTime, Time or timestamp) as this TimeOrDateTime.
304 305 306 |
# File 'lib/time_crisis/tzinfo/time_or_datetime.rb', line 304 def eql?(todt) todt.respond_to?(:to_orig) && to_orig.eql?(todt.to_orig) end |
#hash ⇒ Object
Returns a hash of this TimeOrDateTime.
309 310 311 |
# File 'lib/time_crisis/tzinfo/time_or_datetime.rb', line 309 def hash @orig.hash end |
#hour ⇒ Object
Returns the hour of the day (0..23).
169 170 171 172 173 174 175 176 177 178 179 180 181 |
# File 'lib/time_crisis/tzinfo/time_or_datetime.rb', line 169 def hour if @tctime @tctime.hour elsif @time @time.hour elsif @datetime @datetime.hour elsif @tcdatetime @tcdatetime.hour else to_tc_time.hour end end |
#inspect ⇒ Object
Returns internal object state as a programmer-readable string.
115 116 117 |
# File 'lib/time_crisis/tzinfo/time_or_datetime.rb', line 115 def inspect "#<#{self.class}: #{@orig.inspect}>" end |
#mday ⇒ Object Also known as: day
Returns the day of the month (1..n).
152 153 154 155 156 157 158 159 160 161 162 163 164 |
# File 'lib/time_crisis/tzinfo/time_or_datetime.rb', line 152 def mday if @tctime @tctime.mday elsif @time @time.mday elsif @datetime @datetime.mday elsif @tcdatetime @tcdatetime.mday else to_tc_time.mday end end |
#min ⇒ Object
Returns the minute of the hour (0..59).
184 185 186 187 188 189 190 191 192 193 194 195 196 |
# File 'lib/time_crisis/tzinfo/time_or_datetime.rb', line 184 def min if @tctime @tctime.min elsif @time @time.min elsif @datetime @datetime.min elsif @tcdatetime @tcdatetime.min else to_tc_time.min end end |
#mon ⇒ Object Also known as: month
Returns the month of the year (1..12).
135 136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/time_crisis/tzinfo/time_or_datetime.rb', line 135 def mon if @tctime @tctime.mon elsif @time @time.mon elsif @datetime @datetime.mon elsif @tcdatetime @tcdatetime.mon else to_tc_time.mon end end |
#sec ⇒ Object
Returns the second of the minute (0..60). (60 for a leap second).
199 200 201 202 203 204 205 206 207 208 209 210 211 |
# File 'lib/time_crisis/tzinfo/time_or_datetime.rb', line 199 def sec if @tctime @tctime.sec elsif @time @time.sec elsif @datetime @datetime.sec elsif @tcdatetime @tcdatetime.sec else to_tc_time.sec end end |
#to_datetime ⇒ Object
Returns the time as a DateTime.
68 69 70 71 72 73 74 |
# File 'lib/time_crisis/tzinfo/time_or_datetime.rb', line 68 def to_datetime unless @datetime @datetime = ::DateTime.civil(year, mon, mday, hour, min, sec) end @datetime end |
#to_i ⇒ Object
Returns the time as an integer timestamp.
86 87 88 89 90 91 92 |
# File 'lib/time_crisis/tzinfo/time_or_datetime.rb', line 86 def to_i unless @timestamp @timestamp = to_tc_time.to_i end @timestamp end |
#to_orig ⇒ Object
Returns the time as the original time passed to new.
95 96 97 |
# File 'lib/time_crisis/tzinfo/time_or_datetime.rb', line 95 def to_orig @orig end |
#to_s ⇒ Object
Returns a string representation of the TimeOrDateTime.
100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/time_crisis/tzinfo/time_or_datetime.rb', line 100 def to_s if @orig.is_a?(::TimeCrisis::Time) "TimeCrisis::Time: #{@orig.to_s}" elsif @orig.is_a?(::Time) "Time: #{@orig.to_s}" elsif @orig.is_a?(::TimeCrisis::DateTime) "TimeCrisis::DateTime: #{@orig.to_s}" elsif @orig.is_a?(::DateTime) "DateTime: #{@orig.to_s}" else "Timestamp: #{@orig.to_s}" end end |
#to_tc_datetime ⇒ Object
returns the time as a TimeCrisis::DateTime
77 78 79 80 81 82 83 |
# File 'lib/time_crisis/tzinfo/time_or_datetime.rb', line 77 def to_tc_datetime unless @tcdatetime @tcdatetime = ::TimeCrisis::DateTime.civil(year, mon, mday, hour, min, sec) end @tcdatetime end |
#to_tc_time ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/time_crisis/tzinfo/time_or_datetime.rb', line 55 def to_tc_time unless @tctime if @timestamp @tctime = ::TimeCrisis::Time.at(@timestamp).utc else @tctime = ::TimeCrisis::Time.utc(year, mon, mday, hour, min, sec) end end @tctime end |
#to_time ⇒ Object
Returns the time as a Time.
43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/time_crisis/tzinfo/time_or_datetime.rb', line 43 def to_time unless @time if @timestamp @time = ::Time.at(@timestamp).utc else @time = ::Time.utc(year, mon, mday, hour, min, sec) end end @time end |
#year ⇒ Object
Returns the year.
120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/time_crisis/tzinfo/time_or_datetime.rb', line 120 def year if @tctime @tctime.year elsif @time @time.year elsif @datetime @datetime.year elsif @tcdatetime @tcdatetime.year else to_tc_time.year end end |