Module: ThirdBase::DateTime::InstanceMethods
- Included in:
- ThirdBase::DateTime
- Defined in:
- lib/third_base/datetime.rb
Instance Attribute Summary collapse
-
#not_parsed ⇒ Object
readonly
Which parts of this datetime were guessed instead of being parsed from the input.
-
#offset ⇒ Object
(also: #utc_offset)
readonly
This datetime’s offset from UTC, in seconds.
Instance Method Summary collapse
-
#+(d) ⇒ Object
Return a new datetune with the given number of days added to this datetime.
-
#-(d) ⇒ Object
Return a new datetune with the given number of days subtracted from this datetime.
-
#<=>(datetime) ⇒ Object
Compares two datetimes.
-
#==(datetime) ⇒ Object
Two DateTimes are equal only if their dates and time components are the same, not counting the offset.
- #eql?(datetime) ⇒ Boolean
-
#fract ⇒ Object
Returns the fraction of the day for this datetime (Noon is 0.5).
-
#hour ⇒ Object
Returns the hour of this datetime.
-
#initialize(opts) ⇒ Object
Called by DateTime.new!, should be a hash with the following possible keys:.
-
#min ⇒ Object
Returns the minute of this datetime.
-
#sec ⇒ Object
Returns the second of this datetime.
-
#usec ⇒ Object
Returns the microsecond of this datetime.
-
#zone ⇒ Object
Return the offset as a time zone string (+/-HHMM).
Instance Attribute Details
#not_parsed ⇒ Object (readonly)
Which parts of this datetime were guessed instead of being parsed from the input.
247 248 249 |
# File 'lib/third_base/datetime.rb', line 247 def not_parsed @not_parsed end |
#offset ⇒ Object (readonly) Also known as: utc_offset
This datetime’s offset from UTC, in seconds.
243 244 245 |
# File 'lib/third_base/datetime.rb', line 243 def offset @offset end |
Instance Method Details
#+(d) ⇒ Object
Return a new datetune with the given number of days added to this datetime. If d is a Float adds a fractional date, with possible loss of precision. If d is an integer, the returned date has the same time components as the current date. In both cases, the offset for the new date is the same as for this date.
277 278 279 280 281 282 283 284 285 286 287 288 289 |
# File 'lib/third_base/datetime.rb', line 277 def +(d) case d when Float d, f = d.to_f.divmod(1) f = fract + f m, f = f.divmod(1) self.class.jd_fract(jd+d+m, f, @offset) when Integer new_jd(jd+d) else raise(TypeError, "d must be a Float or Integer") end end |
#-(d) ⇒ Object
Return a new datetune with the given number of days subtracted from this datetime. If d is a DateTime, returns the difference between the two datetimes as a Float, considering both datetimes date, time, and offest.
294 295 296 297 298 299 300 301 302 303 |
# File 'lib/third_base/datetime.rb', line 294 def -(d) case d when self.class (jd - d.jd) + (fract - d.fract) + (@offset - d.offset)/86400.0 when Integer, Float self + -d else raise TypeError, "d should be #{self.class}, Float, or Integer" end end |
#<=>(datetime) ⇒ Object
Compares two datetimes. If the given datetime is an Integer, returns 1 unless this datetime’s time components are all 0, in which case it returns 0. If the given datetime is a Float, calculates this date’s julian date plus the date fraction and compares it to the given datetime, and returns 0 only if the two are very close together. This code does not take into account time offsets.
310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 |
# File 'lib/third_base/datetime.rb', line 310 def <=>(datetime) case datetime when Integer if ((d = (jd <=> datetime)) == 0) (hour == 0 and min == 0 and sec == 0 and usec == 0) ? 0 : 1 else d end when Float diff = jd+fract - datetime if diff.abs <= 1.15740740740741e-011 0 else diff > 0.0 ? 1 : -1 end when self.class ((d = super) == 0) && ((d = (hour <=> datetime.hour)) == 0) && ((d = (min <=> datetime.min)) == 0) && ((d = (sec <=> datetime.sec)) == 0) && ((d = (usec <=> datetime.usec)) == 0) d else raise TypeError, "d should be #{self.class}, Float, or Integer" end end |
#==(datetime) ⇒ Object
Two DateTimes are equal only if their dates and time components are the same, not counting the offset.
334 335 336 337 |
# File 'lib/third_base/datetime.rb', line 334 def ==(datetime) return false unless DateTime === datetime super and hour == datetime.hour and min == datetime.min and sec == datetime.sec and usec == datetime.usec end |
#eql?(datetime) ⇒ Boolean
339 340 341 |
# File 'lib/third_base/datetime.rb', line 339 def eql?(datetime) self == datetime end |
#fract ⇒ Object
Returns the fraction of the day for this datetime (Noon is 0.5)
344 345 346 |
# File 'lib/third_base/datetime.rb', line 344 def fract @fract ||= (@hour*3600+@min*60+@sec+@usec/1000000.0)/86400.0 end |
#hour ⇒ Object
Returns the hour of this datetime.
349 350 351 |
# File 'lib/third_base/datetime.rb', line 349 def hour @hour ||= time_parts[0] end |
#initialize(opts) ⇒ Object
Called by DateTime.new!, should be a hash with the following possible keys:
-
:civil, :commericial, :jd, :ordinal : See ThirdBase::Date#initialize
-
:fract : The fraction of the day (0.5 is Noon)
-
:offset : offset from UTC, in seconds.
-
:parts : an array with 4 elements, hour, minute, second, and microsecond
Raises an ArgumentError if an invalid date is used. DateTime objects are immutable once created.
257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 |
# File 'lib/third_base/datetime.rb', line 257 def initialize(opts) @not_parsed = opts[:not_parsed] || [] @offset = opts[:offset] raise(ArgumentError, 'invalid datetime') unless @offset.is_a?(Integer) and @offset <= 43200 and @offset >= -43200 if opts[:parts] @hour, @min, @sec, @usec = opts[:parts] raise(ArgumentError, 'invalid datetime') unless @hour.is_a?(Integer) and @min.is_a?(Integer) and @sec.is_a?(Integer) and @usec.is_a?(Integer) elsif opts[:fract] @fract = opts[:fract] raise(ArgumentError, 'invalid datetime') unless @fract.is_a?(Float) and @fract < 1.0 and @fract >= 0.0 else raise(ArgumentError, 'invalid datetime') end super(opts) end |
#min ⇒ Object
Returns the minute of this datetime.
354 355 356 |
# File 'lib/third_base/datetime.rb', line 354 def min @min ||= time_parts[1] end |
#sec ⇒ Object
Returns the second of this datetime.
359 360 361 |
# File 'lib/third_base/datetime.rb', line 359 def sec @sec ||= time_parts[2] end |
#usec ⇒ Object
Returns the microsecond of this datetime.
364 365 366 |
# File 'lib/third_base/datetime.rb', line 364 def usec @usec ||= time_parts[3] end |
#zone ⇒ Object
Return the offset as a time zone string (+/-HHMM).
369 370 371 |
# File 'lib/third_base/datetime.rb', line 369 def zone strftime('%Z') end |