X-Git-Url: https://wannabe.guru.org/gitweb/?a=blobdiff_plain;f=utils.py;fp=utils.py;h=e720ef7d565687999c1d41d5445862ddbb69b6b1;hb=c06bfef53f70551e7920bc4facce27f47b89e2ba;hp=fb22e4dd052acfa4731dc62a30b928084cb00051;hpb=6cc940e0df9b8ea937fb955f959fa878c80f0d7c;p=kiosk.git diff --git a/utils.py b/utils.py index fb22e4d..e720ef7 100644 --- a/utils.py +++ b/utils.py @@ -1,15 +1,18 @@ +#!/usr/bin/env python3 + import time import os -import constants from datetime import datetime +import constants -def timestamp(): + +def timestamp() -> str: t = datetime.fromtimestamp(time.time()) return t.strftime("%d/%b/%Y:%H:%M:%S%Z") -def describe_age_of_file(filename): +def describe_age_of_file(filename) -> str: try: now = time.time() ts = os.stat(filename).st_ctime @@ -19,7 +22,7 @@ def describe_age_of_file(filename): return "?????" -def describe_age_of_file_briefly(filename): +def describe_age_of_file_briefly(filename) -> str: try: now = time.time() ts = os.stat(filename).st_ctime @@ -29,18 +32,18 @@ def describe_age_of_file_briefly(filename): return "?????" -def describe_duration(age): +def describe_duration(age: int) -> str: days = divmod(age, constants.seconds_per_day) hours = divmod(days[1], constants.seconds_per_hour) minutes = divmod(hours[1], constants.seconds_per_minute) descr = "" if days[0] > 1: - descr = "%d days, " % days[0] + descr = f"{int(days[0]):d} days, " elif days[0] == 1: descr = "1 day, " if hours[0] > 1: - descr = descr + ("%d hours, " % hours[0]) + descr = descr + f"{int(hours[0]):d} hours, " elif hours[0] == 1: descr = descr + "1 hour, " if len(descr) > 0: @@ -48,20 +51,21 @@ def describe_duration(age): if minutes[0] == 1: descr = descr + "1 minute" else: - descr = descr + ("%d minutes" % minutes[0]) + descr = descr + f"{int(minutes[0]):d} minutes" return descr -def describe_duration_briefly(age): +def describe_duration_briefly(age: int) -> str: days = divmod(age, constants.seconds_per_day) hours = divmod(days[1], constants.seconds_per_hour) minutes = divmod(hours[1], constants.seconds_per_minute) + descr = "" if days[0] > 0: - descr = "%dd " % days[0] + descr = f"{int(days[0]):d}d " if hours[0] > 0: - descr = descr + ("%dh " % hours[0]) - descr = descr + ("%dm" % minutes[0]) + descr = descr + f"{int(hours[0]):d}h " + descr = descr + f"{int(minutes[0]):d}m" return descr