Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Ensure RFC dates between years 0001 and 0099 are parsed correctly #1516

Merged
merged 5 commits into from
Dec 4, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions packages/encoding/src/rfc3339.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)));
Expand Down
7 changes: 6 additions & 1 deletion packages/encoding/src/rfc3339.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,12 @@ 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;
let timestamp = Date.UTC(year, month - 1, day, hour, minute, second, milliSeconds);

// Date.UTC maps year 0-99 to 1900-1999. Ensure the correct year is set and THEN apply the offset
const date = new Date(timestamp);
timestamp = date.setUTCFullYear(year) - tzOffset * 1000;

clockworkgr marked this conversation as resolved.
Show resolved Hide resolved
return new Date(timestamp);
}

Expand Down
13 changes: 1 addition & 12 deletions packages/tendermint-rpc/src/comet38/adaptor/responses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sweet

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wanna run this past @ethanfrey who brought up the original issue that that hacky decodeOptionalTime() was supposed to solve?

signature: data.signature ? fromBase64(data.signature) : undefined,
};
}
Expand Down
13 changes: 1 addition & 12 deletions packages/tendermint-rpc/src/tendermint34/adaptor/responses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
}
Expand Down
13 changes: 1 addition & 12 deletions packages/tendermint-rpc/src/tendermint37/adaptor/responses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
}
Expand Down