duration.rb 0.3.1

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 549fb2b2e23b0389a558e78c74a5603fc51b11b7fff627c4688a346f4fb4638e
4
+ data.tar.gz: 9f822311766eb84320d3a8a47b8581dae27fa0356bdb843a8ea5f385ac098616
5
+ SHA512:
6
+ metadata.gz: e4ebc76e09a03f18d8181244af6036d511466491d64083db02b2cbf753fe92992a55302fa545141a08f8d49363647e2fb1bf750b41452226a16650fd2f923624
7
+ data.tar.gz: 62eb7d26933dbc4cbfaa16f3dbc5facb84d4db97cac9b75ccf9f4ca5ef19817c7264b9a14a1ee8d054393a26328f20c4756d980dc2202c719aa20e64491d3d06
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://www.rubygems.org'
2
+
3
+ gemspec
data/README.md ADDED
@@ -0,0 +1,53 @@
1
+ # duration.rb
2
+
3
+ ## Description
4
+
5
+ Objects for handling durations of time.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+ ```ruby
11
+ gem 'duration.rb'
12
+ ```
13
+
14
+ And then execute:
15
+ ```shell
16
+ $ bundle
17
+ ```
18
+
19
+ Or install it yourself as:
20
+ ```shell
21
+ $ gem install duration.rb
22
+ ```
23
+
24
+ ## Usage
25
+
26
+ ### Basic Duration Creation
27
+ ```ruby
28
+ # Create durations using convenience methods
29
+ 1.second # => #<Seconds: 1>
30
+ 30.minutes # => #<Minutes: 30>
31
+ 2.hours # => #<Hours: 2>
32
+ 7.days # => #<Days: 7>
33
+ ```
34
+
35
+ ### Conversions
36
+ ```ruby
37
+ # Convert between units
38
+ 90.minutes.to_hours # => 1.5
39
+ 1.5.hours.to_minutes # => 90
40
+ 1.day.to_seconds # => 86400
41
+ ```
42
+
43
+ ## Contributing
44
+
45
+ 1. Fork it (https://github.com/thoran/duration.rb/fork)
46
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
47
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
48
+ 4. Push to the branch (`git push origin my-new-feature`)
49
+ 5. Create a new pull request
50
+
51
+ ## License
52
+
53
+ MIT
@@ -0,0 +1,27 @@
1
+ require_relative './lib/Duration/VERSION'
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = 'duration.rb'
5
+
6
+ spec.version = Duration::VERSION
7
+ spec.date = '2025-10-23'
8
+
9
+ spec.summary = "Objects for handling durations of time."
10
+ spec.description = "Objects for handling durations of time."
11
+
12
+ spec.author = 'thoran'
13
+ spec.email = 'code@thoran.com'
14
+ spec.homepage = 'http://github.com/thoran/duration.rb'
15
+ spec.license = 'Ruby'
16
+
17
+ spec.required_ruby_version = '>= 2.5'
18
+
19
+ spec.files = [
20
+ 'duration.rb.gemspec',
21
+ 'Gemfile',
22
+ Dir['lib/**/*.rb'],
23
+ 'README.md',
24
+ Dir['test/**/*.rb']
25
+ ].flatten
26
+ spec.require_paths = ['lib']
27
+ end
@@ -0,0 +1,17 @@
1
+ # Duration/Common.rb
2
+ # Duration::Common
3
+
4
+ # 20251021
5
+ # 0.3.1
6
+
7
+ module Duration
8
+ module Common
9
+ def ago
10
+ Time.now - self.to_seconds.to_f
11
+ end
12
+
13
+ def hence
14
+ Time.now + self.to_seconds.to_f
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,57 @@
1
+ # Duration/Days.rb
2
+ # Duration::Days
3
+
4
+ # 20251021
5
+ # 0.3.1
6
+
7
+ require_relative './Common'
8
+ require_relative './Hours'
9
+ require_relative './Weeks'
10
+
11
+ module Duration
12
+ class Days
13
+ include Common
14
+
15
+ def to_milliseconds
16
+ to_hours.to_milliseconds
17
+ end
18
+
19
+ def to_seconds
20
+ to_hours.to_seconds
21
+ end
22
+
23
+ def to_minutes
24
+ to_hours.to_minutes
25
+ end
26
+
27
+ def to_hours
28
+ Duration::Hours.new(@days * 24)
29
+ end
30
+
31
+ def to_days
32
+ self
33
+ end
34
+
35
+ def to_weeks
36
+ Duration::Weeks.new(@days/7)
37
+ end
38
+
39
+ def to_months
40
+ to_weeks.to_months
41
+ end
42
+
43
+ def to_i
44
+ @days.to_i
45
+ end
46
+
47
+ def to_f
48
+ @days
49
+ end
50
+
51
+ private
52
+
53
+ def initialize(days = nil)
54
+ @days = days.to_f
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,57 @@
1
+ # Duration/Hours.rb
2
+ # Duration::Hours
3
+
4
+ # 20251021
5
+ # 0.3.1
6
+
7
+ require_relative './Common'
8
+ require_relative './Minutes'
9
+ require_relative './Days'
10
+
11
+ module Duration
12
+ class Hours
13
+ include Common
14
+
15
+ def to_milliseconds
16
+ to_minutes.to_milliseconds
17
+ end
18
+
19
+ def to_seconds
20
+ to_minutes.to_seconds
21
+ end
22
+
23
+ def to_minutes
24
+ Duration::Minutes.new(@hours * 60)
25
+ end
26
+
27
+ def to_hours
28
+ self
29
+ end
30
+
31
+ def to_days
32
+ Duration::Days.new(@hours/24)
33
+ end
34
+
35
+ def to_weeks
36
+ to_days.to_weeks
37
+ end
38
+
39
+ def to_months
40
+ to_days.to_months
41
+ end
42
+
43
+ def to_i
44
+ @hours.to_i
45
+ end
46
+
47
+ def to_f
48
+ @hours
49
+ end
50
+
51
+ private
52
+
53
+ def initialize(hours = nil)
54
+ @hours = hours.to_f
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,56 @@
1
+ # Duration/Milliseconds.rb
2
+ # Duration::Milliseconds
3
+
4
+ # 20251021
5
+ # 0.3.1
6
+
7
+ require_relative './Common'
8
+ require_relative './Seconds'
9
+
10
+ module Duration
11
+ class Milliseconds
12
+ include Common
13
+
14
+ def to_milliseconds
15
+ self
16
+ end
17
+
18
+ def to_seconds
19
+ Duration::Seconds.new(@milliseconds/1000)
20
+ end
21
+
22
+ def to_minutes
23
+ to_seconds.to_minutes
24
+ end
25
+
26
+ def to_hours
27
+ to_seconds.to_hours
28
+ end
29
+
30
+ def to_days
31
+ to_seconds.to_days
32
+ end
33
+
34
+ def to_weeks
35
+ to_seconds.to_weeks
36
+ end
37
+
38
+ def to_months
39
+ to_seconds.to_months
40
+ end
41
+
42
+ def to_i
43
+ @milliseconds.to_i
44
+ end
45
+
46
+ def to_f
47
+ @milliseconds
48
+ end
49
+
50
+ private
51
+
52
+ def initialize(milliseconds = nil)
53
+ @milliseconds = milliseconds.to_f
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,57 @@
1
+ # Duration/Minutes.rb
2
+ # Duration::Minutes
3
+
4
+ # 20251021
5
+ # 0.3.1
6
+
7
+ require_relative './Common'
8
+ require_relative './Seconds'
9
+ require_relative './Hours'
10
+
11
+ module Duration
12
+ class Minutes
13
+ include Common
14
+
15
+ def to_milliseconds
16
+ to_seconds.to_milliseconds
17
+ end
18
+
19
+ def to_seconds
20
+ Duration::Seconds.new(@minutes * 60)
21
+ end
22
+
23
+ def to_minutes
24
+ self
25
+ end
26
+
27
+ def to_hours
28
+ Duration::Hours.new(@minutes/60)
29
+ end
30
+
31
+ def to_days
32
+ to_hours.to_days
33
+ end
34
+
35
+ def to_weeks
36
+ to_hours.to_weeks
37
+ end
38
+
39
+ def to_months
40
+ to_hours.to_months
41
+ end
42
+
43
+ def to_i
44
+ @minutes.to_i
45
+ end
46
+
47
+ def to_f
48
+ @minutes
49
+ end
50
+
51
+ private
52
+
53
+ def initialize(minutes = nil)
54
+ @minutes = minutes.to_f
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,56 @@
1
+ # Duration/Months.rb
2
+ # Duration::Months
3
+
4
+ # 20251021
5
+ # 0.3.1
6
+
7
+ require_relative './Common'
8
+ require_relative './Weeks'
9
+
10
+ module Duration
11
+ class Months
12
+ include Common
13
+
14
+ def to_milliseconds
15
+ to_weeks.to_milliseconds
16
+ end
17
+
18
+ def to_seconds
19
+ to_weeks.to_seconds
20
+ end
21
+
22
+ def to_minutes
23
+ to_weeks.to_minutes
24
+ end
25
+
26
+ def to_hours
27
+ to_weeks.to_hours
28
+ end
29
+
30
+ def to_days
31
+ to_weeks.to_days
32
+ end
33
+
34
+ def to_weeks
35
+ Duration::Weeks.new(@months / 12 * 52)
36
+ end
37
+
38
+ def to_months
39
+ self
40
+ end
41
+
42
+ def to_i
43
+ @months.to_i
44
+ end
45
+
46
+ def to_f
47
+ @months
48
+ end
49
+
50
+ private
51
+
52
+ def initialize(months = nil)
53
+ @months = months.to_f
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,46 @@
1
+ # Duration/Numeric.rb
2
+ # Duration::Numeric
3
+
4
+ # 20251021
5
+ # 0.3.1
6
+
7
+ module Duration
8
+ module Numeric
9
+ def milliseconds
10
+ Duration::Milliseconds.new(self)
11
+ end
12
+ alias_method :millisecond, :milliseconds
13
+
14
+ def seconds
15
+ Duration::Seconds.new(self)
16
+ end
17
+ alias_method :second, :seconds
18
+
19
+ def minutes
20
+ Duration::Minutes.new(self)
21
+ end
22
+ alias_method :minute, :minutes
23
+
24
+ def hours
25
+ Duration::Hours.new(self)
26
+ end
27
+ alias_method :hour, :hours
28
+
29
+ def days
30
+ Duration::Days.new(self)
31
+ end
32
+ alias_method :day, :days
33
+
34
+ def weeks
35
+ Duration::Weeks.new(self)
36
+ end
37
+ alias_method :week, :weeks
38
+
39
+ def months
40
+ Duration::Months.new(self)
41
+ end
42
+ alias_method :month, :months
43
+ end
44
+ end
45
+
46
+ Numeric.include(Duration::Numeric)
@@ -0,0 +1,57 @@
1
+ # Duration/Seconds.rb
2
+ # Duration::Seconds
3
+
4
+ # 20251021
5
+ # 0.3.1
6
+
7
+ require_relative './Common'
8
+ require_relative './Milliseconds'
9
+ require_relative './Minutes'
10
+
11
+ module Duration
12
+ class Seconds
13
+ include Common
14
+
15
+ def to_milliseconds
16
+ Duration::Milliseconds.new(@seconds * 1000)
17
+ end
18
+
19
+ def to_seconds
20
+ self
21
+ end
22
+
23
+ def to_minutes
24
+ Duration::Minutes.new(@seconds/60)
25
+ end
26
+
27
+ def to_hours
28
+ to_minutes.to_hours
29
+ end
30
+
31
+ def to_days
32
+ to_minutes.to_days
33
+ end
34
+
35
+ def to_weeks
36
+ to_minutes.to_weeks
37
+ end
38
+
39
+ def to_months
40
+ to_minutes.to_months
41
+ end
42
+
43
+ def to_i
44
+ @seconds.to_i
45
+ end
46
+
47
+ def to_f
48
+ @seconds
49
+ end
50
+
51
+ private
52
+
53
+ def initialize(seconds = nil)
54
+ @seconds = seconds.to_f
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,3 @@
1
+ class Duration
2
+ VERSION = '0.3.1'
3
+ end
@@ -0,0 +1,57 @@
1
+ # Duration/Weeks.rb
2
+ # Duration::Weeks
3
+
4
+ # 20251021
5
+ # 0.3.1
6
+
7
+ require_relative './Common'
8
+ require_relative './Days'
9
+ require_relative './Months'
10
+
11
+ module Duration
12
+ class Weeks
13
+ include Common
14
+
15
+ def to_milliseconds
16
+ to_days.to_milliseconds
17
+ end
18
+
19
+ def to_seconds
20
+ to_days.to_seconds
21
+ end
22
+
23
+ def to_minutes
24
+ to_days.to_minutes
25
+ end
26
+
27
+ def to_hours
28
+ to_days.to_hours
29
+ end
30
+
31
+ def to_days
32
+ Duration::Days.new(@weeks * 7)
33
+ end
34
+
35
+ def to_weeks
36
+ self
37
+ end
38
+
39
+ def to_months
40
+ Duration::Months.new(@weeks / 52 * 12)
41
+ end
42
+
43
+ def to_i
44
+ @weeks.to_i
45
+ end
46
+
47
+ def to_f
48
+ @weeks
49
+ end
50
+
51
+ private
52
+
53
+ def initialize(weeks = nil)
54
+ @weeks = weeks.to_f
55
+ end
56
+ end
57
+ end
data/lib/Duration.rb ADDED
@@ -0,0 +1,14 @@
1
+ # duration.rb
2
+ # duration
3
+
4
+ # 20251022
5
+ # 0.3.1
6
+
7
+ require_relative 'Duration/Milliseconds'
8
+ require_relative 'Duration/Seconds'
9
+ require_relative 'Duration/Minutes'
10
+ require_relative 'Duration/Hours'
11
+ require_relative 'Duration/Days'
12
+ require_relative 'Duration/Weeks'
13
+ require_relative 'Duration/Months'
14
+ require_relative 'Duration/Numeric'
metadata ADDED
@@ -0,0 +1,53 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: duration.rb
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.3.1
5
+ platform: ruby
6
+ authors:
7
+ - thoran
8
+ bindir: bin
9
+ cert_chain: []
10
+ date: 2025-10-23 00:00:00.000000000 Z
11
+ dependencies: []
12
+ description: Objects for handling durations of time.
13
+ email: code@thoran.com
14
+ executables: []
15
+ extensions: []
16
+ extra_rdoc_files: []
17
+ files:
18
+ - Gemfile
19
+ - README.md
20
+ - duration.rb.gemspec
21
+ - lib/Duration.rb
22
+ - lib/Duration/Common.rb
23
+ - lib/Duration/Days.rb
24
+ - lib/Duration/Hours.rb
25
+ - lib/Duration/Milliseconds.rb
26
+ - lib/Duration/Minutes.rb
27
+ - lib/Duration/Months.rb
28
+ - lib/Duration/Numeric.rb
29
+ - lib/Duration/Seconds.rb
30
+ - lib/Duration/VERSION.rb
31
+ - lib/Duration/Weeks.rb
32
+ homepage: http://github.com/thoran/duration.rb
33
+ licenses:
34
+ - Ruby
35
+ metadata: {}
36
+ rdoc_options: []
37
+ require_paths:
38
+ - lib
39
+ required_ruby_version: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: '2.5'
44
+ required_rubygems_version: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: '0'
49
+ requirements: []
50
+ rubygems_version: 3.7.2
51
+ specification_version: 4
52
+ summary: Objects for handling durations of time.
53
+ test_files: []