-
Notifications
You must be signed in to change notification settings - Fork 706
/
fuzzy_clock.star
397 lines (375 loc) · 10.9 KB
/
fuzzy_clock.star
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
"""
Applet: Fuzzy Clock
Author: Max Timkovich
Summary: Human readable time
Description: Display the time in a groovy, human-readable way.
"""
load("encoding/json.star", "json")
load("render.star", "render")
load("schema.star", "schema")
load("time.star", "time")
DEFAULT_LOCATION = {
"lat": 40.7,
"lng": -74.0,
"locality": "Brooklyn",
}
DEFAULT_TIMEZONE = "US/Eastern"
numbersPerLang = {
"nl-NL": {
1: "ÉÉN",
2: "TWEE",
3: "DRIE",
4: "VIER",
5: "VIJF",
6: "ZES",
7: "ZEVEN",
8: "ACHT",
9: "NEGEN",
10: "TIEN",
11: "ELF",
12: "TWAALF",
},
"de-DE": {
1: "EINS",
2: "ZWEI",
3: "DREI",
4: "VIER",
5: "FÜNF",
6: "SECHS",
7: "SIEBEN",
8: "ACHT",
9: "NEUN",
10: "ZEHN",
11: "ELF",
12: "ZWÖLF",
},
"en-US": {
1: "ONE",
2: "TWO",
3: "THREE",
4: "FOUR",
5: "FIVE",
6: "SIX",
7: "SEVEN",
8: "EIGHT",
9: "NINE",
10: "TEN",
11: "ELEVEN",
12: "TWELVE",
},
"es": {
1: "UNA",
2: "DOS",
3: "TRES",
4: "CUATRO",
5: "CINCO",
6: "SEIS",
7: "SIETE",
8: "OCHO",
9: "NUEVE",
10: "DIEZ",
11: "ONCE",
12: "DOCE",
},
}
numbersPerLang["en-GB"] = numbersPerLang["en-US"]
numbersPerLang["nl-BE"] = numbersPerLang["nl-NL"]
numbersPerLang["de-AT"] = numbersPerLang["de-DE"]
numbersPerLang["de-DE-alt"] = numbersPerLang["de-DE"]
numbersPerLang["de-DE-alt2"] = numbersPerLang["de-DE"]
numbersPerLang["de-DE-alt3"] = numbersPerLang["de-DE"]
numbersPerLang["de-CH"] = numbersPerLang["de-DE"]
numbersPerLang["de-CH-alt"] = numbersPerLang["de-DE"]
patternsPerLang = {
"de-AT": {
0: "{hour},UHR",
5: "FÜNF,ÜBER,{hour}",
10: "ZEHN,ÜBER,{hour}",
15: "VIERTEL,ÜBER,{hour}",
20: "ZEHN,VOR HALB,{next_hour}",
25: "FÜNF,VOR HALB,{next_hour}",
30: "HALB,{next_hour}",
35: "FÜNF,NACH HALB,{next_hour}",
40: "ZEHN,NACH HALB,{next_hour}",
45: "VIERTEL,VOR,{next_hour}",
50: "ZEHN,VOR,{next_hour}",
55: "FÜNF,VOR,{next_hour}",
},
"de-CH": {
0: "{hour},UHR",
5: "FÜNF,AB,{hour}",
10: "ZEHN,AB,{hour}",
15: "VIERTEL,AB,{hour}",
20: "ZEHN,VOR HALB,{next_hour}",
25: "FÜNF,VOR HALB,{next_hour}",
30: "HALB,{next_hour}",
35: "FÜNF,NACH HALB,{next_hour}",
40: "ZEHN,NACH HALB,{next_hour}",
45: "VIERTEL,VOR,{next_hour}",
50: "ZEHN,VOR,{next_hour}",
55: "FÜNF,VOR,{next_hour}",
},
"de-CH-alt": {
0: "{hour},UHR",
5: "FÜNF,AB,{hour}",
10: "ZEHN,AB,{hour}",
15: "VIERTEL,AB,{hour}",
20: "ZWANZIG,AB,{hour}",
25: "FÜNF,VOR HALB,{next_hour}",
30: "HALB,{next_hour}",
35: "FÜNF,NACH HALB,{next_hour}",
40: "ZWANZIG,VOR {next_hour}",
45: "VIERTEL,VOR {next_hour}",
50: "ZEHN,VOR,{next_hour}",
55: "FÜNF,VOR,{next_hour}",
},
"de-DE": {
0: "{hour},UHR",
5: "FÜNF,NACH {hour}",
10: "ZEHN,NACH {hour}",
15: "VIERTEL,NACH,{hour}",
20: "ZEHN,VOR HALB,{next_hour}",
25: "FÜNF,VOR HALB,{next_hour}",
30: "HALB,{next_hour}",
35: "FÜNF,NACH HALB,{next_hour}",
40: "ZEHN,NACH HALB,{next_hour}",
45: "VIERTEL,VOR,{next_hour}",
50: "ZEHN,VOR,{next_hour}",
55: "FÜNF,VOR,{next_hour}",
},
"de-DE-alt": {
0: "{hour},UHR",
5: "FÜNF,NACH,{hour}",
10: "ZEHN,NACH,{hour}",
15: "VIERTEL,NACH,{hour}",
20: "ZWANZIG,NACH,{hour}",
25: "FÜNF,VOR HALB,{next_hour}",
30: "HALB,{next_hour}",
35: "FÜNF,NACH HALB,{next_hour}",
40: "ZWANZIG,VOR,{next_hour}",
45: "VIERTEL,VOR,{next_hour}",
50: "ZEHN,VOR,{next_hour}",
55: "FÜNF,VOR,{next_hour}",
},
"de-DE-alt2": {
0: "{hour},UHR",
5: "FÜNF,NACH,{hour}",
10: "ZEHN,NACH,{hour}",
15: "VIERTEL,{next_hour}",
20: "ZEHN,VOR HALB,{next_hour}",
25: "FÜNF,VOR HALB,{next_hour}",
30: "HALB,{next_hour}",
35: "FÜNF,NACH HALB,{next_hour}",
40: "FÜNF VOR,DREIVIERTEL,{next_hour}",
45: "DREI,VIERTEL,{next_hour}",
50: "ZEHN,VOR,{next_hour}",
55: "FÜNF,VOR,{next_hour}",
},
"de-DE-alt3": {
# this is a good fit for Berlin area
0: "{hour},UHR",
5: "FÜNF,NACH,{hour}",
10: "ZEHN,NACH,{hour}",
15: "VIERTEL,{next_hour}",
20: "ZEHN,VOR HALB,{next_hour}",
25: "FÜNF,VOR HALB,{next_hour}",
30: "HALB,{next_hour}",
35: "FÜNF,NACH HALB,{next_hour}",
40: "ZEHN,NACH HALB,{next_hour}",
45: "DREI,VIERTEL,{next_hour}",
50: "ZEHN,VOR,{next_hour}",
55: "FÜNF,VOR,{next_hour}",
},
"en-US": {
0: "{hour},O’CLOCK",
5: "FIVE,PAST,{hour}",
10: "TEN,PAST,{hour}",
15: "QUARTER,PAST,{hour}",
20: "TWENTY,PAST,{hour}",
25: "TWENTY-FIVE,PAST,{hour}",
30: "HALF,PAST,{hour}",
35: "TWENTY-FIVE,TILL,{next_hour}",
40: "TWENTY,TILL,{next_hour}",
45: "QUARTER,TILL,{next_hour}",
50: "TEN,TILL,{next_hour}",
55: "FIVE,TILL,{next_hour}",
},
"en-GB": {
0: "{hour},O’CLOCK",
5: "FIVE,PAST,{hour}",
10: "TEN,PAST,{hour}",
15: "QUARTER,PAST,{hour}",
20: "TWENTY,PAST,{hour}",
25: "TWENTY-FIVE,PAST,{hour}",
30: "HALF,PAST,{hour}",
35: "TWENTY-FIVE,TO,{next_hour}",
40: "TWENTY,TO,{next_hour}",
45: "QUARTER,TO,{next_hour}",
50: "TEN,TO,{next_hour}",
55: "FIVE,TO,{next_hour}",
},
"nl-BE": {
0: "{hour} UUR",
5: "VIJF,NA,{hour}",
10: "TIEN,NA,{hour}",
15: "KWART,NA,{hour}",
20: "TIEN,VOOR HALF,{next_hour}",
25: "VIJF,VOOR HALF,{next_hour}",
30: "HALF,{next_hour}",
35: "VIJF,NA HALF,{next_hour}",
40: "TIEN,NA HALF,{next_hour}",
45: "KWART,VOOR,{next_hour}",
50: "TIEN,VOOR,{next_hour}",
55: "VIJF,VOOR,{next_hour}",
},
"nl-NL": {
0: "{hour},UUR",
5: "VIJF,OVER,{hour}",
10: "TIEN,OVER,{hour}",
15: "KWART,OVER,{hour}",
20: "TIEN,VOOR HALF,{next_hour}",
25: "VIJF,VOOR HALF,{next_hour}",
30: "HALF,{next_hour}",
35: "VIJF,OVER HALF,{next_hour}",
40: "TIEN,OVER HALF,{next_hour}",
45: "KWART,VOOR,{next_hour}",
50: "TIEN,VOOR,{next_hour}",
55: "VIJF,VOOR,{next_hour}",
},
"es": {
0: "LAS {hour}",
5: "LAS {hour},Y,CINCO",
10: "LAS {hour},Y,DIEZ",
15: "LAS {hour},Y,CUARTO",
20: "LAS {hour},Y,VEINTE",
25: "LAS {hour},Y,VEINTICINCO",
30: "LAS {hour},Y,MEDIA",
35: "LAS {next_hour},MENOS,VEINTICINCO",
40: "LAS {next_hour},MENOS,VEINTE",
45: "LAS {next_hour},MENOS,CUARTO",
50: "LAS {next_hour},MENOS,DIEZ",
55: "LAS {next_hour},MENOS,CINCO",
},
}
def display_hour(hour):
"""Returns:
hour to display (in 12h format with 12 instead of 0)
"""
# Handle 24 hour time.
if hour > 12:
hour -= 12
# Handle midnight.
if hour == 0:
hour = 12
return hour
def fuzzy_time(hours, minutes, language):
numbers = numbersPerLang[language]
patterns = patternsPerLang[language]
# Round up to the next 5 minutes
rounded = (minutes + 2) % 60 // 5 * 5
if minutes > 55 and rounded == 0:
hours += 1
pattern = patterns[rounded]
cur_hour = numbers[display_hour(hours)]
next_hour = numbers[display_hour(hours + 1)]
# Special case
if language.startswith("de") and cur_hour == "EINS" and rounded == 0:
cur_hour = "EIN" # "EIN UHR" instead of "EINS UHR"
if language.startswith("es") and (cur_hour == "UNA" or next_hour == "UNA"):
pattern = pattern.replace("LAS", "LA") # "LA UNA" instead of "LAS UNA"
return pattern.format(hour = cur_hour, next_hour = next_hour).split(",")
def main(config):
location = config.get("location")
loc = json.decode(location) if location else DEFAULT_LOCATION
timezone = loc.get("timezone", DEFAULT_TIMEZONE)
now = time.now().in_location(timezone)
hours = now.hour
minutes = now.minute
language = config.get("dialect") or "en-US"
# backwards compatibility
if language == "american":
language = "en-US"
elif language == "british":
language = "en-GB"
fuzzed = fuzzy_time(hours, minutes, language)
# Add some left padding for ~style~.
texts = [render.Text(" " * i + s) for i, s in enumerate(fuzzed)]
return render.Root(
max_age = 60 * 10,
child = render.Padding(
pad = 3,
child = render.Column(
children = texts,
),
),
)
def get_schema():
dialectOptions = [
schema.Option(
display = "American English",
value = "en-US",
),
schema.Option(
display = "British English",
value = "en-GB",
),
schema.Option(
display = "Deutsch",
value = "de-DE",
),
schema.Option(
display = "Deutsch (Österreich)",
value = "de-AT",
),
schema.Option(
display = "Deutsch (Alternative)",
value = "de-DE-alt",
),
schema.Option(
display = "Deutsch (Alternative 2)",
value = "de-DE-alt2",
),
schema.Option(
display = "Deutsch (Alternative 3)",
value = "de-DE-alt3",
),
schema.Option(
display = "Deutsch (Schweiz)",
value = "de-CH",
),
schema.Option(
display = "Deutsch (Schweiz, Alternative)",
value = "de-CH-alt",
),
schema.Option(
display = "Dutch",
value = "nl-NL",
),
schema.Option(
display = "Dutch (Belgium)",
value = "nl-BE",
),
schema.Option(
display = "Spanish",
value = "es",
),
]
return schema.Schema(
version = "1",
fields = [
schema.Location(
id = "location",
name = "Location",
icon = "locationDot",
desc = "Location for which to display time",
),
schema.Dropdown(
id = "dialect",
name = "Language",
icon = "language",
desc = "Language in which to display time",
default = dialectOptions[0].value,
options = dialectOptions,
),
],
)