Class: ISO8601::Years
Overview
A Years atom in a Duration
A “calendar year” is the cyclic time interval in a calendar which is required for one revolution of the Earth around the Sun and approximated to an integral number of “calendar days”.
A “duration year” is the duration of 365 or 366 “calendar days” depending on the start and/or the end of the corresponding time interval within the specific “calendar year”.
Constant Summary collapse
- AVERAGE_FACTOR =
The “duration year” average is calculated through time intervals of 400 “duration years”. Each cycle of 400 “duration years” has 303 “common years” of 365 “calendar days” and 97 “leap years” of 366 “calendar days”.
((365 * 303 + 366 * 97) / 400) * 86400
Instance Attribute Summary
Attributes included from Atomic
Instance Method Summary collapse
-
#factor(base = nil) ⇒ Integer
The Year factor.
-
#initialize(atom) ⇒ Years
constructor
A new instance of Years.
-
#symbol ⇒ Symbol
The atom symbol.
-
#to_seconds(base = nil) ⇒ Numeric
The amount of seconds.
Methods included from Atomic
#<=>, #eql?, #hash, #to_f, #to_i, #to_s, #valid_atom?, #valid_base?, #value
Constructor Details
#initialize(atom) ⇒ Years
Returns a new instance of Years.
23 24 25 26 27 |
# File 'lib/iso8601/years.rb', line 23 def initialize(atom) valid_atom?(atom) @atom = atom end |
Instance Method Details
#factor(base = nil) ⇒ Integer
The Year factor
36 37 38 39 40 41 42 43 |
# File 'lib/iso8601/years.rb', line 36 def factor(base = nil) valid_base?(base) return AVERAGE_FACTOR if base.nil? return adjusted_factor(1, base) if atom.zero? adjusted_factor(atom, base) end |
#symbol ⇒ Symbol
The atom symbol.
67 68 69 |
# File 'lib/iso8601/years.rb', line 67 def symbol :Y end |
#to_seconds(base = nil) ⇒ Numeric
The amount of seconds
TODO: Fractions of year will fail
rubocop:disable Metrics/AbcSize
55 56 57 58 59 60 61 |
# File 'lib/iso8601/years.rb', line 55 def to_seconds(base = nil) valid_base?(base) return factor(base) * atom if base.nil? target = ::Time.new(base.year + atom.to_i, base.month, base.day, base.hour, base.minute, base.second, base.zone) target - base.to_time end |