Skip to content

anonymous /KalleLindstrom secret

require 'optparse'
SECOND_MS = 1000
MINUTE_MS = 60000
HOUR_MS = 3600000
def to_ms(h, m, s, ms)
ms + (s * SECOND_MS) + (m * MINUTE_MS) + (h * HOUR_MS)
end
def format_result(ms)
h = ms / HOUR_MS
ms -= h * HOUR_MS
h = h.to_s
m = ms / MINUTE_MS
ms -= m * MINUTE_MS
m = m.to_s
s = ms / SECOND_MS
ms -= s * SECOND_MS
s = s.to_s
ms = ms.to_s
h = "0#{h}" if h.length == 1
m = "0#{m}" if m.length == 1
s = "0#{s}" if s.length == 1
if ms.length == 1
ms = "00#{ms}"
elsif ms.length == 2
ms = "0#{ms}"
end
"#{h}:#{m}:#{s},#{ms}"
end
def change_time(time, h, m, s, ms)
time =~ /(\d{2}):(\d{2}):(\d{2}),(\d+)/
total_ms = to_ms($1.to_i, $2.to_i, $3.to_i, $4.to_i) + to_ms(h, m, s, ms)
format_result(total_ms)
end
def write_time(input_file, output_file, h, m, s, ms)
File.open(input_file, "r") do |file|
@file_string = file.readlines.join
@file_string.gsub!(/\d{2}:\d{2}:\d{2},\d+/) {|time| change_time(time, h, m, s, ms)}
end
File.open(output_file, "w") do |file|
file.write(@file_string)
end
end
options = {}
option_parser = OptionParser.new do |opts|
options[:operation] = nil
opts.on("--operation OPERATION") {|value| options[:operation] = value}
options[:time] = nil
opts.on('--time TIME') {|value| options[:time] = value}
end
option_parser.parse!
options[:time] =~ /(\d+),(\d+)/
s = $1.to_i
ms = $2.to_i
if options[:operation] == "sub"
s *= -1
ms *= -1
end
input_file = ARGV[0]
output_file = ARGV[1]
write_time(input_file, output_file, 0, 0, s, ms)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.