WARNING: THIS SITE IS A MIRROR OF GITHUB.COM / IT CANNOT LOGIN OR REGISTER ACCOUNTS / THE CONTENTS ARE PROVIDED AS-IS / THIS SITE ASSUMES NO RESPONSIBILITY FOR ANY DISPLAYED CONTENT OR LINKS / IF YOU FOUND SOMETHING MAY NOT GOOD FOR EVERYONE, CONTACT ADMIN AT ilovescratch@foxmail.com
Skip to content

Commit 0487c9d

Browse files
committed
Handle dates from before the epoch.
1 parent 8e6fc3d commit 0487c9d

File tree

4 files changed

+19
-7
lines changed

4 files changed

+19
-7
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# @digitalbazaar/cborld ChangeLog
22

3+
## 8.0.2 - 2025-mm-dd
4+
5+
### Fixed
6+
- Properly encode/decode dates from before the epoch.
7+
38
## 8.0.1 - 2025-05-21
49

510
### Fixed

lib/codecs/ValueEncoder.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*!
2-
* Copyright (c) 2024 Digital Bazaar, Inc. All rights reserved.
2+
* Copyright (c) 2024-2025 Digital Bazaar, Inc. All rights reserved.
33
*/
44
import {
55
bytesFromInt,
@@ -37,7 +37,8 @@ export class ValueEncoder extends CborldEncoder {
3737
const bytes = toBytes({intValue});
3838
return new Token(Type.bytes, bytes);
3939
}
40-
return new Token(Type.uint, intValue);
40+
const tokenType = intValue >= 0 ? Type.uint : Type.negint;
41+
return new Token(tokenType, intValue);
4142
}
4243

4344
static createEncoder({value, converter, termInfo, termType} = {}) {

lib/codecs/XsdDateEncoder.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*!
2-
* Copyright (c) 2021-2024 Digital Bazaar, Inc. All rights reserved.
2+
* Copyright (c) 2021-2025 Digital Bazaar, Inc. All rights reserved.
33
*/
44
import {Token, Type} from 'cborg';
55
import {CborldEncoder} from './CborldEncoder.js';
@@ -21,7 +21,8 @@ export class XsdDateEncoder extends CborldEncoder {
2121
// compression would be lossy, do not compress
2222
return new Token(Type.string, value);
2323
}
24-
return new Token(Type.uint, secondsSinceEpoch);
24+
const tokenType = secondsSinceEpoch >= 0 ? Type.uint : Type.negint;
25+
return new Token(tokenType, secondsSinceEpoch);
2526
}
2627

2728
static createEncoder({value} = {}) {

lib/codecs/XsdDateTimeEncoder.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*!
2-
* Copyright (c) 2021-2024 Digital Bazaar, Inc. All rights reserved.
2+
* Copyright (c) 2021-2025 Digital Bazaar, Inc. All rights reserved.
33
*/
44
import {Token, Type} from 'cborg';
55
import {CborldEncoder} from './CborldEncoder.js';
@@ -14,7 +14,8 @@ export class XsdDateTimeEncoder extends CborldEncoder {
1414
encode() {
1515
const {value, parsed} = this;
1616
const secondsSinceEpoch = Math.floor(parsed / 1000);
17-
const secondsToken = new Token(Type.uint, secondsSinceEpoch);
17+
const tokenType = _getNumberTokenType(secondsSinceEpoch);
18+
const secondsToken = new Token(tokenType, secondsSinceEpoch);
1819
const millisecondIndex = value.indexOf('.');
1920
if(millisecondIndex === -1) {
2021
const expectedDate = new Date(
@@ -38,7 +39,7 @@ export class XsdDateTimeEncoder extends CborldEncoder {
3839
// compress with subsecond precision
3940
const entries = [
4041
secondsToken,
41-
new Token(Type.uint, milliseconds)
42+
new Token(_getNumberTokenType(milliseconds), milliseconds)
4243
];
4344
return [new Token(Type.array, entries.length), entries];
4445
}
@@ -56,3 +57,7 @@ export class XsdDateTimeEncoder extends CborldEncoder {
5657
return new XsdDateTimeEncoder({value, parsed});
5758
}
5859
}
60+
61+
function _getNumberTokenType(number) {
62+
return number >= 0 ? Type.uint : Type.negint;
63+
}

0 commit comments

Comments
 (0)