-
Notifications
You must be signed in to change notification settings - Fork 0
/
time_microservice.py
294 lines (276 loc) · 6.81 KB
/
time_microservice.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
"""API service that returns current date and time in UTC or (optional) passed timezone"""
import socket
import json
from datetime import datetime, timezone, timedelta
def get_utc_time() -> object:
"""
Returns current time in UTC
:return: current UTC time object
"""
return datetime.now(timezone.utc)
def get_adjusted_time(tz_offset) -> object:
"""
Returns current time in passed timezone offset
:param tz_offset: integer of timezone offset from UTC
:return: current time object
"""
try:
tz = timezone(timedelta(hours=float(tz_offset))) # finds timezone info for passed offset
adjusted_time = get_utc_time().astimezone(tz) #converts from UTC time
return adjusted_time
except ValueError:
return None
def format_time(time_obj, tz_offset) -> dict:
"""
Converts time object to dictionary for response to client
:param time_obj: time object from datetime
:param tz_offset: timezone offset from UTC
:return: dictionary of values
"""
return {
"year": time_obj.year,
"month": time_obj.month,
"day": time_obj.day,
"hour": time_obj.hour,
"minute": time_obj.minute,
"second": time_obj.second,
"UTC_offset": tz_offset
}
def timezone_search(tz_shortcode):
"""
Returns UTC offset for passed timezone shortcode
:param tz_shortcode: shortcodes for timezones
(e.g. PST for Pacific Standard Time)
:return: integer for UTC offset, or returns passed
parameter if not in dictionary
"""
# dictionary of timezone shortcodes
shortcodes = {
"ACDT": 10.5,
"ACST": 9.5,
"ACT": -5,
"ACWST": 8.75,
"ADT": -3,
"AEDT": 11,
"AEST": 10,
"AFT": 4.5,
"AKDT": -8,
"AKST": -9,
"ALMT": 6,
"AMST": -3,
"AMT": -4,
"ANAT": 12,
"AQTT": 5,
"ART": -3,
"AST": 3,
"AWST": 8,
"AZOST": 0,
"AZOT": -1,
"AZT": 4,
"BNT": 8,
"BIOT": 6,
"BIT": -12,
"BOT": -4,
"BRST": -2,
"BRT": -3,
"BST": 1,
"BTT": 6,
"CAT": 2,
"CCT": 6.5,
"CDT": -5,
"CEST": 2,
"CET": 1,
"CHADT": 13.75,
"CHAST": 12.75,
"CHOT": 8,
"CHOST": 9,
"CHST": 10,
"CHUT": 10,
"CIST": -8,
"CKT": -10,
"CLST": -3,
"CLT": -4,
"COST": -4,
"COT": -5,
"CST": -6,
"CVT": -1,
"CWST": 8.75,
"CXT": 7,
"DAVT": 7,
"DDUT": 10,
"DFT": 1,
"EASST": -5,
"EAST": -6,
"EAT": 3,
"ECT": -4,
"EDT": -4,
"EEST": 3,
"EET": 2,
"EGST": 0,
"EGT": -1,
"EST": -5,
"FET": 3,
"FJT": 12,
"FKST": -3,
"FKT": -4,
"FNT": -2,
"GALT": -6,
"GAMT": -9,
"GET": 4,
"GFT": -3,
"GILT": 12,
"GIT": -9,
"GMT": 0,
"GST": 4,
"GYT": -4,
"HDT": -9,
"HAEC": 2,
"HST": -10,
"HKT": 8,
"HMT": 5,
"HOVST": 8,
"HOVT": 7,
"ICT": 7,
"IDLW": -12,
"IDT": 3,
"IOT": 6,
"IRDT": 4.5,
"IRKT": 8,
"IRST": 3.5,
"IST": 1,
"JST": 9,
"KALT": 2,
"KGT": 6,
"KOST": 11,
"KRAT": 7,
"KST": 9,
"LHST": 10.5,
"LINT": 14,
"MAGT": 12,
"MART": -9.5,
"MAWT": 5,
"MDT": -6,
"MET": 1,
"MEST": 2,
"MHT": 12,
"MIST": 11,
"MIT": -9.5,
"MMT": 6.5,
"MSK": 3,
"MST": -7,
"MUT": 4,
"MVT": 5,
"MYT": 8,
"NCT": 11,
"NDT": -2.5,
"NFT": 11,
"NOVT": 7,
"NPT": 5.75,
"NST": -3.5,
"NT": -3.5,
"NUT": -11,
"NZDT": 13,
"NZST": 12,
"OMST": 6,
"ORAT": 5,
"PDT": -7,
"PET": -5,
"PETT": 12,
"PGT": 10,
"PHOT": 13,
"PHT": 8,
"PHST": 8,
"PKT": 5,
"PMDT": -2,
"PMST": -3,
"PONT": 11,
"PST": -8,
"PWT": 9,
"PYST": -3,
"PYT": -4,
"RET": 4,
"ROTT": -3,
"SAKT": 11,
"SAMT": 4,
"SAST": 2,
"SBT": 11,
"SCT": 4,
"SDT": -10,
"SGT": 8,
"SLST": 5.5,
"SRET": 11,
"SRT": -3,
"SST": 8,
"SYOT": 3,
"TAHT": -10,
"THA": 7,
"TFT": 5,
"TJT": 5,
"TKT": 13,
"TLT": 9,
"TMT": 5,
"TRT": 3,
"TOT": 13,
"TST": 8,
"TVT": 12,
"ULAST": 9,
"ULAT": 8,
"UTC": 0,
"UYST": -2,
"UYT": -3,
"UZT": 5,
"VET": -4,
"VLAT": 10,
"VOLT": 3,
"VOST": 6,
"VUT": 11,
"WAKT": 12,
"WAST": 2,
"WAT": 1,
"WEST": 1,
"WET": 0,
"WIB": 7,
"WIT": 9,
"WITA": 8,
"WGST": -2,
"WGT": -3,
"WST": 8,
"YAKT": 9,
"YEKT": 5
}
# return value if key is found, else return passed parameter
return shortcodes.get(tz_shortcode.upper(), tz_shortcode)
def process_client(client_socket):
"""
Receives client request and sends timestamp
:param client_socket: the client socket
"""
# format client data as integer
data = client_socket.recv(1024).decode("utf-8")
try:
print(f"Received '{data}'") # print data recieved form client
timezone_offset = timezone_search(data) # check if client sent timezone shortcode
adjusted_time = get_adjusted_time(timezone_offset) # adjust time
if adjusted_time:
response = format_time(adjusted_time, timezone_offset) # format response as dictionary
else:
response = {"error": "Invalid timezone format"}
except ValueError:
response = {"error": "Invalid input"}
print(f"Sending '{response}'\n")
client_socket.send(json.dumps(response).encode("utf-8")) # respond with JSON
def main():
"""Main function to create server socket and listen for client connections"""
host = '127.0.0.1' # set for running locally
port = 13000
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.bind((host, port)) # initialize socket
server_socket.listen()
print(f"Listening on {host}:{port}...")
while True:
client_socket, addr = server_socket.accept() # accept client connection
print(f"Connection from {addr}")
with client_socket:
process_client(client_socket) # process client request
if __name__ == "__main__":
main()