-
Notifications
You must be signed in to change notification settings - Fork 6
/
buildTimezoneData.js
78 lines (67 loc) · 2.18 KB
/
buildTimezoneData.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
const tzdata = require('tzdata');
const fs = require('fs');
const keyBy = require('lodash/keyBy');
const { DateTime, IANAZone } = require('luxon');
const DAY_STEP = 90;
const BOOST_ZONES = new Set([
'America/New_York',
'America/Chicago',
'America/Denver',
'America/Los_Angeles',
]);
function getAllOffsetNames(zoneName) {
let currentTime = DateTime.fromObject({}, { zone: zoneName });
if (currentTime.isOffsetFixed) {
return {
shortOffsetNames: [currentTime.offsetNameShort],
longOffsetNames: [currentTime.offsetNameLong],
};
}
// hackkkkkkk alert: iterate through all days in the past year and find all offset names
// during that time
const shortOffsetNames = new Set();
const longOffsetNames = new Set();
for (let i = 0; i < 366; i += DAY_STEP) {
shortOffsetNames.add(currentTime.offsetNameShort);
longOffsetNames.add(currentTime.offsetNameLong);
currentTime = currentTime.minus({ days: DAY_STEP });
}
return {
shortOffsetNames: [...shortOffsetNames],
longOffsetNames: [...longOffsetNames],
};
}
const filterOffsetNames = ({ shortOffsetNames, longOffsetNames }) => ({
shortOffsetNames: shortOffsetNames.filter((offsetName) => offsetName != null),
longOffsetNames: longOffsetNames.filter((offsetName) => offsetName != null),
});
const getBoostForTimezone = (zoneName) => {
if (BOOST_ZONES.has(zoneName)) {
return 2.0;
}
const slashes = zoneName.match(/\//g) || [];
if (slashes.length !== 1) {
return 0.5;
}
return 1.0;
};
const timezoneOptions = Object.keys(tzdata.zones)
.filter((zoneName) => IANAZone.isValidZone(zoneName))
.filter((zoneName) => typeof tzdata.zones[zoneName] !== 'string')
.map((zoneName) => ({
name: zoneName,
nameKeywords: zoneName.replace(/[/_]/g, ' '),
$boost: getBoostForTimezone(zoneName),
...filterOffsetNames(getAllOffsetNames(zoneName)),
}));
fs.writeFile(
'./app/javascript/BuiltInFormControls/timezoneSelectData.json',
JSON.stringify({ zones: keyBy(timezoneOptions, (zone) => zone.name) }, null, 2),
(err) => {
if (err) {
console.error('Error writing timezone data', err);
} else {
console.log('Timezone data successfully written!');
}
},
);