time_interval 0.1.1 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- data/VERSION +1 -1
- data/lib/time_interval.rb +13 -4
- data/test/test_time_interval.rb +12 -1
- data/time_interval.gemspec +1 -1
- metadata +2 -2
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.1.
|
1
|
+
0.1.2
|
data/lib/time_interval.rb
CHANGED
@@ -1,9 +1,9 @@
|
|
1
1
|
class TimeInterval
|
2
2
|
# == Constants ============================================================
|
3
3
|
|
4
|
-
MASK = (0..30).to_a.collect { |i| (1 << 31) - (1 << (
|
4
|
+
MASK = (0..30).to_a.collect { |i| (1 << 31) - (1 << (31 - i)) }.freeze
|
5
5
|
|
6
|
-
DEFAULT_SCALE = (0..30).to_a.collect { |i| [
|
6
|
+
DEFAULT_SCALE = (0..30).to_a.collect { |i| [ MASK[i], 2 ** i ] }.freeze
|
7
7
|
DEFAULT_SCALE_NAME = (0..30).to_a.freeze
|
8
8
|
|
9
9
|
# == Class Methods ========================================================
|
@@ -21,13 +21,18 @@ class TimeInterval
|
|
21
21
|
|
22
22
|
scale_defn = { }
|
23
23
|
scale_factor = 1
|
24
|
+
mask_offset = 0
|
24
25
|
|
25
26
|
definition.each_with_index do |element, i|
|
26
27
|
case (i % 2)
|
27
28
|
when 0
|
28
29
|
scale_factor *= element
|
29
30
|
when 1
|
30
|
-
|
31
|
+
unless (scale_factor == 1)
|
32
|
+
mask_offset += 1
|
33
|
+
end
|
34
|
+
|
35
|
+
scale_defn[element] = [ MASK[mask_offset], scale_factor ]
|
31
36
|
scale_name_defn << element
|
32
37
|
end
|
33
38
|
end
|
@@ -111,7 +116,11 @@ class TimeInterval
|
|
111
116
|
else
|
112
117
|
scale_details = self.class.scale[at_scale]
|
113
118
|
|
114
|
-
|
119
|
+
if (scale_details[1] == 1)
|
120
|
+
@time
|
121
|
+
else
|
122
|
+
-(scale_details[0] | (@time / scale_details[1]))
|
123
|
+
end
|
115
124
|
end
|
116
125
|
end
|
117
126
|
end
|
data/test/test_time_interval.rb
CHANGED
@@ -9,6 +9,12 @@ class TestTimeInterval < Test::Unit::TestCase
|
|
9
9
|
interval = TimeInterval.new
|
10
10
|
|
11
11
|
assert_equal interval.to_i, Time.now.to_i
|
12
|
+
|
13
|
+
assert_equal interval.to_i, interval.to_i(0)
|
14
|
+
assert_equal -(TimeInterval::MASK[1] | interval.to_i / 2), interval.to_i(1)
|
15
|
+
assert_equal -(TimeInterval::MASK[2] | interval.to_i / 4), interval.to_i(2)
|
16
|
+
assert_equal -(TimeInterval::MASK[3] | interval.to_i / 8), interval.to_i(3)
|
17
|
+
assert_equal -(TimeInterval::MASK[4] | interval.to_i / 16), interval.to_i(4)
|
12
18
|
end
|
13
19
|
|
14
20
|
def test_custom_interval
|
@@ -47,7 +53,12 @@ class TestTimeInterval < Test::Unit::TestCase
|
|
47
53
|
|
48
54
|
# Different interval slices can be obtained by passing in the name from
|
49
55
|
# the definition.
|
50
|
-
|
56
|
+
|
57
|
+
# A 1:1 scaling does not have a mask and is simply represented as-is
|
58
|
+
assert_equal seconds, interval.to_i(:second)
|
59
|
+
|
60
|
+
# All other scaling factors have a reduction, so they need to be masked
|
61
|
+
# to indicate which scale level is used.
|
51
62
|
assert_equal -(TimeInterval::MASK[1] | minutes), interval.to_i(:minute)
|
52
63
|
assert_equal -(TimeInterval::MASK[2] | hours), interval.to_i(:hour)
|
53
64
|
assert_equal -(TimeInterval::MASK[3] | days), interval.to_i(:day)
|
data/time_interval.gemspec
CHANGED