Skip to content

Commit 338a529

Browse files
authored
Merge pull request #99 from hildjj/trim-date
Trim fractional seconds from tag 0 if not needed, so that the output matches the example in RFC8949.
2 parents 50d1fdc + 56f3d61 commit 338a529

File tree

2 files changed

+3
-2
lines changed

2 files changed

+3
-2
lines changed

src/types.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,8 @@ registerEncoder(Date,
138138
case TAG.DATE_EPOCH:
139139
return [opts.dateTag, obj.valueOf() / 1000];
140140
case TAG.DATE_STRING:
141-
return [opts.dateTag, obj.toISOString()];
141+
// Remove fractional seconds if not needed, to match example in 8949
142+
return [opts.dateTag, obj.toISOString().replace(/\.000Z$/, 'Z')];
142143
case TAG.DATE_EPOCH_DAYS:
143144
return [opts.dateTag, Math.floor(obj.valueOf() / DAY_MS)];
144145
case TAG.DATE_FULL:

test/encoder.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,7 @@ test('ignoreGlobalTags', () => {
362362

363363
test('dateTag', () => {
364364
testAll([
365-
[new Date(1363896240000), '', '0xc07818323031332d30332d32315432303a30343a30302e3030305a'],
365+
[new Date(1363896240000), '', 'c074323031332d30332d32315432303a30343a30305a'],
366366
], {dateTag: TAG.DATE_STRING});
367367

368368
testAll([

0 commit comments

Comments
 (0)