@@ -12,6 +12,24 @@ class TimeCalc
12
12
# Allows to easily and correctly calculate number of years/monthes/days/etc between two points in
13
13
# time.
14
14
#
15
+ # @example
16
+ # t1 = Time.parse('2019-06-01 14:50')
17
+ # t2 = Time.parse('2019-06-15 12:10')
18
+ # (TimeCalc.(t2) - t1).div(:day)
19
+ # # => 13
20
+ # # the same:
21
+ # (TimeCalc.(t2) - t1).days
22
+ # # => 13
23
+ # (TimeCalc.(t2) - t1).div(3, :hours)
24
+ # # => 111
25
+ #
26
+ # (TimeCalc.(t2) - t1).factorize
27
+ # # => {:year=>0, :month=>0, :week=>1, :day=>6, :hour=>21, :min=>20, :sec=>0}
28
+ # (TimeCalc.(t2) - t1).factorize(weeks: false)
29
+ # # => {:year=>0, :month=>0, :day=>13, :hour=>21, :min=>20, :sec=>0}
30
+ # (TimeCalc.(t2) - t1).factorize(weeks: false, zeroes: false)
31
+ # # => {:day=>13, :hour=>21, :min=>20, :sec=>0}
32
+ #
15
33
class Diff
16
34
# @private
17
35
attr_reader :from , :to
@@ -80,6 +98,28 @@ def div(span, unit = nil)
80
98
singular_div ( unit ) . div ( span )
81
99
end
82
100
101
+ # @!method years
102
+ # Whole years in diff.
103
+ # @return [Integer]
104
+ # @!method months
105
+ # Whole months in diff.
106
+ # @return [Integer]
107
+ # @!method weeks
108
+ # Whole weeks in diff.
109
+ # @return [Integer]
110
+ # @!method days
111
+ # Whole days in diff.
112
+ # @return [Integer]
113
+ # @!method hours
114
+ # Whole hours in diff.
115
+ # @return [Integer]
116
+ # @!method minutes
117
+ # Whole minutes in diff.
118
+ # @return [Integer]
119
+ # @!method seconds
120
+ # Whole seconds in diff.
121
+ # @return [Integer]
122
+
83
123
# Same as integer modulo: the "rest" of whole division of the distance between two time points by
84
124
# `<span> <units>`. This rest will be also time point, equal to `first diff operand - span units`
85
125
#
0 commit comments