monotime 0.5.0 → 0.6.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 +4 -4
- data/CHANGELOG.md +78 -0
- data/README.md +40 -0
- data/lib/monotime/version.rb +1 -1
- data/lib/monotime.rb +43 -2
- data/monotime.gemspec +1 -0
- metadata +17 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e0df6ca1dab7d99066d318178d9bb7c6300b4e2cd3f28aa90b5027c0e000930b
|
4
|
+
data.tar.gz: 4e3448b26720601704cd8788d102e1a6bd5e4dfe13e198111d4be7f0fd3c3543
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3947fc4c751704def4c92a07a4761dc04c44c1c6a33184c032eff8930f679d604f6e2d58c3f960b81f5d8316bd9ce6e8bd3044654d778f025b5f376812f50afb
|
7
|
+
data.tar.gz: 3c1c6986817e620b1175c5affd9f708f1f5f45e81eba32e48cedf146a54a6f90c94be54576bd96b6bc5901a58f5a8c816749eeaf7ea9ec767c7660f608539fdc
|
data/CHANGELOG.md
ADDED
@@ -0,0 +1,78 @@
|
|
1
|
+
# Changelog
|
2
|
+
|
3
|
+
## [0.6.0] - 2018-10-26
|
4
|
+
### Added
|
5
|
+
- This `CHANGELOG.md` by request of [@celsworth].
|
6
|
+
- Aliases for `Duration.from_*` and `Duration#to_*` without the prefix. e.g.
|
7
|
+
`Duration.from_secs(42).to_secs == 42` can now be written as
|
8
|
+
`Duration.secs(42).secs == 42`.
|
9
|
+
- `Duration#nonzero?`.
|
10
|
+
- `Instant#in_past?` and `Instant#in_future?`.
|
11
|
+
|
12
|
+
## [0.5.0] - 2018-10-13
|
13
|
+
### Added
|
14
|
+
- `Duration#abs` to make a `Duration` positive.
|
15
|
+
- `Duration#-@` to invert the sign of a `Duration`.
|
16
|
+
- `Duration#positive?`
|
17
|
+
- `Duration#negative?`
|
18
|
+
- `Duration#zero?`
|
19
|
+
|
20
|
+
### Changed
|
21
|
+
- `Instant#sleep` with no argument now sleeps until the `Instant`.
|
22
|
+
- `Duration.from_*` no longer coerce their argument to `Float`.
|
23
|
+
- `Duration#==` checks value via `#to_nanos`, not type.
|
24
|
+
- `Duration#eql?` checks value and type.
|
25
|
+
- `Duration#<=>` compares value via `#to_nanos`.
|
26
|
+
|
27
|
+
## [0.4.0] - 2018-10-09
|
28
|
+
### Added
|
29
|
+
- `Instant#sleep` - sleep to a given `Duration` past an `Instant`.
|
30
|
+
- `Instant#sleep_secs` and `Instant#sleep_millis` convenience methods.
|
31
|
+
- `Duration#sleep` - sleep for the `Duration`.
|
32
|
+
- `Duration#*` - multiply a `Duration` by a number.
|
33
|
+
- `Duration#/` - divide a `Duration` by a number.
|
34
|
+
|
35
|
+
### Changed
|
36
|
+
- More `#to_nanos` `Duration` duck-typing.
|
37
|
+
|
38
|
+
## [0.3.0] - 2018-10-04
|
39
|
+
### Added
|
40
|
+
- `#to_nanos` is now used to duck-type `Duration` everywhere.
|
41
|
+
|
42
|
+
### Changed
|
43
|
+
- Make `<=>` return nil on invalid types, rather than raising a `TypeError`.
|
44
|
+
|
45
|
+
### Removed
|
46
|
+
- Dependency on `dry-equalizer`.
|
47
|
+
|
48
|
+
## [0.2.0] - 2018-10-03
|
49
|
+
### Added
|
50
|
+
- `Instant#to_s` as an alias for `#elapsed.to_s`
|
51
|
+
- `Duration#to_nanos`, with some limited duck-typing.
|
52
|
+
|
53
|
+
### Changed
|
54
|
+
- Switch to microseconds internally.
|
55
|
+
- `Duration#to_{secs,millis,micros}` now return a `Float`.
|
56
|
+
- `Instant#ns` is now `protected`.
|
57
|
+
|
58
|
+
### Fixed
|
59
|
+
- `Duration#to_s` zero-stripping with precision=0.
|
60
|
+
- `Instant#-` argument ordering with other `Instant`.
|
61
|
+
- `Duration#to_micros` returns microseconds, not picoseconds.
|
62
|
+
|
63
|
+
### Removed
|
64
|
+
- `Instant` and `Duration` maths methods no longer support passing an `Integer`
|
65
|
+
number of nanoseconds.
|
66
|
+
|
67
|
+
## [0.1.0] - 2018-10-02
|
68
|
+
### Added
|
69
|
+
- Initial release
|
70
|
+
|
71
|
+
|
72
|
+
[0.1.0]: https://github.com/Freaky/monotime/commits/v0.1.0
|
73
|
+
[0.2.0]: https://github.com/Freaky/monotime/commits/v0.2.0
|
74
|
+
[0.3.0]: https://github.com/Freaky/monotime/commits/v0.3.0
|
75
|
+
[0.4.0]: https://github.com/Freaky/monotime/commits/v0.4.0
|
76
|
+
[0.5.0]: https://github.com/Freaky/monotime/commits/v0.5.0
|
77
|
+
[0.6.0]: https://github.com/Freaky/monotime/commits/v0.6.0
|
78
|
+
[@celsworth]: https://github.com/celsworth
|
data/README.md
CHANGED
@@ -5,6 +5,46 @@
|
|
5
5
|
|
6
6
|
A sensible interface to Ruby's monotonic clock, inspired by Rust.
|
7
7
|
|
8
|
+
## Synopsis
|
9
|
+
|
10
|
+
Monotonic clocks are what you use to measure elapsed time: unlike the system
|
11
|
+
clock they're guaranteed to always move forward.
|
12
|
+
|
13
|
+
This is perfectly valid behaviour:
|
14
|
+
|
15
|
+
```ruby
|
16
|
+
start = Time.new
|
17
|
+
do_something
|
18
|
+
elapsed = Time.new - start # -3599.421234
|
19
|
+
```
|
20
|
+
|
21
|
+
Monotonic clocks never do this. Typically you'd avoid this by using
|
22
|
+
`Process#clock_gettime` with the appropriate arguments:
|
23
|
+
|
24
|
+
```ruby
|
25
|
+
start = Process.clock_gettime(Process::CLOCK_MONOTONIC)
|
26
|
+
do_something
|
27
|
+
elapsed = Process.clock_gettime(Process::CLOCK_MONOTONIC) - start
|
28
|
+
```
|
29
|
+
|
30
|
+
But this isn't very Rubyish - it's a thin veneer over the POSIX C API.
|
31
|
+
|
32
|
+
`monotime` offers this alternative, similar to that found in Rust:
|
33
|
+
|
34
|
+
```ruby
|
35
|
+
include Monotime
|
36
|
+
|
37
|
+
start = Instant.now
|
38
|
+
do_something
|
39
|
+
elapsed = start.elapsed
|
40
|
+
|
41
|
+
# or
|
42
|
+
elapsed = Duration.measure { do_something }
|
43
|
+
```
|
44
|
+
|
45
|
+
Where `elapsed` is a dedicated type for measuring durations, with conversion
|
46
|
+
and formatting methods.
|
47
|
+
|
8
48
|
## Installation
|
9
49
|
|
10
50
|
Add this line to your application's Gemfile:
|
data/lib/monotime/version.rb
CHANGED
data/lib/monotime.rb
CHANGED
@@ -47,6 +47,24 @@ module Monotime
|
|
47
47
|
duration_since(self.class.now)
|
48
48
|
end
|
49
49
|
|
50
|
+
# Return whether this +Instant+ is in the past.
|
51
|
+
#
|
52
|
+
# @return [Boolean]
|
53
|
+
def in_past?
|
54
|
+
elapsed.positive?
|
55
|
+
end
|
56
|
+
|
57
|
+
alias past? in_past?
|
58
|
+
|
59
|
+
# Return whether this +Instant+ is in the future.
|
60
|
+
#
|
61
|
+
# @return [Boolean]
|
62
|
+
def in_future?
|
63
|
+
elapsed.negative?
|
64
|
+
end
|
65
|
+
|
66
|
+
alias future? in_future?
|
67
|
+
|
50
68
|
# Sleep until this +Instant+, plus an optional +Duration+, returning a +Duration+
|
51
69
|
# that's either positive if any time was slept, or negative if sleeping would
|
52
70
|
# require time travel.
|
@@ -154,7 +172,7 @@ module Monotime
|
|
154
172
|
|
155
173
|
# Generate a hash for this type and value.
|
156
174
|
#
|
157
|
-
# @return [
|
175
|
+
# @return [Integer]
|
158
176
|
def hash
|
159
177
|
self.class.hash ^ @ns.hash
|
160
178
|
end
|
@@ -184,6 +202,8 @@ module Monotime
|
|
184
202
|
new(Integer(secs * 1_000_000_000))
|
185
203
|
end
|
186
204
|
|
205
|
+
alias secs from_secs
|
206
|
+
|
187
207
|
# Generate a new +Duration+ measuring the given number of milliseconds.
|
188
208
|
#
|
189
209
|
# @param millis [Numeric]
|
@@ -192,6 +212,8 @@ module Monotime
|
|
192
212
|
new(Integer(millis * 1_000_000))
|
193
213
|
end
|
194
214
|
|
215
|
+
alias millis from_millis
|
216
|
+
|
195
217
|
# Generate a new +Duration+ measuring the given number of microseconds.
|
196
218
|
#
|
197
219
|
# @param micros [Numeric]
|
@@ -200,6 +222,8 @@ module Monotime
|
|
200
222
|
new(Integer(micros * 1_000))
|
201
223
|
end
|
202
224
|
|
225
|
+
alias micros from_micros
|
226
|
+
|
203
227
|
# Generate a new +Duration+ measuring the given number of nanoseconds.
|
204
228
|
#
|
205
229
|
# @param nanos [Numeric]
|
@@ -208,6 +232,8 @@ module Monotime
|
|
208
232
|
new(Integer(nanos))
|
209
233
|
end
|
210
234
|
|
235
|
+
alias nanos from_nanos
|
236
|
+
|
211
237
|
# Return a +Duration+ measuring the elapsed time of the yielded block.
|
212
238
|
#
|
213
239
|
# @example
|
@@ -320,7 +346,7 @@ module Monotime
|
|
320
346
|
|
321
347
|
# Generate a hash for this type and value.
|
322
348
|
#
|
323
|
-
# @return [
|
349
|
+
# @return [Integer]
|
324
350
|
def hash
|
325
351
|
self.class.hash ^ to_nanos.hash
|
326
352
|
end
|
@@ -332,6 +358,8 @@ module Monotime
|
|
332
358
|
to_nanos / 1_000_000_000.0
|
333
359
|
end
|
334
360
|
|
361
|
+
alias secs to_secs
|
362
|
+
|
335
363
|
# Return this +Duration+ in milliseconds.
|
336
364
|
#
|
337
365
|
# @return [Float]
|
@@ -339,6 +367,8 @@ module Monotime
|
|
339
367
|
to_nanos / 1_000_000.0
|
340
368
|
end
|
341
369
|
|
370
|
+
alias millis to_millis
|
371
|
+
|
342
372
|
# Return this +Duration+ in microseconds.
|
343
373
|
#
|
344
374
|
# @return [Float]
|
@@ -346,6 +376,8 @@ module Monotime
|
|
346
376
|
to_nanos / 1_000.0
|
347
377
|
end
|
348
378
|
|
379
|
+
alias micros to_micros
|
380
|
+
|
349
381
|
# Return this +Duration+ in nanoseconds.
|
350
382
|
#
|
351
383
|
# @return [Integer]
|
@@ -353,6 +385,8 @@ module Monotime
|
|
353
385
|
@ns
|
354
386
|
end
|
355
387
|
|
388
|
+
alias nanos to_nanos
|
389
|
+
|
356
390
|
# Return true if this +Duration+ is positive.
|
357
391
|
#
|
358
392
|
# @return [Boolean]
|
@@ -374,6 +408,13 @@ module Monotime
|
|
374
408
|
to_nanos.zero?
|
375
409
|
end
|
376
410
|
|
411
|
+
# Return true if this +Duration+ is non-zero.
|
412
|
+
#
|
413
|
+
# @return [Boolean]
|
414
|
+
def nonzero?
|
415
|
+
to_nanos.nonzero?
|
416
|
+
end
|
417
|
+
|
377
418
|
# Sleep for the duration of this +Duration+. Equivalent to
|
378
419
|
# +Kernel.sleep(duration.to_secs)+.
|
379
420
|
#
|
data/monotime.gemspec
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: monotime
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.6.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Thomas Hurst
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-10-
|
11
|
+
date: 2018-10-26 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -52,6 +52,20 @@ dependencies:
|
|
52
52
|
- - "~>"
|
53
53
|
- !ruby/object:Gem::Version
|
54
54
|
version: '5.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: simplecov
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
55
69
|
description:
|
56
70
|
email:
|
57
71
|
- tom@hur.st
|
@@ -62,6 +76,7 @@ files:
|
|
62
76
|
- ".gitignore"
|
63
77
|
- ".rubocop.yml"
|
64
78
|
- ".travis.yml"
|
79
|
+
- CHANGELOG.md
|
65
80
|
- Gemfile
|
66
81
|
- LICENSE.txt
|
67
82
|
- README.md
|