-
Notifications
You must be signed in to change notification settings - Fork 9
/
Stopwatch.py
95 lines (83 loc) · 2.94 KB
/
Stopwatch.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#!/usr/bin/env python
"""Stopwatch, a portable multi-lap timing utility class.
Copyright (C) 2006 George F. Rice
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
"""
import time
class stopwatch:
""" Implements a timer with multiple named lap timers.
A newly created timer is NOT running.
Use start() and stop() to begin/end. Check boolean '.running'.
A lap timer is created on reference.
"""
def __init__(self, name = "stopwatch"):
self.name = name
self.startTime = time.time()
self.elapsedTime = 0.0
self.running = False
self.lap = {}
def start(self):
""" Start or restart the timer.
Note that while the timer is running,
only the getElapsedTime() method is accurate
for determining elapsed time.
"""
if not self.running:
self.startTime = time.time()
self.running = True
def stop(self):
""" Stop the timer and update the elapsedTime attribute.
"""
if self.running:
self.elapsedTime += time.time() - self.startTime
self.running = False
def getElapsedTime(self):
""" Returns the elapsed time as a float in seconds,
regardless of the state of the timer.
"""
if self.running:
return self.elapsedTime + (time.time() - self.startTime)
else:
return self.elapsedTime
def stopLapTimer(self, lap = "lap timer"):
""" Set (or reset) the named (or default) lap timer
to the current elapsed time. The lap time is returned.
"""
self.lap[lap] = self.getElapsedTime()
return self.lap[lap]
def getLapTime(self, lap = "lap timer"):
""" Return the named (or default) lap time. If it doesn't exist,
it is created as the current elapsed time.
"""
try:
return self.lap[lap]
except:
self.lap[lap] = self.getElapsedTime()
return self.lap[lap]
def getFormattedTime(self, lap = None):
""" Return specified lap time (or elapsed time if omitted)
formatted as HH:MM:SS.ds
"""
if lap == None:
_et = self.getElapsedTime()
else:
_et = self.getLapTime(lap)
_et += 0.005 # round to nearest hundredth
_hh = int(_et / 3600)
_et -= (_hh * 3600)
_mm = int(_et / 60)
_et -= (_mm * 60)
_ss = int(_et)
_et -= _ss
_ds = int(_et * 100)
return "%.2d:%.2d:%.2d.%.2d" % (_hh, _mm, _ss, _ds)