time_math2 0.0.6 → 0.0.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +4 -0
- data/lib/time_math/measure.rb +1 -0
- data/lib/time_math/op.rb +1 -1
- data/lib/time_math/units/month.rb +6 -22
- data/lib/time_math/util.rb +15 -1
- data/lib/time_math/version.rb +1 -1
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8c60a385907020504aa0f3ce73225dedc52fc429
|
4
|
+
data.tar.gz: b97719ea5945a91030ed88917d28befe8345222a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: aa251f08c7a3139d1e77e0097c0404e79504a518d3496ead43422a98b552112cb6bde42dd837a8d0df2004c56dc5f9861d68bcc0f20a44f98c59bf4b5fffb714
|
7
|
+
data.tar.gz: 15073277e6847a7112f61d90ffc1c6922dc1566e5da450c19abcf4509978206a5e5266f6098d8949d77341ff65f186d61d2d71b5f6f5aa1ad31913989560d232
|
data/CHANGELOG.md
CHANGED
data/lib/time_math/measure.rb
CHANGED
data/lib/time_math/op.rb
CHANGED
@@ -33,7 +33,7 @@ module TimeMath
|
|
33
33
|
# Note that Op also plays well with {Sequence} (see its docs for more).
|
34
34
|
class Op
|
35
35
|
# @private
|
36
|
-
OPERATIONS = [
|
36
|
+
OPERATIONS = %i[floor ceil round next prev advance decrease].freeze
|
37
37
|
|
38
38
|
attr_reader :operations, :arguments
|
39
39
|
|
@@ -1,4 +1,5 @@
|
|
1
1
|
# encoding: utf-8
|
2
|
+
|
2
3
|
module TimeMath
|
3
4
|
module Units
|
4
5
|
# @private
|
@@ -16,32 +17,15 @@ module TimeMath
|
|
16
17
|
|
17
18
|
protected
|
18
19
|
|
19
|
-
def _succ(tm)
|
20
|
-
return Util.merge(tm, year: tm.year + 1, month: 1) if tm.month == 12
|
21
|
-
|
22
|
-
t = Util.merge(tm, month: tm.month + 1)
|
23
|
-
fix_month(t, t.month + 1)
|
24
|
-
end
|
25
|
-
|
26
|
-
def _prev(tm)
|
27
|
-
return Util.merge(tm, year: tm.year - 1, month: 12) if tm.month == 1
|
28
|
-
|
29
|
-
t = Util.merge(tm, month: tm.month - 1)
|
30
|
-
fix_month(t, t.month - 1)
|
31
|
-
end
|
32
|
-
|
33
20
|
def _advance(tm, steps)
|
34
|
-
|
21
|
+
target = tm.month + steps.to_i
|
22
|
+
m = (target - 1) % 12 + 1
|
23
|
+
dy = (target - 1) / 12
|
24
|
+
Util.merge(tm, year: tm.year + dy, month: m)
|
35
25
|
end
|
36
26
|
|
37
27
|
def _decrease(tm, steps)
|
38
|
-
|
39
|
-
end
|
40
|
-
|
41
|
-
# fix for too far advance/insufficient decrease:
|
42
|
-
# Time.new(2013,2,31) #=> 2013-03-02 00:00:00 +0200
|
43
|
-
def fix_month(t, expected)
|
44
|
-
t.month == expected ? day.decrease(t, t.day) : t
|
28
|
+
_advance(tm, -steps)
|
45
29
|
end
|
46
30
|
end
|
47
31
|
end
|
data/lib/time_math/util.rb
CHANGED
@@ -2,7 +2,7 @@ module TimeMath
|
|
2
2
|
# @private
|
3
3
|
module Util
|
4
4
|
# all except :week
|
5
|
-
NATURAL_UNITS = [
|
5
|
+
NATURAL_UNITS = %i[year month day hour min sec].freeze
|
6
6
|
EMPTY_VALUES = [nil, 1, 1, 0, 0, 0].freeze
|
7
7
|
|
8
8
|
module_function
|
@@ -17,6 +17,7 @@ module TimeMath
|
|
17
17
|
|
18
18
|
def array_to_tm(origin, *components)
|
19
19
|
components = EMPTY_VALUES.zip(components).map { |d, c| c || d }
|
20
|
+
fix_month_day(components)
|
20
21
|
|
21
22
|
case origin
|
22
23
|
when Time
|
@@ -49,5 +50,18 @@ module TimeMath
|
|
49
50
|
components = NATURAL_UNITS.map { |s| hash[s] || 0 }
|
50
51
|
array_to_tm(origin, *components)
|
51
52
|
end
|
53
|
+
|
54
|
+
DAYS_IN_MONTH = [nil, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31].freeze
|
55
|
+
|
56
|
+
def fix_month_day(components)
|
57
|
+
return if components[2].nil? || components[1].nil?
|
58
|
+
days_in_month =
|
59
|
+
if components[1] == 2 && components[0] && Date.gregorian_leap?(components[0])
|
60
|
+
29
|
61
|
+
else
|
62
|
+
DAYS_IN_MONTH[components[1]]
|
63
|
+
end
|
64
|
+
components[2] = [components[2], days_in_month].min
|
65
|
+
end
|
52
66
|
end
|
53
67
|
end
|
data/lib/time_math/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: time_math2
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.7
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Victor Shepelev
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2017-05-30 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rubocop
|
@@ -158,7 +158,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
158
158
|
version: '0'
|
159
159
|
requirements: []
|
160
160
|
rubyforge_project:
|
161
|
-
rubygems_version: 2.
|
161
|
+
rubygems_version: 2.6.10
|
162
162
|
signing_key:
|
163
163
|
specification_version: 4
|
164
164
|
summary: Easy time math
|