From 2919edf4ab3dadc90bb59ebbcd32c263c53aac9a Mon Sep 17 00:00:00 2001 From: Jason Date: Sat, 22 May 2021 00:06:16 -0300 Subject: [PATCH 1/2] Comple the day 2 coding for datetime & timedelta --- days/01-03-datetimes/code/d2c.py | 63 ++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 days/01-03-datetimes/code/d2c.py diff --git a/days/01-03-datetimes/code/d2c.py b/days/01-03-datetimes/code/d2c.py new file mode 100644 index 00000000..4279a2b8 --- /dev/null +++ b/days/01-03-datetimes/code/d2c.py @@ -0,0 +1,63 @@ +from datetime import datetime, timedelta +import os +import re +import urllib.request +from typing import List + + +def convert_to_datetime(line): + """ + Extract timestamp from logline and convert it to a datetime object. + For example calling the function with: + INFO 2014-07-03T23:27:51 supybot Shutdown complete. + returns: + datetime(2014, 7, 3, 23, 27, 51) + """ + timestamp = re.findall(r"\d+\-\d+\-\w+\:\d+\:\d+", line) + #print('Timestamp extracted is ' + timestamp[0]) + return (datetime.strptime(timestamp[0], "%Y-%m-%dT%H:%M:%S")) + +def time_between_shutdowns(loglines): + """ + Extract shutdown events ("Shutdown initiated") from loglines and + calculate the timedelta between the first and last one. + Return this datetime.timedelta object. + """ + shutdown_event_start = 'Shutdown initiated' + + # Extract all the shutdown event timestamps + shutdowntimes = [] + for i in range(len(loglines)): + if (re.search(shutdown_event_start, loglines[i])): + timestamp = convert_to_datetime(loglines[i]) + shutdowntimes.append(timestamp) + print(shutdown_event_start + " at " + str(timestamp.date()) + ", " + str(timestamp.time())) + + # Compute the time between the first & last shutdown event timestamps + deltatime = shutdowntimes[len(shutdowntimes)-1] - shutdowntimes[0] + return deltatime + + +# Establish the name of the local logfile +tmp = os.getenv("TMP", "/tmp") +logfile = os.path.join(tmp, 'log') + +# Read data from the url into the logfile +urllib.request.urlretrieve( + 'https://bites-data.s3.us-east-2.amazonaws.com/messages.log', + logfile +) + +# Read the local logfile into a list loglines +with open(logfile) as f: + loglines = f.readlines() + +# Extract all the timestamps from the log file +logdatetimes = list(range(len(loglines))) +for i in range(len(loglines)): + logdatetimes[i] = convert_to_datetime(loglines[i]) + print("Timestamp " + str(i) + " encoded " + str(logdatetimes[i].date()) + " " + str(logdatetimes[i].time())) + +shutdowndeltatime = time_between_shutdowns(loglines) + +print("Time between shutdowns is (hh:mm:ss)", shutdowndeltatime) \ No newline at end of file From 12685125011d4967d9d90fe5533e8b20037a6bcd Mon Sep 17 00:00:00 2001 From: Jason Date: Sat, 22 May 2021 15:07:13 -0300 Subject: [PATCH 2/2] Complete the day 3 coding creating a simple stopwatch commandline using datetime object. --- days/01-03-datetimes/code/sw.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 days/01-03-datetimes/code/sw.py diff --git a/days/01-03-datetimes/code/sw.py b/days/01-03-datetimes/code/sw.py new file mode 100644 index 00000000..552fd6ef --- /dev/null +++ b/days/01-03-datetimes/code/sw.py @@ -0,0 +1,14 @@ +from datetime import datetime, timedelta + +# Repeat until the use selects to quit +while (1): + what2do = input("\nPlease choose, start timer (s), else quit...") + if (what2do.upper() == 'S'): + starttime = datetime.now() + print("Starting timer... " + str(starttime)) + input("Hit any key to stop timer.") + stoptime = datetime.now() + print("Ellapsed time is " + str(stoptime - starttime)) + else: + print("Bye bye!") + break