This repository has been archived by the owner on Jan 9, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
embed.js
198 lines (192 loc) · 2.94 KB
/
embed.js
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
import { validateSnowflake } from './convert.js'
import { decodeSnowflake, TZ_NAMES } from './util.js'
import dayjs from 'dayjs'
import utc from 'dayjs/plugin/utc.js'
import timezone from 'dayjs/plugin/timezone.js'
import localizedFormat from 'dayjs/plugin/localizedFormat.js'
import advancedFormat from 'dayjs/plugin/advancedFormat.js'
dayjs.extend(utc)
dayjs.extend(timezone)
dayjs.extend(localizedFormat)
dayjs.extend(advancedFormat)
// https://github.com/iamkun/dayjs/issues/732#issuecomment-554383261
function adaptLocale(locale) {
if (locale === 'zn') return 'zh-cn'
if (locale === 'no') return 'nb'
if (dayjsLocales.includes(locale)) return locale
if (dayjsLocales.includes(locale.split('-')[0])) return locale.split('-')[0]
return 'en'
}
let epoch = +process.env.SNOWFLAKE_EPOCH || undefined
export function getEmbedTitle({ l, z, s, f }) {
// Sanitize inputs (queries sent more than once become arrays, thanks Viper)
l = l && l.toString()
z = z && z.toString()
s = s && s.toString()
f = f && f.toString()
if (!z) return false // No time zone means embed disabled
let embedDate
if (s) {
try {
embedDate = validateSnowflake(s, epoch)
} catch (e) {}
} else if (f) {
try {
embedDate = validateSnowflake(decodeSnowflake(f, epoch))
} catch (e) {}
}
if (!embedDate) return false
embedDate = new dayjs(embedDate).tz(TZ_NAMES[parseInt(z, 36)] || 'UTC')
if (l) embedDate = embedDate.locale(adaptLocale(l))
// There's a bug in Day.js where setting locale on a UTC time zone causes the original offset to be subtracted twice, so we use .utc() to force correct UTC time
if (embedDate.utcOffset() === 0) {
return embedDate.utc().format('L LTS') + ' UTC'
}
return embedDate.format('L LTS z')
}
// https://unpkg.com/dayjs@1.10.6/locale.json
const dayjsLocales = [
'af',
'am',
'ar-dz',
'ar-kw',
'ar-ly',
'ar-ma',
'ar-sa',
'ar-tn',
'ar',
'az',
'be',
'bg',
'bi',
'bm',
'bn',
'bo',
'br',
'bs',
'ca',
'cs',
'cv',
'cy',
'da',
'de-at',
'de-ch',
'de',
'dv',
'el',
'en-au',
'en-ca',
'en-gb',
'en-ie',
'en-il',
'en-in',
'en-nz',
'en-sg',
'en-tt',
'en',
'eo',
'es-do',
'es-pr',
'es-us',
'es',
'fi',
'fo',
'fr-ca',
'fr-ch',
'fr',
'fy',
'ga',
'gd',
'gl',
'gom-latn',
'gu',
'he',
'hi',
'hr',
'ht',
'hu',
'hy-am',
'id',
'is',
'it-ch',
'it',
'ja',
'jv',
'ka',
'kk',
'km',
'kn',
'ko',
'ku',
'ky',
'lb',
'lo',
'lt',
'lv',
'me',
'mi',
'mk',
'ml',
'mn',
'mr',
'ms-my',
'ms',
'mt',
'my',
'nb',
'ne',
'nl-be',
'nl',
'nn',
'oc-lnc',
'pa-in',
'pl',
'pt-br',
'pt',
'ro',
'ru',
'rw',
'sd',
'se',
'si',
'sk',
'sl',
'sq',
'sr-cyrl',
'sr',
'ss',
'sv-fi',
'sv',
'sw',
'ta',
'te',
'tet',
'tg',
'th',
'tk',
'tl-ph',
'tlh',
'tr',
'tzl',
'tzm-latn',
'tzm',
'ug-cn',
'uk',
'ur',
'uz-latn',
'uz',
'vi',
'x-pseudo',
'yo',
'zh-cn',
'zh-hk',
'zh-tw',
'zh',
'et',
'eu',
'fa',
]
// Load all locales
for (const locale of dayjsLocales) {
await import(`dayjs/locale/${locale}.js`)
}