time_math2 0.0.7 → 0.0.8

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: 8c60a385907020504aa0f3ce73225dedc52fc429
4
- data.tar.gz: b97719ea5945a91030ed88917d28befe8345222a
3
+ metadata.gz: 35d2a3523dac20994a38d19862500ce1eae98854
4
+ data.tar.gz: 82b7664f95fb1879a4911886cd257c41282f9327
5
5
  SHA512:
6
- metadata.gz: aa251f08c7a3139d1e77e0097c0404e79504a518d3496ead43422a98b552112cb6bde42dd837a8d0df2004c56dc5f9861d68bcc0f20a44f98c59bf4b5fffb714
7
- data.tar.gz: 15073277e6847a7112f61d90ffc1c6922dc1566e5da450c19abcf4509978206a5e5266f6098d8949d77341ff65f186d61d2d71b5f6f5aa1ad31913989560d232
6
+ metadata.gz: 99dc9701481d961e93e9d803e4b943ce390bd03e55e8b23a779456094051d1fce129c5bb2a3900cf7f1d8c64f20be684839e2f1f80234c6a6c79912e78eb7954
7
+ data.tar.gz: 22dcc1315f78bd1d323ae9faed9b5e8ef6b39267ddd65b7c3be40cfe33b4f4ec217c79eecfc89c49c14b37ce60c92497ac49d5b5fa7b5d69a6afbd947687da8b
data/.rubocop_todo.yml ADDED
@@ -0,0 +1,9 @@
1
+ # This configuration was generated by
2
+ # `rubocop --auto-gen-config`
3
+ # on 2017-06-28 17:58:01 +0300 using RuboCop version 0.49.1.
4
+ # The point is for the user to remove these configuration records
5
+ # one by one as the offenses are removed from the code base.
6
+ # Note that changes in the inspected code, or installation of new
7
+ # versions of RuboCop, may require this file to be generated again.
8
+
9
+ # Offense count: 13
data/CHANGELOG.md CHANGED
@@ -1,5 +1,12 @@
1
1
  # TimeMath Changelog
2
2
 
3
+ # 0.0.8 (2017-06-02)
4
+
5
+ * Fix `Units::Base#measure` to correctly measure negative distances (e.g. from > to, thanks @kenn for
6
+ pointing it);
7
+ * Cleanup the same method to work correctly with sub-second precisions and different Time-y types.
8
+ * Drop Ruby 2.0 support, finally.
9
+
3
10
  # 0.0.7 (2017-05-31)
4
11
 
5
12
  * Fix month advancing/decreasing. Thanks @dikond for pointing to problem!
@@ -195,9 +195,9 @@ module TimeMath
195
195
  #
196
196
  # @return [Integer] how many full units are inside the period.
197
197
  # :nocov:
198
- def measure(from, to) # rubocop:disable Lint/UnusedMethodArgument
199
- raise NotImplementedError,
200
- '#measure should be implemented in subclasses'
198
+ def measure(from, to)
199
+ from, to = from.to_time, to.to_time unless from.class == to.class
200
+ from <= to ? _measure(from, to) : -_measure(to, from)
201
201
  end
202
202
  # :nocov:
203
203
 
@@ -8,15 +8,15 @@ module TimeMath
8
8
  super(:month)
9
9
  end
10
10
 
11
- def measure(from, to)
11
+ protected
12
+
13
+ def _measure(from, to)
12
14
  ydiff = to.year - from.year
13
15
  mdiff = to.month - from.month
14
16
 
15
17
  to.day >= from.day ? (ydiff * 12 + mdiff) : (ydiff * 12 + mdiff - 1)
16
18
  end
17
19
 
18
- protected
19
-
20
20
  def _advance(tm, steps)
21
21
  target = tm.month + steps.to_i
22
22
  m = (target - 1) % 12 + 1
@@ -6,12 +6,12 @@ module TimeMath
6
6
  sz * MULTIPLIERS[index..-1].inject(:*)
7
7
  end
8
8
 
9
- def measure(from, to)
9
+ protected
10
+
11
+ def _measure(from, to)
10
12
  ((to.to_time - from.to_time) / to_seconds).to_i
11
13
  end
