File tree Expand file tree Collapse file tree 1 file changed +18
-0
lines changed
days/01-03-datetimes/code Expand file tree Collapse file tree 1 file changed +18
-0
lines changed Original file line number Diff line number Diff line change
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 ]
You can’t perform that action at this time.
0 commit comments