Repository navigation
Expand file tree
/
Copy pathclock.py
More file actions
71 lines (60 loc) · 2.04 KB
/
Copy pathclock.py
File metadata and controls
71 lines (60 loc) · 2.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import threading, time
from datetime import datetime
from zoneinfo import ZoneInfo, available_timezones
result = 0
stop_event = threading.Event()
options = ["stopwatch", "timer", "time zone"]
def stop_watch():
global result
print("\nStopwatch started!")
start_time = time.time()
while not stop_event.is_set():
time.sleep(0.05)
result = time.time() - start_time
def format_date(year, month, day):
if 11 <= day <= 13:
suffix = "th"
else:
suffix = {1: "st", 2: "nd", 3: "rd"}.get(day % 10, "th")
date_obj = datetime(year, month, day)
month_str = date_obj.strftime("%b")
return f"{month_str}. {day}{suffix} {year}"
def time_zone_handler(country):
zones = sorted(filter(lambda x: country in x, available_timezones()))
if not zones:
print(f"No timezones were found that match {country}.")
return
print(f"\nTime zones that match {country}:")
for element in zones:
print(element)
zone_input = input("Which timezone is yours? ")
zone_now = datetime.now(ZoneInfo(zone_input))
print(f"\nToday is {format_date(zone_now.year,zone_now.month, zone_now.day)}.")
print(f"The Time is {zone_now.hour:02d}:{zone_now.minute:02d}.")
print("Welcome to the Clock program!\n")
for i in range(len(options)):
print(f"{i+1}. {options[i].title()}")
m_choose = int(input("Pick your choice: "))
if m_choose == 1:
result = 0
stop_event.clear()
thread = threading.Thread(target=stop_watch, daemon=True)
thread.start()
input("Press Enter to stop the stopwatch...")
stop_event.set()
thread.join()
suffix = "" if f"{result:.2f}" == "1.00" else "s"
print(f"\nStopwatch finished at: {result:.2f} second{suffix}.")
print("Stopwatch done!")
if m_choose == 2:
timer_time = int(input("\nHow many seconds should the timer have? "))
if timer_time < 0:
print("Timer cannot be less than zero.")
else:
for t in range(timer_time, 0, -1):
print(t)
time.sleep(1)
print("Time's up!")
elif m_choose == 3:
country_input = input("\nWhat country are you in? ")
time_zone_handler(country_input)