Class: TimeCalc::Diff
- Inherits:
-
Object
- Object
- TimeCalc::Diff
- Includes:
- Comparable
- Defined in:
- lib/time_calc/diff.rb
Overview
Represents difference between two time-or-date values.
Typically created with just
“‘ruby TimeCalc.(t1) - t2 “`
Allows to easily and correctly calculate number of years/monthes/days/etc between two points in time.
Instance Attribute Summary collapse
- #from ⇒ Object readonly
- #to ⇒ Object readonly
Instance Method Summary collapse
-
#-@ ⇒ Diff
“Negates” the diff by swapping its operands.
- #<=>(other) ⇒ -1, ...
-
#days ⇒ Integer
Whole days in diff.
-
#div(span, unit = nil) ⇒ Integer
(also: #/)
Number of whole ‘<unit>`s between `Diff`’s operands.
- #divmod(span, unit = nil) ⇒ (Integer, Time or Date or DateTime)
- #exact ⇒ Object
-
#factorize(zeroes: true, max: :year, min: :sec, weeks: true) ⇒ Hash<Symbol => Integer>
“Factorizes” the distance between two points in time into units: years, months, weeks, days.
-
#hours ⇒ Integer
Whole hours in diff.
-
#initialize(from, to) ⇒ Diff
constructor
A new instance of Diff.
- #inspect ⇒ Object
-
#minutes ⇒ Integer
Whole minutes in diff.
-
#modulo(span, unit = nil) ⇒ Time, Date or DateTime
(also: #%)
Same as integer modulo: the “rest” of whole division of the distance between two time points by ‘<span> <units>`.
-
#months ⇒ Integer
Whole months in diff.
- #negative? ⇒ true, false
- #positive? ⇒ true, false
-
#seconds ⇒ Integer
Whole seconds in diff.
-
#weeks ⇒ Integer
Whole weeks in diff.
-
#years ⇒ Integer
Whole years in diff.
Constructor Details
#initialize(from, to) ⇒ Diff
Typically you should prefer TimeCalc#- to create Diff.
Returns a new instance of Diff.
42 43 44 |
# File 'lib/time_calc/diff.rb', line 42 def initialize(from, to) @from, @to = coerce(try_unwrap(from), try_unwrap(to)).map(&Value.method(:wrap)) end |
Instance Attribute Details
#from ⇒ Object (readonly)
35 36 37 |
# File 'lib/time_calc/diff.rb', line 35 def from @from end |
#to ⇒ Object (readonly)
35 36 37 |
# File 'lib/time_calc/diff.rb', line 35 def to @to end |
Instance Method Details
#-@ ⇒ Diff
“Negates” the diff by swapping its operands.
53 54 55 |
# File 'lib/time_calc/diff.rb', line 53 def -@ Diff.new(to, from) end |
#<=>(other) ⇒ -1, ...
192 193 194 195 196 |
# File 'lib/time_calc/diff.rb', line 192 def <=>(other) return unless other.is_a?(Diff) exact <=> other.exact end |
#days ⇒ Integer
Whole days in diff.
|
# File 'lib/time_calc/diff.rb', line 98
|
#div(span, unit) ⇒ Integer #div(unit) ⇒ Integer Also known as: /
Returns Number of whole ‘<unit>`s between `Diff`’s operands.
90 91 92 93 94 95 96 |
# File 'lib/time_calc/diff.rb', line 90 def div(span, unit = nil) return -(-self).div(span, unit) if negative? span, unit = 1, span if unit.nil? unit = Units.(unit) singular_div(unit).div(span) end |
#divmod(span, unit) ⇒ (Integer, Time or Date or DateTime) #divmod(unit) ⇒ (Integer, Time or Date or DateTime)
68 69 70 71 |
# File 'lib/time_calc/diff.rb', line 68 def divmod(span, unit = nil) span, unit = 1, span if unit.nil? div(span, unit).then { |res| [res, to.+(res * span, unit).unwrap] } end |
#exact ⇒ Object
177 178 179 |
# File 'lib/time_calc/diff.rb', line 177 def exact from.unwrap.to_time - to.unwrap.to_time end |
#factorize(zeroes: true, max: :year, min: :sec, weeks: true) ⇒ Hash<Symbol => Integer>
“Factorizes” the distance between two points in time into units: years, months, weeks, days.
160 161 162 163 164 165 166 167 168 169 170 171 172 |
# File 'lib/time_calc/diff.rb', line 160 def factorize(zeroes: true, max: :year, min: :sec, weeks: true) t = to f = from select_units(max: Units.(max), min: Units.(min), weeks: weeks) .inject({}) { |res, unit| span, t = Diff.new(f, t).divmod(unit) res.merge(unit => span) }.then { |res| next res if zeroes res.drop_while { |_, v| v.zero? }.to_h } end |
#hours ⇒ Integer
Whole hours in diff.
|
# File 'lib/time_calc/diff.rb', line 98
|
#inspect ⇒ Object
47 48 49 |
# File 'lib/time_calc/diff.rb', line 47 def inspect '#<%s(%s − %s)>' % [self.class, from.unwrap, to.unwrap] end |
#minutes ⇒ Integer
Whole minutes in diff.
|
# File 'lib/time_calc/diff.rb', line 98
|
#modulo(span, unit) ⇒ Time, Date or DateTime #modulo(unit) ⇒ Time, Date or DateTime Also known as: %
Same as integer modulo: the “rest” of whole division of the distance between two time points by ‘<span> <units>`. This rest will be also time point, equal to `first diff operand - span units`
132 133 134 |
# File 'lib/time_calc/diff.rb', line 132 def modulo(span, unit = nil) divmod(span, unit).last end |
#months ⇒ Integer
Whole months in diff.
|
# File 'lib/time_calc/diff.rb', line 98
|
#negative? ⇒ true, false
182 183 184 |
# File 'lib/time_calc/diff.rb', line 182 def negative? exact.negative? end |
#positive? ⇒ true, false
187 188 189 |
# File 'lib/time_calc/diff.rb', line 187 def positive? exact.positive? end |
#seconds ⇒ Integer
Whole seconds in diff.
|
# File 'lib/time_calc/diff.rb', line 98
|
#weeks ⇒ Integer
Whole weeks in diff.
|
# File 'lib/time_calc/diff.rb', line 98
|
#years ⇒ Integer
Whole years in diff.
|
# File 'lib/time_calc/diff.rb', line 98
|