Skip to content

Commit f058939

Browse files
Day 2 commit, parse_time.py
1 parent 5b2cca5 commit f058939

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
from datetime import datetime
2+
3+
def convert_to_datetime(line):
4+
'''Extract date and time from a line of text, return as a datetime object.'''
5+
6+
# Find the part of the string that contains the information about time.
7+
date_string = line.split()[1]
8+
date = datetime.strptime(date_string, "%Y-%m-%dT%H:%M:%S")
9+
return date
10+
11+
def time_between_shutdowns(loglines):
12+
'''TODO 2:
13+
Extract shutdown events ("Shutdown initiated") from loglines and calculate the
14+
timedelta between the first and last one.
15+
Return this datetime.timedelta object.'''
16+
17+
datetimes_from_log = [convert_to_datetime(line) for line in loglines if line.startswith('INFO')]
18+
return datetimes_from_log[-1] - datetimes_from_log[0]

0 commit comments

Comments
 (0)