-
Notifications
You must be signed in to change notification settings - Fork 0
/
time_convertor.py
71 lines (54 loc) · 2.05 KB
/
time_convertor.py
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 copy
import os
from datetime import datetime
from typing import List
from zoneinfo import ZoneInfo
from wox import Wox, WoxAPI
TIMEZONES = {"pst": "US/Pacific", "ct": "US/Central", "ist": "Asia/Kolkata"}
RESULT_TEMPLATE = {
"Title": "{}",
"SubTitle": "copyToClipboard",
"IcoPath": "Images/clock.png",
"JsonRPCAction": {
"method": "copyToClipboard",
"parameters": ["{}"],
"dontHideAfterAction": False,
},
}
class TimeZone(Wox):
def query(self, query):
"""Take a given time we do the following
1. Replace the timezone of time with the first_zone/from_zone
2. Replace the year to current year
- we can even hardcode 2023 for quite sometime
3. Then update the astimezone to to_zone
4. Return strftime('%I:%M %p') of the time we get from Step-3
"""
results = []
try:
time, indication, first_zone, _, second_zone = query.strip().split(" ")
if len(time.split(":")) > 1:
time_obj = datetime.strptime(time + indication, "%I:%M%p")
else:
time_obj = datetime.strptime(time + indication, "%I%p")
fzone = TIMEZONES[first_zone.lower()]
szone = TIMEZONES[second_zone.lower()]
converted_time = (
time_obj.replace(tzinfo=ZoneInfo(fzone))
.replace(year=2023)
.astimezone(ZoneInfo(szone))
.strftime("%I:%M %p")
)
self.add_item(results, converted_time)
except Exception as err:
self.add_item(results, err)
return results
def add_item(self, results: List[dict], converted_time):
template = copy.deepcopy(RESULT_TEMPLATE)
template["Title"] = template["Title"].format(converted_time)
template["JsonRPCAction"]["parameters"][0] = str(converted_time)
results.append(template)
def copyToClipboard(self, value):
os.system("echo | set /p nul=" + value.strip() + "| clip")
if __name__ == "__main__":
TimeZone()