time_math2 0.0.6 → 0.0.7

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 51045d4f122c5237a82ef69caa42a16680a61e53
4
- data.tar.gz: 3efbc480f4aa87b64b07219746f647101b706372
3
+ metadata.gz: 8c60a385907020504aa0f3ce73225dedc52fc429
4
+ data.tar.gz: b97719ea5945a91030ed88917d28befe8345222a
5
5
  SHA512:
6
- metadata.gz: 3a607d28b91d01cb76d1764564cd7e4cb04a57f994ed4931402b1d6625348ce4042e75fb60c9dd9043f0b5bda5a2b81a15149c9d6668d68be7ed97724d136acc
7
- data.tar.gz: e6f66d0e085b1dd0ac4bb1fc57e94dd949a403a6f09a6242da4444285af99eefd158f0229a03d26e3b31714b208c65dfc11e59ee1a89d46012dbdad03fa9c9d3
6
+ metadata.gz: aa251f08c7a3139d1e77e0097c0404e79504a518d3496ead43422a98b552112cb6bde42dd837a8d0df2004c56dc5f9861d68bcc0f20a44f98c59bf4b5fffb714
7
+ data.tar.gz: 15073277e6847a7112f61d90ffc1c6922dc1566e5da450c19abcf4509978206a5e5266f6098d8949d77341ff65f186d61d2d71b5f6f5aa1ad31913989560d232
@@ -1,5 +1,9 @@
1
1
  # TimeMath Changelog
2
2
 
3
+ # 0.0.7 (2017-05-31)
4
+
5
+ * Fix month advancing/decreasing. Thanks @dikond for pointing to problem!
6
+
3
7
  # 0.0.6 (2016-12-14)
4
8
 
5
9
  * Fix approach to timezone info preservation (previously, it was clear bug, emerging from
@@ -1,4 +1,5 @@
1
1
  # encoding: utf-8
2
+
2
3
  module TimeMath
3
4
  # @private
4
5
  module Measure
@@ -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 = [:floor, :ceil, :round, :next, :prev, :advance, :decrease].freeze
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
- steps.to_i.times.inject(tm) { |t| _succ(t) }
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
- steps.to_i.times.inject(tm) { |t| _prev(t) }
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
@@ -2,7 +2,7 @@ module TimeMath
2
2
  # @private
3
3
  module Util
4
4
  # all except :week
5
- NATURAL_UNITS = [:year, :month, :day, :hour, :min, :sec].freeze
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
@@ -1,4 +1,4 @@
1
1
  module TimeMath
2
2
  # @private
3
- VERSION = '0.0.6'.freeze
3
+ VERSION = '0.0.7'.freeze
4
4
  end
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.6
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: 2016-12-14 00:00:00.000000000 Z
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.5.2
161
+ rubygems_version: 2.6.10
162
162
  signing_key:
163
163
  specification_version: 4
164
164
  summary: Easy time math