-
Notifications
You must be signed in to change notification settings - Fork 0
/
time.rb
executable file
·90 lines (67 loc) · 2.07 KB
/
time.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#!/usr/bin/env ruby
unless ARGV.length == 1
puts "Usage: ruby time.rb <filename>"
exit
end
filename = ARGV[0]
# method to subtract time parts
def subtract_time_only(ended, started)
# do all the parsing and string manipulation up front
endSeconds = ended.split(":").last
startSeconds = started.split(":").last
endSeconds = endSeconds.to_i
startSeconds = startSeconds.to_i
endHold = ended.split(":").drop(1).join(":")
startHold = started.split(":").drop(1).join(":")
endMinute = endHold.split(":").first
startMinute = startHold.split(":").first
endHour = ended.split(":").first
startHour = started.split(":").first
# to integers
endHour = endHour.to_i
startHour = startHour.to_i
endMinute = endMinute.to_i
startMinute = startMinute.to_i
# "now check to round up for seconds"
if endSeconds >= 30
endMinute += 1
end
if startSeconds >= 30
startMinute += 1
end
# now check if there's a need to add 60 minutes
if startMinute > endMinute
endMinute += 60
endHour = endHour - 1
end
# do the minute subtraction
resultMinute = endMinute - startMinute
resultHour = endHour - startHour
if resultMinute >= 15
resultHour += 0.5
end
return resultHour
end
File.open(filename,'r') do |file|
file.readlines.each do |line|
counter = 1
task = line.split(".").first
timePart = line.split(".").last
#puts "The task is: #{task}"
#puts "The timePart is: #{timePart}"
started = timePart.split(",,").first
ended = timePart.split(",,").last
puts "Task #{counter} started: #{started}"
puts "Task #{counter} ended: #{ended}"
datePartStarted = started.split(" ").first
timePartStarted = started.split(" ").last
datePartEnded = ended.split(" ").first
timePartEnded = ended.split(" ").last
if datePartStarted == datePartEnded
# diff the time and we're done
end
hours = subtract_time_only("17:15:53", "16:55:17")
puts "#{hours} hours"
counter += 1
end
end