Return to Snippet

Revision: 9629
at November 16, 2008 02:45 by mbanfi


Updated Code
#!/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

Revision: 9628
at November 15, 2008 04:36 by mbanfi


Updated Code
#!/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 the 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

Revision: 9627
at November 15, 2008 04:11 by mbanfi


Initial Code

                                

Initial URL

                                

Initial Description

                                

Initial Title
Calculate elapsed time for timestamps in (log)file

Initial Tags
date, textmate, python

Initial Language
Python