Class: Horologium::Duration

Inherits:
Object
  • Object
show all
Includes:
PreciseValue
Defined in:
lib/horologium/duration.rb,
sig/horologium/duration.rbs

Overview

An amount of time in SI seconds, with no date and no scale attached. Duration.days(1) is always 86,400 SI seconds. Because of leap seconds a civil day can be a second longer or shorter, so a Duration and a calendar day are different things.

A Duration is frozen. Its precision is set when it is built, from the precision in effect unless you pass one. At :standard it holds the seconds as a Numeric::TwoPartFloat, at :exact as a Numeric::Exact.

Examples:

A day is a fixed number of SI seconds

Horologium::Duration.days(1) == Horologium::Duration.seconds(86_400)
# => true

Constant Summary collapse

SECONDS_PER_DAY =

The number of SI seconds in a day.

Returns:

  • (Integer)
86_400
NANOSECONDS_PER_SECOND =

The number of nanoseconds in a second.

Returns:

  • (Integer)
1_000_000_000

Instance Attribute Summary

Attributes included from PreciseValue

#precision, #rational, #value

Class Method Summary collapse

Instance Method Summary collapse

Methods included from PreciseValue

#<=>, #eql?, #hash, #initialize

Class Method Details

.days(count, precision: Horologium.current_precision) ⇒ Horologium::Duration

A duration of count days, each of SECONDS_PER_DAY SI seconds. This counts time and is not tied to the calendar.

Examples:

Horologium::Duration.days(1) == Horologium::Duration.seconds(86_400)
# => true

Parameters:

  • count (Numeric)

    the number of days

  • precision (Symbol) (defaults to: Horologium.current_precision)

    :standard or :exact, taken from the precision in effect when omitted

  • precision: (Symbol) (defaults to: Horologium.current_precision)

Returns:



48
49
50
# File 'lib/horologium/duration.rb', line 48

def days(count, precision: Horologium.current_precision)
  from_seconds(count * SECONDS_PER_DAY, precision)
end

.nanoseconds(count, precision: Horologium.current_precision) ⇒ Horologium::Duration

A duration of count nanoseconds.

Examples:

Horologium::Duration.nanoseconds(1)

Parameters:

  • count (Numeric)

    the number of nanoseconds

  • precision (Symbol) (defaults to: Horologium.current_precision)

    :standard or :exact, taken from the precision in effect when omitted

  • precision: (Symbol) (defaults to: Horologium.current_precision)

Returns:



60
61
62
# File 'lib/horologium/duration.rb', line 60

def nanoseconds(count, precision: Horologium.current_precision)
  from_seconds(Rational(count) / NANOSECONDS_PER_SECOND, precision)
end

.seconds(count, precision: Horologium.current_precision) ⇒ Horologium::Duration

A duration of count SI seconds.

Examples:

Horologium::Duration.seconds(3600)

Parameters:

  • count (Numeric)

    the number of seconds

  • precision (Symbol) (defaults to: Horologium.current_precision)

    :standard or :exact, taken from the precision in effect when omitted

  • precision: (Symbol) (defaults to: Horologium.current_precision)

Returns:



34
35
36
# File 'lib/horologium/duration.rb', line 34

def seconds(count, precision: Horologium.current_precision)
  from_seconds(count, precision)
end

Instance Method Details

#absHorologium::Duration

The same length, never negative.

Examples:

Horologium::Duration.seconds(-3).abs ==
  Horologium::Duration.seconds(3)
# => true

Returns:



92
93
94
# File 'lib/horologium/duration.rb', line 92

def abs
  rational.negative? ? self.class.new(value * -1, precision) : self
end