From d8e1285c3d20bc6052090afeb462243c2594f3a2 Mon Sep 17 00:00:00 2001 From: Kim Peeters Date: Thu, 13 Jun 2024 12:22:46 +0200 Subject: [PATCH] Allowing BC era timestamps in fromAbsolute (#6541) --- packages/@internationalized/date/src/conversion.ts | 2 +- packages/@internationalized/date/tests/conversion.test.js | 8 ++++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/packages/@internationalized/date/src/conversion.ts b/packages/@internationalized/date/src/conversion.ts index ea18f334065..538015c18b6 100644 --- a/packages/@internationalized/date/src/conversion.ts +++ b/packages/@internationalized/date/src/conversion.ts @@ -187,7 +187,7 @@ export function fromAbsolute(ms: number, timeZone: string): ZonedDateTime { let second = date.getUTCSeconds(); let millisecond = date.getUTCMilliseconds(); - return new ZonedDateTime(year, month, day, timeZone, offset, hour, minute, second, millisecond); + return new ZonedDateTime(year < 1 ? 'BC' : 'AD', year < 0 ? Math.abs(year) + 1 : year, month, day, timeZone, offset, hour, minute, second, millisecond); } /** diff --git a/packages/@internationalized/date/tests/conversion.test.js b/packages/@internationalized/date/tests/conversion.test.js index 6d38a6aa627..27040d2c75b 100644 --- a/packages/@internationalized/date/tests/conversion.test.js +++ b/packages/@internationalized/date/tests/conversion.test.js @@ -136,6 +136,14 @@ describe('CalendarDate conversion', function () { date = fromAbsolute(new Date('2020-02-03T10:00Z').getTime(), 'America/New_York'); expect(date).toEqual(new ZonedDateTime(2020, 2, 3, 'America/New_York', -18000000, 5)); }); + + it('should convert a date from absolute in the BC era', function () { + let date = fromAbsolute(new Date('0000-01-01T00:00:00.000Z').getTime(), 'UTC'); + expect(date).toEqual(new ZonedDateTime('BC', 1, 1, 1, 'UTC', 0, 0, 0, 0)); + + date = fromAbsolute(new Date('-000009-01-01T00:00:00.000Z').getTime(), 'UTC'); + expect(date).toEqual(new ZonedDateTime('BC', 10, 1, 1, 'UTC', 0, 0, 0, 0)); + }); }); describe('toCalendar', function () {