time-duration 0.1.2 → 0.1.3
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 +6 -0
- data/lib/time_duration.rb +9 -45
- data/lib/time_duration/duration.rb +50 -0
- data/lib/time_duration/version.rb +1 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: edf65ba8a819a75d0ac5a7d8cb629cc25eaee25f1618e36773872e84dfb57fa8
|
4
|
+
data.tar.gz: 331760d69065cd8ea1325391428b04e5c89043824b6946e69084a02fa6fe75d7
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: '08600fc46316fbec77b9fbe531dac3695e877ac899963c496edef30122ef1496173b98ab3c64319e983e5d495734352f6a24d407240bc1667a3fea7c2b03e51a'
|
7
|
+
data.tar.gz: 52d7233962170be086de18da4429dde694c2d2a9fe93358595760e70904144699bc7d882e42a840491ad0b0e118593a7e9c0d0cb7fc51400f9cd468bd2bcea10
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -39,6 +39,12 @@ TimeDuration::Duration.new(hour: 1) # => 1:00
|
|
39
39
|
TimeDuration::Duration.new(hour: 1, minute: 10) # => 1:10
|
40
40
|
```
|
41
41
|
|
42
|
+
#### Shorthands
|
43
|
+
```ruby
|
44
|
+
TimeDuration.hour(8) # => 8:00
|
45
|
+
TimeDuration.minute(8) # => 0:08
|
46
|
+
```
|
47
|
+
|
42
48
|
### Operations
|
43
49
|
|
44
50
|
```ruby
|
data/lib/time_duration.rb
CHANGED
@@ -1,56 +1,20 @@
|
|
1
1
|
require 'time_duration/version'
|
2
|
+
require 'time_duration/duration'
|
2
3
|
|
3
4
|
module TimeDuration
|
4
5
|
def self.parse(args)
|
5
6
|
Duration.parse(args)
|
6
7
|
end
|
7
8
|
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
attr_accessor :second
|
12
|
-
|
13
|
-
# TODO: format指定できるようにする
|
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
|
-
end
|
18
|
-
|
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
|
-
end
|
25
|
-
|
26
|
-
def hour
|
27
|
-
minute / 60 + second / 3600
|
28
|
-
end
|
29
|
-
|
30
|
-
def minute
|
31
|
-
(second / 60) % 60
|
32
|
-
end
|
33
|
-
|
34
|
-
# TODO: format指定できるようにする
|
35
|
-
def to_s
|
36
|
-
"%d:%02d" % [hour, minute]
|
37
|
-
end
|
38
|
-
|
39
|
-
def +(time_duration)
|
40
|
-
self.class.new(second: second + time_duration.second)
|
41
|
-
end
|
42
|
-
|
43
|
-
def -(time_duration)
|
44
|
-
self.class.new(second: second - time_duration.second)
|
45
|
-
end
|
9
|
+
def self.hour(hour)
|
10
|
+
Duration.new(hour: hour)
|
11
|
+
end
|
46
12
|
|
47
|
-
|
48
|
-
|
49
|
-
|
13
|
+
def self.minute(minute)
|
14
|
+
Duration.new(minute: minute)
|
15
|
+
end
|
50
16
|
|
51
|
-
|
52
|
-
|
53
|
-
to_s
|
54
|
-
end
|
17
|
+
def self.second(second)
|
18
|
+
Duration.new(second: second)
|
55
19
|
end
|
56
20
|
end
|
@@ -0,0 +1,50 @@
|
|
1
|
+
module TimeDuration
|
2
|
+
class Duration
|
3
|
+
include Comparable
|
4
|
+
|
5
|
+
attr_accessor :second
|
6
|
+
|
7
|
+
# TODO: format指定できるようにする
|
8
|
+
def self.parse(time_as_string, format: '%H:%M')
|
9
|
+
hour, minute = time_as_string.split(':').map(&:to_i)
|
10
|
+
new(hour: hour, minute: minute)
|
11
|
+
end
|
12
|
+
|
13
|
+
def initialize(hour: 0, minute: 0, second: 0)
|
14
|
+
hour = hour.to_i
|
15
|
+
minute = minute.to_i
|
16
|
+
second = second.to_i
|
17
|
+
@second = hour * 3600 + minute * 60 + second
|
18
|
+
end
|
19
|
+
|
20
|
+
def hour
|
21
|
+
minute / 60 + second / 3600
|
22
|
+
end
|
23
|
+
|
24
|
+
def minute
|
25
|
+
(second / 60) % 60
|
26
|
+
end
|
27
|
+
|
28
|
+
# TODO: format指定できるようにする
|
29
|
+
def to_s
|
30
|
+
"%d:%02d" % [hour, minute]
|
31
|
+
end
|
32
|
+
|
33
|
+
def +(time_duration)
|
34
|
+
self.class.new(second: second + time_duration.second)
|
35
|
+
end
|
36
|
+
|
37
|
+
def -(time_duration)
|
38
|
+
self.class.new(second: second - time_duration.second)
|
39
|
+
end
|
40
|
+
|
41
|
+
def <=>(time_duration)
|
42
|
+
self.second <=> time_duration.second
|
43
|
+
end
|
44
|
+
|
45
|
+
# override
|
46
|
+
def inspect
|
47
|
+
to_s
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: time-duration
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Daiki Matoba
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-06-
|
11
|
+
date: 2019-06-15 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -69,6 +69,7 @@ files:
|
|
69
69
|
- bin/console
|
70
70
|
- bin/setup
|
71
71
|
- lib/time_duration.rb
|
72
|
+
- lib/time_duration/duration.rb
|
72
73
|
- lib/time_duration/version.rb
|
73
74
|
- time_duration.gemspec
|
74
75
|
homepage: https://github.com/d-mato/time_duration
|