Class: ISO8601::Years

Inherits:
Object
  • Object
show all
Includes:
Atomic
Defined in:
lib/iso8601/years.rb

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

#atom

Instance Method Summary collapse

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.

Parameters:

  • atom (Numeric)

    The atom value



25
26
27
28
29
# File 'lib/iso8601/years.rb', line 25

def initialize(atom)
  valid_atom?(atom)

  @atom = atom
end

Instance Method Details

#factor(base = nil) ⇒ Integer

The Year factor

Parameters:

  • base (ISO8601::DateTime, nil) (defaults to: nil)

    (nil) The base datetime to compute the year length.

Returns:

  • (Integer)


38
39
40
41
42
43
44
45
# 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

#symbolSymbol

The atom symbol.

Returns:

  • (Symbol)


72
73
74
# File 'lib/iso8601/years.rb', line 72

def symbol
  :Y
end

#to_seconds(base = nil) ⇒ Numeric

The amount of seconds

TODO: Fractions of year will fail

rubocop:disable Metrics/AbcSize

Parameters:

  • base (ISO8601::DateTime, nil) (defaults to: nil)

    (nil) The base datetime to compute the year length.

Returns:

  • (Numeric)


58
59
60
61
62
63
64
65
# 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