@mediafly/js-ui 51.6.0-8 → 51.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +6 -0
- package/lib/date-time-zone-city-picker/date-time-zone-city-picker.html +1 -1
- package/lib/src/components/date-time-zone-city-picker/date-time-zone-city-picker.d.ts +8 -4
- package/lib/src/components/date-time-zone-city-picker/date-time-zone-city-picker.js +34 -36
- package/lib/src/components/date-time-zone-city-picker/date-time-zone-city-picker.js.map +1 -1
- package/lib/src/components/date-time-zone-city-picker/time-zone-city-picker.js +8 -4
- package/lib/src/components/date-time-zone-city-picker/time-zone-city-picker.js.map +1 -1
- package/lib/src/components/date-time-zone-city-picker/time-zone-city.service.d.ts +16 -1
- package/lib/src/components/date-time-zone-city-picker/time-zone-city.service.js +114 -10
- package/lib/src/components/date-time-zone-city-picker/time-zone-city.service.js.map +1 -1
- package/lib/tsconfig.deploy.tsbuildinfo +5 -5
- package/package.json +1 -1
- package/src/components/date-time-zone-city-picker/date-time-zone-city-picker.html +1 -1
- package/src/components/date-time-zone-city-picker/date-time-zone-city-picker.ts +34 -35
- package/src/components/date-time-zone-city-picker/time-zone-city-picker.ts +7 -2
- package/src/components/date-time-zone-city-picker/time-zone-city.service.ts +133 -11
package/CHANGELOG.md
CHANGED
|
@@ -21,7 +21,7 @@
|
|
|
21
21
|
</mfly:time-picker>
|
|
22
22
|
|
|
23
23
|
<mfly:time-zone-city-picker
|
|
24
|
-
input-id="$ctrl.inputId
|
|
24
|
+
input-id="{{ $ctrl.inputId }}TimeZone"
|
|
25
25
|
ng-show="!$ctrl.isDisabled"
|
|
26
26
|
on-time-zone-change="$ctrl.updateTimeZone(timeZoneName)"
|
|
27
27
|
time-zone-name="$ctrl.timeZoneName">
|
|
@@ -7,20 +7,24 @@ export interface DateTimeZoneCity {
|
|
|
7
7
|
export default class DateTimeZoneCityPickerController implements IComponentController {
|
|
8
8
|
format: string;
|
|
9
9
|
inputId: string;
|
|
10
|
-
isDisabled
|
|
10
|
+
isDisabled?: boolean;
|
|
11
11
|
isRequired: boolean;
|
|
12
|
-
|
|
12
|
+
value?: DateTimeZoneCity;
|
|
13
13
|
onChange: (param: {
|
|
14
14
|
newValue: DateTimeZoneCity;
|
|
15
15
|
}) => void;
|
|
16
|
-
|
|
16
|
+
onOpen: () => void;
|
|
17
17
|
protected dateLuxon: DateTime | null;
|
|
18
18
|
protected timeLuxon: DateTime | null;
|
|
19
19
|
protected timeZoneName: string | null;
|
|
20
20
|
$onInit(): void;
|
|
21
|
-
|
|
21
|
+
$onChanges(changes: {
|
|
22
|
+
value: ng.IChangesObject<DateTimeZoneCity>;
|
|
23
|
+
}): void;
|
|
24
|
+
private initFromValue;
|
|
22
25
|
protected updateDate(dateLuxon: DateTime): void;
|
|
23
26
|
protected updateTime(timeLuxon: DateTime): void;
|
|
24
27
|
protected updateTimeZone(timeZoneName: string): void;
|
|
25
28
|
private updateModel;
|
|
29
|
+
private datesMatch;
|
|
26
30
|
}
|
|
@@ -14,28 +14,14 @@ const luxon_1 = require("luxon");
|
|
|
14
14
|
const time_zone_city_service_1 = require("./time-zone-city.service");
|
|
15
15
|
let DateTimeZoneCityPickerController = class DateTimeZoneCityPickerController {
|
|
16
16
|
$onInit() {
|
|
17
|
-
|
|
18
|
-
this.ngModelCtrl.$formatters.push((modelValue) => {
|
|
19
|
-
var _a;
|
|
20
|
-
if (modelValue) {
|
|
21
|
-
this.initModelFromAPI(modelValue);
|
|
22
|
-
}
|
|
23
|
-
else {
|
|
24
|
-
this.dateLuxon = null;
|
|
25
|
-
this.timeLuxon = null;
|
|
26
|
-
this.timeZoneName = null;
|
|
27
|
-
}
|
|
28
|
-
(_a = this.onChange) === null || _a === void 0 ? void 0 : _a.call(this, { newValue: modelValue });
|
|
29
|
-
return modelValue;
|
|
30
|
-
});
|
|
31
|
-
this.ngModelCtrl.$parsers.push((modelValue) => {
|
|
32
|
-
var _a;
|
|
33
|
-
(_a = this.onChange) === null || _a === void 0 ? void 0 : _a.call(this, { newValue: modelValue });
|
|
34
|
-
return modelValue;
|
|
35
|
-
});
|
|
17
|
+
this.initFromValue(this.value);
|
|
36
18
|
}
|
|
37
|
-
|
|
38
|
-
|
|
19
|
+
$onChanges(changes) {
|
|
20
|
+
if (changes.value) {
|
|
21
|
+
this.initFromValue(changes.value.currentValue);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
initFromValue(value) {
|
|
39
25
|
let luxon;
|
|
40
26
|
if (!(value === null || value === void 0 ? void 0 : value.dateTime)) {
|
|
41
27
|
luxon = undefined;
|
|
@@ -74,26 +60,25 @@ let DateTimeZoneCityPickerController = class DateTimeZoneCityPickerController {
|
|
|
74
60
|
millisecond: 0
|
|
75
61
|
});
|
|
76
62
|
}
|
|
77
|
-
const
|
|
78
|
-
this.timeZoneName = (
|
|
79
|
-
console.log(`-----> and so: ${this.dateLuxon} and ${this.timeLuxon} and ${this.timeZoneName}`);
|
|
63
|
+
const timeZone = time_zone_city_service_1.getTimeZone(value === null || value === void 0 ? void 0 : value.timeZoneName);
|
|
64
|
+
this.timeZoneName = (timeZone === null || timeZone === void 0 ? void 0 : timeZone.timeZoneName) || Intl.DateTimeFormat().resolvedOptions().timeZone;
|
|
80
65
|
}
|
|
81
66
|
updateDate(dateLuxon) {
|
|
82
|
-
console.log(`-----> updateDate: ${dateLuxon} (time is ${this.timeLuxon})`);
|
|
83
67
|
this.dateLuxon = dateLuxon;
|
|
84
68
|
this.updateModel();
|
|
85
69
|
}
|
|
86
70
|
updateTime(timeLuxon) {
|
|
87
|
-
console.log(`-----> updateTime: ${timeLuxon}`);
|
|
88
71
|
this.timeLuxon = timeLuxon;
|
|
89
72
|
this.updateModel();
|
|
90
73
|
}
|
|
91
74
|
updateTimeZone(timeZoneName) {
|
|
92
|
-
console.log(`-----> updateTimeZone: ${timeZoneName}`);
|
|
93
75
|
this.timeZoneName = timeZoneName;
|
|
94
76
|
this.updateModel();
|
|
95
77
|
}
|
|
96
78
|
updateModel() {
|
|
79
|
+
var _a, _b, _c;
|
|
80
|
+
const oldDateTime = (_a = this.value) === null || _a === void 0 ? void 0 : _a.dateTime;
|
|
81
|
+
const oldTimeZoneName = (_b = this.value) === null || _b === void 0 ? void 0 : _b.timeZoneName;
|
|
97
82
|
let dateTime;
|
|
98
83
|
if (this.dateLuxon && this.timeLuxon) {
|
|
99
84
|
const targetZone = this.timeZoneName || 'utc';
|
|
@@ -109,10 +94,22 @@ let DateTimeZoneCityPickerController = class DateTimeZoneCityPickerController {
|
|
|
109
94
|
zone: targetZone
|
|
110
95
|
});
|
|
111
96
|
}
|
|
112
|
-
this.
|
|
113
|
-
dateTime,
|
|
97
|
+
this.value = {
|
|
98
|
+
dateTime: dateTime || null,
|
|
114
99
|
timeZoneName: this.timeZoneName
|
|
115
|
-
}
|
|
100
|
+
};
|
|
101
|
+
if (!this.datesMatch(oldDateTime, dateTime) || oldTimeZoneName !== this.timeZoneName) {
|
|
102
|
+
(_c = this.onChange) === null || _c === void 0 ? void 0 : _c.call(this, { newValue: this.value });
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
datesMatch(dateTime1, dateTime2) {
|
|
106
|
+
if (dateTime1 instanceof luxon_1.DateTime && dateTime2 instanceof luxon_1.DateTime) {
|
|
107
|
+
return dateTime1.toISO() === dateTime2.toISO();
|
|
108
|
+
}
|
|
109
|
+
else if (dateTime1 instanceof Date && dateTime2 instanceof Date) {
|
|
110
|
+
return dateTime1.toISOString() === dateTime2.toISOString();
|
|
111
|
+
}
|
|
112
|
+
return false;
|
|
116
113
|
}
|
|
117
114
|
};
|
|
118
115
|
__decorate([
|
|
@@ -124,28 +121,29 @@ __decorate([
|
|
|
124
121
|
__metadata("design:type", String)
|
|
125
122
|
], DateTimeZoneCityPickerController.prototype, "inputId", void 0);
|
|
126
123
|
__decorate([
|
|
127
|
-
decorators_1.Input(
|
|
124
|
+
decorators_1.Input(),
|
|
128
125
|
__metadata("design:type", Boolean)
|
|
129
126
|
], DateTimeZoneCityPickerController.prototype, "isDisabled", void 0);
|
|
130
127
|
__decorate([
|
|
131
128
|
decorators_1.Input('@'),
|
|
132
129
|
__metadata("design:type", Boolean)
|
|
133
130
|
], DateTimeZoneCityPickerController.prototype, "isRequired", void 0);
|
|
131
|
+
__decorate([
|
|
132
|
+
decorators_1.Input(),
|
|
133
|
+
__metadata("design:type", Object)
|
|
134
|
+
], DateTimeZoneCityPickerController.prototype, "value", void 0);
|
|
134
135
|
__decorate([
|
|
135
136
|
decorators_1.Output('&?'),
|
|
136
137
|
__metadata("design:type", Function)
|
|
137
|
-
], DateTimeZoneCityPickerController.prototype, "
|
|
138
|
+
], DateTimeZoneCityPickerController.prototype, "onChange", void 0);
|
|
138
139
|
__decorate([
|
|
139
140
|
decorators_1.Output('&?'),
|
|
140
141
|
__metadata("design:type", Function)
|
|
141
|
-
], DateTimeZoneCityPickerController.prototype, "
|
|
142
|
+
], DateTimeZoneCityPickerController.prototype, "onOpen", void 0);
|
|
142
143
|
DateTimeZoneCityPickerController = __decorate([
|
|
143
144
|
decorators_1.Component({
|
|
144
145
|
selector: 'mflyDateTimeZoneCityPicker',
|
|
145
146
|
template: require('./date-time-zone-city-picker.html'),
|
|
146
|
-
require: {
|
|
147
|
-
ngModelCtrl: 'ngModel'
|
|
148
|
-
}
|
|
149
147
|
})
|
|
150
148
|
], DateTimeZoneCityPickerController);
|
|
151
149
|
exports.default = DateTimeZoneCityPickerController;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"date-time-zone-city-picker.js","sourceRoot":"","sources":["../../../../src/components/date-time-zone-city-picker/date-time-zone-city-picker.ts"],"names":[],"mappings":";;;;;;;;;;;AACA,8CAAwD;AACxD,iCAAgC;AAChC,qEAAsD;
|
|
1
|
+
{"version":3,"file":"date-time-zone-city-picker.js","sourceRoot":"","sources":["../../../../src/components/date-time-zone-city-picker/date-time-zone-city-picker.ts"],"names":[],"mappings":";;;;;;;;;;;AACA,8CAAwD;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IAAX,kBAAK,CAAC,GAAG,CAAC;;iEAAgB;AAClB;IAAR,kBAAK,EAAE;;oEAAqB;AACjB;IAAX,kBAAK,CAAC,GAAG,CAAC;;oEAAoB;AACtB;IAAR,kBAAK,EAAE;;+DAAyB;AACnB;IAAb,mBAAM,CAAC,IAAI,CAAC;;kEAA0D;AACzD;IAAb,mBAAM,CAAC,IAAI,CAAC;;gEAAmB;AARZ,gCAAgC;IAJpD,sBAAS,CAAC;QACV,QAAQ,EAAE,4BAA4B;QACtC,QAAQ,EAAE,OAAO,CAAC,mCAAmC,CAAC;KACtD,CAAC;GACmB,gCAAgC,CA8HpD;kBA9HoB,gCAAgC"}
|
|
@@ -17,7 +17,7 @@ let TimeZoneCityPickerController = class TimeZoneCityPickerController {
|
|
|
17
17
|
}
|
|
18
18
|
$onInit() {
|
|
19
19
|
this.timeZones = time_zone_city_service_1.getTimeZones();
|
|
20
|
-
this.setTimeZone(this.timeZoneName
|
|
20
|
+
this.setTimeZone(this.timeZoneName);
|
|
21
21
|
}
|
|
22
22
|
$onChanges(changes) {
|
|
23
23
|
if (changes.timeZoneName) {
|
|
@@ -29,12 +29,16 @@ let TimeZoneCityPickerController = class TimeZoneCityPickerController {
|
|
|
29
29
|
this.onTimeZoneChange({ timeZoneName: timeZone.timeZoneName });
|
|
30
30
|
}
|
|
31
31
|
setTimeZone(timeZoneName) {
|
|
32
|
-
var _a, _b;
|
|
33
|
-
|
|
32
|
+
var _a, _b, _c;
|
|
33
|
+
const oldTimeZone = this.timeZone;
|
|
34
|
+
this.timeZone = (_c = (_b = (_a = time_zone_city_service_1.getTimeZone(timeZoneName)) !== null && _a !== void 0 ? _a : time_zone_city_service_1.getTimeZone(Intl.DateTimeFormat().resolvedOptions().timeZone)) !== null && _b !== void 0 ? _b : time_zone_city_service_1.getTimeZone('America/Chicago')) !== null && _c !== void 0 ? _c : this.timeZones[0];
|
|
35
|
+
if (oldTimeZone !== this.timeZone) {
|
|
36
|
+
this.onTimeZoneChange({ timeZoneName: this.timeZone.timeZoneName });
|
|
37
|
+
}
|
|
34
38
|
}
|
|
35
39
|
};
|
|
36
40
|
__decorate([
|
|
37
|
-
decorators_1.Input(),
|
|
41
|
+
decorators_1.Input('@'),
|
|
38
42
|
__metadata("design:type", String)
|
|
39
43
|
], TimeZoneCityPickerController.prototype, "inputId", void 0);
|
|
40
44
|
__decorate([
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"time-zone-city-picker.js","sourceRoot":"","sources":["../../../../src/components/date-time-zone-city-picker/time-zone-city-picker.ts"],"names":[],"mappings":";;;;;;;;;;;AACA,4DAAsE;AACtE,qEAA8E;AAM9E,IAAqB,4BAA4B,GAAjD,MAAqB,4BAA4B;IAAjD;QAOW,cAAS,GAAe,EAAE,CAAA;
|
|
1
|
+
{"version":3,"file":"time-zone-city-picker.js","sourceRoot":"","sources":["../../../../src/components/date-time-zone-city-picker/time-zone-city-picker.ts"],"names":[],"mappings":";;;;;;;;;;;AACA,4DAAsE;AACtE,qEAA8E;AAM9E,IAAqB,4BAA4B,GAAjD,MAAqB,4BAA4B;IAAjD;QAOW,cAAS,GAAe,EAAE,CAAA;IAgCrC,CAAC;IA9BA,OAAO;QAEN,IAAI,CAAC,SAAS,GAAG,qCAAY,EAAE,CAAA;QAC/B,IAAI,CAAC,WAAW,CAAC,IAAI,CAAC,YAAY,CAAC,CAAA;IACpC,CAAC;IAED,UAAU,CAAC,OAAoD;QAE9D,IAAI,OAAO,CAAC,YAAY,EAAE;YACzB,IAAI,CAAC,WAAW,CAAC,OAAO,CAAC,YAAY,CAAC,YAAY,CAAC,CAAA;SACnD;IACF,CAAC;IAES,cAAc,CAAC,QAAkB;QAE1C,IAAI,CAAC,QAAQ,GAAG,QAAQ,CAAA;QACxB,IAAI,CAAC,gBAAgB,CAAC,EAAE,YAAY,EAAE,QAAQ,CAAC,YAAY,EAAE,CAAC,CAAA;IAC/D,CAAC;IAEO,WAAW,CAAC,YAAoB;;QAEvC,MAAM,WAAW,GAAG,IAAI,CAAC,QAAQ,CAAA;QACjC,IAAI,CAAC,QAAQ,GAAG,MAAA,MAAA,MAAA,oCAAW,CAAC,YAAY,CAAC,mCACrC,oCAAW,CAAC,IAAI,CAAC,cAAc,EAAE,CAAC,eAAe,EAAE,CAAC,QAAQ,CAAC,mCAC7D,oCAAW,CAAC,iBAAiB,CAAC,mCAC9B,IAAI,CAAC,SAAS,CAAC,CAAC,CAAC,CAAA;QACrB,IAAI,WAAW,KAAK,IAAI,CAAC,QAAQ,EAAE;YAClC,IAAI,CAAC,gBAAgB,CAAC,EAAE,YAAY,EAAE,IAAI,CAAC,QAAQ,CAAC,YAAY,EAAE,CAAC,CAAA;SACnE;IACF,CAAC;CACD,CAAA;AArCY;IAAX,kBAAK,CAAC,GAAG,CAAC;;6DAAgB;AAClB;IAAR,kBAAK,EAAE;;kEAAqB;AACnB;IAAT,mBAAM,EAAE;;sEAA4D;AAJjD,4BAA4B;IAJhD,sBAAS,CAAC;QACV,QAAQ,EAAE,wBAAwB;QAClC,QAAQ,EAAE,OAAO,CAAC,8BAA8B,CAAC;KACjD,CAAC;GACmB,4BAA4B,CAuChD;kBAvCoB,4BAA4B"}
|
|
@@ -1,9 +1,17 @@
|
|
|
1
|
+
export interface DSTSignature {
|
|
2
|
+
observesDST: boolean;
|
|
3
|
+
signature: string;
|
|
4
|
+
}
|
|
1
5
|
export interface TimeZone {
|
|
2
6
|
/** e.g. 'Chicago, Reynosa, Winnipeg' */
|
|
3
7
|
cityNames: string;
|
|
4
8
|
/** A signature of the Daylight Saving Time rules for the time zone. */
|
|
5
9
|
dstSignature: string;
|
|
6
|
-
/**
|
|
10
|
+
/** The population of the most populous city in the time zone. */
|
|
11
|
+
leadCityPopulation: number;
|
|
12
|
+
/** Whether the time zone observes Daylight Saving Time. */
|
|
13
|
+
observesDST: boolean;
|
|
14
|
+
/** The offset when not in DST. e.g. '-06:00' */
|
|
7
15
|
offset: string;
|
|
8
16
|
/** e.g. -360 */
|
|
9
17
|
offsetMinutes: number;
|
|
@@ -18,6 +26,13 @@ export interface TimeZone {
|
|
|
18
26
|
* You don't need to call this. It's only for testing.
|
|
19
27
|
*/
|
|
20
28
|
export declare function clearTimeZoneCache(): void;
|
|
29
|
+
/**
|
|
30
|
+
* Given a date that includes a time offset, find a matching IANA time zone name.
|
|
31
|
+
* More of our users are in places that observe DST, so we bias towards choosing those: Chicago rather than Mexico City; London rather than Lagos.
|
|
32
|
+
* Where there are still multiple matches, we choose the most populous city.
|
|
33
|
+
* @param dateTime - e.g. '2025-01-01T00:00:00.000-06:00'
|
|
34
|
+
*/
|
|
35
|
+
export declare function getBestGuessTimeZoneCity(dateTimeISO: string): string | undefined;
|
|
21
36
|
/**
|
|
22
37
|
* Finds the time zone that matches the time zone and DST signature of the given time zone name.
|
|
23
38
|
* @param timeZoneName - e.g. 'America/Chicago'
|
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.getTimeZones = exports.getTimeZone = exports.clearTimeZoneCache = void 0;
|
|
3
|
+
exports.getTimeZones = exports.getTimeZone = exports.getBestGuessTimeZoneCity = exports.clearTimeZoneCache = void 0;
|
|
4
4
|
const tzdb_1 = require("@vvo/tzdb");
|
|
5
5
|
const cityPopulation_1 = require("./cityPopulation");
|
|
6
|
+
const thisYear = new Date().getFullYear();
|
|
6
7
|
let cachedTimeZones = null;
|
|
7
8
|
/**
|
|
8
9
|
* You don't need to call this. It's only for testing.
|
|
@@ -11,6 +12,87 @@ function clearTimeZoneCache() {
|
|
|
11
12
|
cachedTimeZones = null;
|
|
12
13
|
}
|
|
13
14
|
exports.clearTimeZoneCache = clearTimeZoneCache;
|
|
15
|
+
/**
|
|
16
|
+
* Given a date that includes a time offset, find a matching IANA time zone name.
|
|
17
|
+
* More of our users are in places that observe DST, so we bias towards choosing those: Chicago rather than Mexico City; London rather than Lagos.
|
|
18
|
+
* Where there are still multiple matches, we choose the most populous city.
|
|
19
|
+
* @param dateTime - e.g. '2025-01-01T00:00:00.000-06:00'
|
|
20
|
+
*/
|
|
21
|
+
function getBestGuessTimeZoneCity(dateTimeISO) {
|
|
22
|
+
var _a;
|
|
23
|
+
if (!dateTimeISO) {
|
|
24
|
+
return undefined;
|
|
25
|
+
}
|
|
26
|
+
const date = new Date(dateTimeISO);
|
|
27
|
+
if (isNaN(date.getTime())) {
|
|
28
|
+
return undefined;
|
|
29
|
+
}
|
|
30
|
+
let offsetMinutes;
|
|
31
|
+
const z = /Z$/i.test(dateTimeISO);
|
|
32
|
+
if (z) {
|
|
33
|
+
offsetMinutes = 0;
|
|
34
|
+
}
|
|
35
|
+
else {
|
|
36
|
+
const offsetMatch = dateTimeISO.match(/([+-])(\d{2}):?(\d{2})$/);
|
|
37
|
+
if (!offsetMatch) {
|
|
38
|
+
offsetMinutes = 0;
|
|
39
|
+
}
|
|
40
|
+
else {
|
|
41
|
+
offsetMinutes = parseInt(offsetMatch[2], 10) * 60 + parseInt(offsetMatch[3], 10);
|
|
42
|
+
if (offsetMatch[1] === '-') {
|
|
43
|
+
offsetMinutes = -offsetMinutes;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
// Find all time zones that match the given date.
|
|
48
|
+
const candidates = [];
|
|
49
|
+
for (const timeZone of getTimeZones()) {
|
|
50
|
+
// For each time zone, format the given datetime to its time zone and see if the offset matches.
|
|
51
|
+
// This takes DST into account, as defined for that instant at that location.
|
|
52
|
+
const dtf = new Intl.DateTimeFormat('en', {
|
|
53
|
+
timeZone: timeZone.timeZoneName,
|
|
54
|
+
timeZoneName: 'shortOffset',
|
|
55
|
+
hour: '2-digit', // forces a time - needed for timeZoneName to appear in parts
|
|
56
|
+
});
|
|
57
|
+
const parts = dtf.formatToParts(new Date(dateTimeISO));
|
|
58
|
+
const tzName = (_a = parts.find(p => p.type === 'timeZoneName')) === null || _a === void 0 ? void 0 : _a.value;
|
|
59
|
+
if (!tzName) {
|
|
60
|
+
continue;
|
|
61
|
+
}
|
|
62
|
+
let tzOffsetMinutes = 0;
|
|
63
|
+
if (tzName !== 'UTC' && tzName !== 'GMT') {
|
|
64
|
+
const m = tzName.match(/^GMT([+-])(\d{1,2})(?::?(\d{2}))?$/);
|
|
65
|
+
if (!m) {
|
|
66
|
+
continue;
|
|
67
|
+
}
|
|
68
|
+
const h = parseInt(m[2], 10);
|
|
69
|
+
const min = m[3] ? parseInt(m[3], 10) : 0;
|
|
70
|
+
tzOffsetMinutes = h * 60 + min;
|
|
71
|
+
if (m[1] === '-') {
|
|
72
|
+
tzOffsetMinutes = -tzOffsetMinutes;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
if (tzOffsetMinutes === offsetMinutes) {
|
|
76
|
+
candidates.push(timeZone);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
if (candidates.length === 0) {
|
|
80
|
+
return undefined;
|
|
81
|
+
}
|
|
82
|
+
// More of our users are in places that observe DST, so we should prefer those. Choose Chicago over Mexico City; London over Lagos.
|
|
83
|
+
// Otherwise, choose the most populous city.
|
|
84
|
+
candidates.sort((a, b) => {
|
|
85
|
+
if (a.observesDST && !b.observesDST) {
|
|
86
|
+
return -1;
|
|
87
|
+
}
|
|
88
|
+
if (!a.observesDST && b.observesDST) {
|
|
89
|
+
return 1;
|
|
90
|
+
}
|
|
91
|
+
return b.leadCityPopulation - a.leadCityPopulation;
|
|
92
|
+
});
|
|
93
|
+
return candidates[0].timeZoneName;
|
|
94
|
+
}
|
|
95
|
+
exports.getBestGuessTimeZoneCity = getBestGuessTimeZoneCity;
|
|
14
96
|
/**
|
|
15
97
|
* Finds the time zone that matches the time zone and DST signature of the given time zone name.
|
|
16
98
|
* @param timeZoneName - e.g. 'America/Chicago'
|
|
@@ -28,7 +110,7 @@ function getTimeZone(timeZoneName) {
|
|
|
28
110
|
// e.g. given 'Europe/Isle_of_Man', return the time zone for 'Europe/London'.
|
|
29
111
|
const samples = getDateSamples();
|
|
30
112
|
const dstSignature = getDSTSignature(timeZoneName, samples);
|
|
31
|
-
return timeZones.find(tz => tz.dstSignature === dstSignature);
|
|
113
|
+
return timeZones.find(tz => tz.dstSignature === dstSignature.signature);
|
|
32
114
|
}
|
|
33
115
|
exports.getTimeZone = getTimeZone;
|
|
34
116
|
/**
|
|
@@ -46,6 +128,8 @@ function getTimeZones() {
|
|
|
46
128
|
// the same offset, we group the time zones together.
|
|
47
129
|
// As of 2025, this reduces the number of time zones to a more manageable 67. This exact number may change in
|
|
48
130
|
// future as browsers are updated with changes to time zones and DST rules.
|
|
131
|
+
// For example, Cairo currently has its own entry, but we expect it to be merged with Eastern Europe in 2028,
|
|
132
|
+
// when it will have been five years since they adopted the same DST rules.
|
|
49
133
|
if (cachedTimeZones) {
|
|
50
134
|
// Compiling the list takes about 20ms to run, so worth caching.
|
|
51
135
|
return cachedTimeZones;
|
|
@@ -60,13 +144,13 @@ function getTimeZones() {
|
|
|
60
144
|
const groups = new Map();
|
|
61
145
|
for (const timezone of timezonesWithDSTSignatures) {
|
|
62
146
|
const cityName = (_a = timezone.mainCities[0]) !== null && _a !== void 0 ? _a : timezone.countryName;
|
|
63
|
-
const group = groups.get(timezone.dstSignature);
|
|
147
|
+
const group = groups.get(timezone.dstSignature.signature);
|
|
64
148
|
const population = (_b = cityPopulation_1.cityPopulation.get(`${cityName}, ${timezone.countryName}`)) !== null && _b !== void 0 ? _b : 0;
|
|
65
149
|
if (!group) {
|
|
66
150
|
const offsetHour = Math.floor(Math.abs(timezone.rawOffsetInMinutes) / 60);
|
|
67
151
|
const offsetMinute = Math.abs(timezone.rawOffsetInMinutes) % 60;
|
|
68
152
|
const offset = `${timezone.rawOffsetInMinutes < 0 ? '-' : '+'}${offsetHour.toString().padStart(2, '0')}:${offsetMinute.toString().padStart(2, '0')}`;
|
|
69
|
-
groups.set(timezone.dstSignature, {
|
|
153
|
+
groups.set(timezone.dstSignature.signature, {
|
|
70
154
|
cities: [{
|
|
71
155
|
city: cityName,
|
|
72
156
|
country: timezone.countryName,
|
|
@@ -75,7 +159,8 @@ function getTimeZones() {
|
|
|
75
159
|
timeZoneAlternativeName: timezone.alternativeName,
|
|
76
160
|
timeZoneName: timezone.name,
|
|
77
161
|
}],
|
|
78
|
-
dstSignature: timezone.dstSignature,
|
|
162
|
+
dstSignature: timezone.dstSignature.signature,
|
|
163
|
+
observesDST: timezone.dstSignature.observesDST,
|
|
79
164
|
offset,
|
|
80
165
|
offsetMinutes: timezone.rawOffsetInMinutes,
|
|
81
166
|
});
|
|
@@ -102,6 +187,8 @@ function getTimeZones() {
|
|
|
102
187
|
.map(group => ({
|
|
103
188
|
cityNames: getCityList(group.cities),
|
|
104
189
|
dstSignature: group.dstSignature,
|
|
190
|
+
leadCityPopulation: group.cities[0].population,
|
|
191
|
+
observesDST: group.observesDST,
|
|
105
192
|
offset: group.offset,
|
|
106
193
|
offsetMinutes: group.offsetMinutes,
|
|
107
194
|
timeZoneAbbreviation: group.cities[0].timeZoneAbbreviation,
|
|
@@ -121,10 +208,11 @@ function getCityList(cities) {
|
|
|
121
208
|
}
|
|
122
209
|
return result;
|
|
123
210
|
}
|
|
124
|
-
// To detect places with the same DST rules, make a signature for each timezone: a list of offset minutes at each sample time
|
|
211
|
+
// To detect places with the same DST rules, make a signature for each timezone: a list of offset minutes at each sample time.
|
|
125
212
|
function getDSTSignature(timezoneName, sampleTimes) {
|
|
126
213
|
var _a, _b;
|
|
127
214
|
try {
|
|
215
|
+
let observesDST = false;
|
|
128
216
|
const formatter = new Intl.DateTimeFormat('en', {
|
|
129
217
|
timeZone: timezoneName,
|
|
130
218
|
timeZoneName: 'shortOffset',
|
|
@@ -132,17 +220,33 @@ function getDSTSignature(timezoneName, sampleTimes) {
|
|
|
132
220
|
minute: '2-digit',
|
|
133
221
|
});
|
|
134
222
|
const sig = [];
|
|
223
|
+
let prevOffset;
|
|
135
224
|
for (const sample of sampleTimes) {
|
|
136
225
|
const parts = formatter.formatToParts(sample);
|
|
137
|
-
const
|
|
138
|
-
sig.push(
|
|
226
|
+
const offset = (_b = (_a = parts.find(p => p.type === 'timeZoneName')) === null || _a === void 0 ? void 0 : _a.value) !== null && _b !== void 0 ? _b : '';
|
|
227
|
+
sig.push(offset);
|
|
228
|
+
// Only say "it observes DST" if it does so now or in the future. (Mexico stopped in 2022, for example.)
|
|
229
|
+
if (prevOffset && prevOffset !== offset && sample.getFullYear() >= thisYear) {
|
|
230
|
+
observesDST = true;
|
|
231
|
+
}
|
|
232
|
+
prevOffset = offset;
|
|
139
233
|
}
|
|
140
|
-
return
|
|
234
|
+
return {
|
|
235
|
+
observesDST,
|
|
236
|
+
signature: sig.join(','),
|
|
237
|
+
};
|
|
141
238
|
}
|
|
142
239
|
catch (error) {
|
|
143
|
-
return
|
|
240
|
+
return {
|
|
241
|
+
observesDST: false,
|
|
242
|
+
signature: '',
|
|
243
|
+
};
|
|
144
244
|
}
|
|
145
245
|
}
|
|
246
|
+
// These are the dates we sample to detect places with the same DST rules. This is imperfect: some places might switch DST on
|
|
247
|
+
// slightly different dates. A more accurate signature would do a binary chop to identify the exact date of each switch. That
|
|
248
|
+
// would be too slow, so we'll go with this for now. As of 2025, the only misidentified time zone is Asia/Hebron, which
|
|
249
|
+
// misaligns with Europe/Athens for two weeks in Spring and one day in Autumn. I'm happy to call this good enough.
|
|
146
250
|
function getDateSamples() {
|
|
147
251
|
const now = new Date();
|
|
148
252
|
const offsets = [-5, -1, 0, 1, 5];
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"time-zone-city.service.js","sourceRoot":"","sources":["../../../../src/components/date-time-zone-city-picker/time-zone-city.service.ts"],"names":[],"mappings":";;;AAAA,oCAAwC;AACxC,qDAAiD;
|
|
1
|
+
{"version":3,"file":"time-zone-city.service.js","sourceRoot":"","sources":["../../../../src/components/date-time-zone-city-picker/time-zone-city.service.ts"],"names":[],"mappings":";;;AAAA,oCAAwC;AACxC,qDAAiD;AA4BjD,MAAM,QAAQ,GAAG,IAAI,I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,CAAA;IACrD,MAAM,QAAQ,GAAG;QAChB,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,EAAE,EAAE;QACf,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,EAAE,EAAE,EAAG,SAAS;KAC3B,CAAA;IACD,MAAM,OAAO,GAAW,EAAE,CAAA;IAE1B,KAAK,MAAM,CAAC,IAAI,KAAK,EAAE;QACtB,KAAK,MAAM,EAAE,CAAC,EAAE,CAAC,EAAE,IAAI,QAAQ,EAAE;YAChC,OAAO,CAAC,IAAI,CAAC,IAAI,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,CAAA;SAClD;KACD;IAED,OAAO,OAAO,CAAA;AACf,CAAC"}
|
|
@@ -472,17 +472,17 @@
|
|
|
472
472
|
"affectsGlobalScope": false
|
|
473
473
|
},
|
|
474
474
|
"../src/components/date-time-zone-city-picker/time-zone-city.service.ts": {
|
|
475
|
-
"version": "
|
|
476
|
-
"signature": "
|
|
475
|
+
"version": "cf19883bc48570fd4c35bda0cff96ad97f5a18f5d820b835c5f521132d0fdf9a",
|
|
476
|
+
"signature": "6a86fca3385ffad61564712f543b0a1118a296fd1a4c2755e9bee863b81f4e14",
|
|
477
477
|
"affectsGlobalScope": false
|
|
478
478
|
},
|
|
479
479
|
"../src/components/date-time-zone-city-picker/date-time-zone-city-picker.ts": {
|
|
480
|
-
"version": "
|
|
481
|
-
"signature": "
|
|
480
|
+
"version": "634fc279a35ed1a1ddd05460b8fe0d16339aa04a5b1837f130a3026e5d6965f9",
|
|
481
|
+
"signature": "936d63bde981ffa379951aa5294253b8cc972ad2a81cfa724445c214660ecce1",
|
|
482
482
|
"affectsGlobalScope": false
|
|
483
483
|
},
|
|
484
484
|
"../src/components/date-time-zone-city-picker/time-zone-city-picker.ts": {
|
|
485
|
-
"version": "
|
|
485
|
+
"version": "64a70292914bce5630068b20132037a4207078784cfcddbafcf7b2714f318569",
|
|
486
486
|
"signature": "6066bd236b98fe3c130fad8cd2c1644e18e3c86032785958211e875620520c32",
|
|
487
487
|
"affectsGlobalScope": false
|
|
488
488
|
},
|
package/package.json
CHANGED
|
@@ -21,7 +21,7 @@
|
|
|
21
21
|
</mfly:time-picker>
|
|
22
22
|
|
|
23
23
|
<mfly:time-zone-city-picker
|
|
24
|
-
input-id="$ctrl.inputId
|
|
24
|
+
input-id="{{ $ctrl.inputId }}TimeZone"
|
|
25
25
|
ng-show="!$ctrl.isDisabled"
|
|
26
26
|
on-time-zone-change="$ctrl.updateTimeZone(timeZoneName)"
|
|
27
27
|
time-zone-name="$ctrl.timeZoneName">
|
|
@@ -11,20 +11,16 @@ export interface DateTimeZoneCity {
|
|
|
11
11
|
@Component({
|
|
12
12
|
selector: 'mflyDateTimeZoneCityPicker',
|
|
13
13
|
template: require('./date-time-zone-city-picker.html'),
|
|
14
|
-
require: {
|
|
15
|
-
ngModelCtrl: 'ngModel'
|
|
16
|
-
}
|
|
17
14
|
})
|
|
18
15
|
export default class DateTimeZoneCityPickerController implements IComponentController {
|
|
19
16
|
|
|
20
17
|
@Input('@') format: string
|
|
21
18
|
@Input('@') inputId: string
|
|
22
|
-
@Input(
|
|
19
|
+
@Input() isDisabled?: boolean
|
|
23
20
|
@Input('@') isRequired: boolean
|
|
24
|
-
@
|
|
21
|
+
@Input() value?: DateTimeZoneCity
|
|
25
22
|
@Output('&?') onChange: (param: { newValue: DateTimeZoneCity }) => void
|
|
26
|
-
|
|
27
|
-
protected ngModelCtrl: ng.INgModelController
|
|
23
|
+
@Output('&?') onOpen: () => void
|
|
28
24
|
|
|
29
25
|
protected dateLuxon: DateTime | null
|
|
30
26
|
protected timeLuxon: DateTime | null
|
|
@@ -32,28 +28,18 @@ export default class DateTimeZoneCityPickerController implements IComponentContr
|
|
|
32
28
|
|
|
33
29
|
$onInit(): void {
|
|
34
30
|
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
}
|
|
44
|
-
this.onChange?.({ newValue: modelValue })
|
|
45
|
-
return modelValue
|
|
46
|
-
})
|
|
47
|
-
|
|
48
|
-
this.ngModelCtrl.$parsers.push((modelValue: DateTimeZoneCity) => {
|
|
49
|
-
this.onChange?.({ newValue: modelValue })
|
|
50
|
-
return modelValue
|
|
51
|
-
})
|
|
31
|
+
this.initFromValue(this.value)
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
$onChanges(changes: { value: ng.IChangesObject<DateTimeZoneCity> }): void {
|
|
35
|
+
|
|
36
|
+
if (changes.value) {
|
|
37
|
+
this.initFromValue(changes.value.currentValue)
|
|
38
|
+
}
|
|
52
39
|
}
|
|
53
40
|
|
|
54
|
-
private
|
|
41
|
+
private initFromValue(value: DateTimeZoneCity | undefined): void {
|
|
55
42
|
|
|
56
|
-
console.log(`-----> initModelFromAPI: ${JSON.stringify(value)}`)
|
|
57
43
|
let luxon: DateTime | undefined
|
|
58
44
|
if (!value?.dateTime) {
|
|
59
45
|
luxon = undefined
|
|
@@ -90,34 +76,33 @@ export default class DateTimeZoneCityPickerController implements IComponentContr
|
|
|
90
76
|
})
|
|
91
77
|
}
|
|
92
78
|
|
|
93
|
-
const
|
|
94
|
-
this.timeZoneName =
|
|
95
|
-
console.log(`-----> and so: ${this.dateLuxon} and ${this.timeLuxon} and ${this.timeZoneName}`)
|
|
79
|
+
const timeZone = getTimeZone(value?.timeZoneName)
|
|
80
|
+
this.timeZoneName = timeZone?.timeZoneName || Intl.DateTimeFormat().resolvedOptions().timeZone
|
|
96
81
|
}
|
|
97
82
|
|
|
98
83
|
protected updateDate(dateLuxon: DateTime): void {
|
|
99
84
|
|
|
100
|
-
console.log(`-----> updateDate: ${dateLuxon} (time is ${this.timeLuxon})`)
|
|
101
85
|
this.dateLuxon = dateLuxon
|
|
102
86
|
this.updateModel()
|
|
103
87
|
}
|
|
104
88
|
|
|
105
89
|
protected updateTime(timeLuxon: DateTime): void {
|
|
106
90
|
|
|
107
|
-
console.log(`-----> updateTime: ${timeLuxon}`)
|
|
108
91
|
this.timeLuxon = timeLuxon
|
|
109
92
|
this.updateModel()
|
|
110
93
|
}
|
|
111
94
|
|
|
112
95
|
protected updateTimeZone(timeZoneName: string): void {
|
|
113
96
|
|
|
114
|
-
console.log(`-----> updateTimeZone: ${timeZoneName}`)
|
|
115
97
|
this.timeZoneName = timeZoneName
|
|
116
98
|
this.updateModel()
|
|
117
99
|
}
|
|
118
100
|
|
|
119
101
|
private updateModel(): void {
|
|
120
102
|
|
|
103
|
+
const oldDateTime = this.value?.dateTime
|
|
104
|
+
const oldTimeZoneName = this.value?.timeZoneName
|
|
105
|
+
|
|
121
106
|
let dateTime: DateTime | undefined
|
|
122
107
|
if (this.dateLuxon && this.timeLuxon) {
|
|
123
108
|
const targetZone = this.timeZoneName || 'utc'
|
|
@@ -134,9 +119,23 @@ export default class DateTimeZoneCityPickerController implements IComponentContr
|
|
|
134
119
|
})
|
|
135
120
|
}
|
|
136
121
|
|
|
137
|
-
this.
|
|
138
|
-
dateTime,
|
|
122
|
+
this.value = {
|
|
123
|
+
dateTime: dateTime || null,
|
|
139
124
|
timeZoneName: this.timeZoneName
|
|
140
|
-
}
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
if (!this.datesMatch(oldDateTime, dateTime) || oldTimeZoneName !== this.timeZoneName) {
|
|
128
|
+
this.onChange?.({ newValue: this.value })
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
private datesMatch(dateTime1: DateTime | Date | null | undefined, dateTime2: DateTime | Date | null | undefined): boolean {
|
|
133
|
+
|
|
134
|
+
if (dateTime1 instanceof DateTime && dateTime2 instanceof DateTime) {
|
|
135
|
+
return dateTime1.toISO() === dateTime2.toISO()
|
|
136
|
+
} else if (dateTime1 instanceof Date && dateTime2 instanceof Date) {
|
|
137
|
+
return dateTime1.toISOString() === dateTime2.toISOString()
|
|
138
|
+
}
|
|
139
|
+
return false
|
|
141
140
|
}
|
|
142
141
|
}
|
|
@@ -8,7 +8,7 @@ import { getTimeZone, getTimeZones, TimeZone } from './time-zone-city.service'
|
|
|
8
8
|
})
|
|
9
9
|
export default class TimeZoneCityPickerController implements IComponentController {
|
|
10
10
|
|
|
11
|
-
@Input() inputId: string
|
|
11
|
+
@Input('@') inputId: string
|
|
12
12
|
@Input() timeZoneName: string
|
|
13
13
|
@Output() onTimeZoneChange: (param: { timeZoneName: string }) => void
|
|
14
14
|
|
|
@@ -18,7 +18,7 @@ export default class TimeZoneCityPickerController implements IComponentControlle
|
|
|
18
18
|
$onInit(): void {
|
|
19
19
|
|
|
20
20
|
this.timeZones = getTimeZones()
|
|
21
|
-
this.setTimeZone(this.timeZoneName
|
|
21
|
+
this.setTimeZone(this.timeZoneName)
|
|
22
22
|
}
|
|
23
23
|
|
|
24
24
|
$onChanges(changes: { timeZoneName: ng.IChangesObject<string> }): void {
|
|
@@ -36,8 +36,13 @@ export default class TimeZoneCityPickerController implements IComponentControlle
|
|
|
36
36
|
|
|
37
37
|
private setTimeZone(timeZoneName: string): void {
|
|
38
38
|
|
|
39
|
+
const oldTimeZone = this.timeZone
|
|
39
40
|
this.timeZone = getTimeZone(timeZoneName)
|
|
41
|
+
?? getTimeZone(Intl.DateTimeFormat().resolvedOptions().timeZone)
|
|
40
42
|
?? getTimeZone('America/Chicago')
|
|
41
43
|
?? this.timeZones[0]
|
|
44
|
+
if (oldTimeZone !== this.timeZone) {
|
|
45
|
+
this.onTimeZoneChange({ timeZoneName: this.timeZone.timeZoneName })
|
|
46
|
+
}
|
|
42
47
|
}
|
|
43
48
|
}
|
|
@@ -1,12 +1,21 @@
|
|
|
1
1
|
import { rawTimeZones } from '@vvo/tzdb'
|
|
2
2
|
import { cityPopulation } from './cityPopulation'
|
|
3
3
|
|
|
4
|
+
export interface DSTSignature {
|
|
5
|
+
observesDST: boolean
|
|
6
|
+
signature: string
|
|
7
|
+
}
|
|
8
|
+
|
|
4
9
|
export interface TimeZone {
|
|
5
10
|
/** e.g. 'Chicago, Reynosa, Winnipeg' */
|
|
6
11
|
cityNames: string
|
|
7
12
|
/** A signature of the Daylight Saving Time rules for the time zone. */
|
|
8
13
|
dstSignature: string
|
|
9
|
-
/**
|
|
14
|
+
/** The population of the most populous city in the time zone. */
|
|
15
|
+
leadCityPopulation: number
|
|
16
|
+
/** Whether the time zone observes Daylight Saving Time. */
|
|
17
|
+
observesDST: boolean
|
|
18
|
+
/** The offset when not in DST. e.g. '-06:00' */
|
|
10
19
|
offset: string
|
|
11
20
|
/** e.g. -360 */
|
|
12
21
|
offsetMinutes: number
|
|
@@ -18,6 +27,8 @@ export interface TimeZone {
|
|
|
18
27
|
timeZoneName: string
|
|
19
28
|
}
|
|
20
29
|
|
|
30
|
+
const thisYear = new Date().getFullYear()
|
|
31
|
+
|
|
21
32
|
let cachedTimeZones: TimeZone[] | null = null
|
|
22
33
|
|
|
23
34
|
/**
|
|
@@ -27,6 +38,94 @@ export function clearTimeZoneCache(): void {
|
|
|
27
38
|
cachedTimeZones = null
|
|
28
39
|
}
|
|
29
40
|
|
|
41
|
+
/**
|
|
42
|
+
* Given a date that includes a time offset, find a matching IANA time zone name.
|
|
43
|
+
* More of our users are in places that observe DST, so we bias towards choosing those: Chicago rather than Mexico City; London rather than Lagos.
|
|
44
|
+
* Where there are still multiple matches, we choose the most populous city.
|
|
45
|
+
* @param dateTime - e.g. '2025-01-01T00:00:00.000-06:00'
|
|
46
|
+
*/
|
|
47
|
+
export function getBestGuessTimeZoneCity(dateTimeISO: string): string | undefined {
|
|
48
|
+
|
|
49
|
+
if (!dateTimeISO) {
|
|
50
|
+
return undefined
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
const date = new Date(dateTimeISO)
|
|
54
|
+
if (isNaN(date.getTime())) {
|
|
55
|
+
return undefined
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
let offsetMinutes: number
|
|
59
|
+
const z = /Z$/i.test(dateTimeISO)
|
|
60
|
+
if (z) {
|
|
61
|
+
offsetMinutes = 0
|
|
62
|
+
} else {
|
|
63
|
+
const offsetMatch = dateTimeISO.match(/([+-])(\d{2}):?(\d{2})$/)
|
|
64
|
+
if (!offsetMatch) {
|
|
65
|
+
offsetMinutes = 0
|
|
66
|
+
} else {
|
|
67
|
+
offsetMinutes = parseInt(offsetMatch[2], 10) * 60 + parseInt(offsetMatch[3], 10)
|
|
68
|
+
if (offsetMatch[1] === '-') {
|
|
69
|
+
offsetMinutes = -offsetMinutes
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
// Find all time zones that match the given date.
|
|
75
|
+
const candidates: TimeZone[] = []
|
|
76
|
+
for (const timeZone of getTimeZones()) {
|
|
77
|
+
|
|
78
|
+
// For each time zone, format the given datetime to its time zone and see if the offset matches.
|
|
79
|
+
// This takes DST into account, as defined for that instant at that location.
|
|
80
|
+
const dtf = new Intl.DateTimeFormat('en', {
|
|
81
|
+
timeZone: timeZone.timeZoneName,
|
|
82
|
+
timeZoneName: 'shortOffset' as any, // TypeScript doesn't know that 'shortOffset' is safe: https://caniuse.com/?search=shortOffset
|
|
83
|
+
hour: '2-digit', // forces a time - needed for timeZoneName to appear in parts
|
|
84
|
+
})
|
|
85
|
+
|
|
86
|
+
const parts = dtf.formatToParts(new Date(dateTimeISO))
|
|
87
|
+
const tzName = parts.find(p => p.type === 'timeZoneName')?.value
|
|
88
|
+
if (!tzName) {
|
|
89
|
+
continue
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
let tzOffsetMinutes = 0
|
|
93
|
+
if (tzName !== 'UTC' && tzName !== 'GMT') {
|
|
94
|
+
const m = tzName.match(/^GMT([+-])(\d{1,2})(?::?(\d{2}))?$/)
|
|
95
|
+
if (!m) {
|
|
96
|
+
continue
|
|
97
|
+
}
|
|
98
|
+
const h = parseInt(m[2], 10)
|
|
99
|
+
const min = m[3] ? parseInt(m[3], 10) : 0
|
|
100
|
+
tzOffsetMinutes = h * 60 + min
|
|
101
|
+
if (m[1] === '-') {
|
|
102
|
+
tzOffsetMinutes = -tzOffsetMinutes
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
if (tzOffsetMinutes === offsetMinutes) {
|
|
106
|
+
candidates.push(timeZone)
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
if (candidates.length === 0) {
|
|
111
|
+
return undefined
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
// More of our users are in places that observe DST, so we should prefer those. Choose Chicago over Mexico City; London over Lagos.
|
|
115
|
+
// Otherwise, choose the most populous city.
|
|
116
|
+
candidates.sort((a, b) => {
|
|
117
|
+
if (a.observesDST && !b.observesDST) {
|
|
118
|
+
return -1
|
|
119
|
+
}
|
|
120
|
+
if (!a.observesDST && b.observesDST) {
|
|
121
|
+
return 1
|
|
122
|
+
}
|
|
123
|
+
return b.leadCityPopulation - a.leadCityPopulation
|
|
124
|
+
})
|
|
125
|
+
|
|
126
|
+
return candidates[0].timeZoneName
|
|
127
|
+
}
|
|
128
|
+
|
|
30
129
|
/**
|
|
31
130
|
* Finds the time zone that matches the time zone and DST signature of the given time zone name.
|
|
32
131
|
* @param timeZoneName - e.g. 'America/Chicago'
|
|
@@ -50,7 +149,7 @@ export function getTimeZone(timeZoneName: string | null | undefined): TimeZone |
|
|
|
50
149
|
const samples = getDateSamples()
|
|
51
150
|
const dstSignature = getDSTSignature(timeZoneName, samples)
|
|
52
151
|
|
|
53
|
-
return timeZones.find(tz => tz.dstSignature === dstSignature)
|
|
152
|
+
return timeZones.find(tz => tz.dstSignature === dstSignature.signature)
|
|
54
153
|
}
|
|
55
154
|
|
|
56
155
|
/**
|
|
@@ -70,6 +169,8 @@ export function getTimeZones(): TimeZone[] {
|
|
|
70
169
|
|
|
71
170
|
// As of 2025, this reduces the number of time zones to a more manageable 67. This exact number may change in
|
|
72
171
|
// future as browsers are updated with changes to time zones and DST rules.
|
|
172
|
+
// For example, Cairo currently has its own entry, but we expect it to be merged with Eastern Europe in 2028,
|
|
173
|
+
// when it will have been five years since they adopted the same DST rules.
|
|
73
174
|
|
|
74
175
|
|
|
75
176
|
if (cachedTimeZones) {
|
|
@@ -88,6 +189,7 @@ export function getTimeZones(): TimeZone[] {
|
|
|
88
189
|
timeZoneName: string // 'America/Chicago'
|
|
89
190
|
}[]
|
|
90
191
|
dstSignature: string
|
|
192
|
+
observesDST: boolean
|
|
91
193
|
offset: string
|
|
92
194
|
offsetMinutes: number
|
|
93
195
|
}
|
|
@@ -107,13 +209,13 @@ export function getTimeZones(): TimeZone[] {
|
|
|
107
209
|
const groups = new Map<string, TimeZoneCityGroup>()
|
|
108
210
|
for (const timezone of timezonesWithDSTSignatures) {
|
|
109
211
|
const cityName = timezone.mainCities[0] ?? timezone.countryName
|
|
110
|
-
const group = groups.get(timezone.dstSignature)
|
|
212
|
+
const group = groups.get(timezone.dstSignature.signature)
|
|
111
213
|
const population = cityPopulation.get(`${cityName}, ${timezone.countryName}`) ?? 0
|
|
112
214
|
if (!group) {
|
|
113
215
|
const offsetHour = Math.floor(Math.abs(timezone.rawOffsetInMinutes) / 60)
|
|
114
216
|
const offsetMinute = Math.abs(timezone.rawOffsetInMinutes) % 60
|
|
115
217
|
const offset = `${timezone.rawOffsetInMinutes < 0 ? '-' : '+'}${offsetHour.toString().padStart(2, '0')}:${offsetMinute.toString().padStart(2, '0')}`
|
|
116
|
-
groups.set(timezone.dstSignature, {
|
|
218
|
+
groups.set(timezone.dstSignature.signature, {
|
|
117
219
|
cities: [{
|
|
118
220
|
city: cityName,
|
|
119
221
|
country: timezone.countryName,
|
|
@@ -122,7 +224,8 @@ export function getTimeZones(): TimeZone[] {
|
|
|
122
224
|
timeZoneAlternativeName: timezone.alternativeName,
|
|
123
225
|
timeZoneName: timezone.name,
|
|
124
226
|
}],
|
|
125
|
-
dstSignature: timezone.dstSignature,
|
|
227
|
+
dstSignature: timezone.dstSignature.signature,
|
|
228
|
+
observesDST: timezone.dstSignature.observesDST,
|
|
126
229
|
offset,
|
|
127
230
|
offsetMinutes: timezone.rawOffsetInMinutes,
|
|
128
231
|
})
|
|
@@ -150,6 +253,8 @@ export function getTimeZones(): TimeZone[] {
|
|
|
150
253
|
.map(group => ({
|
|
151
254
|
cityNames: getCityList(group.cities),
|
|
152
255
|
dstSignature: group.dstSignature,
|
|
256
|
+
leadCityPopulation: group.cities[0].population,
|
|
257
|
+
observesDST: group.observesDST,
|
|
153
258
|
offset: group.offset,
|
|
154
259
|
offsetMinutes: group.offsetMinutes,
|
|
155
260
|
timeZoneAbbreviation: group.cities[0].timeZoneAbbreviation,
|
|
@@ -175,10 +280,11 @@ function getCityList(cities: { city: string, country: string }[]) {
|
|
|
175
280
|
return result
|
|
176
281
|
}
|
|
177
282
|
|
|
178
|
-
// To detect places with the same DST rules, make a signature for each timezone: a list of offset minutes at each sample time
|
|
179
|
-
function getDSTSignature(timezoneName: string, sampleTimes: Date[]) {
|
|
283
|
+
// To detect places with the same DST rules, make a signature for each timezone: a list of offset minutes at each sample time.
|
|
284
|
+
function getDSTSignature(timezoneName: string, sampleTimes: Date[]): DSTSignature {
|
|
180
285
|
|
|
181
286
|
try {
|
|
287
|
+
let observesDST = false
|
|
182
288
|
const formatter = new Intl.DateTimeFormat('en', {
|
|
183
289
|
timeZone: timezoneName,
|
|
184
290
|
timeZoneName: 'shortOffset' as any, // TypeScript doesn't know that 'shortOffset' is safe: https://caniuse.com/?search=shortOffset
|
|
@@ -187,18 +293,34 @@ function getDSTSignature(timezoneName: string, sampleTimes: Date[]) {
|
|
|
187
293
|
})
|
|
188
294
|
|
|
189
295
|
const sig: string[] = []
|
|
296
|
+
let prevOffset: string | undefined
|
|
190
297
|
for (const sample of sampleTimes) {
|
|
191
298
|
const parts = formatter.formatToParts(sample)
|
|
192
|
-
const
|
|
193
|
-
sig.push(
|
|
299
|
+
const offset = parts.find(p => p.type === 'timeZoneName')?.value ?? ''
|
|
300
|
+
sig.push(offset)
|
|
301
|
+
// Only say "it observes DST" if it does so now or in the future. (Mexico stopped in 2022, for example.)
|
|
302
|
+
if (prevOffset && prevOffset !== offset && sample.getFullYear() >= thisYear) {
|
|
303
|
+
observesDST = true
|
|
304
|
+
}
|
|
305
|
+
prevOffset = offset
|
|
194
306
|
}
|
|
195
307
|
|
|
196
|
-
return
|
|
308
|
+
return {
|
|
309
|
+
observesDST,
|
|
310
|
+
signature: sig.join(','),
|
|
311
|
+
}
|
|
197
312
|
} catch (error) {
|
|
198
|
-
return
|
|
313
|
+
return {
|
|
314
|
+
observesDST: false,
|
|
315
|
+
signature: '',
|
|
316
|
+
}
|
|
199
317
|
}
|
|
200
318
|
}
|
|
201
319
|
|
|
320
|
+
// These are the dates we sample to detect places with the same DST rules. This is imperfect: some places might switch DST on
|
|
321
|
+
// slightly different dates. A more accurate signature would do a binary chop to identify the exact date of each switch. That
|
|
322
|
+
// would be too slow, so we'll go with this for now. As of 2025, the only misidentified time zone is Asia/Hebron, which
|
|
323
|
+
// misaligns with Europe/Athens for two weeks in Spring and one day in Autumn. I'm happy to call this good enough.
|
|
202
324
|
function getDateSamples(): Date[] {
|
|
203
325
|
|
|
204
326
|
const now = new Date()
|