duration.rb 0.3.1 → 0.3.3
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 +20 -3
- data/duration.rb.gemspec +1 -1
- data/lib/Duration/Common.rb +0 -3
- data/lib/Duration/Days.rb +0 -3
- data/lib/Duration/Hours.rb +0 -3
- data/lib/Duration/Milliseconds.rb +0 -3
- data/lib/Duration/Minutes.rb +0 -3
- data/lib/Duration/Months.rb +0 -3
- data/lib/Duration/Numeric.rb +0 -3
- data/lib/Duration/Seconds.rb +0 -3
- data/lib/Duration/VERSION.rb +2 -2
- data/lib/Duration/Weeks.rb +0 -3
- data/lib/Duration.rb +4 -4
- data/test/Duration/Days_test.rb +75 -0
- data/test/Duration/Hours_test.rb +75 -0
- data/test/Duration/Milliseconds_test.rb +75 -0
- data/test/Duration/Minutes_test.rb +75 -0
- data/test/Duration/Months_test.rb +77 -0
- data/test/Duration/Numeric_test.rb +111 -0
- data/test/Duration/Seconds_test.rb +75 -0
- data/test/Duration/Weeks_test.rb +75 -0
- data/test/test_all.rb +5 -0
- data/test/test_helper.rb +6 -0
- metadata +16 -3
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 331e01e9221cf04af8f902433555ee8b71a4cfe254df6393cead5e75b3c63942
|
|
4
|
+
data.tar.gz: 37efa6a608b7b2c301ce42eb2401618c96eb038cf874573eb693b79da88e3d18
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 180f1e84cd3348f726e206f86113b236ef4635962d5f54b0b4a83e6e4df58718b6b6202f201d5ab913c5561e77c34dc2879943cb34645334a01b52d314a2ffae
|
|
7
|
+
data.tar.gz: 66ab65f1add027207892c16dc71b4c45c0d9e26bbca579e683612286a240c51b125cd7a4d7bda776877bc150e813059dcbd1935d60b64dbbf828fae48557e467
|
data/README.md
CHANGED
|
@@ -34,10 +34,27 @@ $ gem install duration.rb
|
|
|
34
34
|
|
|
35
35
|
### Conversions
|
|
36
36
|
```ruby
|
|
37
|
+
# Convert to integers
|
|
38
|
+
1.second.to_i # => 1
|
|
39
|
+
30.minutes.to_i # => 30
|
|
40
|
+
2.hours.to_i # => 2
|
|
41
|
+
7.days.to_i # => 7
|
|
42
|
+
|
|
43
|
+
# Convert to floats
|
|
44
|
+
1.second.to_f # => 1.0
|
|
45
|
+
30.minutes.to_f # => 30.0
|
|
46
|
+
2.hours.to_f # => 2.0
|
|
47
|
+
7.days.to_f # => 7.0
|
|
48
|
+
|
|
37
49
|
# Convert between units
|
|
38
|
-
90.minutes.to_hours
|
|
39
|
-
1.5.hours.to_minutes
|
|
40
|
-
1.day.to_seconds
|
|
50
|
+
90.minutes.to_hours # => <Hours: 1.5>
|
|
51
|
+
1.5.hours.to_minutes # => <Minutes: 90>
|
|
52
|
+
1.day.to_seconds # => <Seconds: 86400>
|
|
53
|
+
|
|
54
|
+
# Chain conversions
|
|
55
|
+
90.minutes.to_hours.to_f # => 1.5
|
|
56
|
+
1.5.hours.to_minutes.to_i # => 90
|
|
57
|
+
1.day.to_seconds.to_i # => 86400
|
|
41
58
|
```
|
|
42
59
|
|
|
43
60
|
## Contributing
|
data/duration.rb.gemspec
CHANGED
|
@@ -4,7 +4,7 @@ Gem::Specification.new do |spec|
|
|
|
4
4
|
spec.name = 'duration.rb'
|
|
5
5
|
|
|
6
6
|
spec.version = Duration::VERSION
|
|
7
|
-
spec.date = '2025-10-
|
|
7
|
+
spec.date = '2025-10-25'
|
|
8
8
|
|
|
9
9
|
spec.summary = "Objects for handling durations of time."
|
|
10
10
|
spec.description = "Objects for handling durations of time."
|
data/lib/Duration/Common.rb
CHANGED
data/lib/Duration/Days.rb
CHANGED
data/lib/Duration/Hours.rb
CHANGED
data/lib/Duration/Minutes.rb
CHANGED
data/lib/Duration/Months.rb
CHANGED
data/lib/Duration/Numeric.rb
CHANGED
data/lib/Duration/Seconds.rb
CHANGED
data/lib/Duration/VERSION.rb
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
|
|
2
|
-
VERSION = '0.3.
|
|
1
|
+
module Duration
|
|
2
|
+
VERSION = '0.3.3'
|
|
3
3
|
end
|
data/lib/Duration/Weeks.rb
CHANGED
data/lib/Duration.rb
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
1
|
-
#
|
|
2
|
-
#
|
|
1
|
+
# Duration.rb
|
|
2
|
+
# Duration
|
|
3
3
|
|
|
4
|
-
|
|
5
|
-
# 0.3.1
|
|
4
|
+
module Duration; end
|
|
6
5
|
|
|
7
6
|
require_relative 'Duration/Milliseconds'
|
|
8
7
|
require_relative 'Duration/Seconds'
|
|
@@ -12,3 +11,4 @@ require_relative 'Duration/Days'
|
|
|
12
11
|
require_relative 'Duration/Weeks'
|
|
13
12
|
require_relative 'Duration/Months'
|
|
14
13
|
require_relative 'Duration/Numeric'
|
|
14
|
+
require_relative 'Duration/VERSION'
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
# test/duration/days_test.rb
|
|
2
|
+
|
|
3
|
+
require_relative '../test_helper'
|
|
4
|
+
|
|
5
|
+
describe Duration::Days do
|
|
6
|
+
describe "initialization" do
|
|
7
|
+
it "initializes with a float" do
|
|
8
|
+
days = Duration::Days.new(7.5)
|
|
9
|
+
_(days.instance_variable_get(:@days)).must_equal 7.5
|
|
10
|
+
_(days.instance_variable_get(:@days)).must_be_kind_of Float
|
|
11
|
+
end
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
describe "conversions" do
|
|
15
|
+
before do
|
|
16
|
+
@days = Duration::Days.new(2)
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
it "converts to milliseconds" do
|
|
20
|
+
ms = @days.to_milliseconds
|
|
21
|
+
_(ms).must_be_instance_of Duration::Milliseconds
|
|
22
|
+
_(ms.to_f).must_equal 172800000.0
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
it "converts to seconds" do
|
|
26
|
+
secs = @days.to_seconds
|
|
27
|
+
_(secs).must_be_instance_of Duration::Seconds
|
|
28
|
+
_(secs.to_f).must_equal 172800.0
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
it "converts to minutes" do
|
|
32
|
+
mins = @days.to_minutes
|
|
33
|
+
_(mins).must_be_instance_of Duration::Minutes
|
|
34
|
+
_(mins.to_f).must_equal 2880.0
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
it "converts to hours" do
|
|
38
|
+
hours = @days.to_hours
|
|
39
|
+
_(hours).must_be_instance_of Duration::Hours
|
|
40
|
+
_(hours.to_f).must_equal 48.0
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
it "converts to weeks" do
|
|
44
|
+
weeks = @days.to_weeks
|
|
45
|
+
_(weeks).must_be_instance_of Duration::Weeks
|
|
46
|
+
_(weeks.to_f).must_be_close_to 0.2857, 0.001
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
it "converts to months" do
|
|
50
|
+
months = @days.to_months
|
|
51
|
+
_(months).must_be_instance_of Duration::Months
|
|
52
|
+
_(months.to_f).must_be_close_to 0.06593, 0.00001
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
it "returns itself for to_days" do
|
|
56
|
+
_((@days.to_days).object_id).must_equal @days.object_id
|
|
57
|
+
end
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
describe "time calculations" do
|
|
61
|
+
it "calculates ago from current time" do
|
|
62
|
+
days = Duration::Days.new(1)
|
|
63
|
+
time_ago = days.ago
|
|
64
|
+
_(time_ago).must_be_instance_of Time
|
|
65
|
+
_(Time.now - time_ago).must_be_close_to 86400.0, 1.0
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
it "calculates hence from current time" do
|
|
69
|
+
days = Duration::Days.new(1)
|
|
70
|
+
time_hence = days.hence
|
|
71
|
+
_(time_hence).must_be_instance_of Time
|
|
72
|
+
_(time_hence - Time.now).must_be_close_to 86400.0, 1.0
|
|
73
|
+
end
|
|
74
|
+
end
|
|
75
|
+
end
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
# test/duration/hours_test.rb
|
|
2
|
+
|
|
3
|
+
require_relative '../test_helper'
|
|
4
|
+
|
|
5
|
+
describe Duration::Hours do
|
|
6
|
+
describe "initialization" do
|
|
7
|
+
it "initializes with a float" do
|
|
8
|
+
hours = Duration::Hours.new(2.5)
|
|
9
|
+
_(hours.instance_variable_get(:@hours)).must_equal 2.5
|
|
10
|
+
_(hours.instance_variable_get(:@hours)).must_be_kind_of Float
|
|
11
|
+
end
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
describe "conversions" do
|
|
15
|
+
subject do
|
|
16
|
+
Duration::Hours.new(3)
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
it "converts to milliseconds" do
|
|
20
|
+
ms = subject.to_milliseconds
|
|
21
|
+
_(ms).must_be_instance_of Duration::Milliseconds
|
|
22
|
+
_(ms.to_f).must_equal 10800000.0
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
it "converts to seconds" do
|
|
26
|
+
secs = subject.to_seconds
|
|
27
|
+
_(secs).must_be_instance_of Duration::Seconds
|
|
28
|
+
_(secs.to_f).must_equal 10800.0
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
it "converts to minutes" do
|
|
32
|
+
mins = subject.to_minutes
|
|
33
|
+
_(mins).must_be_instance_of Duration::Minutes
|
|
34
|
+
_(mins.to_f).must_equal 180.0
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
it "converts to days" do
|
|
38
|
+
days = subject.to_days
|
|
39
|
+
_(days).must_be_instance_of Duration::Days
|
|
40
|
+
_(days.to_f).must_equal 0.125
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
it "converts to weeks" do
|
|
44
|
+
weeks = subject.to_weeks
|
|
45
|
+
_(weeks).must_be_instance_of Duration::Weeks
|
|
46
|
+
_(weeks.to_f).must_be_close_to 0.017857, 0.000001
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
it "converts to months" do
|
|
50
|
+
months = subject.to_months
|
|
51
|
+
_(months).must_be_instance_of Duration::Months
|
|
52
|
+
_(months.to_f).must_be_close_to 0.004120, 0.000001
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
it "returns itself for to_hours" do
|
|
56
|
+
_((subject.to_hours).object_id).must_equal subject.object_id
|
|
57
|
+
end
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
describe "time calculations" do
|
|
61
|
+
it "calculates ago from current time" do
|
|
62
|
+
hours = Duration::Hours.new(2)
|
|
63
|
+
time_ago = hours.ago
|
|
64
|
+
_(time_ago).must_be_instance_of Time
|
|
65
|
+
_(Time.now - time_ago).must_be_close_to 7200.0, 1.0
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
it "calculates hence from current time" do
|
|
69
|
+
hours = Duration::Hours.new(2)
|
|
70
|
+
time_hence = hours.hence
|
|
71
|
+
_(time_hence).must_be_instance_of Time
|
|
72
|
+
_(time_hence - Time.now).must_be_close_to 7200.0, 1.0
|
|
73
|
+
end
|
|
74
|
+
end
|
|
75
|
+
end
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
# test/duration/milliseconds_test.rb
|
|
2
|
+
|
|
3
|
+
require_relative '../test_helper'
|
|
4
|
+
|
|
5
|
+
describe Duration::Milliseconds do
|
|
6
|
+
describe "initialization" do
|
|
7
|
+
it "initializes with an float" do
|
|
8
|
+
ms = Duration::Milliseconds.new(1500.5)
|
|
9
|
+
_(ms.instance_variable_get(:@milliseconds)).must_equal 1500.5
|
|
10
|
+
_(ms.instance_variable_get(:@milliseconds)).must_be_kind_of Float
|
|
11
|
+
end
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
describe "conversions" do
|
|
15
|
+
subject do
|
|
16
|
+
Duration::Milliseconds.new(5000)
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
it "converts to seconds" do
|
|
20
|
+
seconds = subject.to_seconds
|
|
21
|
+
_(seconds).must_be_instance_of Duration::Seconds
|
|
22
|
+
_(seconds.to_f).must_equal 5.0
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
it "converts to minutes" do
|
|
26
|
+
minutes = subject.to_minutes
|
|
27
|
+
_(minutes).must_be_instance_of Duration::Minutes
|
|
28
|
+
_(minutes.to_f).must_be_close_to 0.0833, 0.0001
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
it "converts to hours" do
|
|
32
|
+
hours = subject.to_hours
|
|
33
|
+
_(hours).must_be_instance_of Duration::Hours
|
|
34
|
+
_(hours.to_f).must_be_close_to 0.00138, 0.00001
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
it "converts to days" do
|
|
38
|
+
days = subject.to_days
|
|
39
|
+
_(days).must_be_instance_of Duration::Days
|
|
40
|
+
_(days.to_f).must_be_close_to 5.787037e-5, 0.000001
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
it "converts to weeks" do
|
|
44
|
+
weeks = subject.to_weeks
|
|
45
|
+
_(weeks).must_be_instance_of Duration::Weeks
|
|
46
|
+
_(weeks.to_f).must_be_close_to 8.267195e-6, 0.000001
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
it "converts to months" do
|
|
50
|
+
months = subject.to_months
|
|
51
|
+
_(months).must_be_instance_of Duration::Months
|
|
52
|
+
_(months.to_f).must_be_close_to 1.907814e-6, 0.000001
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
it "returns itself for to_milliseconds" do
|
|
56
|
+
_((subject.to_milliseconds).object_id).must_equal subject.object_id
|
|
57
|
+
end
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
describe "time calculations" do
|
|
61
|
+
it "calculates ago from current time" do
|
|
62
|
+
ms = Duration::Milliseconds.new(1000)
|
|
63
|
+
time_ago = ms.ago
|
|
64
|
+
_(time_ago).must_be_instance_of Time
|
|
65
|
+
_(time_ago).must_be :<, Time.now
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
it "calculates hence from current time" do
|
|
69
|
+
ms = Duration::Milliseconds.new(1000)
|
|
70
|
+
time_hence = ms.hence
|
|
71
|
+
_(time_hence).must_be_instance_of Time
|
|
72
|
+
_(time_hence).must_be :>, Time.now
|
|
73
|
+
end
|
|
74
|
+
end
|
|
75
|
+
end
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
# test/duration/minutes_test.rb
|
|
2
|
+
|
|
3
|
+
require_relative '../test_helper'
|
|
4
|
+
|
|
5
|
+
describe Duration::Minutes do
|
|
6
|
+
describe "initialization" do
|
|
7
|
+
it "initializes with a float" do
|
|
8
|
+
mins = Duration::Minutes.new(30.5)
|
|
9
|
+
_(mins.instance_variable_get(:@minutes)).must_equal 30.5
|
|
10
|
+
_(mins.instance_variable_get(:@minutes)).must_be_kind_of Float
|
|
11
|
+
end
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
describe "conversions" do
|
|
15
|
+
subject do
|
|
16
|
+
Duration::Minutes.new(90)
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
it "converts to milliseconds" do
|
|
20
|
+
ms = subject.to_milliseconds
|
|
21
|
+
_(ms).must_be_instance_of Duration::Milliseconds
|
|
22
|
+
_(ms.to_f).must_equal 5400000.0
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
it "converts to seconds" do
|
|
26
|
+
secs = subject.to_seconds
|
|
27
|
+
_(secs).must_be_instance_of Duration::Seconds
|
|
28
|
+
_(secs.to_f).must_equal 5400.0
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
it "converts to hours" do
|
|
32
|
+
hours = subject.to_hours
|
|
33
|
+
_(hours).must_be_instance_of Duration::Hours
|
|
34
|
+
_(hours.to_f).must_equal 1.5
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
it "converts to days" do
|
|
38
|
+
days = subject.to_days
|
|
39
|
+
_(days).must_be_instance_of Duration::Days
|
|
40
|
+
_(days.to_f).must_equal 0.0625
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
it "converts to weeks" do
|
|
44
|
+
weeks = subject.to_weeks
|
|
45
|
+
_(weeks).must_be_instance_of Duration::Weeks
|
|
46
|
+
_(weeks.to_f).must_be_close_to 0.008928, 0.000001
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
it "converts to months" do
|
|
50
|
+
months = subject.to_months
|
|
51
|
+
_(months).must_be_instance_of Duration::Months
|
|
52
|
+
_(months.to_f).must_be_close_to 0.00206, 0.000001
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
it "returns itself for to_minutes" do
|
|
56
|
+
_((subject.to_minutes).object_id).must_equal subject.object_id
|
|
57
|
+
end
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
describe "time calculations" do
|
|
61
|
+
it "calculates ago from current time" do
|
|
62
|
+
mins = Duration::Minutes.new(30)
|
|
63
|
+
time_ago = mins.ago
|
|
64
|
+
_(time_ago).must_be_instance_of Time
|
|
65
|
+
_(Time.now - time_ago).must_be_close_to 1800.0, 1.0
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
it "calculates hence from current time" do
|
|
69
|
+
mins = Duration::Minutes.new(30)
|
|
70
|
+
time_hence = mins.hence
|
|
71
|
+
_(time_hence).must_be_instance_of Time
|
|
72
|
+
_(time_hence - Time.now).must_be_close_to 1800.0, 1.0
|
|
73
|
+
end
|
|
74
|
+
end
|
|
75
|
+
end
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
# test/duration/months_test.rb
|
|
2
|
+
|
|
3
|
+
require_relative '../test_helper'
|
|
4
|
+
|
|
5
|
+
describe Duration::Months do
|
|
6
|
+
describe "initialization" do
|
|
7
|
+
it "initializes with a float" do
|
|
8
|
+
months = Duration::Months.new(3.5)
|
|
9
|
+
_(months.instance_variable_get(:@months)).must_equal 3.5
|
|
10
|
+
_(months.instance_variable_get(:@months)).must_be_kind_of Float
|
|
11
|
+
end
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
describe "conversions" do
|
|
15
|
+
subject do
|
|
16
|
+
Duration::Months.new(2)
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
it "converts to milliseconds" do
|
|
20
|
+
ms = subject.to_milliseconds
|
|
21
|
+
_(ms).must_be_instance_of Duration::Milliseconds
|
|
22
|
+
_(ms.to_f).must_equal 5241600000.0
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
it "converts to seconds" do
|
|
26
|
+
secs = subject.to_seconds
|
|
27
|
+
_(secs).must_be_instance_of Duration::Seconds
|
|
28
|
+
_(secs.to_f).must_equal 5241600.0
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
it "converts to minutes" do
|
|
32
|
+
mins = subject.to_minutes
|
|
33
|
+
_(mins).must_be_instance_of Duration::Minutes
|
|
34
|
+
_(mins.to_f).must_equal 87360.0
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
it "converts to hours" do
|
|
38
|
+
hours = subject.to_hours
|
|
39
|
+
_(hours).must_be_instance_of Duration::Hours
|
|
40
|
+
_(hours.to_f).must_equal 1456.0
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
it "converts to days" do
|
|
44
|
+
days = subject.to_days
|
|
45
|
+
_(days).must_be_instance_of Duration::Days
|
|
46
|
+
_(days.to_f).must_be_close_to 60.66, 0.01
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
it "converts to weeks" do
|
|
50
|
+
weeks = subject.to_weeks
|
|
51
|
+
_(weeks).must_be_instance_of Duration::Weeks
|
|
52
|
+
_(weeks.to_f).must_be_close_to 8.66, 0.01
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
it "returns itself for to_months" do
|
|
56
|
+
_((subject.to_months).object_id).must_equal subject.object_id
|
|
57
|
+
end
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
describe "time calculations" do
|
|
61
|
+
it "calculates ago from current time" do
|
|
62
|
+
months = Duration::Months.new(1)
|
|
63
|
+
time_ago = months.ago
|
|
64
|
+
_(time_ago).must_be_instance_of Time
|
|
65
|
+
# Approximately 30 days worth of seconds
|
|
66
|
+
_(Time.now - time_ago).must_be :>, 2500000.0
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
it "calculates hence from current time" do
|
|
70
|
+
months = Duration::Months.new(1)
|
|
71
|
+
time_hence = months.hence
|
|
72
|
+
_(time_hence).must_be_instance_of Time
|
|
73
|
+
# Approximately 30 days worth of seconds
|
|
74
|
+
_(time_hence - Time.now).must_be :>, 2500000.0
|
|
75
|
+
end
|
|
76
|
+
end
|
|
77
|
+
end
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
# test/duration/numeric_test.rb
|
|
2
|
+
|
|
3
|
+
require_relative '../test_helper'
|
|
4
|
+
|
|
5
|
+
describe Duration::Numeric do
|
|
6
|
+
describe "millisecond extensions" do
|
|
7
|
+
it "creates Milliseconds from integer" do
|
|
8
|
+
ms = 1000.milliseconds
|
|
9
|
+
_(ms).must_be_instance_of Duration::Milliseconds
|
|
10
|
+
_(ms.to_i).must_equal 1000
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
it "supports singular alias" do
|
|
14
|
+
ms = 1.millisecond
|
|
15
|
+
_(ms).must_be_instance_of Duration::Milliseconds
|
|
16
|
+
_(ms.to_i).must_equal 1
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
describe "second extensions" do
|
|
21
|
+
it "creates Seconds from integer" do
|
|
22
|
+
secs = 60.seconds
|
|
23
|
+
_(secs).must_be_instance_of Duration::Seconds
|
|
24
|
+
_(secs.to_i).must_equal 60
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
it "supports singular alias" do
|
|
28
|
+
sec = 1.second
|
|
29
|
+
_(sec).must_be_instance_of Duration::Seconds
|
|
30
|
+
_(sec.to_i).must_equal 1
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
describe "minute extensions" do
|
|
35
|
+
it "creates Minutes from integer" do
|
|
36
|
+
mins = 30.minutes
|
|
37
|
+
_(mins).must_be_instance_of Duration::Minutes
|
|
38
|
+
_(mins.to_i).must_equal 30
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
it "supports singular alias" do
|
|
42
|
+
min = 1.minute
|
|
43
|
+
_(min).must_be_instance_of Duration::Minutes
|
|
44
|
+
_(min.to_i).must_equal 1
|
|
45
|
+
end
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
describe "hour extensions" do
|
|
49
|
+
it "creates Hours from integer" do
|
|
50
|
+
hours = 24.hours
|
|
51
|
+
_(hours).must_be_instance_of Duration::Hours
|
|
52
|
+
_(hours.to_i).must_equal 24
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
it "supports singular alias" do
|
|
56
|
+
hour = 1.hour
|
|
57
|
+
_(hour).must_be_instance_of Duration::Hours
|
|
58
|
+
_(hour.to_i).must_equal 1
|
|
59
|
+
end
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
describe "day extensions" do
|
|
63
|
+
it "creates Days from integer" do
|
|
64
|
+
days = 7.days
|
|
65
|
+
_(days).must_be_instance_of Duration::Days
|
|
66
|
+
_(days.to_i).must_equal 7
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
it "supports singular alias" do
|
|
70
|
+
day = 1.day
|
|
71
|
+
_(day).must_be_instance_of Duration::Days
|
|
72
|
+
_(day.to_i).must_equal 1
|
|
73
|
+
end
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
describe "week extensions" do
|
|
77
|
+
it "creates Weeks from integer" do
|
|
78
|
+
weeks = 4.weeks
|
|
79
|
+
_(weeks).must_be_instance_of Duration::Weeks
|
|
80
|
+
_(weeks.to_i).must_equal 4
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
it "supports singular alias" do
|
|
84
|
+
week = 1.week
|
|
85
|
+
_(week).must_be_instance_of Duration::Weeks
|
|
86
|
+
_(week.to_i).must_equal 1
|
|
87
|
+
end
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
describe "month extensions" do
|
|
91
|
+
it "creates Months from integer" do
|
|
92
|
+
months = 6.months
|
|
93
|
+
_(months).must_be_instance_of Duration::Months
|
|
94
|
+
_(months.to_i).must_equal 6
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
it "supports singular alias" do
|
|
98
|
+
month = 1.month
|
|
99
|
+
_(month).must_be_instance_of Duration::Months
|
|
100
|
+
_(month.to_i).must_equal 1
|
|
101
|
+
end
|
|
102
|
+
end
|
|
103
|
+
|
|
104
|
+
describe "float support" do
|
|
105
|
+
it "works with float values" do
|
|
106
|
+
mins = 1.5.minutes
|
|
107
|
+
_(mins).must_be_instance_of Duration::Minutes
|
|
108
|
+
_(mins.to_f).must_equal 1.5
|
|
109
|
+
end
|
|
110
|
+
end
|
|
111
|
+
end
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
# test/duration/seconds_test.rb
|
|
2
|
+
|
|
3
|
+
require_relative '../test_helper'
|
|
4
|
+
|
|
5
|
+
describe Duration::Seconds do
|
|
6
|
+
describe "initialization" do
|
|
7
|
+
it "initializes with a float" do
|
|
8
|
+
secs = Duration::Seconds.new(60.5)
|
|
9
|
+
_(secs.instance_variable_get(:@seconds)).must_equal 60.5
|
|
10
|
+
_(secs.instance_variable_get(:@seconds)).must_be_kind_of Float
|
|
11
|
+
end
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
describe "conversions" do
|
|
15
|
+
subject do
|
|
16
|
+
Duration::Seconds.new(90)
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
it "converts to milliseconds" do
|
|
20
|
+
ms = subject.to_milliseconds
|
|
21
|
+
_(ms).must_be_instance_of Duration::Milliseconds
|
|
22
|
+
_(ms.to_f).must_equal 90000.0
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
it "converts to minutes" do
|
|
26
|
+
mins = subject.to_minutes
|
|
27
|
+
_(mins).must_be_instance_of Duration::Minutes
|
|
28
|
+
_(mins.to_f).must_equal 1.5
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
it "converts to hours" do
|
|
32
|
+
hours = subject.to_hours
|
|
33
|
+
_(hours).must_be_instance_of Duration::Hours
|
|
34
|
+
_(hours.to_f).must_equal 0.025
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
it "converts to days" do
|
|
38
|
+
days = subject.to_days
|
|
39
|
+
_(days).must_be_instance_of Duration::Days
|
|
40
|
+
_(days.to_f).must_be_close_to 0.001041, 0.000001
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
it "converts to weeks" do
|
|
44
|
+
weeks = subject.to_weeks
|
|
45
|
+
_(weeks).must_be_instance_of Duration::Weeks
|
|
46
|
+
_(weeks.to_f).must_be_close_to 0.0001488, 0.000001
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
it "converts to months" do
|
|
50
|
+
months = subject.to_months
|
|
51
|
+
_(months).must_be_instance_of Duration::Months
|
|
52
|
+
_(months.to_f).must_be_close_to 3.4340659e-05, 0.00000001
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
it "returns itself for to_seconds" do
|
|
56
|
+
_((subject.to_seconds).object_id).must_equal subject.object_id
|
|
57
|
+
end
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
describe "time calculations" do
|
|
61
|
+
it "calculates ago from current time" do
|
|
62
|
+
secs = Duration::Seconds.new(30)
|
|
63
|
+
time_ago = secs.ago
|
|
64
|
+
_(time_ago).must_be_instance_of Time
|
|
65
|
+
_(Time.now - time_ago).must_be_close_to 30.0, 1.0
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
it "calculates hence from current time" do
|
|
69
|
+
secs = Duration::Seconds.new(30)
|
|
70
|
+
time_hence = secs.hence
|
|
71
|
+
_(time_hence).must_be_instance_of Time
|
|
72
|
+
_(time_hence - Time.now).must_be_close_to 30.0, 1.0
|
|
73
|
+
end
|
|
74
|
+
end
|
|
75
|
+
end
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
# test/duration/weeks_test.rb
|
|
2
|
+
|
|
3
|
+
require_relative '../test_helper'
|
|
4
|
+
|
|
5
|
+
describe Duration::Weeks do
|
|
6
|
+
describe "initialization" do
|
|
7
|
+
it "initializes with a float" do
|
|
8
|
+
weeks = Duration::Weeks.new(2.5)
|
|
9
|
+
_(weeks.instance_variable_get(:@weeks)).must_equal 2.5
|
|
10
|
+
_(weeks.instance_variable_get(:@weeks)).must_be_kind_of Float
|
|
11
|
+
end
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
describe "conversions" do
|
|
15
|
+
subject do
|
|
16
|
+
Duration::Weeks.new(2)
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
it "converts to milliseconds" do
|
|
20
|
+
ms = subject.to_milliseconds
|
|
21
|
+
_(ms).must_be_instance_of Duration::Milliseconds
|
|
22
|
+
_(ms.to_f).must_equal 1209600000.0
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
it "converts to seconds" do
|
|
26
|
+
secs = subject.to_seconds
|
|
27
|
+
_(secs).must_be_instance_of Duration::Seconds
|
|
28
|
+
_(secs.to_f).must_equal 1209600.0
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
it "converts to minutes" do
|
|
32
|
+
mins = subject.to_minutes
|
|
33
|
+
_(mins).must_be_instance_of Duration::Minutes
|
|
34
|
+
_(mins.to_f).must_equal 20160.0
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
it "converts to hours" do
|
|
38
|
+
hours = subject.to_hours
|
|
39
|
+
_(hours).must_be_instance_of Duration::Hours
|
|
40
|
+
_(hours.to_f).must_equal 336.0
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
it "converts to days" do
|
|
44
|
+
days = subject.to_days
|
|
45
|
+
_(days).must_be_instance_of Duration::Days
|
|
46
|
+
_(days.to_f).must_equal 14.0
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
it "converts to months" do
|
|
50
|
+
months = subject.to_months
|
|
51
|
+
_(months).must_be_instance_of Duration::Months
|
|
52
|
+
_(months.to_f).must_be_close_to 0.4615384, 0.0000001
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
it "returns itself for to_weeks" do
|
|
56
|
+
_((subject.to_weeks).object_id).must_equal subject.object_id
|
|
57
|
+
end
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
describe "time calculations" do
|
|
61
|
+
it "calculates ago from current time" do
|
|
62
|
+
weeks = Duration::Weeks.new(1)
|
|
63
|
+
time_ago = weeks.ago
|
|
64
|
+
_(time_ago).must_be_instance_of Time
|
|
65
|
+
_(Time.now - time_ago).must_be_close_to 604800.0, 1.0
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
it "calculates hence from current time" do
|
|
69
|
+
weeks = Duration::Weeks.new(1)
|
|
70
|
+
time_hence = weeks.hence
|
|
71
|
+
_(time_hence).must_be_instance_of Time
|
|
72
|
+
_(time_hence - Time.now).must_be_close_to 604800.0, 1.0
|
|
73
|
+
end
|
|
74
|
+
end
|
|
75
|
+
end
|
data/test/test_all.rb
ADDED
data/test/test_helper.rb
ADDED
metadata
CHANGED
|
@@ -1,13 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: duration.rb
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.3.
|
|
4
|
+
version: 0.3.3
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- thoran
|
|
8
|
+
autorequire:
|
|
8
9
|
bindir: bin
|
|
9
10
|
cert_chain: []
|
|
10
|
-
date: 2025-10-
|
|
11
|
+
date: 2025-10-25 00:00:00.000000000 Z
|
|
11
12
|
dependencies: []
|
|
12
13
|
description: Objects for handling durations of time.
|
|
13
14
|
email: code@thoran.com
|
|
@@ -29,10 +30,21 @@ files:
|
|
|
29
30
|
- lib/Duration/Seconds.rb
|
|
30
31
|
- lib/Duration/VERSION.rb
|
|
31
32
|
- lib/Duration/Weeks.rb
|
|
33
|
+
- test/Duration/Days_test.rb
|
|
34
|
+
- test/Duration/Hours_test.rb
|
|
35
|
+
- test/Duration/Milliseconds_test.rb
|
|
36
|
+
- test/Duration/Minutes_test.rb
|
|
37
|
+
- test/Duration/Months_test.rb
|
|
38
|
+
- test/Duration/Numeric_test.rb
|
|
39
|
+
- test/Duration/Seconds_test.rb
|
|
40
|
+
- test/Duration/Weeks_test.rb
|
|
41
|
+
- test/test_all.rb
|
|
42
|
+
- test/test_helper.rb
|
|
32
43
|
homepage: http://github.com/thoran/duration.rb
|
|
33
44
|
licenses:
|
|
34
45
|
- Ruby
|
|
35
46
|
metadata: {}
|
|
47
|
+
post_install_message:
|
|
36
48
|
rdoc_options: []
|
|
37
49
|
require_paths:
|
|
38
50
|
- lib
|
|
@@ -47,7 +59,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
47
59
|
- !ruby/object:Gem::Version
|
|
48
60
|
version: '0'
|
|
49
61
|
requirements: []
|
|
50
|
-
rubygems_version: 3.
|
|
62
|
+
rubygems_version: 3.1.6
|
|
63
|
+
signing_key:
|
|
51
64
|
specification_version: 4
|
|
52
65
|
summary: Objects for handling durations of time.
|
|
53
66
|
test_files: []
|