class ISO8601::Years

A Years atom in a {ISO8601::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”.

Constants

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”.

Public Class Methods

new(atom) click to toggle source

@param [Numeric] atom The atom value

# File lib/iso8601/years.rb, line 25
def initialize(atom)
  valid_atom?(atom)

  @atom = atom
end

Public Instance Methods

factor(base = nil) click to toggle source

The Year factor

@param [ISO8601::DateTime, nil] base (nil) The base datetime to compute

the year length.

@return [Integer]

# File lib/iso8601/years.rb, line 38
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() click to toggle source

The atom symbol.

@return [Symbol]

# File lib/iso8601/years.rb, line 72
def symbol
  :Y
end
to_seconds(base = nil) click to toggle source

The amount of seconds

TODO: Fractions of year will fail

@param [ISO8601::DateTime, nil] base (nil) The base datetime to compute

the year length.

@return [Numeric]

rubocop:disable Metrics/AbcSize

# File lib/iso8601/years.rb, line 58
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

Private Instance Methods

adjusted_factor(atom, base) click to toggle source
# File lib/iso8601/years.rb, line 78
def adjusted_factor(atom, base)
  (::Time.utc((base.year + atom).to_i) - ::Time.utc(base.year)) / atom
end
year(atom, base) click to toggle source
# File lib/iso8601/years.rb, line 82
def year(atom, base)
  (base.year + atom).to_i
end