easy_time 0.1.2 → 0.2.0

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 4495654dbc3739198ac730b17b43fd61d66edf258f3a1506e7608414b6e16328
4
- data.tar.gz: f3cb77f9f6a71a2d0c48811f3f69153f93678eda2e7510db58fc26657cfe4764
3
+ metadata.gz: 778f54e349e3d08aa3382639092e1c82bac41c6f2251628daa15e0d916e6bfce
4
+ data.tar.gz: 07f883f381d1fc7820567efeff2b686343c34b9932d3a9e8427508e1b4dc09b5
5
5
  SHA512:
6
- metadata.gz: 1bff901b47f340553598a7e1f618faaf011ead4e86c3a32abf84a9edd64afecd043a932811d83a63d6820e319a89fb4c0df2f451733f1c22a5b1465585f83415
7
- data.tar.gz: c87ede1b52cd24227c3734c41230af038ee3710ee46dffd9c11776dab8dfe73f8553999e5d04da9ba15f45a4f52a0ebc9133c2a8da228e9c5c2af48240b0bdcc
6
+ metadata.gz: e89457620f63806559c3c55ea84167eb1ebe1d8649be37cb429b8f4de90585a402b5921cc68cf6e9392fa99ad3a18d218ed2da1c954caeafcb63e0aed762f0d7
7
+ data.tar.gz: 8f8a023f14f337fe43c3651b684477789f6ce16dd9616d4f8f05e8a9eac82b534dbbed56acd746350da57b932f2fedb7344ba2041f4d573211c37528fc70c7c0
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- easy_time (0.1.2)
4
+ easy_time (0.2.0)
5
5
  activesupport
6
6
 
7
7
  GEM
data/README.md CHANGED
@@ -137,7 +137,7 @@ A `String` value is parsed with the `convert` method.
137
137
 
138
138
  Most modern time format strings are recognized: ISO8601, RFD2822, HTTPDate,
139
139
  XMLSchema, and even some natural date and time formats. Many systems use an
