Skip to content

Instantly share code, notes, and snippets.

@davetron5000
Created June 22, 2012 13:10
Show Gist options
  • Star 19 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save davetron5000/2972651 to your computer and use it in GitHub Desktop.
Save davetron5000/2972651 to your computer and use it in GitHub Desktop.
DanTimer
#!/usr/bin/ruby
require 'time'
def format_time(seconds)
hours = (seconds / 3600).to_i
minutes = ((seconds % 3600) / 60).to_i
seconds = (seconds % 60).to_i
minutes = "0#{minutes}" if minutes < 10
seconds = "0#{seconds}" if seconds < 10
"#{hours}:#{minutes}:#{seconds}"
end
start = Time.now
ARGF.each do |line|
time = Time.now
puts "#{format_time(time - start)}: #{line}"
end
puts "#{format_time(Time.now - start)}: DONE!"
$ ./dantimer.rb > notes.txt
Start of show
Merlin talks about agency
Construction at Marco's house
Some cool new mac thing
Siracusa insists he hasn't listend to Build & Analyze
Merlin still talking about agency
^D
$ cat notes.txt
0:00:02: Start of show
0:00:10: Merlin talks about agency
0:00:18: Construction at Marco's house
0:00:27: Some cool new mac thing
0:00:40: Siracusa insists he hasn't listend to Build & Analyze
0:00:50: Merlin still talking about agency
0:00:51: DONE!
@peterc
Copy link

peterc commented Jun 23, 2012

Sorry, but I couldn't resist the golfing opportunity! (I always learn something..)

start = Time.now.to_i
l = ->_{ puts Time.at(Time.now - start).utc.strftime("%T")[1..-1] + ": " + _ }
ARGF.each(&l)
l['DONE!']

Or saner/more conventional:

start = Time.now.to_i

ARGF.each do |line|
  puts "#{Time.at(Time.now - start).utc.strftime("%-k:%M:%S")}: #{line}"
end
puts "#{Time.at(Time.now - start).utc.strftime("%-k:%M:%S")}: DONE!"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment