Last active
December 29, 2015 08:29
-
-
Save jbaiera/7643893 to your computer and use it in GitHub Desktop.
A simple python script that calculates timestamp differences up to 24 hours apart.
Useful if you do a lot of performance measurements by hand.
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/python | |
import os | |
import getopt | |
import sys | |
hour = 60 * 60 | |
minute = 60 | |
def timeDiff(s=0, e=0): | |
d = e - s; | |
dh = d/hour | |
dm = (d-(dh*hour))/minute | |
ds = d - (dh*hour) - (dm*minute) | |
strlist = [] | |
strlist.append(`dh`) | |
strlist.append(':') | |
strlist.append(`dm`) | |
strlist.append(':') | |
strlist.append(`ds`) | |
return ''.join(strlist) | |
def splitTimeArg(t='0:0:0'): | |
str = t.split(':') | |
return str[0:3] | |
def sumTime(tl=[0,0,0]): | |
t = (int(tl[0]) * hour) + (int(tl[1]) * minute) + int(tl[2]) | |
return t | |
def main(): | |
try: | |
opts, args = getopt.getopt(sys.argv[1:], "hs:e:r", ["help", 'start-time', 'end-time', 'rollover']) | |
except getopt.error, msg: | |
print msg | |
print "for help use --help" | |
sys.exit(2) | |
start = -1 | |
end = -1 | |
roll = False | |
for o, a in opts: | |
if o in ('-h', '--help'): | |
print "td -s 00:00:00 -e 00:00:00 [-h]" | |
print "Calculates the difference in time between the start time and end time" | |
print "If end time is less than start time, the result will be negative." | |
print "If -r (--rollover) is specified and end time is less that start time, " | |
print " then it will be assumed that end time is the next day away and will " | |
print " have 24 hours added to it." | |
sys.exit(0) | |
if o in ('-s', '--start-time'): | |
start = sumTime(splitTimeArg(a)) | |
if o in ('-e', '--end-time'): | |
end = sumTime(splitTimeArg(a)) | |
if o in ('-r', '--rollover'): | |
roll = True | |
if start == -1 or end == -1: | |
print "Please specify start and end time" | |
sys.exit(2) | |
if end < start and roll: | |
end = end + 24*hour | |
print timeDiff(start, end) | |
sys.exit(0) | |
if __name__ == "__main__": | |
main(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment