Skip to content

Commit

Permalink
Adjust ISO8601 representation for years between 1 BCE and 999 CE (#157)
Browse files Browse the repository at this point in the history
  • Loading branch information
LiviaMedeiros authored May 23, 2022
1 parent 4f8b04c commit b251dc0
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 14 deletions.
4 changes: 2 additions & 2 deletions lib/ecmascript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2431,12 +2431,12 @@ function GetPossibleInstantsFor(

export function ISOYearString(year: number) {
let yearString;
if (year < 1000 || year > 9999) {
if (year < 0 || year > 9999) {
const sign = year < 0 ? '-' : '+';
const yearNumber = MathAbs(year);
yearString = sign + `000000${yearNumber}`.slice(-6);
} else {
yearString = `${year}`;
yearString = `0000${year}`.slice(-4);
}
return yearString;
}
Expand Down
18 changes: 12 additions & 6 deletions test/timezone.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -390,21 +390,27 @@ describe('TimeZone', () => {
equal(`${tz.getInstantFor(dt)}`, '2019-10-29T09:46:38.271986102Z');
});
it('year ≤ 99', () => {
const dt = Temporal.PlainDateTime.from('+000098-10-29T10:46:38.271986102');
let dt = Temporal.PlainDateTime.from('0098-10-29T10:46:38.271986102');
const tz = Temporal.TimeZone.from('+06:00');
equal(`${tz.getInstantFor(dt)}`, '+000098-10-29T04:46:38.271986102Z');
equal(`${tz.getInstantFor(dt)}`, '0098-10-29T04:46:38.271986102Z');
dt = Temporal.PlainDateTime.from('+000098-10-29T10:46:38.271986102');
equal(`${tz.getInstantFor(dt)}`, '0098-10-29T04:46:38.271986102Z');
});
it('year < 1', () => {
let dt = Temporal.PlainDateTime.from('+000000-10-29T10:46:38.271986102');
let dt = Temporal.PlainDateTime.from('0000-10-29T10:46:38.271986102');
const tz = Temporal.TimeZone.from('+06:00');
equal(`${tz.getInstantFor(dt)}`, '+000000-10-29T04:46:38.271986102Z');
equal(`${tz.getInstantFor(dt)}`, '0000-10-29T04:46:38.271986102Z');
dt = Temporal.PlainDateTime.from('+000000-10-29T10:46:38.271986102');
equal(`${tz.getInstantFor(dt)}`, '0000-10-29T04:46:38.271986102Z');
dt = Temporal.PlainDateTime.from('-001000-10-29T10:46:38.271986102');
equal(`${tz.getInstantFor(dt)}`, '-001000-10-29T04:46:38.271986102Z');
});
it('year 0 leap day', () => {
const dt = Temporal.PlainDateTime.from('+000000-02-29T00:00');
let dt = Temporal.PlainDateTime.from('0000-02-29T00:00');
const tz = Temporal.TimeZone.from('Europe/London');
equal(`${tz.getInstantFor(dt)}`, '+000000-02-29T00:01:15Z');
equal(`${tz.getInstantFor(dt)}`, '0000-02-29T00:01:15Z');
dt = Temporal.PlainDateTime.from('+000000-02-29T00:00');
equal(`${tz.getInstantFor(dt)}`, '0000-02-29T00:01:15Z');
});
it('outside of Instant range', () => {
const max = Temporal.PlainDateTime.from('+275760-09-13T23:59:59.999999999');
Expand Down
18 changes: 12 additions & 6 deletions test/zoneddatetime.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2438,18 +2438,24 @@ describe('ZonedDateTime', () => {
equal(`${zdt.toInstant()}`, '2019-10-29T09:46:38.271986102Z');
});
it('year ≤ 99', () => {
const zdt = ZonedDateTime.from('+000098-10-29T10:46:38.271986102+00:00[UTC]');
equal(`${zdt.toInstant()}`, '+000098-10-29T10:46:38.271986102Z');
let zdt = ZonedDateTime.from('0098-10-29T10:46:38.271986102+00:00[UTC]');
equal(`${zdt.toInstant()}`, '0098-10-29T10:46:38.271986102Z');
zdt = ZonedDateTime.from('+000098-10-29T10:46:38.271986102+00:00[UTC]');
equal(`${zdt.toInstant()}`, '0098-10-29T10:46:38.271986102Z');
});
it('year < 1', () => {
let zdt = ZonedDateTime.from('+000000-10-29T10:46:38.271986102+00:00[UTC]');
equal(`${zdt.toInstant()}`, '+000000-10-29T10:46:38.271986102Z');
let zdt = ZonedDateTime.from('0000-10-29T10:46:38.271986102+00:00[UTC]');
equal(`${zdt.toInstant()}`, '0000-10-29T10:46:38.271986102Z');
zdt = ZonedDateTime.from('+000000-10-29T10:46:38.271986102+00:00[UTC]');
equal(`${zdt.toInstant()}`, '0000-10-29T10:46:38.271986102Z');
zdt = ZonedDateTime.from('-001000-10-29T10:46:38.271986102+00:00[UTC]');
equal(`${zdt.toInstant()}`, '-001000-10-29T10:46:38.271986102Z');
});
it('year 0 leap day', () => {
const zdt = ZonedDateTime.from('+000000-02-29T00:00-00:01:15[Europe/London]');
equal(`${zdt.toInstant()}`, '+000000-02-29T00:01:15Z');
let zdt = ZonedDateTime.from('0000-02-29T00:00-00:01:15[Europe/London]');
equal(`${zdt.toInstant()}`, '0000-02-29T00:01:15Z');
zdt = ZonedDateTime.from('+000000-02-29T00:00-00:01:15[Europe/London]');
equal(`${zdt.toInstant()}`, '0000-02-29T00:01:15Z');
});
});
describe('ZonedDateTime.toPlainDate()', () => {
Expand Down

0 comments on commit b251dc0

Please sign in to comment.