Class: Noir::Command::Calculate::Time
Constant Summary
collapse
- TimeFormat =
"%Y/%m/%d %H:%M:%S"
- TimeRegexp =
Regexp.new('^\s*\d{4}/\d{2}/\d{2} \d{2}:\d{2}:\d{2}$')
Class Method Summary
collapse
sub_commands
check_command_not_found, description, sub_commands
Class Method Details
.calc_total(time_pairs) ⇒ Object
53
54
55
|
# File 'lib/noir/command/calculate/time.rb', line 53
def calc_total time_pairs
time_pairs.map{|start, finish| finish - start}.inject(:+) || 0
end
|
.execute(*args) ⇒ Object
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
# File 'lib/noir/command/calculate/time.rb', line 10
def execute *args
raise 'Please input target on commant arguments. (directory or .txt file)' if args.size.zero?
files = args.map{|f| (f)}.flatten
time_pairs = files.map{|f| pick_up_times(f)}
total_time = 0
files.zip(time_pairs) do |file, pairs|
print_file_total file, pairs
total_time += calc_total(pairs)
puts '-----'
end
puts "all total time : #{seconds_to_string(total_time)}"
end
|
27
28
29
30
31
32
33
34
35
36
|
# File 'lib/noir/command/calculate/time.rb', line 27
def path
raise "File not found. #{path}" unless File.exists?(path)
return path if File.file?(path)
if File.directory?(path)
return Dir.entries(path).reject{|filename| filename.start_with?('.') || File.directory?(filename)}
end
raise "Invalid path. please input file or directory #{path}"
end
|
.pick_up_times(path) ⇒ Object
38
39
40
41
42
43
44
45
46
47
48
49
50
51
|
# File 'lib/noir/command/calculate/time.rb', line 38
def pick_up_times path
text = File.read(path)
times = text.split("\n").select{|t| t =~ TimeRegexp}.map{|t| t.match(TimeRegexp).to_s.gsub(/^\s*/, '')}
raise "Count of formatted time is not even. : #{path}" unless times.size.even?
times = times.map{|t| Time.strptime(t, TimeFormat)}
raise "Formatted time was not sorted in #{path}" unless times.sort == times
diffs = times.map.each_slice(2).to_a
return diffs
end
|
.print_file_total(file, time_pairs) ⇒ Object
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
|
# File 'lib/noir/command/calculate/time.rb', line 57
def print_file_total file, time_pairs
if time_pairs.empty?
puts("Formatted time not found in : #{file}")
return
end
puts file
time_pairs.each do |start, finish|
diff = finish - start
puts "#{start.strftime(TimeFormat)} => #{finish.strftime(TimeFormat)} : #{seconds_to_string(diff)}"
end
puts "file total : #{seconds_to_string(calc_total(time_pairs))}"
end
|
.seconds_to_string(time_sec) ⇒ Object
73
74
75
76
77
78
79
|
# File 'lib/noir/command/calculate/time.rb', line 73
def seconds_to_string time_sec
hours = time_sec.to_i / 3600
mins = (time_sec - (3600 * hours)) / 60
seconds = (time_sec - (3600 * hours)) % 60
format("%d:%02d:%02d", hours, mins, seconds)
end
|