Let fromRfc3339 return mutable Date type

This commit is contained in:
Simon Warta 2021-02-12 14:37:24 +01:00
parent 7e83910617
commit da9459553e
3 changed files with 7 additions and 6 deletions

View File

@ -57,6 +57,9 @@
### Changed
- @cosmjs/encoding: Change return type of `fromRfc3339` from `ReadonlyDate` to
`Date` as the caller becomes the owner of the object and can safely mutate it
in any way.
- @cosmjs/launchpad-ledger: Renamed to @cosmjs/ledger-amino.
- @cosmjs/ledger-amino: `LedgerSigner.sign` method renamed `signAmino`.

View File

@ -7,7 +7,7 @@ function padded(integer: number, length = 2): string {
return filled.substring(filled.length - length);
}
export function fromRfc3339(str: string): ReadonlyDate {
export function fromRfc3339(str: string): Date {
const matches = rfc3339Matcher.exec(str);
if (!matches) {
throw new Error("Date string is not in RFC3339 format");
@ -40,9 +40,8 @@ export function fromRfc3339(str: string): ReadonlyDate {
const tzOffset = tzOffsetSign * (tzOffsetHours * 60 + tzOffsetMinutes) * 60; // seconds
return new ReadonlyDate(
ReadonlyDate.UTC(year, month - 1, day, hour, minute, second, milliSeconds) - tzOffset * 1000,
);
const timestamp = Date.UTC(year, month - 1, day, hour, minute, second, milliSeconds) - tzOffset * 1000;
return new Date(timestamp);
}
export function toRfc3339(date: Date | ReadonlyDate): string {

View File

@ -12,8 +12,7 @@ export interface DateWithNanoseconds extends Date {
}
export function fromRfc3339WithNanoseconds(dateTimeString: string): DateWithNanoseconds {
// fromRfc3339 should give the caller a regular Date directly
const out: DateWithNanoseconds = new Date(fromRfc3339(dateTimeString).getTime());
const out: DateWithNanoseconds = fromRfc3339(dateTimeString);
const nanosecondsMatch = dateTimeString.match(/\.(\d+)Z$/);
const nanoseconds = nanosecondsMatch ? nanosecondsMatch[1].slice(3) : "";
out.nanoseconds = parseInt(nanoseconds.padEnd(6, "0"), 10);