Working on the voice command logic.
[kiosk.git] / utils.py
1 #!/usr/bin/env python3
2
3 import time
4 import os
5 from datetime import datetime
6
7 import constants
8
9
10 def timestamp() -> str:
11     t = datetime.fromtimestamp(time.time())
12     return t.strftime("%d/%b/%Y:%H:%M:%S%Z")
13
14
15 def describe_age_of_file(filename: str) -> str:
16     try:
17         now = time.time()
18         ts = os.stat(filename).st_ctime
19         age = now - ts
20         age = int(age)
21         return describe_duration(age)
22     except Exception as e:
23         return "?????"
24
25
26 def describe_age_of_file_briefly(filename: str) -> str:
27     try:
28         now = time.time()
29         ts = os.stat(filename).st_ctime
30         age = now - ts
31         age = int(age)
32         return describe_duration_briefly(age)
33     except Exception as e:
34         return "?????"
35
36
37 def describe_duration(age: int) -> str:
38     days = divmod(age, constants.seconds_per_day)
39     hours = divmod(days[1], constants.seconds_per_hour)
40     minutes = divmod(hours[1], constants.seconds_per_minute)
41
42     descr = ""
43     if days[0] > 1:
44         descr = f"{int(days[0]):d} days, "
45     elif days[0] == 1:
46         descr = "1 day, "
47     if hours[0] > 1:
48         descr = descr + f"{int(hours[0]):d} hours, "
49     elif hours[0] == 1:
50         descr = descr + "1 hour, "
51     if len(descr) > 0:
52         descr = descr + "and "
53     if minutes[0] == 1:
54         descr = descr + "1 minute"
55     else:
56         descr = descr + f"{int(minutes[0]):d} minutes"
57     return descr
58
59
60 def describe_duration_briefly(age: int) -> str:
61     days = divmod(age, constants.seconds_per_day)
62     hours = divmod(days[1], constants.seconds_per_hour)
63     minutes = divmod(hours[1], constants.seconds_per_minute)
64
65     descr = ""
66     if days[0] > 0:
67         descr = f"{int(days[0]):d}d "
68     if hours[0] > 0:
69         descr = descr + f"{int(hours[0]):d}h "
70     descr = descr + f"{int(minutes[0]):d}m"
71     return descr
72
73
74 # x = describe_age_of_file_briefly("pages/clock_10_none.html")
75 # print x