140
- [ISO8601](https://en.wikipedia.org/wiki/ISO_8601) string, which has date, time,
140
+ [ISO8601](https://en.wikipedia.org/wiki/ISO_8601) string, which has date, time,
141
141
  and timezone components, with each variant having some variations.
142
142
 
143
143
  With `EasyTime`, you don't have to know which time string format to use,
@@ -178,6 +178,18 @@ or
178
178
 
179
179
  EasyTime.between?(t1, time_range) # => true if t1 is within the time_range
180
180
 
181
+ ### Easy Time Arithmetic
182
+
183
+ These class methods make it easy to do arithmetic on time values and strings.
184
+
185
+ EasyTime.add(time1, duration) # => new EasyTime value
186
+
187
+ EasyTime.subtract(time1, time2) # => duration
188
+
189
+ EasyTime.subtract(time, duration) # => new EasyTime value
190
+
191
+ In the above examples, `time1` can be any of the usual Date and Time classes,
192
+ including a time string value.
181
193
 
182
194
  ### Configuring the Comparison Tolerance
183
195
 
@@ -230,12 +242,12 @@ Arithmetic operators with auto-conversion are also supported:
230
242
  t1 - t2
231
243
  t1 - duration
232
244
 
233
- The `t2` can be a date, time or even a time string. In which case, the are
245
+ The `t2` value can be a date, time or even a time string. In which case, it is
234
246
  converted to a `Time` value, and the subtraction is performed between two
235
- dates, with a duration result.
247
+ times, with a duration result.
236
248
 
237
249
  When arithmetic is performed with a `duration` value, then the result is a new
238
- updated `EasyTime` value
250
+ updated `EasyTime` value.
239
251
 
240
252
  ### Auto-Conversion Of Comparison Values
241
253
 
@@ -280,14 +292,36 @@ After checking out the repo, run `bin/setup` to install dependencies. Then, run
280
292
  `rake spec` to run the tests. You can also run `bin/console` for an interactive
281
293
  prompt that will allow you to experiment.
282
294
 
283
- To install this gem onto your local machine, run `bundle exec rake install`. To
284
- release a new version, update the version number in `version.rb`, and then run
285
- `bundle exec rake release`, which will create a git tag for the version, push
286
- git commits and tags, and push the `.gem` file to
287
- [rubygems.org](https://rubygems.org).
295
+ To install this gem onto your local machine, run `bundle exec rake install`.
296
+
297
+ ## Testing
298
+
299
+ There is a complete set of tests in the `spec` directory. To run all the tests:
300
+
301
+ bundle exec rake spec
302
+
303
+ To run all the tests, with `simplecov` code coverage analysis:
304
+
305
+ bundle exec rake spec:coverage
306
+
307
+ Then, to review the code coverage:
308
+
309
+ open coverage/index.html
310
+
311
+ ## Additional Documentation
312
+
313
+ The `easy_time` gem has been developed using `yard` markdown, so it is fairly
314
+ simple to review the gem API documentation:
315
+
316
+ bundle exec yard
317
+ open doc/index.html
288
318
 
289
319
  ## Contributing
290
320
 
321
+ If you wish to contribute, please fork the repo, create a new branch for your suggested
322
+ improvement, and create a pull request based off of your forked repo branch. Please do
323
+ not make changes in the version.
324
+
291
325
  Bug reports and pull requests are welcome on GitHub at https://github.com/aks/easy_time.
292
326
 
293
327
 
data/lib/easy_time.rb CHANGED
@@ -23,25 +23,36 @@ require 'easy_time/convert'
23
23
  # `RFC2822`, `HTTPDate`, `XMLSchema`, and `ISO8601` strings and provides
24
24
  # comparisons that have an adjustable tolerance. With `EasyTime` methods, you
25
25
  # can reliably compare two timestamps and determine which one is "newer", "older"
26
- # or the "same" withing a configurable tolerance. The default comparison
27
- # tolerance is 1.minute.
26
+ # or the "same" withing a configurable tolerance.
28
27
  #
29
- # In other words, if you have a time-stamp from an `ActiveRecord` object that is
30
- # a few seconds different from a related object obtained from a 3rd-party system,
31
- # (eg: AWS S3), then logically, from an application perspective, these two
32
- # objects could be considered having the "same" time-stamp.
28
+ # The default comparison tolerance is 1 second. This means that if a local
29
+ # object is created at time t1 and then transferred across the network and used
30
+ # to create a corresponding object in a 3rd-party system, eg: AWS S3, at time
31
+ # t2, then if `(t1 - t2).abs` is < 1.second the two times would be logicall the
32
+ # same.
33
+ #
34
+ # In other words, if you have a time-stamp from an `ActiveRecord` object that
35
+ # is a few milliseconds different from a related object obtained from a
36
+ # 3rd-party system, (eg: AWS S3), then logically, from an application
37
+ # perspective, these two objects could be considered having the "same"
38
+ # time-stamp.
33
39
  #
34
40
  # This is quite useful when one is trying to keep state synchronized between
35
41
  # different systems. How does one know if an object is "newer" or "older" than
36
- # that from another system? If the system time from the connected systems varies
37
- # by a few or more seconds, then comparisons needs to have some tolerance.
42
+ # that from another system? If the system time from the connected systems
43
+ # varies by a few or more seconds, then comparisons needs to have some
44
+ # tolerance.
38
45
  #
39
46
  # Having a tolerant comparison makes "newness" and "oldness" checks easier to
40
47
  # manage across systems with possibly varying time sources.
41
48
  #
49
+ # However, it is also important to keep the tolerance as small as possible in
50
+ # order to avoid causing more problems, such as false equivalences when objects
51
+ # really were created at different moments in time.
52
+ #
42
53
  # `EasyTime` objects are just like Time objects, except:
43
54
  #
44
- # - they auto-convert most date and time objects to Time objects
55
+ # - they auto-convert most time objects, including strings, to Time objects
45
56
  # - they provide configurable tolerant comparisons between two time objects
46
57
  #
47
58
  # Even if you decide to set the configurable comparison tolerance to zero
@@ -56,7 +67,7 @@ require 'easy_time/convert'
56
67
  #
57
68
  # The conversion to an `EasyTime` can also be provided with a tolerance value:
58
69
  #
59
- # time.easy_time(tolerance: 5.seconds)
70
+ # time.easy_time(tolerance: 2.seconds)
60
71
  #
61
72
  # These are the currently known date and time classes the values of which will
62
73
  # be automatically converted to an `EasyTime` value with tolerant comparisons:
@@ -85,7 +96,7 @@ class EasyTime
85
96
  #
86
97
  # EasyTime.comparison_tolerance = 0
87
98
 
88
- DEFAULT_TIME_COMPARISON_TOLERANCE = 1.minute
99
+ DEFAULT_TIME_COMPARISON_TOLERANCE = 1.second
89
100
 
90
101
  class << self
91
102
  # @example These comparison methods observe the comparison tolerance
@@ -160,6 +171,27 @@ class EasyTime
160
171
  new(time1, tolerance: tolerance) <=> time2
161
172
  end
162
173
 
174
+ # These methods make it easy to add or subtract dates and durations
175
+ # EasyTime.add(time, duration, tolerance: nil)
176
+ # EasyTime.subtract(time, time_or_duration, tolerance: nil)
177
+
178
+ # @param time [Time,DateTime,ActiveSupport::TimeWithZone,Date,String,Integer,Array<Integer>] a time value
179
+ # @param duration [ActiveSupport::Duration,Integer,Float] a duration value
180
+ # @return [EasyTime] the `time` with the `duration` added
181
+
182
+ def add(time, duration)
183
+ EasyTime.new(time) + duration
184
+ end
185
+
186
+ # @param time [Time,DateTime,ActiveSupport::TimeWithZone,Date,String,Integer,Array<Integer>] a time value
187
+ # @param time_or_duration [Time,DateTime,ActiveSupport::Duration,Integer,Float] a time or duration value
188
+ # @return [EasyTime,Duration] an `EasyTime` value, when a duration is subtracted from a time, or
189
+ # a duration _(Integer)_ value, when one time is subtracted from another time.
190
+
191
+ def subtract(time, time_or_duration)
192
+ EasyTime.new(time) - time_or_duration
193
+ end
194
+
163
195
  attr_writer :comparison_tolerance
164
196
 
165
197
  # Class methods to set the class-level comparison tolerance
@@ -287,7 +319,7 @@ class EasyTime
287
319
 
288
320
  def <=>(other)
289
321
  diff = self - other # note: this has a side-effect of setting @other_time
290
- if diff && diff.abs.to_i <= comparison_tolerance.to_i
322
+ if diff && diff.to_i.abs <= comparison_tolerance.to_i
291
323
  0
292
324
  elsif diff
293
325
  time <=> other_time
@@ -1,3 +1,3 @@
1
1
  class EasyTime
2
- VERSION = "0.1.2"
2
+ VERSION = "0.2.0"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: easy_time
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alan Stebbens