1

If the log file was just like any other log file it is easy to parse.

But now I have 5 log files containing data such as :

Time Log:
2/23/12: 9:10pm - 11:40pm getting familiar with Flash
2/29/12: 12:50pm - 2:00pm getting familiar with Flash
3/1/12: 6:00pm - 11:40pm getting familiar with Flash

How to calculate the total time spent by parsing the time log file?

Simran Munot
  • 87
  • 1
  • 2
  • 10

1 Answers1

1

Just use regex to parse time out of log file and calculate the difference with datetime module.
Please refer below code.

import re
import datetime

log="""2/23/12: 9:10pm - 11:40pm getting familiar with Flash
2/29/12: 12:50pm - 2:00pm getting familiar with Flash
3/1/12: 6:00pm - 11:40pm getting familiar with Flash"""

times=re.findall("(\d{1,2}:\d{1,2}[ap]m)\s*-\s*(\d{1,2}:\d{1,2}[ap]m)",log)

print(sum([datetime.datetime.strptime(t[1],"%I:%M%p")-datetime.datetime.strptime(t[0],"%I:%M%p") for t in times],datetime.timedelta()))

Output

9:20:00
Liju
  • 2,273
  • 3
  • 6
  • 21