-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstant.rb
63 lines (53 loc) · 1.04 KB
/
instant.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
class Instant
MINUTES_IN_DAY = 1440
@time
def initialize(t)
if t.is_a? Numeric
@time = t
elsif t.is_a? String
@time = convert_to_m(t)
else
raise "Invalid value to initialize an Instant."
end
end
def to_i
@time
end
def to_s
h = (@time / 60).to_i.to_s.rjust(2, '0')
m = (@time % 60).to_i.to_s.rjust(2, '0')
h + ":" + m
end
def +(t)
Instant.new(@time + t.to_i)
end
def -(t)
Instant.new(@time - t.to_i)
end
def self.now
Instant.new(Time.now.hour.to_s + ":" + Time.now.min.to_s)
end
private
def convert_to_m(s)
r = split_with_separator(s)
if r >= MINUTES_IN_DAY
r = split_without_separator(s)
end
r
end
def split_with_separator(s)
sep = /[\.:]/
h = s.split(sep)[0]
m = s.split(sep)[1]
to_m(h, m)
end
def split_without_separator(s)
s = s.gsub(/\D/,'').to_s.rjust(4,'0')
h = s[0..1]
m = s[2..3]
to_m(h, m)
end
def to_m(h, m)
h.to_i * 60 + m.to_i
end
end