@internationalized/date 3.6.0 → 3.8.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.
Files changed (64) hide show
  1. package/dist/BuddhistCalendar.main.js.map +1 -1
  2. package/dist/BuddhistCalendar.module.js.map +1 -1
  3. package/dist/CalendarDate.main.js.map +1 -1
  4. package/dist/CalendarDate.module.js.map +1 -1
  5. package/dist/EthiopicCalendar.main.js.map +1 -1
  6. package/dist/EthiopicCalendar.module.js.map +1 -1
  7. package/dist/GregorianCalendar.main.js.map +1 -1
  8. package/dist/GregorianCalendar.module.js.map +1 -1
  9. package/dist/HebrewCalendar.main.js.map +1 -1
  10. package/dist/HebrewCalendar.module.js.map +1 -1
  11. package/dist/IndianCalendar.main.js.map +1 -1
  12. package/dist/IndianCalendar.module.js.map +1 -1
  13. package/dist/IslamicCalendar.main.js.map +1 -1
  14. package/dist/IslamicCalendar.module.js.map +1 -1
  15. package/dist/JapaneseCalendar.main.js.map +1 -1
  16. package/dist/JapaneseCalendar.module.js.map +1 -1
  17. package/dist/PersianCalendar.main.js.map +1 -1
  18. package/dist/PersianCalendar.module.js.map +1 -1
  19. package/dist/TaiwanCalendar.main.js.map +1 -1
  20. package/dist/TaiwanCalendar.module.js.map +1 -1
  21. package/dist/conversion.main.js +1 -1
  22. package/dist/conversion.main.js.map +1 -1
  23. package/dist/conversion.mjs +2 -2
  24. package/dist/conversion.module.js +2 -2
  25. package/dist/conversion.module.js.map +1 -1
  26. package/dist/createCalendar.main.js.map +1 -1
  27. package/dist/createCalendar.module.js.map +1 -1
  28. package/dist/import.mjs +2 -2
  29. package/dist/main.js +1 -0
  30. package/dist/main.js.map +1 -1
  31. package/dist/manipulation.main.js.map +1 -1
  32. package/dist/manipulation.module.js.map +1 -1
  33. package/dist/module.js +2 -2
  34. package/dist/module.js.map +1 -1
  35. package/dist/queries.main.js +27 -15
  36. package/dist/queries.main.js.map +1 -1
  37. package/dist/queries.mjs +27 -16
  38. package/dist/queries.module.js +27 -16
  39. package/dist/queries.module.js.map +1 -1
  40. package/dist/string.main.js +9 -9
  41. package/dist/string.main.js.map +1 -1
  42. package/dist/string.mjs +9 -9
  43. package/dist/string.module.js +9 -9
  44. package/dist/string.module.js.map +1 -1
  45. package/dist/types.d.ts +42 -26
  46. package/dist/types.d.ts.map +1 -1
  47. package/package.json +2 -2
  48. package/src/CalendarDate.ts +14 -14
  49. package/src/calendars/BuddhistCalendar.ts +5 -5
  50. package/src/calendars/EthiopicCalendar.ts +10 -10
  51. package/src/calendars/GregorianCalendar.ts +4 -4
  52. package/src/calendars/HebrewCalendar.ts +5 -5
  53. package/src/calendars/IndianCalendar.ts +5 -5
  54. package/src/calendars/IslamicCalendar.ts +7 -7
  55. package/src/calendars/JapaneseCalendar.ts +6 -6
  56. package/src/calendars/PersianCalendar.ts +3 -3
  57. package/src/calendars/TaiwanCalendar.ts +5 -5
  58. package/src/conversion.ts +6 -6
  59. package/src/createCalendar.ts +2 -2
  60. package/src/index.ts +3 -1
  61. package/src/manipulation.ts +7 -7
  62. package/src/queries.ts +40 -26
  63. package/src/string.ts +9 -11
  64. package/src/types.ts +17 -2
@@ -13,7 +13,7 @@
13
13
  // Portions of the code in this file are based on code from ICU.
14
14
  // Original licensing can be found in the NOTICE file in the root directory of this source tree.
15
15
 
16
- import {AnyCalendarDate, Calendar} from '../types';
16
+ import {AnyCalendarDate, Calendar, CalendarIdentifier} from '../types';
17
17
  import {CalendarDate} from '../CalendarDate';
18
18
  import {mod, Mutable} from '../utils';
19
19
 
