forked from tgstation/tgstation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Timeconverter.py
204 lines (194 loc) · 7.34 KB
/
Timeconverter.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
#Sources:
# http://wwp.greenwichmeantime.com/time-zone/usa/eastern-time/convert/
# http://www.timeanddate.com/library/abbreviations/timezones/na/
# Times are GMT +- x
# For eq.
# EST = -5
# GMT = 0
# UTC = 0
#Times are in hours,
#2.5 = 2 and half hours
global times
times = {"ADT":-3,"HAA":-3, #Synonyms on the same line
"AKDT":-8,"HAY":-8,
"AKST":-9,"HNY":-9,
"AST":-4,"HNA":-4,
"CDT":-5,"HAC":-5,
"CST":-6,"HNC":-6,
"EDT":-4,"HAE":-4,
"EGST":0,
"EGT":-1,
"EST":-5,"HNE":-5,"ET":-5,
"HADT":-9,
"HAST":-10,
"MDT":-6,"HAR":-6,
"MST":-7,"HNR":-7,
"NDT":-2.5,"HAT":-2.5,
"NST":-3.5,"HNT":-3.5,
"PDT":-7,"HAP":-7,
"PMDT":-2,
"PMST":-3,
"PST":-8,"HNP":-8,"PT":-8,
"WGST":-2,
"WGT":-3,
"GMT":0,
"UTC":0}
def converter(zones,time):
#Zones should be a list containing
# ( From zone
# To zone )
global times
#from_z = for example UTC+00:00, WGT or GMT-05:30
#to_z = same style as above.
from_z,to_z = zones
from_z = from_z.upper()
to_z = to_z.upper()
if from_z.find("+") != -1:
from_zone_offset = from_z[from_z.find("+"):]
if ":" in from_zone_offset:
try:
from_zone_offset1,from_zone_offset2 = from_zone_offset.split(":")
except ValueError:
return "Too many or too small amount of values"
try:
from_zone_offset = int(from_zone_offset1) + int(from_zone_offset2)/60.0
except:
return "Error, the 'From Zone' variable has an incorrect offset number"
else:
try:
from_zone_offset = float(from_zone_offset)
except:
return "Error, the 'From Zone' variable has an incorrect offset number"
try:
from_zone_realtime = from_zone_offset + times[from_z[:from_z.find("+")]]
except KeyError:
return "Incorrect From zone"
elif "-" in from_z:
from_zone_offset = from_z[from_z.find("-"):]
if ":" in from_zone_offset:
from_zone_offset1,from_zone_offset2 = from_zone_offset.split(":")
try:
from_zone_offset = -int(from_zone_offset1) + int(from_zone_offset2)/60.0
except:
return "Error, the 'From Zone' variable has an incorrect offset number"
else:
try:
from_zone_offset = -float(from_zone_offset)
except:
return "Error, the 'From Zone' variable has an incorrect offset number"
from_zone_realtime = times[from_z[:from_z.find("-")]] - from_zone_offset
pass
else:
from_zone_offset = 0
try:
from_zone_realtime = from_zone_offset + times[from_z]
except KeyError:
return "Incorrect From zone"
if to_z.find("+") != -1:
to_zone_offset = to_z[to_z.find("+"):]
if ":" in to_zone_offset:
try:
to_zone_offset1,to_zone_offset2 = to_zone_offset.split(":")
except ValueError:
return "Too many or too small amount of values"
try:
to_zone_offset = int(to_zone_offset1) + int(to_zone_offset2)/60.0
except:
return "Error, the 'To Zone' variable has an incorrect offset number"
else:
try:
to_zone_offset = float(to_zone_offset)
except:
return "Error, the 'To Zone' variable has an incorrect offset number"
try:
to_zone_realtime = to_zone_offset + times[to_z[:to_z.find("+")]]
except KeyError:
return "The zone you want the time to be changed to is not found"
elif "-" in to_z:
to_zone_offset = to_z[to_z.find("-"):]
if ":" in to_zone_offset:
to_zone_offset1,to_zone_offset2 = to_zone_offset.split(":")
try:
to_zone_offset = -int(to_zone_offset1) + int(to_zone_offset2)/60.0
except:
return "Error, the 'To Zone' variable has an incorrect offset number"
else:
try:
to_zone_offset = -float(to_zone_offset)
except:
return "Error, the 'To Zone' variable has an incorrect offset number"
to_zone_realtime = times[to_z[:to_z.find("-")]] -to_zone_offset
pass
else:
to_zone_offset = 0
try:
to_zone_realtime = to_zone_offset + times[to_z]
except KeyError:
return "Incorrect To zone"
try:
time_hour,time_minute = time.split(":")
time_hour,time_minute = int(time_hour),int(time_minute)
string = ":"
except:
try:
time_hour,time_minute = time.split(".")
time_hour,time_minute = int(time_hour),int(time_minute)
string = "."
except ValueError:
return "The time was input in an odd way"
if to_zone_realtime % 1.0 == 0.0 and from_zone_realtime % 1.0 == 0.0:
time_hour = time_hour + (to_zone_realtime - from_zone_realtime)
return str(int(time_hour))+string+str(int(time_minute))
else:
if to_zone_realtime % 1.0 != 0.0 and from_zone_realtime % 1.0 != 0.0:
time_minute = time_minute + (((to_zone_realtime % 1.0) * 60) - ((from_zone_realtime % 1.0) * 60))
elif to_zone_realtime % 1.0 != 0.0 and from_zone_realtime % 1.0 == 0.0:
time_minute = time_minute + (((to_zone_realtime % 1.0) * 60) - 0)
elif to_zone_realtime % 1.0 == 0.0 and from_zone_realtime % 1.0 != 0.0:
time_minute = time_minute + (0 - ((from_zone_realtime % 1.0) * 60))
else:
print "Wut?"
time_hour = time_hour + (int(to_zone_realtime//1) - int(from_zone_realtime//1))
return str(int(time_hour))+string+str(int(time_minute))
def formatter(time):
if "." in time:
string = "."
elif ":" in time:
string = ":"
else:
return time
hours,minutes = time.split(string)
days = 0
if int(minutes) < 0:
buphours = int(hours)
hours,minutes = divmod(int(minutes),60)
hours += buphours
if int(minutes) > 60:
hours,minutes = divmod(int(minutes),60)
hours += int(hours)
if int(hours) < 0:
days = 0
days,hours = divmod(int(hours),24)
if int(hours) > 24:
days = 0
days,hours = divmod(int(hours),24)
if int(hours) == 24 and int(minutes) > 0:
days += 1
hours = int(hours) - 24
hours = str(hours)
minutes = str(minutes)
if len(minutes) == 1:
minutes = "0"+minutes
if len(hours) == 1:
hours = "0"+hours
if days > 0:
if days == 1:
return hours+string+minutes+" (Tomorrow)"
else:
return hours+string+minutes+" (After "+str(days)+" days)"
elif days < 0:
if days == -1:
return hours+string+minutes+" (Yesterday)"
else:
return hours+string+minutes+" ("+str(abs(days))+" days ago)"
return hours+string+minutes