/ Published in: Python
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
#!/usr/bin/env python """Calculate elapsed time for timestamps in logfile. Syntax: time_elapsed.py <filename> <first-line-number> <last-line-number>""" import sys, linecache from time import mktime, strftime, strptime, gmtime filename, first_line, last_line = sys.argv[1], int(sys.argv[2]), int(sys.argv[3]) print "Lines for the given range (%d-%d) in file (%s):" % (first_line, last_line, filename) print "-" * 70 for line in range(first_line, last_line+1): print linecache.getline(filename, line).strip() # The first 16 characters in each line of the file are the timestamp. # Modify this for your needs. begin_time = linecache.getline(filename, first_line).strip()[0:15] end_time = linecache.getline(filename, last_line).strip()[0:15] # Use this code if the logfile has a delimiter which seperates fields. # In this example ";": #begin_time = linecache.getline(filename, first_line).split(";")[0] #end_time = linecache.getline(filename, last_line).split(";")[0] # Format strings: http://docs.python.org/library/time.html#time.strftime elapsed_sec = mktime(strptime(end_time, "%b %d %H:%M:%S")) - mktime(strptime(begin_time, "%b %d %H:%M:%S")) elapsed_readable = strftime("%H:%M:%S", gmtime(elapsed_sec)) print "-" * 70 print "Elapsed time (seconds) :", elapsed_sec print "Elapsed time (HH:MM:SS):", elapsed_readable