12
14
 
13
- protected
14
-
15
15
  def _advance(tm, steps)
16
16
  _shift(tm, to_seconds(steps))
17
17
  end
@@ -6,16 +6,16 @@ module TimeMath
6
6
  super(:year)
7
7
  end
8
8
 
9
- def measure(from, to)
10
- if Util.merge(from, year: to.year) < to
9
+ protected
10
+
11
+ def _measure(from, to)
12
+ if Util.merge(from, year: to.year) <= to
11
13
  to.year - from.year
12
14
  else
13
15
  to.year - from.year - 1
14
16
  end
15
17
  end
16
18
 
17
- protected
18
-
19
19
  def _advance(tm, steps)
20
20
  Util.merge(tm, year: tm.year + steps.to_i)
21
21
  end
@@ -1,8 +1,8 @@
1
1
  module TimeMath
2
2
  # @private
3
3
  module Util
4
- # all except :week
5
- NATURAL_UNITS = %i[year month day hour min sec].freeze
4
+ COMMON_UNITS = %i[year month day hour min sec].freeze
5
+ NATURAL_UNITS = [*COMMON_UNITS, :subsec].freeze
6
6
  EMPTY_VALUES = [nil, 1, 1, 0, 0, 0].freeze
7
7
 
8
8
  module_function
@@ -43,11 +43,12 @@ module TimeMath
43
43
  end
44
44
 
45
45
  def tm_to_hash(tm)
46
- Hash[*NATURAL_UNITS.flat_map { |s| [s, tm.send(s)] }]
46
+ NATURAL_UNITS.map { |s| [s, extract_component(tm, s)] }.to_h
47
47
  end
48
48
 
49
49
  def hash_to_tm(origin, hash)
50
- components = NATURAL_UNITS.map { |s| hash[s] || 0 }
50
+ components = NATURAL_UNITS[0..-2].map { |s| hash[s] || 0 }
51
+ components[-1] += (hash[:subsec] || hash[:sec_fraction] || 0)
51
52
  array_to_tm(origin, *components)
52
53
  end
53
54
 
@@ -63,5 +64,18 @@ module TimeMath
63
64
  end
64
65
  components[2] = [components[2], days_in_month].min
65
66
  end
67
+
68
+ private
69
+
70
+ module_function
71
+
72
+ def extract_component(tm, component)
73
+ case component
74
+ when :subsec, :sec_fraction
75
+ tm.is_a?(Time) ? tm.subsec : tm.send(:sec_fraction)
76
+ when *COMMON_UNITS
77
+ tm.send(component)
78
+ end
79
+ end
66
80
  end
67
81
  end
@@ -1,4 +1,4 @@
1
1
  module TimeMath
2
2
  # @private
3
- VERSION = '0.0.7'.freeze
3
+ VERSION = '0.0.8'.freeze
4
4
  end
data/time_math2.gemspec CHANGED
@@ -31,6 +31,7 @@ Gem::Specification.new do |s|
31
31
 
32
32
  s.add_development_dependency 'rubocop', '>= 0.30'
33
33
  s.add_development_dependency 'rspec', '>= 3'
34
+ s.add_development_dependency 'rubocop-rspec'
34
35
  s.add_development_dependency 'rspec-its', '~> 1'
35
36
  s.add_development_dependency 'simplecov', '~> 0.9'
36
37
  s.add_development_dependency 'rake'
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.7
4
+ version: 0.0.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Victor Shepelev
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-05-30 00:00:00.000000000 Z
11
+ date: 2017-07-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rubocop
@@ -38,6 +38,20 @@ dependencies:
38
38
  - - ">="
39
39
  - !ruby/object:Gem::Version
40
40
  version: '3'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rubocop-rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
41
55
  - !ruby/object:Gem::Dependency
42
56
  name: rspec-its
43
57
  requirement: !ruby/object:Gem::Requirement
@@ -118,6 +132,7 @@ executables: []
118
132
  extensions: []
119
133
  extra_rdoc_files: []
120
134
  files:
135
+ - ".rubocop_todo.yml"
121
136
  - ".yardopts"
122
137
  - CHANGELOG.md
123
138
  - LICENSE.txt