Created
January 20, 2017 22:35
-
-
Save timuruski/3687ede5459f3cd9af21b947084ec146 to your computer and use it in GitHub Desktop.
Script for dumping the time difference between a bunch of log lines.
This file contains 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
#!/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