timing 0.0.11 → 0.1.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/README.md +6 -1
- data/lib/timing/interval.rb +45 -11
- data/lib/timing/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 360bd7d8b70b61144022f1ba72527e969bd89825
|
4
|
+
data.tar.gz: 948ef3019b6e63ded7ed4c8e0658ff36b00c26d7
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5dd7d1e1340e44e1691ca26ab5ce51fd50bafdc627a45a64d4b7072be7cd2ed30b555d241e7d8e441fb6c73c72496ea569a7f7c125cf938cef5f93e3f248bcb5
|
7
|
+
data.tar.gz: 0ccdcb9ea732e7ed8a7c7e295c20a5f606e25126a9ecae8ae0e9c41d5ef7c85b0afddf7ea598d2ac87cb7a858bdd04e2aaf1baff123a651b3edc75b8a433666a
|
data/README.md
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
|
3
3
|
[](https://rubygems.org/gems/timing)
|
4
4
|
[](https://travis-ci.org/gabynaiman/timing)
|
5
|
-
[](https://coveralls.io/
|
5
|
+
[](https://coveralls.io/github/gabynaiman/timing?branch=master)
|
6
6
|
[](https://codeclimate.com/github/gabynaiman/timing)
|
7
7
|
[](https://gemnasium.com/gabynaiman/timing)
|
8
8
|
|
@@ -77,6 +77,11 @@ interval.to_minutes # => 10080.0
|
|
77
77
|
interval.to_hours # => 168.0
|
78
78
|
interval.to_days # => 7.0
|
79
79
|
interval.to_weeks # => 1.0
|
80
|
+
|
81
|
+
interval = Timing::Interval.seconds(1299785)
|
82
|
+
interval.to_human # => 2w 1d 1h 3m 5s
|
83
|
+
interval.to_human(biggest_unit: 'd') # => 15d 1h 3m 5s
|
84
|
+
interval.to_human(smallest_unit: 'm') # => 2w 1d 1h 3m
|
80
85
|
```
|
81
86
|
|
82
87
|
|
data/lib/timing/interval.rb
CHANGED
@@ -17,8 +17,28 @@ module Timing
|
|
17
17
|
w: 60 * 60 * 24 * 7
|
18
18
|
}
|
19
19
|
|
20
|
+
MULTIPLIER = {
|
21
|
+
s: 60,
|
22
|
+
m: 60,
|
23
|
+
h: 24,
|
24
|
+
d: 7,
|
25
|
+
w: 1
|
26
|
+
}
|
27
|
+
|
28
|
+
UNITS = UNITS_NAMES.map(&:first)
|
29
|
+
|
20
30
|
REGEXP = /^([\d\.]+)([smhdw])$/
|
21
31
|
|
32
|
+
def self.parse(expression)
|
33
|
+
match = REGEXP.match expression.strip
|
34
|
+
raise "Invalid interval expression #{expression}" unless match
|
35
|
+
new match.captures[0].to_f * CONVERSIONS[match.captures[1].to_sym]
|
36
|
+
end
|
37
|
+
|
38
|
+
def self.between(time_1, time_2)
|
39
|
+
new (time_1 - time_2).round
|
40
|
+
end
|
41
|
+
|
22
42
|
def initialize(seconds)
|
23
43
|
raise ArgumentError, "#{seconds} is not a number" unless seconds.is_a? Numeric
|
24
44
|
super seconds.abs
|
@@ -47,24 +67,38 @@ module Timing
|
|
47
67
|
end
|
48
68
|
|
49
69
|
def to_s
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
70
|
+
representations = UNITS.map.with_index do |unit, i|
|
71
|
+
representation = to_representation(unit, false, false)
|
72
|
+
[representation, "#{representation.to_i}#{unit}"]
|
73
|
+
end
|
74
|
+
pair = representations.reverse.detect{ |value,representation| value == value.to_i }
|
75
|
+
pair && pair[1] || "#{to_seconds}s"
|
76
|
+
end
|
77
|
+
|
78
|
+
def to_human(options={})
|
79
|
+
biggest_unit = options.fetch(:biggest_unit, :w)
|
80
|
+
smallest_unit = options.fetch(:smallest_unit, :s)
|
81
|
+
last_index = UNITS.index(biggest_unit.to_sym)
|
82
|
+
first_index = UNITS.index(smallest_unit.to_sym)
|
83
|
+
units = UNITS[first_index..last_index]
|
84
|
+
representations = units.map.with_index do |unit, i|
|
85
|
+
acumulate = (i != last_index - first_index)
|
86
|
+
representation = to_representation(unit, acumulate)
|
87
|
+
[representation, "#{representation}#{unit}"]
|
88
|
+
end
|
89
|
+
representations.select{ |(value, string)| value > 0 }.map(&:last).reverse.join(' ')
|
54
90
|
end
|
55
91
|
|
56
92
|
def inspect
|
57
93
|
"#{to_s} (#{to_seconds})"
|
58
94
|
end
|
59
95
|
|
60
|
-
|
61
|
-
match = REGEXP.match expression.strip
|
62
|
-
raise "Invalid interval expression #{expression}" unless match
|
63
|
-
new match.captures[0].to_f * CONVERSIONS[match.captures[1].to_sym]
|
64
|
-
end
|
96
|
+
protected
|
65
97
|
|
66
|
-
def
|
67
|
-
|
98
|
+
def to_representation(unit, acumulate=false, truncate=true)
|
99
|
+
value = to_f / CONVERSIONS[unit]
|
100
|
+
value = value % MULTIPLIER[unit] if acumulate
|
101
|
+
truncate ? value.truncate : value
|
68
102
|
end
|
69
103
|
|
70
104
|
end
|
data/lib/timing/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: timing
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Gabriel Naiman
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-
|
11
|
+
date: 2017-03-21 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: treetop
|