Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
ddcb1fd
Add createCalendar prop to Calendar/RangeCalendar
ToyWalrus Feb 11, 2025
2ca395a
Update date math for adding/subtracting months
ToyWalrus Feb 18, 2025
6172fd6
Add doc comments
ToyWalrus Feb 18, 2025
f48fcf1
Simplify isEqualDay/Month/Year
ToyWalrus Feb 18, 2025
d86e283
Display the calendar month name based on the result of getCurrentMonth()
ToyWalrus Feb 18, 2025
15f2b0d
Format aria labels based on current month
ToyWalrus Feb 19, 2025
0ae33ff
Refactor to return weeksInMonth from useCalendarGrid hook
ToyWalrus Feb 19, 2025
82a262b
Fix month's year display in calendar header for custom calendars
ToyWalrus Feb 19, 2025
729600c
Update Calendar and RangeCalendar docs to talk about custom calendars
ToyWalrus Feb 19, 2025
d880b79
Add custom calendar stories to Calendar and RangeCalendar
ToyWalrus Feb 19, 2025
a265261
Fix range selection styles for custom calendar
ToyWalrus Feb 19, 2025
471db7f
Write tests for custom calendar date math
ToyWalrus Feb 19, 2025
79ee152
Write tests for changes to queries.ts
ToyWalrus Feb 19, 2025
dce90a9
Use customCalendarImpl in stories
ToyWalrus Feb 19, 2025
a30b47a
Write tests for visible range description with custom calendar
ToyWalrus Feb 20, 2025
ede8d9e
Update calendar docs to use jsx syntax
ToyWalrus Feb 20, 2025
7b54cbb
Merge branch 'main' into custom-calendar-support
ToyWalrus Feb 20, 2025
a617fca
Update calendar docs to fully implement Calendar interface to satisfy…
ToyWalrus Feb 20, 2025
5191f42
Update calendar docs
ToyWalrus Feb 20, 2025
e9ae17a
Merge branch 'main' into custom-calendar-support
ToyWalrus Feb 20, 2025
cee9ee5
wip
devongovett Mar 12, 2025
348dba1
fix
devongovett Mar 12, 2025
648b8d6
feat: use suggestion from @devongovett to implement custom cal w/ jul…
ToyWalrus Mar 17, 2025
d6c5e56
chore: remove unnecessary code in queries & manipulation
ToyWalrus Mar 17, 2025
8fe7e49
feat: zero-index the week pattern in 454 calendar impl
ToyWalrus Mar 18, 2025
f736857
chore: remove unused code in utils
ToyWalrus Mar 18, 2025
c1357af
test: update useCalendar tests for custom calendar
ToyWalrus Mar 18, 2025
efe3924
test: verify conversion for end of big year
ToyWalrus Mar 18, 2025
e8eac82
feat: update createCalendar prop types
ToyWalrus Mar 18, 2025
27d3355
feat: add createCalendar prop to DatePicker and DateRangePicker
ToyWalrus Mar 18, 2025
360b56b
Update docs
devongovett Mar 17, 2025
fd6815d
update examples
devongovett Mar 17, 2025
743ad29
ts
devongovett Mar 17, 2025
615f719
docs: update docs for DatePicker and DateRangePicker
ToyWalrus Mar 18, 2025
71392e2
Merge branch 'main' of github.com:adobe/react-spectrum into custom-ca…
ToyWalrus Mar 18, 2025
933937b
docs: remove custom calendar system section in useCalendar hooks
ToyWalrus Mar 18, 2025
df2b1b0
chore: more code cleanup
ToyWalrus Mar 18, 2025
23dc0f0
fix: mdx example
ToyWalrus Mar 18, 2025
fbf0ed0
Merge branch 'main' of github.com:adobe/react-spectrum into toywalrus…
devongovett Mar 25, 2025
9a04084
Add isEqualCalendar function
devongovett Mar 25, 2025
c960800
Update docs
devongovett Mar 25, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions packages/@internationalized/date/docs/Calendar.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -226,3 +226,69 @@ function createCalendar(identifier) {
## Interface

<ClassAPI links={docs.links} class={docs.exports.Calendar} />

## Custom calendars

You can create your own custom calendar system by implementing the `Calendar` interface shown above. This enables calendars that follow custom business rules. An example would be a fiscal year calendar that follows a [4-5-4 format](https://nrf.com/resources/4-5-4-calendar), where month ranges don't follow the usual Gregorian calendar.

To implement a calendar, either extend an existing implementation (e.g. `GregorianCalendar`) or implement the `Calendar` interface from scratch. The most important methods are `fromJulianDay` and `toJulianDay`, which convert between the calendar's year/month/day numbering system and a [Julian Day Number](https://en.wikipedia.org/wiki/Julian_day). This allows converting dates between calendar systems. Other methods such as `getDaysInMonth` and `getMonthsInYear` can be implemented to define how dates are organized in your calendar system.

The following code is an example of how you might implement a custom 4-5-4 calendar (though implementing a true 4-5-4 calendar would be more nuanced than this).

```tsx
import type {AnyCalendarDate, Calendar} from '@internationalized/date';
import {CalendarDate, GregorianCalendar, startOfWeek} from '@internationalized/date';

const weekPattern = [4, 5, 4, 4, 5, 4, 4, 5, 4, 4, 5, 4];

class Custom454 extends GregorianCalendar {
// Months always have either 4 or 5 full weeks.
getDaysInMonth(date) {
return weekPattern[date.month - 1] * 7;
}

// Enable conversion between calendar systems.
fromJulianDay(jd: number): CalendarDate {
let gregorian = super.fromJulianDay(jd);

// Start from the beginning of the first week of the gregorian year
// and add weeks until we find the month.
let monthStart = startOfWeek(new CalendarDate(gregorian.year, 1, 1), 'en');
for (let months = 0; months < weekPattern.length; months++) {
let weeksInMonth = weekPattern[months];
let monthEnd = monthStart.add({weeks: weeksInMonth});
if (monthEnd.compare(gregorian) > 0) {
let days = gregorian.compare(monthStart);
return new CalendarDate(this, monthStart.year, months + 1, days + 1);
}
monthStart = monthEnd;
}

throw Error('Date is not in any month somehow!');
}

toJulianDay(date: AnyCalendarDate): number {
let monthStart = startOfWeek(new CalendarDate(date.year, 1, 1), 'en');
for (let month = 1; month < date.month; month++) {
monthStart = monthStart.add({weeks: weekPattern[month - 1]});
}

let gregorian = monthStart.add({days: date.day - 1});
return super.toJulianDay(gregorian);
}

isEqual(other: Calendar) {
return other instanceof Custom454;
}
}
```

This enables dates to be converted between calendar systems.

```tsx
import {GregorianCalendar, toCalendar} from '@internationalized/date';

let date = new CalendarDate(new Custom454(), 2024, 2, 1);
let gregorianDate = toCalendar(date, new GregorianCalendar());
// => new CalendarDate(new GregorianCalendar(), 2024, 1, 29);
```
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
// Portions of the code in this file are based on code from ICU.
// Original licensing can be found in the NOTICE file in the root directory of this source tree.

import {AnyCalendarDate} from '../types';
import {AnyCalendarDate, CalendarIdentifier} from '../types';
import {CalendarDate} from '../CalendarDate';
import {fromExtendedYear, getExtendedYear, GregorianCalendar} from './GregorianCalendar';

Expand All @@ -25,7 +25,7 @@ const BUDDHIST_ERA_START = -543;
* era, identified as 'BE'.
*/
export class BuddhistCalendar extends GregorianCalendar {
identifier = 'buddhist';
identifier: CalendarIdentifier = 'buddhist';

fromJulianDay(jd: number): CalendarDate {
let gregorianDate = super.fromJulianDay(jd);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
// Portions of the code in this file are based on code from ICU.
// Original licensing can be found in the NOTICE file in the root directory of this source tree.

import {AnyCalendarDate, Calendar} from '../types';
import {AnyCalendarDate, Calendar, CalendarIdentifier} from '../types';
import {CalendarDate} from '../CalendarDate';
import {Mutable} from '../utils';

Expand Down Expand Up @@ -66,7 +66,7 @@ function getDaysInMonth(year: number, month: number) {
* on whether it is a leap year. Two eras are supported: 'AA' and 'AM'.
*/
export class EthiopicCalendar implements Calendar {
identifier = 'ethiopic';
identifier: CalendarIdentifier = 'ethiopic';

fromJulianDay(jd: number): CalendarDate {
let [year, month, day] = julianDayToCE(ETHIOPIC_EPOCH, jd);
Expand Down Expand Up @@ -117,7 +117,7 @@ export class EthiopicCalendar implements Calendar {
* except years were measured from a different epoch. Only one era is supported: 'AA'.
*/
export class EthiopicAmeteAlemCalendar extends EthiopicCalendar {
identifier = 'ethioaa'; // also known as 'ethiopic-amete-alem' in ICU
identifier: CalendarIdentifier = 'ethioaa'; // also known as 'ethiopic-amete-alem' in ICU

fromJulianDay(jd: number): CalendarDate {
let [year, month, day] = julianDayToCE(ETHIOPIC_EPOCH, jd);
Expand All @@ -141,7 +141,7 @@ export class EthiopicAmeteAlemCalendar extends EthiopicCalendar {
* on whether it is a leap year. Two eras are supported: 'BCE' and 'CE'.
*/
export class CopticCalendar extends EthiopicCalendar {
identifier = 'coptic';
identifier: CalendarIdentifier = 'coptic';

fromJulianDay(jd: number): CalendarDate {
let [year, month, day] = julianDayToCE(COPTIC_EPOCH, jd);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
// Portions of the code in this file are based on code from ICU.
// Original licensing can be found in the NOTICE file in the root directory of this source tree.

import {AnyCalendarDate, Calendar} from '../types';
import {AnyCalendarDate, Calendar, CalendarIdentifier} from '../types';
import {CalendarDate} from '../CalendarDate';
import {mod, Mutable} from '../utils';

Expand Down Expand Up @@ -68,7 +68,7 @@ const daysInMonth = {
* Years always contain 12 months, and 365 or 366 days depending on whether it is a leap year.
*/
export class GregorianCalendar implements Calendar {
identifier = 'gregory';
identifier: CalendarIdentifier = 'gregory';

fromJulianDay(jd: number): CalendarDate {
let jd0 = jd;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
// Portions of the code in this file are based on code from ICU.
// Original licensing can be found in the NOTICE file in the root directory of this source tree.

import {AnyCalendarDate, Calendar} from '../types';
import {AnyCalendarDate, Calendar, CalendarIdentifier} from '../types';
import {CalendarDate} from '../CalendarDate';
import {mod, Mutable} from '../utils';

Expand Down Expand Up @@ -128,7 +128,7 @@ function getDaysInMonth(year: number, month: number): number {
* In leap years, an extra month is inserted at month 6.
*/
export class HebrewCalendar implements Calendar {
identifier = 'hebrew';
identifier: CalendarIdentifier = 'hebrew';

fromJulianDay(jd: number): CalendarDate {
let d = jd - HEBREW_EPOCH;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
// Portions of the code in this file are based on code from ICU.
// Original licensing can be found in the NOTICE file in the root directory of this source tree.

import {AnyCalendarDate} from '../types';
import {AnyCalendarDate, CalendarIdentifier} from '../types';
import {CalendarDate} from '../CalendarDate';
import {fromExtendedYear, GregorianCalendar, gregorianToJulianDay, isLeapYear} from './GregorianCalendar';

Expand All @@ -29,7 +29,7 @@ const INDIAN_YEAR_START = 80;
* in each year, with either 30 or 31 days. Only one era identifier is supported: 'saka'.
*/
export class IndianCalendar extends GregorianCalendar {
identifier = 'indian';
identifier: CalendarIdentifier = 'indian';

fromJulianDay(jd: number): CalendarDate {
// Gregorian date for Julian day
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
// Portions of the code in this file are based on code from ICU.
// Original licensing can be found in the NOTICE file in the root directory of this source tree.

import {AnyCalendarDate, Calendar} from '../types';
import {AnyCalendarDate, Calendar, CalendarIdentifier} from '../types';
import {CalendarDate} from '../CalendarDate';

const CIVIL_EPOC = 1948440; // CE 622 July 16 Friday (Julian calendar) / CE 622 July 19 (Gregorian calendar)
Expand Down Expand Up @@ -50,7 +50,7 @@ function isLeapYear(year: number): boolean {
* Learn more about the available Islamic calendars [here](https://cldr.unicode.org/development/development-process/design-proposals/islamic-calendar-types).
*/
export class IslamicCivilCalendar implements Calendar {
identifier = 'islamic-civil';
identifier: CalendarIdentifier = 'islamic-civil';

fromJulianDay(jd: number): CalendarDate {
return julianDayToIslamic(this, CIVIL_EPOC, jd);
Expand Down Expand Up @@ -95,7 +95,7 @@ export class IslamicCivilCalendar implements Calendar {
* Learn more about the available Islamic calendars [here](https://cldr.unicode.org/development/development-process/design-proposals/islamic-calendar-types).
*/
export class IslamicTabularCalendar extends IslamicCivilCalendar {
identifier = 'islamic-tbla';
identifier: CalendarIdentifier = 'islamic-tbla';

fromJulianDay(jd: number): CalendarDate {
return julianDayToIslamic(this, ASTRONOMICAL_EPOC, jd);
Expand Down Expand Up @@ -145,7 +145,7 @@ function umalquraYearLength(year: number): number {
* Learn more about the available Islamic calendars [here](https://cldr.unicode.org/development/development-process/design-proposals/islamic-calendar-types).
*/
export class IslamicUmalquraCalendar extends IslamicCivilCalendar {
identifier = 'islamic-umalqura';
identifier: CalendarIdentifier = 'islamic-umalqura';

constructor() {
super();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
// Portions of the code in this file are based on code from the TC39 Temporal proposal.
// Original licensing can be found in the NOTICE file in the root directory of this source tree.

import {AnyCalendarDate} from '../types';
import {AnyCalendarDate, CalendarIdentifier} from '../types';
import {CalendarDate} from '../CalendarDate';
import {GregorianCalendar} from './GregorianCalendar';
import {Mutable} from '../utils';
Expand Down Expand Up @@ -70,7 +70,7 @@ function toGregorian(date: AnyCalendarDate) {
* Note that eras before 1868 (Gregorian) are not currently supported by this implementation.
*/
export class JapaneseCalendar extends GregorianCalendar {
identifier = 'japanese';
identifier: CalendarIdentifier = 'japanese';

fromJulianDay(jd: number): CalendarDate {
let date = super.fromJulianDay(jd);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
// Portions of the code in this file are based on code from ICU.
// Original licensing can be found in the NOTICE file in the root directory of this source tree.

import {AnyCalendarDate, Calendar} from '../types';
import {AnyCalendarDate, Calendar, CalendarIdentifier} from '../types';
import {CalendarDate} from '../CalendarDate';
import {mod} from '../utils';

Expand Down Expand Up @@ -42,7 +42,7 @@ const MONTH_START = [
* around the March equinox.
*/
export class PersianCalendar implements Calendar {
identifier = 'persian';
identifier: CalendarIdentifier = 'persian';

fromJulianDay(jd: number): CalendarDate {
let daysSinceEpoch = jd - PERSIAN_EPOCH;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
// Portions of the code in this file are based on code from ICU.
// Original licensing can be found in the NOTICE file in the root directory of this source tree.

import {AnyCalendarDate} from '../types';
import {AnyCalendarDate, CalendarIdentifier} from '../types';
import {CalendarDate} from '../CalendarDate';
import {fromExtendedYear, getExtendedYear, GregorianCalendar} from './GregorianCalendar';
import {Mutable} from '../utils';
Expand Down Expand Up @@ -41,7 +41,7 @@ function gregorianToTaiwan(year: number): [string, number] {
* 'before_minguo' and 'minguo'.
*/
export class TaiwanCalendar extends GregorianCalendar {
identifier = 'roc'; // Republic of China
identifier: CalendarIdentifier = 'roc'; // Republic of China

fromJulianDay(jd: number): CalendarDate {
let date = super.fromJulianDay(jd);
Expand Down
4 changes: 2 additions & 2 deletions packages/@internationalized/date/src/conversion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {AnyCalendarDate, AnyDateTime, AnyTime, Calendar, DateFields, Disambiguat
import {CalendarDate, CalendarDateTime, Time, ZonedDateTime} from './CalendarDate';
import {constrain} from './manipulation';
import {getExtendedYear, GregorianCalendar} from './calendars/GregorianCalendar';
import {getLocalTimeZone} from './queries';
import {getLocalTimeZone, isEqualCalendar} from './queries';
import {Mutable} from './utils';

export function epochFromDate(date: AnyDateTime): number {
Expand Down Expand Up @@ -260,7 +260,7 @@ export function toTime(dateTime: CalendarDateTime | ZonedDateTime): Time {

/** Converts a date from one calendar system to another. */
export function toCalendar<T extends AnyCalendarDate>(date: T, calendar: Calendar): T {
if (date.calendar.identifier === calendar.identifier) {
if (isEqualCalendar(date.calendar, calendar)) {
return date;
}

Expand Down
4 changes: 2 additions & 2 deletions packages/@internationalized/date/src/createCalendar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
*/

import {BuddhistCalendar} from './calendars/BuddhistCalendar';
import {Calendar} from './types';
import {Calendar, CalendarIdentifier} from './types';
import {CopticCalendar, EthiopicAmeteAlemCalendar, EthiopicCalendar} from './calendars/EthiopicCalendar';
import {GregorianCalendar} from './calendars/GregorianCalendar';
import {HebrewCalendar} from './calendars/HebrewCalendar';
Expand All @@ -22,7 +22,7 @@ import {PersianCalendar} from './calendars/PersianCalendar';
import {TaiwanCalendar} from './calendars/TaiwanCalendar';

/** Creates a `Calendar` instance from a Unicode calendar identifier string. */
export function createCalendar(name: string): Calendar {
export function createCalendar(name: CalendarIdentifier): Calendar {
switch (name) {
case 'buddhist':
return new BuddhistCalendar();
Expand Down
4 changes: 3 additions & 1 deletion packages/@internationalized/date/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export type {
AnyTime,
AnyDateTime,
Calendar,
CalendarIdentifier,
DateDuration,
TimeDuration,
DateTimeDuration,
Expand Down Expand Up @@ -74,7 +75,8 @@ export {
minDate,
maxDate,
isWeekend,
isWeekday
isWeekday,
isEqualCalendar
} from './queries';
export {
parseDate,
Expand Down
17 changes: 9 additions & 8 deletions packages/@internationalized/date/src/queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
* governing permissions and limitations under the License.
*/

import {AnyCalendarDate, AnyTime} from './types';
import {AnyCalendarDate, AnyTime, Calendar} from './types';
import {CalendarDate, CalendarDateTime, ZonedDateTime} from './CalendarDate';
import {fromAbsolute, toAbsolute, toCalendar, toCalendarDate} from './conversion';
import {weekStartData} from './weekStartData';
Expand Down Expand Up @@ -42,21 +42,22 @@ export function isSameYear(a: DateValue, b: DateValue): boolean {

/** Returns whether the given dates occur on the same day, and are of the same calendar system. */
export function isEqualDay(a: DateValue, b: DateValue): boolean {
return a.calendar.identifier === b.calendar.identifier && a.era === b.era && a.year === b.year && a.month === b.month && a.day === b.day;
return isEqualCalendar(a.calendar, b.calendar) && isSameDay(a, b);
}

/** Returns whether the given dates occur in the same month, and are of the same calendar system. */
export function isEqualMonth(a: DateValue, b: DateValue): boolean {
a = startOfMonth(a);
b = startOfMonth(b);
return a.calendar.identifier === b.calendar.identifier && a.era === b.era && a.year === b.year && a.month === b.month;
return isEqualCalendar(a.calendar, b.calendar) && isSameMonth(a, b);
}

/** Returns whether the given dates occur in the same year, and are of the same calendar system. */
export function isEqualYear(a: DateValue, b: DateValue): boolean {
a = startOfYear(a);
b = startOfYear(b);
return a.calendar.identifier === b.calendar.identifier && a.era === b.era && a.year === b.year;
return isEqualCalendar(a.calendar, b.calendar) && isSameYear(a, b);
}

/** Returns whether two calendars are the same. */
export function isEqualCalendar(a: Calendar, b: Calendar): boolean {
return a.isEqual?.(b) ?? b.isEqual?.(a) ?? a.identifier === b.identifier;
}

/** Returns whether the date is today in the given time zone. */
Expand Down
19 changes: 17 additions & 2 deletions packages/@internationalized/date/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,19 @@ export interface AnyTime {
/** An interface that is compatible with any object with both date and time fields. */
export interface AnyDateTime extends AnyCalendarDate, AnyTime {}

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';

/**
* The Calendar interface represents a calendar system, including information
* about how days, months, years, and eras are organized, and methods to perform
* arithmetic on dates.
*/
export interface Calendar {
/** A string identifier for the calendar, as defined by Unicode CLDR. */
identifier: string,
/**
* A string identifier for the calendar, as defined by Unicode CLDR.
* See [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/supportedValuesOf#supported_calendar_types).
*/
identifier: CalendarIdentifier,

/** Creates a CalendarDate in this calendar from the given Julian day number. */
fromJulianDay(jd: number): CalendarDate,
Expand Down Expand Up @@ -69,6 +74,16 @@ export interface Calendar {
* eras may begin in the middle of a month.
*/
getMinimumDayInMonth?(date: AnyCalendarDate): number,
/**
* Returns a date that is the first day of the month for the given date.
* This is used to determine the month that the given date falls in, if
* the calendar has months that do not align with the standard calendar months
* (e.g. fiscal calendars).
*/
getFormattableMonth?(date: AnyCalendarDate): CalendarDate,

/** Returns whether the given calendar is the same as this calendar. */
isEqual?(calendar: Calendar): boolean,

/** @private */
balanceDate?(date: AnyCalendarDate): void,
Expand Down
Loading