Skip to content

Commit 2f9ed77

Browse files
fix: bug with encoding time types
1 parent 23971b3 commit 2f9ed77

7 files changed

Lines changed: 19 additions & 6 deletions

File tree

CHANGELOG.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
# Changelog
22

3+
## [9.0.2]
4+
5+
- Fix bug when encoding `DATE` values having a year less than 1000.
6+
- Fix bug when encoding `DATE-TIME` values having a year less than 1000.
7+
- Fix bug when encoding `TIME-OF-DAY` values having hours, minutes, or seconds
8+
that are single-digit.
9+
- Fix bug when encoding `UTCTime` values having a year less than 1000.
10+
- Fix bug when encoding `GeneralizedTime` values having a year less than 1000.
11+
312
## [9.0.1]
413

514
- Fix types export

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,5 +48,5 @@
4848
"test": "node --test"
4949
},
5050
"types": "./dist/index.d.mts",
51-
"version": "9.0.1"
51+
"version": "9.0.2"
5252
}

source/codecs/x690/encoders/encodeDate.mts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ function encodeDate (date: DATE): Uint8Array {
1212
}
1313
// NOTE: Using .toISOString() will not work, because it is in UTC time.
1414
return convertTextToBytes(
15-
date.getFullYear().toString()
15+
date.getFullYear().toString().padStart(4, "0")
1616
+ (date.getMonth() + 1).toString().padStart(2, "0")
1717
+ date.getDate().toString().padStart(2, "0"),
1818
);

source/codecs/x690/encoders/encodeDateTime.mts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ function encodeDateTime (value: DATE_TIME): Uint8Array {
1212
}
1313
// NOTE: Using .toISOString() will not work, because it is in UTC time.
1414
return convertTextToBytes(
15-
value.getFullYear().toString()
15+
value.getFullYear().toString().padStart(4, "0")
1616
+ (value.getMonth() + 1).toString().padStart(2, "0")
1717
+ value.getDate().toString().padStart(2, "0")
1818
+ value.getHours().toString().padStart(2, "0")

source/codecs/x690/encoders/encodeGeneralizedTime.mts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import convertTextToBytes from "../../../utils/convertTextToBytes.mjs";
33

44
export default
55
function encodeGeneralizedTime (value: GeneralizedTime): Uint8Array {
6-
const year: string = value.getUTCFullYear().toString();
6+
const year: string = value.getUTCFullYear().toString().padStart(4, "0");
77
const month: string = (value.getUTCMonth() + 1).toString().padStart(2, "0");
88
const day: string = value.getUTCDate().toString().padStart(2, "0");
99
const hour: string = value.getUTCHours().toString().padStart(2, "0");

source/codecs/x690/encoders/encodeTimeOfDay.mts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,9 @@ import convertTextToBytes from "../../../utils/convertTextToBytes.mjs";
1313
*/
1414
export default
1515
function encodeTimeOfDay (time: TIME_OF_DAY): Uint8Array {
16-
return convertTextToBytes(`${time.getHours()}${time.getMinutes()}${time.getSeconds()}`);
16+
return convertTextToBytes(
17+
time.getHours().toString().padStart(2, "0")
18+
+ time.getMinutes().toString().padStart(2, "0")
19+
+ time.getSeconds().toString().padStart(2, "0")
20+
);
1721
}

source/codecs/x690/encoders/encodeUTCTime.mts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import convertTextToBytes from "../../../utils/convertTextToBytes.mjs";
44
export default
55
function encodeUTCTime (value: UTCTime): Uint8Array {
66
let year: string = value.getUTCFullYear().toString();
7-
year = (year.substring(year.length - 2, year.length)); // Will fail if you supply a <2 digit date.
7+
year = (year.substring(year.length - 2, year.length)).padStart(2, "0"); // Will fail if you supply a <2 digit date.
88
const month: string = (value.getUTCMonth() + 1).toString().padStart(2, "0");
99
const day: string = value.getUTCDate().toString().padStart(2, "0");
1010
const hour: string = value.getUTCHours().toString().padStart(2, "0");

0 commit comments

Comments
 (0)