Created
May 27, 2012 12:26
-
-
Save ymakino/2813658 to your computer and use it in GitHub Desktop.
A simple ruby program for measuring time duration.
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 | |
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