rounding 1.0.0 → 1.0.1

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: b4d99f27fefc944b1bcc3a0a47db81474752063b
4
- data.tar.gz: 4a189b36da5764596dc6ab7aafab2ebecc1ecdd8
3
+ metadata.gz: b5698d02622bed949a29add843feb34d2307e4cc
4
+ data.tar.gz: 21a31eed7cf22829238814958cf9bc8789211aa4
5
5
  SHA512:
6
- metadata.gz: 27f3843718eeedbd3729d67dd769342b5e862345a98bdac80b7a1c60eaf074b06018c319658cfa55f76dfa03efc0ea6795deafed07d1f4ca8e86a516e8ce3e24
7
- data.tar.gz: e53bc824025c5c8c3f78474678a0e738b93d3513f0e6c77eae8735d6d4009301c839413101e62c984c15e3bfc78ccb0cdc308577a2f8403ebf6c498f5c4ee343
6
+ metadata.gz: b9e34d49c10670cbe0f49ded5cb3863ee54cea856eb5ac80160138ae4c1c200ebf6402225bc4d2bb39342904efd4e4089437a96748f1ebb66cbbbe26815ca7c9
7
+ data.tar.gz: cff52d3c2a07575a0c836746326906184058362b31ff8ecbee5593bac99fb7ea90c706a3a93332b3ab3868b77c44f51d1f6548e0cbece603e9afd3780dc91f8a
@@ -0,0 +1,8 @@
1
+ ### 2015-04-27 - v1.0.1
2
+
3
+ - Fix rounding errors when `ActiveSupport::Duration` objects were used. Because of quirks of Ruby, they where coerced into floats even for valid Fixnums like `1.second`. The fix ensures that they are converted to rationals for perfect rounding.
4
+ - Allows you to use the gem without `require "date"`. (The gem will not `require "date"` for you.) Previously, the gem errored.
5
+
6
+ ### 2014-09-09 - v1.0.0
7
+
8
+ Initial Release.
@@ -1,3 +1,3 @@
1
1
  Rounding is dedicated to the public domain by its author, Brian Hempel. No rights are reserved. No restrictions are placed on the use of Rounding. That freedom also means, of course, that no warrenty of fitness is claimed; use Rounding at your own risk.
2
2
 
3
- Public domain dedication is explained by the CC0 1.0 summary (and only the summary) at https://creativecommons.org/publicdomain/zero/1.0/
3
+ This public domain dedication follows the the CC0 1.0 at https://creativecommons.org/publicdomain/zero/1.0/
data/README.md CHANGED
@@ -84,9 +84,9 @@ For all methods, the class of the result depends on the input arguments.
84
84
  If you need super-precise rounding, use Rationals.
85
85
 
86
86
  ```ruby
87
- time = Time.now.to_f # => 1410230400.4758651
88
- time.round_to(0.0001) # => 1410230400.4759002 # Float is not exact
89
- time.round_to(1.to_r/10_000) # => (14102304004759/10000)
87
+ seconds = Time.now.to_f # => 1410230400.4758651
88
+ seconds.round_to(0.0001) # => 1410230400.4759002 # Float is not exact
89
+ seconds.round_to(1.to_r/10_000) # => (14102304004759/10000)
90
90
  ```
91
91
 
92
92
  You can provide an offset if you want the multiples to start counting from something other than zero.
@@ -210,23 +210,33 @@ The `round_in_utc_to`, `floor_in_utc_to`, and `ceil_in_utc_to` are also availabl
210
210
 
