Skip to content

Instantly share code, notes, and snippets.

@ymakino
Created May 27, 2012 12:26
Show Gist options
  • Save ymakino/2813658 to your computer and use it in GitHub Desktop.
Save ymakino/2813658 to your computer and use it in GitHub Desktop.
A simple ruby program for measuring time duration.
#!/usr/bin/env ruby
class SimpleStopWatch
def initialize()
@base = Time.now
@pausing = false
@pausing_secs = 0
@min = 0
@sec = 0
@rem = 0
end
def start()
@base = Time.now
@pausing = false
@pausing_secs = 0
@min = 0
@sec = 0
@rem = 0
end
def pause()
unless @pausing
update()
@pause_time = Time.now
@pausing = true
end
end
def resume()
if @pausing
@resume_time = Time.now
@pausing_secs += @resume_time - @pause_time
@pausing = false
end
end
def update()
unless @pausing
cur = Time.now
diff = ((cur - @base - @pausing_secs) * 100).to_i
@min = (diff / (60 * 100))
@sec = (diff % (60 * 100)) / 100
@rem = (diff % (60 * 100)) % 100
end
end
def to_s()
"%.2d:%.2d.%.2d" % [@min, @sec, @rem]
end
end
i=0
ssw = SimpleStopWatch.new()
print ("%4d %s" % [i, ssw.to_s])
i+=1
while (l = STDIN.readline)
case l[0,1]
when "q" then exit(0)
when "p" then ssw.pause()
when "r" then ssw.resume()
when "s" then ssw.start()
else ssw.update()
end
print ("%4d %s" % [i, ssw.to_s])
i+=1
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment