time-duration 0.1.1 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/README.md +3 -3
- data/lib/time_duration.rb +18 -18
- data/lib/time_duration/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 352c7207321f8fdb32924ec6eaff8b4e75e77d7d1bf14378fc8ec035cbe59829
|
4
|
+
data.tar.gz: 015b1a1b49f3a12d90df40ffcade70c557038afd1ea73d20916b2cd2a66b35ce
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 480146a36635db00d21432e380cee4c76a84979531d718f6585c07efcebcbeb1ffbfaddf110ac1cad46e4f243793a1fcd42c6c2a6df7c86522343f860465a6b1
|
7
|
+
data.tar.gz: fd059a60b892f96f0e27fd2b10e1d07bb2d5aef7ed665214db376a0c86dfe8028c69533f9ae5779d1feba753b4328c253effcc1ccfddfda1dcf204e0f68c13c0
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -34,9 +34,9 @@ duration.to_s # => "2:10"
|
|
34
34
|
|
35
35
|
```ruby
|
36
36
|
TimeDuration::Duration.new # => 0:00
|
37
|
-
TimeDuration::Duration.new(
|
38
|
-
TimeDuration::Duration.new(
|
39
|
-
TimeDuration::Duration.new(
|
37
|
+
TimeDuration::Duration.new(minute: 10) # => 0:10
|
38
|
+
TimeDuration::Duration.new(hour: 1) # => 1:00
|
39
|
+
TimeDuration::Duration.new(hour: 1, minute: 10) # => 1:10
|
40
40
|
```
|
41
41
|
|
42
42
|
### Operations
|
data/lib/time_duration.rb
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
require
|
1
|
+
require 'time_duration/version'
|
2
2
|
|
3
3
|
module TimeDuration
|
4
4
|
def self.parse(args)
|
@@ -8,44 +8,44 @@ module TimeDuration
|
|
8
8
|
class Duration
|
9
9
|
include Comparable
|
10
10
|
|
11
|
-
attr_accessor :
|
11
|
+
attr_accessor :second
|
12
12
|
|
13
13
|
# TODO: format指定できるようにする
|
14
|
-
def self.parse(time_as_string)
|
15
|
-
|
16
|
-
new(
|
14
|
+
def self.parse(time_as_string, format: '%H:%M')
|
15
|
+
hour, minute = time_as_string.split(':').map(&:to_i)
|
16
|
+
new(hour: hour, minute: minute)
|
17
17
|
end
|
18
18
|
|
19
|
-
def initialize(
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
@
|
19
|
+
def initialize(hour: 0, minute: 0, second: 0)
|
20
|
+
hour = hour.to_i
|
21
|
+
minute = minute.to_i
|
22
|
+
second = second.to_i
|
23
|
+
@second = hour * 3600 + minute * 60 + second
|
24
24
|
end
|
25
25
|
|
26
|
-
def
|
27
|
-
|
26
|
+
def hour
|
27
|
+
minute / 60 + second / 3600
|
28
28
|
end
|
29
29
|
|
30
|
-
def
|
31
|
-
(
|
30
|
+
def minute
|
31
|
+
(second / 60) % 60
|
32
32
|
end
|
33
33
|
|
34
34
|
# TODO: format指定できるようにする
|
35
35
|
def to_s
|
36
|
-
"%d:%02d" % [
|
36
|
+
"%d:%02d" % [hour, minute]
|
37
37
|
end
|
38
38
|
|
39
39
|
def +(time_duration)
|
40
|
-
self.class.new(
|
40
|
+
self.class.new(second: second + time_duration.second)
|
41
41
|
end
|
42
42
|
|
43
43
|
def -(time_duration)
|
44
|
-
self.class.new(
|
44
|
+
self.class.new(second: second - time_duration.second)
|
45
45
|
end
|
46
46
|
|
47
47
|
def <=>(time_duration)
|
48
|
-
self.
|
48
|
+
self.second <=> time_duration.second
|
49
49
|
end
|
50
50
|
|
51
51
|
# override
|