211
211
  ```ruby
212
212
  fortnight = 2.weeks
213
+ unix_epoch_minus_four = DateTime.new(1970, 1, 1, 0, 0, 0, "-0400")
213
214
 
214
215
  # UNIX epoch is a Thursday in UTC, Julian epoch is a Monday
215
216
 
216
217
  date_time.round_to(fortnight) # => Mon, 15 Sep 2014 00:00:00 -0400
217
218
  date_time.round_in_utc_to(fortnight) # => Wed, 10 Sep 2014 20:00:00 -0400
218
-
219
- unix_epoch_minus_four = DateTime.new(1970, 1, 1, 0, 0, 0, "-0400")
220
219
  date_time.round_to(fortnight, unix_epoch_minus_four) # => Thu, 11 Sep 2014 00:00:00 -0400
221
220
  ```
222
221
 
223
222
  Happy rounding!
224
223
 
224
+ ## Changelog
225
+
226
+ ### 2015-04-27 - v1.0.1
227
+
228
+ - Fix rounding errors when `ActiveSupport::Duration` objects were used. Because of quirks of Ruby, they where coerced into floats even for valid Fixnums like `1.second`. The fix ensures that they are converted to rationals for perfect rounding.
229
+ - Allows you to use the gem without `require "date"`. (The gem will not `require "date"` for you.) Previously, the gem errored.
230
+
231
+ ### 2014-09-09 - v1.0.0
232
+
233
+ Initial Release.
234
+
225
235
  ## License
226
236
 
227
237
  Rounding is dedicated to the public domain by its author, Brian Hempel. No rights are reserved. No restrictions are placed on the use of Rounding. That freedom also means, of course, that no warrenty of fitness is claimed; use Rounding at your own risk.
228
238
 
229
- Public domain dedication is explained by the CC0 1.0 summary (and only the summary) at https://creativecommons.org/publicdomain/zero/1.0/
239
+ This public domain dedication follows the the CC0 1.0 at https://creativecommons.org/publicdomain/zero/1.0/
230
240
 
231
241
  ## Contributing
232
242
 
@@ -1,5 +1,9 @@
1
1
  require 'rounding/time_extensions'
2
2
 
3
+ # Don't explode if you are not using Dates.
4
+ class Date
5
+ end
6
+
3
7
  class DateTime < Date
4
8
  include Rounding::TimeExtensions
5
9
 
@@ -47,7 +51,7 @@ class DateTime < Date
47
51
  if defined?(ActiveSupport::Duration) && ActiveSupport::Duration === value
48
52
  value.to_r / 1.day.to_r
49
53
  else
50
- value
54
+ value
51
55
  end
52
56
  end
53
57
  end
@@ -1,5 +1,6 @@
1
1
  module Rounding::TimeExtensions
2
2
  def floor_to(step, around=-utc_offset)
3
+ step = step.to_r
3
4
  around = around.to_r
4
5
  rational_self = self.to_r
5
6
  difference = rational_self - rational_self.floor_to(step, around)
@@ -7,6 +8,7 @@ module Rounding::TimeExtensions
7
8
  end
8
9
 
9
10
  def ceil_to(step, around=-utc_offset)
11
+ step = step.to_r
10
12
  around = around.to_r
11
13
  rational_self = self.to_r
12
14
  difference = rational_self - rational_self.ceil_to(step, around)
@@ -14,6 +16,7 @@ module Rounding::TimeExtensions
14
16
  end
15
17
 
16
18
  def round_to(step, around=-utc_offset)
19
+ step = step.to_r
17
20
  around = around.to_r
18
21
  rational_self = self.to_r
19
22
  difference = rational_self - rational_self.round_to(step, around)
@@ -1,3 +1,3 @@
1
1
  module Rounding
2
- VERSION = "1.0.0"
2
+ VERSION = "1.0.1"
3
3
  end
@@ -66,6 +66,7 @@ describe "Rounding" do
66
66
  [Time.at(1409955065, 284499), 1.to_r/1000] => Time.at(1409955065, 284000),
67
67
  [Time.at(1409955065, 284500), 1.to_r/1000] => Time.at(1409955065, 284000),
68
68
  [Time.at(1409955065, 284501), 1.to_r/1000] => Time.at(1409955065, 284000),
