secret
anonymous / MichaelKohl
Last active

Michael Kohl Fun Solution

  • Download Gist
MichaelKohl
Ruby
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
#!/usr/bin/env ruby
# encoding: utf-8
 
require 'optparse'
require 'time'
require 'fileutils'
 
options = {}
ARGV[0] = "-h" unless (5..6).include? ARGV.length
 
optparse = OptionParser.new do |opts|
opts.banner = "Usage: #{File.basename($0)} OPTIONS input_file [output_file]"
opts.separator ''
opts.separator 'Specific options:'
opts.on( "-o", "--operation OPERATION", [:add, :subtract], "[add|subtract]" ) do |o|
options[:operation] = o
end
opts.on( "-t", "--time OFFSET", Float, "time offset (as float)" ) do |t|
options[:time] = t
end
opts.separator ''
opts.on_tail("-h", "--help", "Show this help message.") { puts opts; exit }
end
begin
optparse.parse!
rescue OptionParser::InvalidArgument
puts optparse.help
exit 1
end
 
infile = ARGV[0]
outfile = ARGV[1] || "#{infile}.new"
options[:time] *= -1 if options[:operation] == :subtract
 
shift_timestamp = -> ts do
t = Time.parse(ts.gsub(',','.')) + options[:time]
"#{t.strftime("%H:%M:%S")},#{t.usec.to_s[0..2]}"
end
 
begin
File.open(outfile, "w") do |f|
File.open(infile).each_line do |orig_line|
if orig_line.match(/(.*) --> (.*)/)
start, stop = $1, $2
f.puts "#{shift_timestamp[start]} --> #{shift_timestamp[stop]}"
else
f.puts orig_line.chomp
end
end
end
rescue => e
FileUtils.remove(outfile)
puts e.message
exit 2
end

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.