Merge branch 'fix/rfc-3339-full-year'

This commit is contained in:
Simon Warta 2023-12-04 17:26:57 +01:00
commit b922729093
6 changed files with 41 additions and 41 deletions

View File

@ -6,6 +6,15 @@ and this project adheres to
## [Unreleased]
### Fixed
- @cosmjs/encoding: Ensure RFC dates between years 0001 and 0099 are parsed
correctly. ([#1516])
- @cosmjs/tendermint-rpc: Remove hacky `decodeOptionalTime()` from adaptors now
that `time.Time` dates are parsed correctly. ([#1516])
[#1516]: https://github.com/cosmos/cosmjs/pull/1516
## [0.32.0] - 2023-11-23
### Added

View File

@ -58,6 +58,27 @@ describe("RFC3339", () => {
expect(fromRfc3339("2002-10-02T11:12:13.999Z")).toEqual(new Date(Date.UTC(2002, 9, 2, 11, 12, 13, 999)));
});
it("parses dates between years 0 and 99 with and without timezones", () => {
expect(fromRfc3339("0001-01-01T00:00:00.000Z")).toEqual(
new Date(new Date(Date.UTC(1, 0, 1, 0, 0, 0, 0)).setUTCFullYear(1)),
);
expect(fromRfc3339("0000-01-01T00:00:00.000Z")).toEqual(
new Date(new Date(Date.UTC(0, 0, 1, 0, 0, 0, 0)).setUTCFullYear(0)),
);
expect(fromRfc3339("1999-01-01T00:00:00.000Z")).toEqual(
new Date(new Date(Date.UTC(1999, 0, 1, 0, 0, 0, 0)).setUTCFullYear(1999)),
);
expect(fromRfc3339("0099-01-01T00:00:00.000Z")).toEqual(
new Date(new Date(Date.UTC(99, 0, 1, 0, 0, 0, 0)).setUTCFullYear(99)),
);
expect(fromRfc3339("0010-01-01T00:00:00+01:00")).toEqual(
new Date(new Date(Date.UTC(9, 11, 31, 23, 0, 0, 0)).setUTCFullYear(9)),
);
expect(fromRfc3339("0100-01-01T00:00:00+01:00")).toEqual(
new Date(new Date(Date.UTC(99, 11, 31, 23, 0, 0, 0)).setUTCFullYear(99)),
);
});
it("parses dates with low precision fractional seconds", () => {
// 1 digit
expect(fromRfc3339("2002-10-02T11:12:13.0Z")).toEqual(new Date(Date.UTC(2002, 9, 2, 11, 12, 13, 0)));

View File

@ -40,8 +40,11 @@ export function fromRfc3339(str: string): Date {
const tzOffset = tzOffsetSign * (tzOffsetHours * 60 + tzOffsetMinutes) * 60; // seconds
const timestamp = Date.UTC(year, month - 1, day, hour, minute, second, milliSeconds) - tzOffset * 1000;
return new Date(timestamp);
const date = new Date();
date.setUTCFullYear(year, month - 1, day);
date.setUTCHours(hour, minute, second, milliSeconds);
return new Date(date.getTime() - tzOffset * 1000);
}
export function toRfc3339(date: Date | ReadonlyDate): string {

View File

@ -3,7 +3,7 @@ import { fromBase64, fromHex } from "@cosmjs/encoding";
import { JsonRpcSuccessResponse } from "@cosmjs/json-rpc";
import { assert } from "@cosmjs/utils";
import { DateWithNanoseconds, fromRfc3339WithNanoseconds } from "../../dates";
import { fromRfc3339WithNanoseconds } from "../../dates";
import { apiToBigInt, apiToSmallInt } from "../../inthelpers";
import { SubscriptionEvent } from "../../rpcclients";
import { BlockIdFlag, CommitSignature, ValidatorPubkey } from "../../types";
@ -467,22 +467,11 @@ type RpcSignature = {
readonly signature: string | null;
};
/**
* In some cases a timestamp is optional and set to the value 0 in Go.
* This can lead to strings like "0001-01-01T00:00:00Z" (see https://github.com/cosmos/cosmjs/issues/704#issuecomment-797122415).
* This decoder tries to clean up such encoding from the API and turn them
* into undefined values.
*/
function decodeOptionalTime(timestamp: string): DateWithNanoseconds | undefined {
const nonZeroTime = timestamp && !timestamp.startsWith("0001-01-01");
return nonZeroTime ? fromRfc3339WithNanoseconds(timestamp) : undefined;
}
function decodeCommitSignature(data: RpcSignature): CommitSignature {
return {
blockIdFlag: decodeBlockIdFlag(data.block_id_flag),
validatorAddress: data.validator_address ? fromHex(data.validator_address) : undefined,
timestamp: decodeOptionalTime(data.timestamp),
timestamp: data.timestamp ? fromRfc3339WithNanoseconds(data.timestamp) : undefined,
signature: data.signature ? fromBase64(data.signature) : undefined,
};
}

View File

@ -3,7 +3,7 @@ import { fromBase64, fromHex } from "@cosmjs/encoding";
import { JsonRpcSuccessResponse } from "@cosmjs/json-rpc";
import { assert } from "@cosmjs/utils";
import { DateWithNanoseconds, fromRfc3339WithNanoseconds } from "../../dates";
import { fromRfc3339WithNanoseconds } from "../../dates";
import { apiToBigInt, apiToSmallInt } from "../../inthelpers";
import { SubscriptionEvent } from "../../rpcclients";
import { BlockIdFlag, CommitSignature, ValidatorPubkey } from "../../types";
@ -464,22 +464,11 @@ type RpcSignature = {
readonly signature: string | null;
};
/**
* In some cases a timestamp is optional and set to the value 0 in Go.
* This can lead to strings like "0001-01-01T00:00:00Z" (see https://github.com/cosmos/cosmjs/issues/704#issuecomment-797122415).
* This decoder tries to clean up such encoding from the API and turn them
* into undefined values.
*/
function decodeOptionalTime(timestamp: string): DateWithNanoseconds | undefined {
const nonZeroTime = timestamp && !timestamp.startsWith("0001-01-01");
return nonZeroTime ? fromRfc3339WithNanoseconds(timestamp) : undefined;
}
function decodeCommitSignature(data: RpcSignature): CommitSignature {
return {
blockIdFlag: decodeBlockIdFlag(data.block_id_flag),
validatorAddress: data.validator_address ? fromHex(data.validator_address) : undefined,
timestamp: decodeOptionalTime(data.timestamp),
timestamp: data.timestamp ? fromRfc3339WithNanoseconds(data.timestamp) : undefined,
signature: data.signature ? fromBase64(data.signature) : undefined,
};
}

View File

@ -3,7 +3,7 @@ import { fromBase64, fromHex } from "@cosmjs/encoding";
import { JsonRpcSuccessResponse } from "@cosmjs/json-rpc";
import { assert } from "@cosmjs/utils";
import { DateWithNanoseconds, fromRfc3339WithNanoseconds } from "../../dates";
import { fromRfc3339WithNanoseconds } from "../../dates";
import { apiToBigInt, apiToSmallInt } from "../../inthelpers";
import { SubscriptionEvent } from "../../rpcclients";
import { BlockIdFlag, CommitSignature, ValidatorPubkey } from "../../types";
@ -465,22 +465,11 @@ type RpcSignature = {
readonly signature: string | null;
};
/**
* In some cases a timestamp is optional and set to the value 0 in Go.
* This can lead to strings like "0001-01-01T00:00:00Z" (see https://github.com/cosmos/cosmjs/issues/704#issuecomment-797122415).
* This decoder tries to clean up such encoding from the API and turn them
* into undefined values.
*/
function decodeOptionalTime(timestamp: string): DateWithNanoseconds | undefined {
const nonZeroTime = timestamp && !timestamp.startsWith("0001-01-01");
return nonZeroTime ? fromRfc3339WithNanoseconds(timestamp) : undefined;
}
function decodeCommitSignature(data: RpcSignature): CommitSignature {
return {
blockIdFlag: decodeBlockIdFlag(data.block_id_flag),
validatorAddress: data.validator_address ? fromHex(data.validator_address) : undefined,
timestamp: decodeOptionalTime(data.timestamp),
timestamp: data.timestamp ? fromRfc3339WithNanoseconds(data.timestamp) : undefined,
signature: data.signature ? fromBase64(data.signature) : undefined,
};
}