From de58d9f481e637248639f3aa1f7186f3521deb7b Mon Sep 17 00:00:00 2001 From: xiehuanyi <74809554+xiehuanyi@users.noreply.github.com> Date: Tue, 13 Oct 2026 21:01:49 +0300 Subject: [PATCH 1/2] Preserve complete dates for expanded ISO years --- lib/serializer.js | 2 +- test/date-expanded-year.test.js | 13 +++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) create mode 100644 test/date-expanded-year.test.js diff --git a/lib/serializer.js b/lib/serializer.js index 1b3a77d8..b58ca483 100644 --- a/lib/serializer.js +++ b/lib/serializer.js @@ -108,7 +108,7 @@ module.exports = class Serializer { asDate (date) { if (date === null) return '""' if (date instanceof Date) { - return '"' + new Date(date.getTime() - (date.getTimezoneOffset() * 60000)).toISOString().slice(0, 10) + '"' + return '"' + new Date(date.getTime() - (date.getTimezoneOffset() * 60000)).toISOString().slice(0, -14) + '"' } if (typeof date === 'string') { return asString(date) diff --git a/test/date-expanded-year.test.js b/test/date-expanded-year.test.js new file mode 100644 index 00000000..89768388 --- /dev/null +++ b/test/date-expanded-year.test.js @@ -0,0 +1,13 @@ +'use strict' + +const { test } = require('node:test') +const build = require('..') + +process.env.TZ = 'UTC' + +for (const iso of ['+010000-02-03T04:05:06.000Z', '-000001-02-03T04:05:06.000Z', '0000-02-03T04:05:06.000Z', '9999-02-03T04:05:06.000Z']) { + test(`preserve the whole calendar date for ${iso}`, (t) => { + const stringify = build({ type: 'string', format: 'date' }) + t.assert.equal(stringify(new Date(iso)), JSON.stringify(iso.split('T')[0])) + }) +} From 24c39ea892382e930f48b59ac122fbba11696eef Mon Sep 17 00:00:00 2001 From: xiehuanyi <74809554+xiehuanyi@users.noreply.github.com> Date: Thu, 22 Oct 2026 06:07:08 +0300 Subject: [PATCH 2/2] Fix expanded-year time serialization and consolidate date tests --- lib/serializer.js | 2 +- test/date-expanded-year.test.js | 13 ------------- test/date.test.js | 12 ++++++++++++ 3 files changed, 13 insertions(+), 14 deletions(-) delete mode 100644 test/date-expanded-year.test.js diff --git a/lib/serializer.js b/lib/serializer.js index b58ca483..acd3cdc4 100644 --- a/lib/serializer.js +++ b/lib/serializer.js @@ -119,7 +119,7 @@ module.exports = class Serializer { asTime (date) { if (date === null) return '""' if (date instanceof Date) { - return '"' + new Date(date.getTime() - (date.getTimezoneOffset() * 60000)).toISOString().slice(11, 19) + '"' + return '"' + new Date(date.getTime() - (date.getTimezoneOffset() * 60000)).toISOString().slice(-13, -5) + '"' } if (typeof date === 'string') { return asString(date) diff --git a/test/date-expanded-year.test.js b/test/date-expanded-year.test.js deleted file mode 100644 index 89768388..00000000 --- a/test/date-expanded-year.test.js +++ /dev/null @@ -1,13 +0,0 @@ -'use strict' - -const { test } = require('node:test') -const build = require('..') - -process.env.TZ = 'UTC' - -for (const iso of ['+010000-02-03T04:05:06.000Z', '-000001-02-03T04:05:06.000Z', '0000-02-03T04:05:06.000Z', '9999-02-03T04:05:06.000Z']) { - test(`preserve the whole calendar date for ${iso}`, (t) => { - const stringify = build({ type: 'string', format: 'date' }) - t.assert.equal(stringify(new Date(iso)), JSON.stringify(iso.split('T')[0])) - }) -} diff --git a/test/date.test.js b/test/date.test.js index 79f21b3d..baf874e4 100644 --- a/test/date.test.js +++ b/test/date.test.js @@ -6,6 +6,18 @@ const build = require('..') process.env.TZ = 'UTC' +for (const iso of ['+010000-02-03T04:05:06.000Z', '-000001-02-03T04:05:06.000Z', '0000-02-03T04:05:06.000Z', '9999-02-03T04:05:06.000Z']) { + test(`preserve the whole calendar date for ${iso}`, (t) => { + const stringify = build({ type: 'string', format: 'date' }) + t.assert.equal(stringify(new Date(iso)), JSON.stringify(iso.split('T')[0])) + }) + + test(`preserve the time for ${iso}`, (t) => { + const stringify = build({ type: 'string', format: 'time' }) + t.assert.equal(stringify(new Date(iso)), '"04:05:06"') + }) +} + test('render a date in a string as JSON', (t) => { t.plan(2)