Skip to content

Commit 3949762

Browse files
Allowing BC era timestamps in fromAbsolute (#6541) (#6542)
Co-authored-by: Yihui Liao <[email protected]>
1 parent c2c7fb0 commit 3949762

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed

packages/@internationalized/date/src/conversion.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ export function fromAbsolute(ms: number, timeZone: string): ZonedDateTime {
187187
let second = date.getUTCSeconds();
188188
let millisecond = date.getUTCMilliseconds();
189189

190-
return new ZonedDateTime(year, month, day, timeZone, offset, hour, minute, second, millisecond);
190+
return new ZonedDateTime(year < 1 ? 'BC' : 'AD', year < 0 ? Math.abs(year) + 1 : year, month, day, timeZone, offset, hour, minute, second, millisecond);
191191
}
192192

193193
/**

packages/@internationalized/date/tests/conversion.test.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,14 @@ describe('CalendarDate conversion', function () {
136136
date = fromAbsolute(new Date('2020-02-03T10:00Z').getTime(), 'America/New_York');
137137
expect(date).toEqual(new ZonedDateTime(2020, 2, 3, 'America/New_York', -18000000, 5));
138138
});
139+
140+
it('should convert a date from absolute in the BC era', function () {
141+
let date = fromAbsolute(new Date('0000-01-01T00:00:00.000Z').getTime(), 'UTC');
142+
expect(date).toEqual(new ZonedDateTime('BC', 1, 1, 1, 'UTC', 0, 0, 0, 0));
143+
144+
date = fromAbsolute(new Date('-000009-01-01T00:00:00.000Z').getTime(), 'UTC');
145+
expect(date).toEqual(new ZonedDateTime('BC', 10, 1, 1, 'UTC', 0, 0, 0, 0));
146+
});
139147
});
140148

141149
describe('toCalendar', function () {

0 commit comments

Comments
 (0)