@internationalized/date 3.0.0-alpha.0 → 3.0.0-alpha.1
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/package.json +4 -3
- package/src/CalendarDate.ts +302 -0
- package/src/DateFormatter.ts +190 -0
- package/src/calendars/BuddhistCalendar.ts +45 -0
- package/src/calendars/EthiopicCalendar.ts +169 -0
- package/src/calendars/GregorianCalendar.ts +114 -0
- package/src/calendars/HebrewCalendar.ts +196 -0
- package/src/calendars/IndianCalendar.ts +120 -0
- package/src/calendars/IslamicCalendar.ts +203 -0
- package/src/calendars/JapaneseCalendar.ts +160 -0
- package/src/calendars/PersianCalendar.ts +92 -0
- package/src/calendars/TaiwanCalendar.ts +70 -0
- package/src/conversion.ts +274 -0
- package/src/createCalendar.ts +54 -0
- package/src/index.ts +28 -0
- package/src/manipulation.ts +445 -0
- package/src/queries.ts +217 -0
- package/src/string.ts +180 -0
- package/src/types.ts +97 -0
- package/src/utils.ts +37 -0
- package/src/weekStartData.ts +108 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@internationalized/date",
|
|
3
|
-
"version": "3.0.0-alpha.
|
|
3
|
+
"version": "3.0.0-alpha.1",
|
|
4
4
|
"description": "Internationalized calendar and date manipulation utilities",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"main": "dist/main.js",
|
|
@@ -8,7 +8,8 @@
|
|
|
8
8
|
"types": "dist/types.d.ts",
|
|
9
9
|
"source": "src/index.ts",
|
|
10
10
|
"files": [
|
|
11
|
-
"dist"
|
|
11
|
+
"dist",
|
|
12
|
+
"src"
|
|
12
13
|
],
|
|
13
14
|
"sideEffects": false,
|
|
14
15
|
"repository": {
|
|
@@ -21,5 +22,5 @@
|
|
|
21
22
|
"publishConfig": {
|
|
22
23
|
"access": "public"
|
|
23
24
|
},
|
|
24
|
-
"gitHead": "
|
|
25
|
+
"gitHead": "16fc29ac722793ffaea083f13760d82ab341cc8d"
|
|
25
26
|
}
|
|
@@ -0,0 +1,302 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright 2020 Adobe. All rights reserved.
|
|
3
|
+
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
* you may not use this file except in compliance with the License. You may obtain a copy
|
|
5
|
+
* of the License at http://www.apache.org/licenses/LICENSE-2.0
|
|
6
|
+
*
|
|
7
|
+
* Unless required by applicable law or agreed to in writing, software distributed under
|
|
8
|
+
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
|
|
9
|
+
* OF ANY KIND, either express or implied. See the License for the specific language
|
|
10
|
+
* governing permissions and limitations under the License.
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
import {add, addTime, addZoned, constrain, constrainTime, cycleDate, cycleTime, cycleZoned, set, setTime, setZoned, subtract, subtractTime, subtractZoned} from './manipulation';
|
|
14
|
+
import {AnyCalendarDate, AnyTime, Calendar, CycleOptions, CycleTimeOptions, DateField, DateFields, Disambiguation, Duration, TimeField, TimeFields} from './types';
|
|
15
|
+
import {compareDate, compareTime} from './queries';
|
|
16
|
+
import {dateTimeToString, dateToString, timeToString, zonedDateTimeToString} from './string';
|
|
17
|
+
import {GregorianCalendar} from './calendars/GregorianCalendar';
|
|
18
|
+
import {toCalendarDateTime, toDate, toZoned, zonedToDate} from './conversion';
|
|
19
|
+
|
|
20
|
+
function shiftArgs(args: any[]) {
|
|
21
|
+
let calendar: Calendar = typeof args[0] === 'object'
|
|
22
|
+
? args.shift()
|
|
23
|
+
: new GregorianCalendar();
|
|
24
|
+
|
|
25
|
+
let era: string;
|
|
26
|
+
if (typeof args[0] === 'string') {
|
|
27
|
+
era = args.shift();
|
|
28
|
+
} else {
|
|
29
|
+
let eras = calendar.getEras();
|
|
30
|
+
era = eras[eras.length - 1];
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
let year = args.shift();
|
|
34
|
+
let month = args.shift();
|
|
35
|
+
let day = args.shift();
|
|
36
|
+
|
|
37
|
+
return [calendar, era, year, month, day];
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export class CalendarDate {
|
|
41
|
+
// This prevents TypeScript from allowing other types with the same fields to match.
|
|
42
|
+
// i.e. a ZonedDateTime should not be be passable to a parameter that expects CalendarDate.
|
|
43
|
+
// If that behavior is desired, use the AnyCalendarDate interface instead.
|
|
44
|
+
#type;
|
|
45
|
+
public readonly calendar: Calendar;
|
|
46
|
+
public readonly era: string;
|
|
47
|
+
public readonly year: number;
|
|
48
|
+
public readonly month: number;
|
|
49
|
+
public readonly day: number;
|
|
50
|
+
|
|
51
|
+
constructor(year: number, month: number, day: number);
|
|
52
|
+
constructor(calendar: Calendar, year: number, month: number, day: number);
|
|
53
|
+
constructor(calendar: Calendar, era: string, year: number, month: number, day: number);
|
|
54
|
+
constructor(...args: any[]) {
|
|
55
|
+
let [calendar, era, year, month, day] = shiftArgs(args);
|
|
56
|
+
this.calendar = calendar;
|
|
57
|
+
this.era = era;
|
|
58
|
+
this.year = year;
|
|
59
|
+
this.month = month;
|
|
60
|
+
this.day = day;
|
|
61
|
+
|
|
62
|
+
constrain(this);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
copy(): CalendarDate {
|
|
66
|
+
if (this.era) {
|
|
67
|
+
return new CalendarDate(this.calendar, this.era, this.year, this.month, this.day);
|
|
68
|
+
} else {
|
|
69
|
+
return new CalendarDate(this.calendar, this.year, this.month, this.day);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
add(duration: Duration) {
|
|
74
|
+
return add(this, duration);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
subtract(duration: Duration) {
|
|
78
|
+
return subtract(this, duration);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
set(fields: DateFields) {
|
|
82
|
+
return set(this, fields);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
cycle(field: DateField, amount: number, options?: CycleOptions) {
|
|
86
|
+
return cycleDate(this, field, amount, options);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
toDate(timeZone: string) {
|
|
90
|
+
return toDate(this, timeZone);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
toString() {
|
|
94
|
+
return dateToString(this);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
compare(b: AnyCalendarDate) {
|
|
98
|
+
return compareDate(this, b);
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
export class Time {
|
|
103
|
+
// This prevents TypeScript from allowing other types with the same fields to match.
|
|
104
|
+
#type;
|
|
105
|
+
|
|
106
|
+
constructor(
|
|
107
|
+
public readonly hour: number = 0,
|
|
108
|
+
public readonly minute: number = 0,
|
|
109
|
+
public readonly second: number = 0,
|
|
110
|
+
public readonly millisecond: number = 0
|
|
111
|
+
) {
|
|
112
|
+
constrainTime(this);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
copy(): Time {
|
|
116
|
+
return new Time(this.hour, this.minute, this.second, this.millisecond);
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
add(duration: Duration) {
|
|
120
|
+
return addTime(this, duration);
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
subtract(duration: Duration) {
|
|
124
|
+
return subtractTime(this, duration);
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
set(fields: TimeFields) {
|
|
128
|
+
return setTime(this, fields);
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
cycle(field: TimeField, amount: number, options?: CycleTimeOptions) {
|
|
132
|
+
return cycleTime(this, field, amount, options);
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
toString() {
|
|
136
|
+
return timeToString(this);
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
compare(b: AnyTime) {
|
|
140
|
+
return compareTime(this, b);
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
export class CalendarDateTime {
|
|
145
|
+
// This prevents TypeScript from allowing other types with the same fields to match.
|
|
146
|
+
#type;
|
|
147
|
+
public readonly calendar: Calendar;
|
|
148
|
+
public readonly era: string;
|
|
149
|
+
public readonly year: number;
|
|
150
|
+
public readonly month: number;
|
|
151
|
+
public readonly day: number;
|
|
152
|
+
public readonly hour: number;
|
|
153
|
+
public readonly minute: number;
|
|
154
|
+
public readonly second: number;
|
|
155
|
+
public readonly millisecond: number;
|
|
156
|
+
|
|
157
|
+
constructor(year: number, month: number, day: number, hour?: number, minute?: number, second?: number, millisecond?: number);
|
|
158
|
+
constructor(calendar: Calendar, year: number, month: number, day: number, hour?: number, minute?: number, second?: number, millisecond?: number);
|
|
159
|
+
constructor(calendar: Calendar, era: string, year: number, month: number, day: number, hour?: number, minute?: number, second?: number, millisecond?: number);
|
|
160
|
+
constructor(...args: any[]) {
|
|
161
|
+
let [calendar, era, year, month, day] = shiftArgs(args);
|
|
162
|
+
this.calendar = calendar;
|
|
163
|
+
this.era = era;
|
|
164
|
+
this.year = year;
|
|
165
|
+
this.month = month;
|
|
166
|
+
this.day = day;
|
|
167
|
+
this.hour = args.shift() || 0;
|
|
168
|
+
this.minute = args.shift() || 0;
|
|
169
|
+
this.second = args.shift() || 0;
|
|
170
|
+
this.millisecond = args.shift() || 0;
|
|
171
|
+
|
|
172
|
+
constrain(this);
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
copy(): CalendarDateTime {
|
|
176
|
+
if (this.era) {
|
|
177
|
+
return new CalendarDateTime(this.calendar, this.era, this.year, this.month, this.day, this.hour, this.minute, this.second, this.millisecond);
|
|
178
|
+
} else {
|
|
179
|
+
return new CalendarDateTime(this.calendar, this.year, this.month, this.day, this.hour, this.minute, this.second, this.millisecond);
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
add(duration: Duration) {
|
|
184
|
+
return add(this, duration);
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
subtract(duration: Duration) {
|
|
188
|
+
return subtract(this, duration);
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
set(fields: DateFields & TimeFields) {
|
|
192
|
+
return set(setTime(this, fields), fields);
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
cycle(field: DateField | TimeField, amount: number, options?: CycleTimeOptions) {
|
|
196
|
+
switch (field) {
|
|
197
|
+
case 'era':
|
|
198
|
+
case 'year':
|
|
199
|
+
case 'month':
|
|
200
|
+
case 'day':
|
|
201
|
+
return cycleDate(this, field, amount, options);
|
|
202
|
+
default:
|
|
203
|
+
return cycleTime(this, field, amount, options);
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
toDate(timeZone: string) {
|
|
208
|
+
return toDate(this, timeZone);
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
toString() {
|
|
212
|
+
return dateTimeToString(this);
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
compare(b: CalendarDate | CalendarDateTime | ZonedDateTime) {
|
|
216
|
+
let res = compareDate(this, b);
|
|
217
|
+
if (res === 0) {
|
|
218
|
+
return compareTime(this, toCalendarDateTime(b));
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
return res;
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
export class ZonedDateTime {
|
|
226
|
+
// This prevents TypeScript from allowing other types with the same fields to match.
|
|
227
|
+
#type;
|
|
228
|
+
public readonly calendar: Calendar;
|
|
229
|
+
public readonly era: string;
|
|
230
|
+
public readonly year: number;
|
|
231
|
+
public readonly month: number;
|
|
232
|
+
public readonly day: number;
|
|
233
|
+
public readonly hour: number;
|
|
234
|
+
public readonly minute: number;
|
|
235
|
+
public readonly second: number;
|
|
236
|
+
public readonly millisecond: number;
|
|
237
|
+
public readonly timeZone: string;
|
|
238
|
+
public readonly offset: number;
|
|
239
|
+
|
|
240
|
+
constructor(year: number, month: number, day: number, timeZone: string, offset: number, hour?: number, minute?: number, second?: number, millisecond?: number);
|
|
241
|
+
constructor(calendar: Calendar, year: number, month: number, day: number, timeZone: string, offset: number, hour?: number, minute?: number, second?: number, millisecond?: number);
|
|
242
|
+
constructor(calendar: Calendar, era: string, year: number, month: number, day: number, timeZone: string, offset: number, hour?: number, minute?: number, second?: number, millisecond?: number);
|
|
243
|
+
constructor(...args: any[]) {
|
|
244
|
+
let [calendar, era, year, month, day] = shiftArgs(args);
|
|
245
|
+
let timeZone = args.shift();
|
|
246
|
+
let offset = args.shift();
|
|
247
|
+
this.calendar = calendar;
|
|
248
|
+
this.era = era;
|
|
249
|
+
this.year = year;
|
|
250
|
+
this.month = month;
|
|
251
|
+
this.day = day;
|
|
252
|
+
this.timeZone = timeZone;
|
|
253
|
+
this.offset = offset;
|
|
254
|
+
this.hour = args.shift() || 0;
|
|
255
|
+
this.minute = args.shift() || 0;
|
|
256
|
+
this.second = args.shift() || 0;
|
|
257
|
+
this.millisecond = args.shift() || 0;
|
|
258
|
+
|
|
259
|
+
constrain(this);
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
copy(): ZonedDateTime {
|
|
263
|
+
if (this.era) {
|
|
264
|
+
return new ZonedDateTime(this.calendar, this.era, this.year, this.month, this.day, this.timeZone, this.offset, this.hour, this.minute, this.second, this.millisecond);
|
|
265
|
+
} else {
|
|
266
|
+
return new ZonedDateTime(this.calendar, this.year, this.month, this.day, this.timeZone, this.offset, this.hour, this.minute, this.second, this.millisecond);
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
add(duration: Duration) {
|
|
271
|
+
return addZoned(this, duration);
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
subtract(duration: Duration) {
|
|
275
|
+
return subtractZoned(this, duration);
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
set(fields: DateFields & TimeFields, disambiguation?: Disambiguation) {
|
|
279
|
+
return setZoned(this, fields, disambiguation);
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
cycle(field: DateField | TimeField, amount: number, options?: CycleTimeOptions) {
|
|
283
|
+
return cycleZoned(this, field, amount, options);
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
toDate() {
|
|
287
|
+
return zonedToDate(this);
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
toString() {
|
|
291
|
+
return zonedDateTimeToString(this);
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
toAbsoluteString() {
|
|
295
|
+
return this.toDate().toISOString();
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
compare(b: CalendarDate | CalendarDateTime | ZonedDateTime) {
|
|
299
|
+
// TODO: Is this a bad idea??
|
|
300
|
+
return this.toDate().getTime() - toZoned(b, this.timeZone).toDate().getTime();
|
|
301
|
+
}
|
|
302
|
+
}
|
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright 2020 Adobe. All rights reserved.
|
|
3
|
+
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
* you may not use this file except in compliance with the License. You may obtain a copy
|
|
5
|
+
* of the License at http://www.apache.org/licenses/LICENSE-2.0
|
|
6
|
+
*
|
|
7
|
+
* Unless required by applicable law or agreed to in writing, software distributed under
|
|
8
|
+
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
|
|
9
|
+
* OF ANY KIND, either express or implied. See the License for the specific language
|
|
10
|
+
* governing permissions and limitations under the License.
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
let formatterCache = new Map<string, Intl.DateTimeFormat>();
|
|
14
|
+
|
|
15
|
+
interface ResolvedDateTimeFormatOptions extends Intl.ResolvedDateTimeFormatOptions {
|
|
16
|
+
hourCycle?: Intl.DateTimeFormatOptions['hourCycle']
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
interface DateRangeFormatPart extends Intl.DateTimeFormatPart {
|
|
20
|
+
source: 'startRange' | 'endRange' | 'shared'
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export class DateFormatter implements Intl.DateTimeFormat {
|
|
24
|
+
private formatter: Intl.DateTimeFormat;
|
|
25
|
+
private options: Intl.DateTimeFormatOptions;
|
|
26
|
+
private resolvedHourCycle: Intl.DateTimeFormatOptions['hourCycle'];
|
|
27
|
+
|
|
28
|
+
constructor(locale: string, options: Intl.DateTimeFormatOptions = {}) {
|
|
29
|
+
this.formatter = getCachedDateFormatter(locale, options);
|
|
30
|
+
this.options = options;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
format(value: Date): string {
|
|
34
|
+
return this.formatter.format(value);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
formatToParts(value: Date): Intl.DateTimeFormatPart[] {
|
|
38
|
+
return this.formatter.formatToParts(value);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
formatRange(start: Date, end: Date): string {
|
|
42
|
+
// @ts-ignore
|
|
43
|
+
if (typeof this.formatter.formatRange === 'function') {
|
|
44
|
+
// @ts-ignore
|
|
45
|
+
return this.formatter.formatRange(start, end);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
if (end < start) {
|
|
49
|
+
throw new RangeError('End date must be >= start date');
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
// Very basic fallback for old browsers.
|
|
53
|
+
return `${this.formatter.format(start)} – ${this.formatter.format(end)}`;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
formatRangeToParts(start: Date, end: Date): DateRangeFormatPart[] {
|
|
57
|
+
// @ts-ignore
|
|
58
|
+
if (typeof this.formatter.formatRangeToParts === 'function') {
|
|
59
|
+
// @ts-ignore
|
|
60
|
+
return this.formatter.formatRangeToParts(start, end);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
if (end < start) {
|
|
64
|
+
throw new RangeError('End date must be >= start date');
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
let startParts = this.formatter.formatToParts(start);
|
|
68
|
+
let endParts = this.formatter.formatToParts(end);
|
|
69
|
+
return [
|
|
70
|
+
...startParts.map(p => ({...p, source: 'startRange'} as DateRangeFormatPart)),
|
|
71
|
+
{type: 'literal', value: ' – ', source: 'shared'},
|
|
72
|
+
...endParts.map(p => ({...p, source: 'endRange'} as DateRangeFormatPart))
|
|
73
|
+
];
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
resolvedOptions(): ResolvedDateTimeFormatOptions {
|
|
77
|
+
let resolvedOptions = this.formatter.resolvedOptions() as ResolvedDateTimeFormatOptions;
|
|
78
|
+
if (hasBuggyResolvedHourCycle()) {
|
|
79
|
+
if (!this.resolvedHourCycle) {
|
|
80
|
+
this.resolvedHourCycle = getResolvedHourCycle(resolvedOptions.locale, this.options);
|
|
81
|
+
}
|
|
82
|
+
resolvedOptions.hourCycle = this.resolvedHourCycle;
|
|
83
|
+
resolvedOptions.hour12 = this.resolvedHourCycle === 'h11' || this.resolvedHourCycle === 'h12';
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
return resolvedOptions;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
// There are multiple bugs involving the hour12 and hourCycle options in various browser engines.
|
|
91
|
+
// - Chrome [1] (and the ECMA 402 spec [2]) resolve hour12: false in English and other locales to h24 (24:00 - 23:59)
|
|
92
|
+
// rather than h23 (00:00 - 23:59). Same can happen with hour12: true in French, which Chrome resolves to h11 (00:00 - 11:59)
|
|
93
|
+
// rather than h12 (12:00 - 11:59).
|
|
94
|
+
// - WebKit returns an incorrect hourCycle resolved option in the French locale due to incorrect parsing of 'h' literal
|
|
95
|
+
// in the resolved pattern. It also formats incorrectly when specifying the hourCycle option for the same reason. [3]
|
|
96
|
+
// [1] https://bugs.chromium.org/p/chromium/issues/detail?id=1045791
|
|
97
|
+
// [2] https://github.com/tc39/ecma402/issues/402
|
|
98
|
+
// [3] https://bugs.webkit.org/show_bug.cgi?id=229313
|
|
99
|
+
|
|
100
|
+
// https://github.com/unicode-org/cldr/blob/018b55eff7ceb389c7e3fc44e2f657eae3b10b38/common/supplemental/supplementalData.xml#L4774-L4802
|
|
101
|
+
const hour12Preferences = {
|
|
102
|
+
true: {
|
|
103
|
+
// Only Japanese uses the h11 style for 12 hour time. All others use h12.
|
|
104
|
+
ja: 'h11'
|
|
105
|
+
},
|
|
106
|
+
false: {
|
|
107
|
+
// All locales use h23 for 24 hour time. None use h24.
|
|
108
|
+
}
|
|
109
|
+
};
|
|
110
|
+
|
|
111
|
+
function getCachedDateFormatter(locale: string, options: Intl.DateTimeFormatOptions = {}): Intl.DateTimeFormat {
|
|
112
|
+
// Work around buggy hour12 behavior in Chrome / ECMA 402 spec by using hourCycle instead.
|
|
113
|
+
// Only apply the workaround if the issue is detected, because the hourCycle option is buggy in Safari.
|
|
114
|
+
if (typeof options.hour12 === 'boolean' && hasBuggyHour12Behavior()) {
|
|
115
|
+
options = {...options};
|
|
116
|
+
let pref = hour12Preferences[String(options.hour12)][locale.split('-')[0]];
|
|
117
|
+
let defaultHourCycle = options.hour12 ? 'h12' : 'h23';
|
|
118
|
+
options.hourCycle = pref ?? defaultHourCycle;
|
|
119
|
+
delete options.hour12;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
let cacheKey = locale + (options ? Object.entries(options).sort((a, b) => a[0] < b[0] ? -1 : 1).join() : '');
|
|
123
|
+
if (formatterCache.has(cacheKey)) {
|
|
124
|
+
return formatterCache.get(cacheKey);
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
let numberFormatter = new Intl.DateTimeFormat(locale, options);
|
|
128
|
+
formatterCache.set(cacheKey, numberFormatter);
|
|
129
|
+
return numberFormatter;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
let _hasBuggyHour12Behavior: boolean = null;
|
|
133
|
+
function hasBuggyHour12Behavior() {
|
|
134
|
+
if (_hasBuggyHour12Behavior == null) {
|
|
135
|
+
_hasBuggyHour12Behavior = new Intl.DateTimeFormat('en-US', {
|
|
136
|
+
hour: 'numeric',
|
|
137
|
+
hour12: false
|
|
138
|
+
}).format(new Date(2020, 2, 3, 0)) === '24';
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
return _hasBuggyHour12Behavior;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
let _hasBuggyResolvedHourCycle: boolean = null;
|
|
145
|
+
function hasBuggyResolvedHourCycle() {
|
|
146
|
+
if (_hasBuggyResolvedHourCycle == null) {
|
|
147
|
+
_hasBuggyResolvedHourCycle = (new Intl.DateTimeFormat('fr', {
|
|
148
|
+
hour: 'numeric',
|
|
149
|
+
hour12: false
|
|
150
|
+
}).resolvedOptions() as ResolvedDateTimeFormatOptions).hourCycle === 'h12';
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
return _hasBuggyResolvedHourCycle;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
function getResolvedHourCycle(locale: string, options: Intl.DateTimeFormatOptions) {
|
|
157
|
+
if (!options.timeStyle && !options.hour) {
|
|
158
|
+
return undefined;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
// Work around buggy results in resolved hourCycle and hour12 options in WebKit.
|
|
162
|
+
// Format the minimum possible hour and maximum possible hour in a day and parse the results.
|
|
163
|
+
locale = locale.replace(/(-u-)?-nu-[a-zA-Z0-9]+/, '');
|
|
164
|
+
locale += (locale.includes('-u-') ? '' : '-u') + '-nu-latn';
|
|
165
|
+
let formatter = getCachedDateFormatter(locale, {
|
|
166
|
+
...options,
|
|
167
|
+
timeZone: undefined // use local timezone
|
|
168
|
+
});
|
|
169
|
+
|
|
170
|
+
let min = parseInt(formatter.formatToParts(new Date(2020, 2, 3, 0)).find(p => p.type === 'hour').value, 10);
|
|
171
|
+
let max = parseInt(formatter.formatToParts(new Date(2020, 2, 3, 23)).find(p => p.type === 'hour').value, 10);
|
|
172
|
+
|
|
173
|
+
if (min === 0 && max === 23) {
|
|
174
|
+
return 'h23';
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
if (min === 24 && max === 23) {
|
|
178
|
+
return 'h24';
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
if (min === 0 && max === 11) {
|
|
182
|
+
return 'h11';
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
if (min === 12 && max === 11) {
|
|
186
|
+
return 'h12';
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
throw new Error('Unexpected hour cycle result');
|
|
190
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright 2020 Adobe. All rights reserved.
|
|
3
|
+
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
* you may not use this file except in compliance with the License. You may obtain a copy
|
|
5
|
+
* of the License at http://www.apache.org/licenses/LICENSE-2.0
|
|
6
|
+
*
|
|
7
|
+
* Unless required by applicable law or agreed to in writing, software distributed under
|
|
8
|
+
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
|
|
9
|
+
* OF ANY KIND, either express or implied. See the License for the specific language
|
|
10
|
+
* governing permissions and limitations under the License.
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
// Portions of the code in this file are based on code from ICU.
|
|
14
|
+
// Original licensing can be found in the NOTICE file in the root directory of this source tree.
|
|
15
|
+
|
|
16
|
+
import {AnyCalendarDate} from '../types';
|
|
17
|
+
import {CalendarDate} from '../CalendarDate';
|
|
18
|
+
import {GregorianCalendar} from './GregorianCalendar';
|
|
19
|
+
import {Mutable} from '../utils';
|
|
20
|
+
|
|
21
|
+
const BUDDHIST_ERA_START = -543;
|
|
22
|
+
|
|
23
|
+
export class BuddhistCalendar extends GregorianCalendar {
|
|
24
|
+
identifier = 'buddhist';
|
|
25
|
+
|
|
26
|
+
fromJulianDay(jd: number): CalendarDate {
|
|
27
|
+
let date = super.fromJulianDay(jd) as Mutable<CalendarDate>;
|
|
28
|
+
date.year -= BUDDHIST_ERA_START;
|
|
29
|
+
return date as CalendarDate;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
toJulianDay(date: AnyCalendarDate) {
|
|
33
|
+
return super.toJulianDay(
|
|
34
|
+
new CalendarDate(
|
|
35
|
+
date.year + BUDDHIST_ERA_START,
|
|
36
|
+
date.month,
|
|
37
|
+
date.day
|
|
38
|
+
)
|
|
39
|
+
);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
getEras() {
|
|
43
|
+
return ['BE'];
|
|
44
|
+
}
|
|
45
|
+
}
|