@@ -68,7 +68,7 @@ const daysInMonth = {
68
68
  * Years always contain 12 months, and 365 or 366 days depending on whether it is a leap year.
69
69
  */
70
70
  export class GregorianCalendar implements Calendar {
71
- identifier = 'gregory';
71
+ identifier: CalendarIdentifier = 'gregory';
72
72
 
73
73
  fromJulianDay(jd: number): CalendarDate {
74
74
  let jd0 = jd;
@@ -118,7 +118,7 @@ export class GregorianCalendar implements Calendar {
118
118
  return 9999;
119
119
  }
120
120
 
121
- getEras() {
121
+ getEras(): string[] {
122
122
  return ['BC', 'AD'];
123
123
  }
124
124
 
@@ -126,7 +126,7 @@ export class GregorianCalendar implements Calendar {
126
126
  return date.era === 'BC';
127
127
  }
128
128
 
129
- balanceDate(date: Mutable<AnyCalendarDate>) {
129
+ balanceDate(date: Mutable<AnyCalendarDate>): void {
130
130
  if (date.year <= 0) {
131
131
  date.era = date.era === 'BC' ? 'AD' : 'BC';
132
132
  date.year = 1 - date.year;
@@ -13,7 +13,7 @@
13
13
  // Portions of the code in this file are based on code from ICU.
14
14
  // Original licensing can be found in the NOTICE file in the root directory of this source tree.
15
15
 
16
- import {AnyCalendarDate, Calendar} from '../types';
16
+ import {AnyCalendarDate, Calendar, CalendarIdentifier} from '../types';
17
17
  import {CalendarDate} from '../CalendarDate';
18
18
  import {mod, Mutable} from '../utils';
19
19
 
@@ -128,7 +128,7 @@ function getDaysInMonth(year: number, month: number): number {
128
128
  * In leap years, an extra month is inserted at month 6.
129
129
  */
130
130
  export class HebrewCalendar implements Calendar {
131
- identifier = 'hebrew';
131
+ identifier: CalendarIdentifier = 'hebrew';
132
132
 
133
133
  fromJulianDay(jd: number): CalendarDate {
134
134
  let d = jd - HEBREW_EPOCH;
@@ -159,7 +159,7 @@ export class HebrewCalendar implements Calendar {
159
159
  return new CalendarDate(this, year, month, day);
160
160
  }
161
161
 
162
- toJulianDay(date: AnyCalendarDate) {
162
+ toJulianDay(date: AnyCalendarDate): number {
163
163
  let jd = startOfYear(date.year);
164
164
  for (let month = 1; month < date.month; month++) {
165
165
  jd += getDaysInMonth(date.year, month);
@@ -185,11 +185,11 @@ export class HebrewCalendar implements Calendar {
185
185
  return 9999;
186
186
  }
187
187
 
188
- getEras() {
188
+ getEras(): string[] {
189
189
  return ['AM'];
190
190
  }
191
191
 
192
- balanceYearMonth(date: Mutable<AnyCalendarDate>, previousDate: AnyCalendarDate) {
192
+ balanceYearMonth(date: Mutable<AnyCalendarDate>, previousDate: AnyCalendarDate): void {
193
193
  // Keep date in the same month when switching between leap years and non leap years
194
194
  if (previousDate.year !== date.year) {
195
195
  if (isLeapYear(previousDate.year) && !isLeapYear(date.year) && previousDate.month > 6) {
@@ -13,7 +13,7 @@
13
13
  // Portions of the code in this file are based on code from ICU.
14
14
  // Original licensing can be found in the NOTICE file in the root directory of this source tree.
15
15
 
16
- import {AnyCalendarDate} from '../types';
16
+ import {AnyCalendarDate, CalendarIdentifier} from '../types';
17
17
  import {CalendarDate} from '../CalendarDate';
18
18
  import {fromExtendedYear, GregorianCalendar, gregorianToJulianDay, isLeapYear} from './GregorianCalendar';
19
19
 
@@ -29,7 +29,7 @@ const INDIAN_YEAR_START = 80;
29
29
  * in each year, with either 30 or 31 days. Only one era identifier is supported: 'saka'.
30
30
  */
31
31
  export class IndianCalendar extends GregorianCalendar {
32
- identifier = 'indian';
32
+ identifier: CalendarIdentifier = 'indian';
33
33
 
34
34
  fromJulianDay(jd: number): CalendarDate {
35
35
  // Gregorian date for Julian day
@@ -75,7 +75,7 @@ export class IndianCalendar extends GregorianCalendar {
75
75
  return new CalendarDate(this, indianYear, indianMonth, indianDay);
76
76
  }
77
77
 
78
- toJulianDay(date: AnyCalendarDate) {
78
+ toJulianDay(date: AnyCalendarDate): number {
79
79
  let extendedYear = date.year + INDIAN_ERA_START;
80
80
  let [era, year] = fromExtendedYear(extendedYear);
81
81
 
@@ -121,9 +121,9 @@ export class IndianCalendar extends GregorianCalendar {
121
121
  return 9919;
122
122
  }
123
123
 
124
- getEras() {
124
+ getEras(): string[] {
125
125
  return ['saka'];
126
126
  }
127
127
 
128
- balanceDate() {}
128
+ balanceDate(): void {}
129
129
  }
@@ -13,7 +13,7 @@
13
13
  // Portions of the code in this file are based on code from ICU.
14
14
  // Original licensing can be found in the NOTICE file in the root directory of this source tree.
15
15
 
16
- import {AnyCalendarDate, Calendar} from '../types';
16
+ import {AnyCalendarDate, Calendar, CalendarIdentifier} from '../types';
17
17
  import {CalendarDate} from '../CalendarDate';
18
18
 
19
19
  const CIVIL_EPOC = 1948440; // CE 622 July 16 Friday (Julian calendar) / CE 622 July 19 (Gregorian calendar)
@@ -50,13 +50,13 @@ function isLeapYear(year: number): boolean {
50
50
  * Learn more about the available Islamic calendars [here](https://cldr.unicode.org/development/development-process/design-proposals/islamic-calendar-types).
51
51
  */
52
52
  export class IslamicCivilCalendar implements Calendar {
53
- identifier = 'islamic-civil';
53
+ identifier: CalendarIdentifier = 'islamic-civil';
54
54
 
55
55
  fromJulianDay(jd: number): CalendarDate {
56
56
  return julianDayToIslamic(this, CIVIL_EPOC, jd);
57
57
  }
58
58
 
59
- toJulianDay(date: AnyCalendarDate) {
59
+ toJulianDay(date: AnyCalendarDate): number {
60
60
  return islamicToJulianDay(CIVIL_EPOC, date.year, date.month, date.day);
61
61
  }
62
62
 
@@ -82,7 +82,7 @@ export class IslamicCivilCalendar implements Calendar {
82
82
  return 9665;
83
83
  }
84
84
 
85
- getEras() {
85
+ getEras(): string[] {
86
86
  return ['AH'];
87
87
  }
88
88
  }
@@ -95,13 +95,13 @@ export class IslamicCivilCalendar implements Calendar {
95
95
  * Learn more about the available Islamic calendars [here](https://cldr.unicode.org/development/development-process/design-proposals/islamic-calendar-types).
96
96
  */
97
97
  export class IslamicTabularCalendar extends IslamicCivilCalendar {
98
- identifier = 'islamic-tbla';
98
+ identifier: CalendarIdentifier = 'islamic-tbla';
99
99
 
100
100
  fromJulianDay(jd: number): CalendarDate {
101
101
  return julianDayToIslamic(this, ASTRONOMICAL_EPOC, jd);
102
102
  }
103
103
 
104
- toJulianDay(date: AnyCalendarDate) {
104
+ toJulianDay(date: AnyCalendarDate): number {
105
105
  return islamicToJulianDay(ASTRONOMICAL_EPOC, date.year, date.month, date.day);
106
106
  }
107
107
  }
@@ -145,7 +145,7 @@ function umalquraYearLength(year: number): number {
145
145
  * Learn more about the available Islamic calendars [here](https://cldr.unicode.org/development/development-process/design-proposals/islamic-calendar-types).
146
146
  */
147
147
  export class IslamicUmalquraCalendar extends IslamicCivilCalendar {
148
- identifier = 'islamic-umalqura';
148
+ identifier: CalendarIdentifier = 'islamic-umalqura';
149
149
 
150
150
  constructor() {
151
151
  super();
@@ -13,7 +13,7 @@
13
13
  // Portions of the code in this file are based on code from the TC39 Temporal proposal.
14
14
  // Original licensing can be found in the NOTICE file in the root directory of this source tree.
15
15
 
16
- import {AnyCalendarDate} from '../types';
16
+ import {AnyCalendarDate, CalendarIdentifier} from '../types';
17
17
  import {CalendarDate} from '../CalendarDate';
18
18
  import {GregorianCalendar} from './GregorianCalendar';
19
19
  import {Mutable} from '../utils';
@@ -70,7 +70,7 @@ function toGregorian(date: AnyCalendarDate) {
70
70
  * Note that eras before 1868 (Gregorian) are not currently supported by this implementation.
71
71
  */
72
72
  export class JapaneseCalendar extends GregorianCalendar {
73
- identifier = 'japanese';
73
+ identifier: CalendarIdentifier = 'japanese';
74
74
 
75
75
  fromJulianDay(jd: number): CalendarDate {
76
76
  let date = super.fromJulianDay(jd);
@@ -85,11 +85,11 @@ export class JapaneseCalendar extends GregorianCalendar {
85
85
  );
86
86
  }
87
87
 
88
- toJulianDay(date: AnyCalendarDate) {
88
+ toJulianDay(date: AnyCalendarDate): number {
89
89
  return super.toJulianDay(toGregorian(date));
90
90
  }
91
91
 
92
- balanceDate(date: Mutable<AnyCalendarDate>) {
92
+ balanceDate(date: Mutable<AnyCalendarDate>): void {
93
93
  let gregorianDate = toGregorian(date);
94
94
  let era = findEraFromGregorianDate(gregorianDate);
95
95
 
@@ -102,7 +102,7 @@ export class JapaneseCalendar extends GregorianCalendar {
102
102
  this.constrainDate(date);
103
103
  }
104
104
 
105
- constrainDate(date: Mutable<AnyCalendarDate>) {
105
+ constrainDate(date: Mutable<AnyCalendarDate>): void {
106
106
  let idx = ERA_NAMES.indexOf(date.era);
107
107
  let end = ERA_END_DATES[idx];
108
108
  if (end != null) {
@@ -131,7 +131,7 @@ export class JapaneseCalendar extends GregorianCalendar {
131
131
  }
132
132
  }
133
133
 
134
- getEras() {
134
+ getEras(): string[] {
135
135
  return ERA_NAMES;
136
136
  }
137
137
 
@@ -13,7 +13,7 @@
13
13
  // Portions of the code in this file are based on code from ICU.
14
14
  // Original licensing can be found in the NOTICE file in the root directory of this source tree.
15
15
 
16
- import {AnyCalendarDate, Calendar} from '../types';
16
+ import {AnyCalendarDate, Calendar, CalendarIdentifier} from '../types';
17
17
  import {CalendarDate} from '../CalendarDate';
18
18
  import {mod} from '../utils';
19
19
 
@@ -42,7 +42,7 @@ const MONTH_START = [
42
42
  * around the March equinox.
43
43
  */
44
44
  export class PersianCalendar implements Calendar {
45
- identifier = 'persian';
45
+ identifier: CalendarIdentifier = 'persian';
46
46
 
47
47
  fromJulianDay(jd: number): CalendarDate {
48
48
  let daysSinceEpoch = jd - PERSIAN_EPOCH;
@@ -80,7 +80,7 @@ export class PersianCalendar implements Calendar {
80
80
  return isLeapYear ? 30 : 29;
81
81
  }
82
82
 
83
- getEras() {
83
+ getEras(): string[] {
84
84
  return ['AP'];
85
85
  }
86
86
 
@@ -13,7 +13,7 @@
13
13
  // Portions of the code in this file are based on code from ICU.
14
14
  // Original licensing can be found in the NOTICE file in the root directory of this source tree.
15
15
 
16
- import {AnyCalendarDate} from '../types';
16
+ import {AnyCalendarDate, CalendarIdentifier} from '../types';
17
17
  import {CalendarDate} from '../CalendarDate';
18
18
  import {fromExtendedYear, getExtendedYear, GregorianCalendar} from './GregorianCalendar';
19
19
  import {Mutable} from '../utils';
@@ -41,7 +41,7 @@ function gregorianToTaiwan(year: number): [string, number] {
41
41
  * 'before_minguo' and 'minguo'.
42
42
  */
43
43
  export class TaiwanCalendar extends GregorianCalendar {
44
- identifier = 'roc'; // Republic of China
44
+ identifier: CalendarIdentifier = 'roc'; // Republic of China
45
45
 
46
46
  fromJulianDay(jd: number): CalendarDate {
47
47
  let date = super.fromJulianDay(jd);
@@ -50,15 +50,15 @@ export class TaiwanCalendar extends GregorianCalendar {
50
50
  return new CalendarDate(this, era, year, date.month, date.day);
51
51
  }
52
52
 
53
- toJulianDay(date: AnyCalendarDate) {
53
+ toJulianDay(date: AnyCalendarDate): number {
54
54
  return super.toJulianDay(toGregorian(date));
55
55
  }
56
56
 
57
- getEras() {
57
+ getEras(): string[] {
58
58
  return ['before_minguo', 'minguo'];
59
59
  }
60
60
 
61
- balanceDate(date: Mutable<AnyCalendarDate>) {
61
+ balanceDate(date: Mutable<AnyCalendarDate>): void {
62
62
  let [era, year] = gregorianToTaiwan(gregorianYear(date));
63
63
  date.era = era;
64
64
  date.year = year;
package/src/conversion.ts CHANGED
@@ -17,16 +17,16 @@ import {AnyCalendarDate, AnyDateTime, AnyTime, Calendar, DateFields, Disambiguat
17
17
  import {CalendarDate, CalendarDateTime, Time, ZonedDateTime} from './CalendarDate';
18
18
  import {constrain} from './manipulation';
19
19
  import {getExtendedYear, GregorianCalendar} from './calendars/GregorianCalendar';
20
- import {getLocalTimeZone} from './queries';
20
+ import {getLocalTimeZone, isEqualCalendar} from './queries';
21
21
  import {Mutable} from './utils';
22
22
 
23
- export function epochFromDate(date: AnyDateTime) {
23
+ export function epochFromDate(date: AnyDateTime): number {
24
24
  date = toCalendar(date, new GregorianCalendar());
25
25
  let year = getExtendedYear(date.era, date.year);
26
26
  return epochFromParts(year, date.month, date.day, date.hour, date.minute, date.second, date.millisecond);
27
27
  }
28
28
 
29
- function epochFromParts(year: number, month: number, day: number, hour: number, minute: number, second: number, millisecond: number) {
29
+ function epochFromParts(year: number, month: number, day: number, hour: number, minute: number, second: number, millisecond: number): number {
30
30
  // Note: Date.UTC() interprets one and two-digit years as being in the
31
31
  // 20th century, so don't use it
32
32
  let date = new Date();
@@ -35,7 +35,7 @@ function epochFromParts(year: number, month: number, day: number, hour: number,
35
35
  return date.getTime();
36
36
  }
37
37
 
38
- export function getTimeZoneOffset(ms: number, timeZone: string) {
38
+ export function getTimeZoneOffset(ms: number, timeZone: string): number {
39
39
  // Fast path for UTC.
40
40
  if (timeZone === 'UTC') {
41
41
  return 0;
@@ -260,7 +260,7 @@ export function toTime(dateTime: CalendarDateTime | ZonedDateTime): Time {
260
260
 
261
261
  /** Converts a date from one calendar system to another. */
262
262
  export function toCalendar<T extends AnyCalendarDate>(date: T, calendar: Calendar): T {
263
- if (date.calendar.identifier === calendar.identifier) {
263
+ if (isEqualCalendar(date.calendar, calendar)) {
264
264
  return date;
265
265
  }
266
266
 
@@ -292,7 +292,7 @@ export function toZoned(date: CalendarDate | CalendarDateTime | ZonedDateTime, t
292
292
  return fromAbsolute(ms, timeZone);
293
293
  }
294
294
 
295
- export function zonedToDate(date: ZonedDateTime) {
295
+ export function zonedToDate(date: ZonedDateTime): Date {
296
296
  let ms = epochFromDate(date) - date.offset;
297
297
  return new Date(ms);
298
298
  }
@@ -11,7 +11,7 @@
11
11
  */
12
12
 
13
13
  import {BuddhistCalendar} from './calendars/BuddhistCalendar';
14
- import {Calendar} from './types';
14
+ import {Calendar, CalendarIdentifier} from './types';
15
15
  import {CopticCalendar, EthiopicAmeteAlemCalendar, EthiopicCalendar} from './calendars/EthiopicCalendar';
16
16
  import {GregorianCalendar} from './calendars/GregorianCalendar';
17
17
  import {HebrewCalendar} from './calendars/HebrewCalendar';
@@ -22,7 +22,7 @@ import {PersianCalendar} from './calendars/PersianCalendar';
22
22
  import {TaiwanCalendar} from './calendars/TaiwanCalendar';
23
23
 
24
24
  /** Creates a `Calendar` instance from a Unicode calendar identifier string. */
25
- export function createCalendar(name: string): Calendar {
25
+ export function createCalendar(name: CalendarIdentifier): Calendar {
26
26
  switch (name) {
27
27
  case 'buddhist':
28
28
  return new BuddhistCalendar();
package/src/index.ts CHANGED
@@ -15,6 +15,7 @@ export type {
15
15
  AnyTime,
16
16
  AnyDateTime,
17
17
  Calendar,
18
+ CalendarIdentifier,
18
19
  DateDuration,
19
20
  TimeDuration,
20
21
  DateTimeDuration,
@@ -74,7 +75,8 @@ export {
74
75
  minDate,
75
76
  maxDate,
76
77
  isWeekend,
77
- isWeekday
78
+ isWeekday,
79
+ isEqualCalendar
78
80
  } from './queries';
79
81
  export {
80
82
  parseDate,
@@ -21,7 +21,7 @@ const ONE_HOUR = 3600000;
21
21
  export function add(date: CalendarDateTime, duration: DateTimeDuration): CalendarDateTime;
22
22
  export function add(date: CalendarDate, duration: DateDuration): CalendarDate;
23
23
  export function add(date: CalendarDate | CalendarDateTime, duration: DateTimeDuration): CalendarDate | CalendarDateTime;
24
- export function add(date: CalendarDate | CalendarDateTime, duration: DateTimeDuration) {
24
+ export function add(date: CalendarDate | CalendarDateTime, duration: DateTimeDuration): Mutable<AnyCalendarDate | AnyDateTime> {
25
25
  let mutableDate: Mutable<AnyCalendarDate | AnyDateTime> = date.copy();
26
26
  let days = 'hour' in mutableDate ? addTimeFields(mutableDate, duration) : 0;
27
27
 
@@ -118,7 +118,7 @@ function constrainMonthDay(date: Mutable<AnyCalendarDate>) {
118
118
  date.day = Math.max(1, Math.min(date.calendar.getDaysInMonth(date), date.day));
119
119
  }
120
120
 
121
- export function constrain(date: Mutable<AnyCalendarDate>) {
121
+ export function constrain(date: Mutable<AnyCalendarDate>): void {
122
122
  if (date.calendar.constrainDate) {
123
123
  date.calendar.constrainDate(date);
124
124
  }
@@ -146,7 +146,7 @@ export function subtract(date: CalendarDate | CalendarDateTime, duration: DateTi
146
146
 
147
147
  export function set(date: CalendarDateTime, fields: DateFields): CalendarDateTime;
148
148
  export function set(date: CalendarDate, fields: DateFields): CalendarDate;
149
- export function set(date: CalendarDate | CalendarDateTime, fields: DateFields) {
149
+ export function set(date: CalendarDate | CalendarDateTime, fields: DateFields): Mutable<AnyCalendarDate> {
150
150
  let mutableDate: Mutable<AnyCalendarDate> = date.copy();
151
151
 
152
152
  if (fields.era != null) {
@@ -171,7 +171,7 @@ export function set(date: CalendarDate | CalendarDateTime, fields: DateFields) {
171
171
 
172
172
  export function setTime(value: CalendarDateTime, fields: TimeFields): CalendarDateTime;
173
173
  export function setTime(value: Time, fields: TimeFields): Time;
174
- export function setTime(value: Time | CalendarDateTime, fields: TimeFields) {
174
+ export function setTime(value: Time | CalendarDateTime, fields: TimeFields): Mutable<Time | CalendarDateTime> {
175
175
  let mutableValue: Mutable<Time | CalendarDateTime> = value.copy();
176
176
 
177
177
  if (fields.hour != null) {
@@ -210,7 +210,7 @@ function balanceTime(time: Mutable<AnyTime>): number {
210
210
  return days;
211
211
  }
212
212
 
213
- export function constrainTime(time: Mutable<AnyTime>) {
213
+ export function constrainTime(time: Mutable<AnyTime>): void {
214
214
  time.millisecond = Math.max(0, Math.min(time.millisecond, 1000));
215
215
  time.second = Math.max(0, Math.min(time.second, 59));
216
216
  time.minute = Math.max(0, Math.min(time.minute, 59));
@@ -245,7 +245,7 @@ export function subtractTime(time: Time, duration: TimeDuration): Time {
245
245
 
246
246
  export function cycleDate(value: CalendarDateTime, field: DateField, amount: number, options?: CycleOptions): CalendarDateTime;
247
247
  export function cycleDate(value: CalendarDate, field: DateField, amount: number, options?: CycleOptions): CalendarDate;
248
- export function cycleDate(value: CalendarDate | CalendarDateTime, field: DateField, amount: number, options?: CycleOptions) {
248
+ export function cycleDate(value: CalendarDate | CalendarDateTime, field: DateField, amount: number, options?: CycleOptions): Mutable<CalendarDate | CalendarDateTime> {
249
249
  let mutable: Mutable<CalendarDate | CalendarDateTime> = value.copy();
250
250
 
251
251
  switch (field) {
@@ -300,7 +300,7 @@ export function cycleDate(value: CalendarDate | CalendarDateTime, field: DateFie
300
300
 
301
301
  export function cycleTime(value: CalendarDateTime, field: TimeField, amount: number, options?: CycleTimeOptions): CalendarDateTime;
302
302
  export function cycleTime(value: Time, field: TimeField, amount: number, options?: CycleTimeOptions): Time;
303
- export function cycleTime(value: Time | CalendarDateTime, field: TimeField, amount: number, options?: CycleTimeOptions) {
303
+ export function cycleTime(value: Time | CalendarDateTime, field: TimeField, amount: number, options?: CycleTimeOptions): Mutable<Time | CalendarDateTime> {
304
304
  let mutable: Mutable<Time | CalendarDateTime> = value.copy();
305
305
 
306
306
  switch (field) {
package/src/queries.ts CHANGED
@@ -10,7 +10,7 @@
10
10
  * governing permissions and limitations under the License.
11
11
  */
12
12
 
13
- import {AnyCalendarDate, AnyTime} from './types';
13
+ import {AnyCalendarDate, AnyTime, Calendar} from './types';
14
14
  import {CalendarDate, CalendarDateTime, ZonedDateTime} from './CalendarDate';
15
15
  import {fromAbsolute, toAbsolute, toCalendar, toCalendarDate} from './conversion';
16
16
  import {weekStartData} from './weekStartData';
@@ -42,21 +42,22 @@ export function isSameYear(a: DateValue, b: DateValue): boolean {
42
42
 
43
43
  /** Returns whether the given dates occur on the same day, and are of the same calendar system. */
44
44
  export function isEqualDay(a: DateValue, b: DateValue): boolean {
45
- return a.calendar.identifier === b.calendar.identifier && a.era === b.era && a.year === b.year && a.month === b.month && a.day === b.day;
45
+ return isEqualCalendar(a.calendar, b.calendar) && isSameDay(a, b);
46
46
  }
47
47
 
48
48
  /** Returns whether the given dates occur in the same month, and are of the same calendar system. */
49
49
  export function isEqualMonth(a: DateValue, b: DateValue): boolean {
50
- a = startOfMonth(a);
51
- b = startOfMonth(b);
52
- return a.calendar.identifier === b.calendar.identifier && a.era === b.era && a.year === b.year && a.month === b.month;
50
+ return isEqualCalendar(a.calendar, b.calendar) && isSameMonth(a, b);
53
51
  }
54
52
 
55
53
  /** Returns whether the given dates occur in the same year, and are of the same calendar system. */
56
54
  export function isEqualYear(a: DateValue, b: DateValue): boolean {
57
- a = startOfYear(a);
58
- b = startOfYear(b);
59
- return a.calendar.identifier === b.calendar.identifier && a.era === b.era && a.year === b.year;
55
+ return isEqualCalendar(a.calendar, b.calendar) && isSameYear(a, b);
56
+ }
57
+
58
+ /** Returns whether two calendars are the same. */
59
+ export function isEqualCalendar(a: Calendar, b: Calendar): boolean {
60
+ return a.isEqual?.(b) ?? b.isEqual?.(a) ?? a.identifier === b.identifier;
60
61
  }
61
62
 
62
63
  /** Returns whether the date is today in the given time zone. */
@@ -64,17 +65,30 @@ export function isToday(date: DateValue, timeZone: string): boolean {
64
65
  return isSameDay(date, today(timeZone));
65
66
  }
66
67
 
68
+ const DAY_MAP = {
69
+ sun: 0,
70
+ mon: 1,
71
+ tue: 2,
72
+ wed: 3,
73
+ thu: 4,
74
+ fri: 5,
75
+ sat: 6
76
+ };
77
+
78
+ type DayOfWeek = 'sun' | 'mon' | 'tue' | 'wed' | 'thu' | 'fri' | 'sat';
79
+
67
80
  /**
68
81
  * Returns the day of week for the given date and locale. Days are numbered from zero to six,
69
82
  * where zero is the first day of the week in the given locale. For example, in the United States,
70
83
  * the first day of the week is Sunday, but in France it is Monday.
71
84
  */
72
- export function getDayOfWeek(date: DateValue, locale: string): number {
85
+ export function getDayOfWeek(date: DateValue, locale: string, firstDayOfWeek?: DayOfWeek): number {
73
86
  let julian = date.calendar.toJulianDay(date);
74
87
 
75
88
  // If julian is negative, then julian % 7 will be negative, so we adjust
76
89
  // accordingly. Julian day 0 is Monday.
77
- let dayOfWeek = Math.ceil(julian + 1 - getWeekStart(locale)) % 7;
90
+ let weekStart = firstDayOfWeek ? DAY_MAP[firstDayOfWeek] : getWeekStart(locale);
91
+ let dayOfWeek = Math.ceil(julian + 1 - weekStart) % 7;
78
92
  if (dayOfWeek < 0) {
79
93
  dayOfWeek += 7;
80
94
  }
@@ -164,7 +178,7 @@ export function endOfYear(date: DateValue): DateValue {
164
178
  return endOfMonth(date.add({months: date.calendar.getMonthsInYear(date) - date.month}));
165
179
  }
166
180
 
167
- export function getMinimumMonthInYear(date: AnyCalendarDate) {
181
+ export function getMinimumMonthInYear(date: AnyCalendarDate): number {
168
182
  if (date.calendar.getMinimumMonthInYear) {
169
183
  return date.calendar.getMinimumMonthInYear(date);
170
184
  }
@@ -172,7 +186,7 @@ export function getMinimumMonthInYear(date: AnyCalendarDate) {
172
186
  return 1;
173
187
  }
174
188
 
175
- export function getMinimumDayInMonth(date: AnyCalendarDate) {
189
+ export function getMinimumDayInMonth(date: AnyCalendarDate): number {
176
190
  if (date.calendar.getMinimumDayInMonth) {
177
191
  return date.calendar.getMinimumDayInMonth(date);
178
192
  }
@@ -181,22 +195,22 @@ export function getMinimumDayInMonth(date: AnyCalendarDate) {
181
195
  }
182
196
 
183
197
  /** Returns the first date of the week for the given date and locale. */
184
- export function startOfWeek(date: ZonedDateTime, locale: string): ZonedDateTime;
185
- export function startOfWeek(date: CalendarDateTime, locale: string): CalendarDateTime;
186
- export function startOfWeek(date: CalendarDate, locale: string): CalendarDate;
187
- export function startOfWeek(date: DateValue, locale: string): DateValue;
188
- export function startOfWeek(date: DateValue, locale: string): DateValue {
189
- let dayOfWeek = getDayOfWeek(date, locale);
198
+ export function startOfWeek(date: ZonedDateTime, locale: string, firstDayOfWeek?: DayOfWeek): ZonedDateTime;
199
+ export function startOfWeek(date: CalendarDateTime, locale: string, firstDayOfWeek?: DayOfWeek): CalendarDateTime;
200
+ export function startOfWeek(date: CalendarDate, locale: string, firstDayOfWeek?: DayOfWeek): CalendarDate;
201
+ export function startOfWeek(date: DateValue, locale: string, firstDayOfWeek?: DayOfWeek): DateValue;
202
+ export function startOfWeek(date: DateValue, locale: string, firstDayOfWeek?: DayOfWeek): DateValue {
203
+ let dayOfWeek = getDayOfWeek(date, locale, firstDayOfWeek);
190
204
  return date.subtract({days: dayOfWeek});
191
205
  }
192
206
 
193
207
  /** Returns the last date of the week for the given date and locale. */
194
- export function endOfWeek(date: ZonedDateTime, locale: string): ZonedDateTime;
195
- export function endOfWeek(date: CalendarDateTime, locale: string): CalendarDateTime;
196
- export function endOfWeek(date: CalendarDate, locale: string): CalendarDate;
197
- export function endOfWeek(date: DateValue, locale: string): DateValue;
198
- export function endOfWeek(date: DateValue, locale: string): DateValue {
199
- return startOfWeek(date, locale).add({days: 6});
208
+ export function endOfWeek(date: ZonedDateTime, locale: string, firstDayOfWeek?: DayOfWeek): ZonedDateTime;
209
+ export function endOfWeek(date: CalendarDateTime, locale: string, firstDayOfWeek?: DayOfWeek): CalendarDateTime;
210
+ export function endOfWeek(date: CalendarDate, locale: string, firstDayOfWeek?: DayOfWeek): CalendarDate;
211
+ export function endOfWeek(date: DateValue, locale: string, firstDayOfWeek?: DayOfWeek): DateValue;
212
+ export function endOfWeek(date: DateValue, locale: string, firstDayOfWeek?: DayOfWeek): DateValue {
213
+ return startOfWeek(date, locale, firstDayOfWeek).add({days: 6});
200
214
  }
201
215
 
202
216
  const cachedRegions = new Map<string, string>();
@@ -233,9 +247,9 @@ function getWeekStart(locale: string): number {
233
247
  }
234
248
 
235
249
  /** Returns the number of weeks in the given month and locale. */
236
- export function getWeeksInMonth(date: DateValue, locale: string): number {
250
+ export function getWeeksInMonth(date: DateValue, locale: string, firstDayOfWeek?: DayOfWeek): number {
237
251
  let days = date.calendar.getDaysInMonth(date);
238
- return Math.ceil((getDayOfWeek(startOfMonth(date), locale) + days) / 7);
252
+ return Math.ceil((getDayOfWeek(startOfMonth(date), locale, firstDayOfWeek) + days) / 7);
239
253
  }
240
254
 
241
255
  /** Returns the lesser of the two provider dates. */
package/src/string.ts CHANGED
@@ -234,16 +234,14 @@ export function parseDuration(value: string): Required<DateTimeDuration> {
234
234
 
235
235
  const parseDurationGroup = (
236
236
  group: string | undefined,
237
- isNegative: boolean,
238
- min: number,
239
- max: number
237
+ isNegative: boolean
240
238
  ): number => {
241
239
  if (!group) {
242
240
  return 0;
243
241
  }
244
242
  try {
245
243
  const sign = isNegative ? -1 : 1;
246
- return sign * parseNumber(group.replace(',', '.'), min, max);
244
+ return sign * Number(group.replace(',', '.'));
247
245
  } catch {
248
246
  throw new Error(`Invalid ISO 8601 Duration string: ${value}`);
249
247
  }
@@ -267,13 +265,13 @@ export function parseDuration(value: string): Required<DateTimeDuration> {
267
265
  }
268
266
 
269
267
  const duration: Mutable<DateTimeDuration> = {
270
- years: parseDurationGroup(match.groups?.years, isNegative, 0, 9999),
271
- months: parseDurationGroup(match.groups?.months, isNegative, 0, 12),
272
- weeks: parseDurationGroup(match.groups?.weeks, isNegative, 0, Infinity),
273
- days: parseDurationGroup(match.groups?.days, isNegative, 0, 31),
274
- hours: parseDurationGroup(match.groups?.hours, isNegative, 0, 23),
275
- minutes: parseDurationGroup(match.groups?.minutes, isNegative, 0, 59),
276
- seconds: parseDurationGroup(match.groups?.seconds, isNegative, 0, 59)
268
+ years: parseDurationGroup(match.groups?.years, isNegative),
269
+ months: parseDurationGroup(match.groups?.months, isNegative),
270
+ weeks: parseDurationGroup(match.groups?.weeks, isNegative),
271
+ days: parseDurationGroup(match.groups?.days, isNegative),
272
+ hours: parseDurationGroup(match.groups?.hours, isNegative),
273
+ minutes: parseDurationGroup(match.groups?.minutes, isNegative),
274
+ seconds: parseDurationGroup(match.groups?.seconds, isNegative)
277
275
  };
278
276
 
279
277
  if (duration.hours !== undefined && ((duration.hours % 1) !== 0) && (duration.minutes || duration.seconds)) {
package/src/types.ts CHANGED
@@ -34,14 +34,19 @@ export interface AnyTime {
34
34
  /** An interface that is compatible with any object with both date and time fields. */
35
35
  export interface AnyDateTime extends AnyCalendarDate, AnyTime {}
36
36
 
37
+ export type CalendarIdentifier = 'gregory' | 'buddhist' | 'chinese' | 'coptic' | 'dangi' | 'ethioaa' | 'ethiopic' | 'hebrew' | 'indian' | 'islamic' | 'islamic-umalqura' | 'islamic-tbla' | 'islamic-civil' | 'islamic-rgsa' | 'iso8601' | 'japanese' | 'persian' | 'roc';
38
+
37
39
  /**
38
40
  * The Calendar interface represents a calendar system, including information
39
41
  * about how days, months, years, and eras are organized, and methods to perform
40
42
  * arithmetic on dates.
41
43
  */
42
44
  export interface Calendar {
43
- /** A string identifier for the calendar, as defined by Unicode CLDR. */
44
- identifier: string,
45
+ /**
46
+ * A string identifier for the calendar, as defined by Unicode CLDR.
47
+ * See [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/supportedValuesOf#supported_calendar_types).
48
+ */
49
+ identifier: CalendarIdentifier,
45
50
 
46
51
  /** Creates a CalendarDate in this calendar from the given Julian day number. */
47
52
  fromJulianDay(jd: number): CalendarDate,
@@ -69,6 +74,16 @@ export interface Calendar {
69
74
  * eras may begin in the middle of a month.
70
75
  */
71
76
  getMinimumDayInMonth?(date: AnyCalendarDate): number,
77
+ /**
78
+ * Returns a date that is the first day of the month for the given date.
79
+ * This is used to determine the month that the given date falls in, if
80
+ * the calendar has months that do not align with the standard calendar months
81
+ * (e.g. fiscal calendars).
82
+ */
83
+ getFormattableMonth?(date: AnyCalendarDate): CalendarDate,
84
+
85
+ /** Returns whether the given calendar is the same as this calendar. */
86
+ isEqual?(calendar: Calendar): boolean,
72
87
 
73
88
  /** @private */
74
89
  balanceDate?(date: AnyCalendarDate): void,