Skip to content

Instantly share code, notes, and snippets.

@timuruski
Created January 20, 2017 22:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save timuruski/3687ede5459f3cd9af21b947084ec146 to your computer and use it in GitHub Desktop.
Save timuruski/3687ede5459f3cd9af21b947084ec146 to your computer and use it in GitHub Desktop.
Script for dumping the time difference between a bunch of log lines.
#!/usr/bin/env ruby
require 'time'
# USAGE: This takes a bunch of lines as input, looks for dates, sorts them and
# then outputs the difference between the smallest and largest.
#
# pbpaste | ruby delta-t
# tail -f log/development.log | ruby delta-t
#
# TODO:
# - Add more time formats
# - Better output formatting
# EXAMPLE: 15:07:24 2017-01-20
TIMESTAMP_PATTERN = /\d{2}:\d{2}:\d{2} \d{4}-\d{2}-\d{2}/
ARGF.readlines
.map { |line| line[TIMESTAMP_PATTERN] }.compact
.map { |timestamp| Time.parse(timestamp) }
.sort
.minmax
.tap { |min, max|
total = max - min
minutes = total / 60
seconds = total % 60
puts format('%02d:%02d', minutes, seconds)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment