secret
anonymous / KalleLindstrom
Last active

  • Download Gist
KalleLindstrom
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
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)

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.