Last active
August 29, 2015 13:58
-
-
Save unfo/9952244 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/ruby | |
require 'date' | |
THRESHOLD = ARGV[0].to_i || 10 # seconds | |
prev_line = '' | |
prev_dtm = nil | |
prev_line_printed = false | |
def seconds_between(dtm1, dtm2) | |
seconds_in_a_day = 24 * 60 * 60 | |
((dtm2 - dtm1) * seconds_in_a_day).round # dtm - dtm is a Rational of how many days | |
end | |
def humanize(seconds) | |
hours = seconds / (60*60) | |
seconds -= (hours * 60*60) | |
minutes = seconds / 60 | |
seconds -= (minutes * 60) | |
'%02d:%02d:%02d' % [hours,minutes,seconds] | |
end | |
STDIN.each_line do |line| | |
timestamp_str = line.scan(/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}/) | |
next if timestamp_str.nil? | |
dtm = DateTime.parse(timestamp_str.first) || timestamp_str.empty? | |
unless prev_dtm.nil? | |
secs = seconds_between(prev_dtm, dtm) | |
if secs > THRESHOLD | |
unless prev_line_printed | |
puts | |
puts " "*9 + prev_line | |
end | |
puts humanize(secs) + " " + line | |
prev_line_printed = true | |
else | |
prev_line_printed = false | |
end | |
end | |
prev_dtm = dtm | |
prev_line = line | |
end |
Author
unfo
commented
Apr 3, 2014
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment