[Days 01–03] #100DaysOfCode
Dealing with Datetimes
Let’s imagine we have a log file logfile
looking like this:
INFO 2014-07-03T23:27:51 supybot Shutdown initiated.
ERROR 2014-07-03T23:24:31 supybot Invalid user dictionary file.
...
INFO 2014-07-03T23:31:22 supybot Shutdown initiated.
Part 1
Let’s propose a Python function that could parse a line of that file into a datetime object (think datetime(2014, 7, 3, 23, 27, 51)
)
from datetime import datetimedef convert_to_datetime(line):
date_line = datetime.fromisoformat(line.split(' ')[1])
return date_line
Another way to achieve the same result (offered as a solution by the task designers):
def convert_to_datetime(line):
timestamp = line.split()[1]
date_str = '%Y-%m-%dT%H:%M:%S'
return datetime.strptime(timestamp, date_str)
Now we are facing a question: what’s the difference between datetime.fromisoformat()
and datetime.strptime()
. Let’s see.
datetime.fromisoformat(date_string)
according to the Python docs returns adate
object (year, month and day in an idealized calendar) corresponding to a date_string given in the formatYYYY-MM-DD
, so it recognizes this particular format only.datetime.strptime(date_string, format)
returns a fulldatetime
object (year, month, day, hour, minute, second, microsecond, tzinfo) corresponding to date_string, parsed according to format. You can see an example of such format syntax in the second function code above.
Part 2
Now let’s create a function that takes in the log file, identifies first and last system shutdown events and calculates a time difference between these two events.
First we process the logfile to obtain a list of its lines:
with open(logfile) as f:
loglines = f.readlines()
Next, my suggestion of the required function:
def time_between_shutdowns(loglines):
shutdown_list = [convert_to_datetime(el) for el in loglines if 'Shutdown initiated' in el]
return shutdown_list[-1] - shutdown_list[0]
The function returns a timedelta
object, reserved for storing duration, the difference between two dates or times. Below you’ll find a portion of the code that showcases timedelta
capabilities.
from datetime import datetime
from datetime import timedeltat = timedelta(days=4, hours=10)t.days
#4t.seconds
#36000t.hours
#Traceback (most recent call last):
#File "<pyshell#119>", line 1, in <module> t.hours
#AttributeError: 'datetime.timedelta' object has no attribute 'hours't.seconds / 60 / 60
#10.0t.seconds / 3600
#10.0#########eta = timedelta(hours=6)today = datetime.today()today
#datetime.datetime(2018, 2, 19, 14, 55, 19, 197404)today + eta
#datetime.datetime(2018, 2, 19, 20, 55, 19, 197404)str(today + eta)
#'2018-02-19 20:55:19.197404'
You can find the practical task here