Class: Horologium::Duration
- Inherits:
-
Object
- Object
- Horologium::Duration
- 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.
Two durations add and subtract to give another duration, and one negates.
Mixing a :standard and an :exact operand gives an :exact result.
Adding an Instant to a Duration raises DimensionalError; it is
Instant#+ that shifts a point by a span.
Constant Summary collapse
- SECONDS_PER_DAY =
The number of SI seconds in a day.
86_400- NANOSECONDS_PER_SECOND =
The number of nanoseconds in a second.
1_000_000_000
Instance Attribute Summary
Attributes included from PreciseValue
Class Method Summary collapse
-
.days(count, precision: Horologium.current_precision) ⇒ Horologium::Duration
A duration of
countdays, each of SECONDS_PER_DAY SI seconds. -
.nanoseconds(count, precision: Horologium.current_precision) ⇒ Horologium::Duration
A duration of
countnanoseconds. -
.seconds(count, precision: Horologium.current_precision) ⇒ Horologium::Duration
A duration of
countSI seconds.
Instance Method Summary collapse
- #+(other) ⇒ Horologium::Duration
-
#-(other) ⇒ Horologium::Duration
Negative when the other is the longer of the two.
-
#-@ ⇒ Horologium::Duration
The same length, the other way round.
-
#abs ⇒ Horologium::Duration
The same length, never negative.
- #inspect ⇒ String
- #negative? ⇒ Boolean
- #positive? ⇒ Boolean
-
#to_f ⇒ Float
The duration in SI seconds.
-
#to_r ⇒ Rational
The duration in SI seconds, exactly.
- #zero? ⇒ Boolean
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.
53 54 55 |
# File 'lib/horologium/duration.rb', line 53 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.
65 66 67 |
# File 'lib/horologium/duration.rb', line 65 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.
39 40 41 |
# File 'lib/horologium/duration.rb', line 39 def seconds(count, precision: Horologium.current_precision) from_seconds(count, precision) end |
Instance Method Details
#+(other) ⇒ Horologium::Duration
86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/horologium/duration.rb', line 86 def +(other) unless other.is_a?(Duration) raise DimensionalError, "cannot add a #{other.class} to a Duration; " \ "only a Duration combines with a Duration" end precision = Numeric::Precision.resolve(self.precision, other.precision) self.class.new( Numeric::Precision.add(value, other.value), precision ) end |
#-(other) ⇒ Horologium::Duration
Negative when the other is the longer of the two.
106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/horologium/duration.rb', line 106 def -(other) unless other.is_a?(Duration) raise DimensionalError, "cannot subtract a #{other.class} from a Duration; " \ "only a Duration combines with a Duration" end precision = Numeric::Precision.resolve(self.precision, other.precision) self.class.new( Numeric::Precision.subtract(value, other.value), precision ) end |
#-@ ⇒ Horologium::Duration
The same length, the other way round.
124 125 126 |
# File 'lib/horologium/duration.rb', line 124 def -@ self.class.new(value * -1, precision) end |
#abs ⇒ Horologium::Duration
The same length, never negative.
131 132 133 |
# File 'lib/horologium/duration.rb', line 131 def abs negative? ? -self : self end |
#inspect ⇒ String
166 167 168 |
# File 'lib/horologium/duration.rb', line 166 def inspect format("#<%s %s s (%s)>", self.class, to_f, precision) end |
#negative? ⇒ Boolean
141 142 143 |
# File 'lib/horologium/duration.rb', line 141 def negative? rational.negative? end |
#positive? ⇒ Boolean
146 147 148 |
# File 'lib/horologium/duration.rb', line 146 def positive? rational.positive? end |
#to_f ⇒ Float
The duration in SI seconds. A Float has about 15 digits, so a long duration loses its small end here; use #to_r for the whole of it.
161 162 163 |
# File 'lib/horologium/duration.rb', line 161 def to_f value.to_f end |
#to_r ⇒ Rational
The duration in SI seconds, exactly.
153 154 155 |
# File 'lib/horologium/duration.rb', line 153 def to_r rational end |
#zero? ⇒ Boolean
136 137 138 |
# File 'lib/horologium/duration.rb', line 136 def zero? rational.zero? end |