69
+ [Time.at(1430438399999999999.to_r/1000000000), 1.second] => Time.at(1430438399),
69
70
  [TIME_ZONE.local(2014, 9, 5, 18, 8, 30), 60] => TIME_ZONE.local(2014, 9, 5, 18, 8, 00),
70
71
  [DateTime.new(2014, 9, 5, 18, 8, 29, "-04:00"), 60.to_r/ONE_DAY] => DateTime.new(2014, 9, 5, 18, 8, 00, "-04:00"),
71
72
  [DateTime.new(2014, 9, 5, 18, 8, 30, "-05:00"), 60.to_r/ONE_DAY] => DateTime.new(2014, 9, 5, 18, 8, 00, "-05:00"),
@@ -123,6 +124,7 @@ describe "Rounding" do
123
124
  [Time.at(1409955065, 284499), 1.to_r/1000] => Time.at(1409955065, 285000),
124
125
  [Time.at(1409955065, 284500), 1.to_r/1000] => Time.at(1409955065, 285000),
125
126
  [Time.at(1409955065, 284501), 1.to_r/1000] => Time.at(1409955065, 285000),
127
+ [Time.at(1430438398000000001.to_r/1000000000), 1.second] => Time.at(1430438399),
126
128
  [TIME_ZONE.local(2014, 9, 5, 18, 8, 30), 60] => TIME_ZONE.local(2014, 9, 5, 18, 9, 00),
127
129
  [DateTime.new(2014, 9, 5, 18, 8, 29, "-04:00"), 60.to_r/ONE_DAY] => DateTime.new(2014, 9, 5, 18, 9, 00, "-04:00"),
128
130
  [DateTime.new(2014, 9, 5, 18, 8, 30, "-05:00"), 60.to_r/ONE_DAY] => DateTime.new(2014, 9, 5, 18, 9, 00, "-05:00"),
@@ -194,6 +196,8 @@ describe "Rounding" do
194
196
  [Time.at(1409955065, 284499), 1.to_r/1000] => Time.at(1409955065, 284000),
195
197
  [Time.at(1409955065, 284500), 1.to_r/1000] => Time.at(1409955065, 285000),
196
198
  [Time.at(1409955065, 284501), 1.to_r/1000] => Time.at(1409955065, 285000),
199
+ [Time.at(1430438399500000000.to_r/1000000000), 1.second] => Time.at(1430438400),
200
+ [Time.at(1430438399499999999.to_r/1000000000), 1.second] => Time.at(1430438399),
197
201
  [TIME_ZONE.local(2014, 9, 5, 18, 8, 30), 60] => TIME_ZONE.local(2014, 9, 5, 18, 9, 00),
198
202
  [DateTime.new(2014, 9, 5, 18, 8, 29, "-04:00"), 60.to_r/ONE_DAY] => DateTime.new(2014, 9, 5, 18, 8, 00, "-04:00"),
199
203
  [DateTime.new(2014, 9, 5, 18, 8, 30, "-05:00"), 60.to_r/ONE_DAY] => DateTime.new(2014, 9, 5, 18, 9, 00, "-05:00"),
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rounding
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Brian Hempel
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-09-09 00:00:00.000000000 Z
11
+ date: 2015-04-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -76,6 +76,7 @@ extra_rdoc_files: []
76
76
  files:
77
77
  - ".gitignore"
78
78
  - ".travis.yml"
79
+ - CHANGELOG.md
79
80
  - Gemfile
80
81
  - LICENSE.txt
81
82
  - README.md
@@ -110,7 +111,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
110
111
  version: '0'
111
112
  requirements: []
112
113
  rubyforge_project:
113
- rubygems_version: 2.2.2
114
+ rubygems_version: 2.4.5
114
115
  signing_key:
115
116
  specification_version: 4
116
117
  summary: Floor/nearest/ceiling rounding by arbitrary steps for Integers, Floats, Times,