Add fromSeconds
This commit is contained in:
@@ -4,6 +4,7 @@ import {
|
|||||||
DateTime,
|
DateTime,
|
||||||
DateWithNanoseconds,
|
DateWithNanoseconds,
|
||||||
fromRfc3339WithNanoseconds,
|
fromRfc3339WithNanoseconds,
|
||||||
|
fromSeconds,
|
||||||
toRfc3339WithNanoseconds,
|
toRfc3339WithNanoseconds,
|
||||||
toSeconds,
|
toSeconds,
|
||||||
} from "./dates";
|
} from "./dates";
|
||||||
@@ -61,6 +62,62 @@ describe("dates", () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe("fromSeconds", () => {
|
||||||
|
it("works", () => {
|
||||||
|
{
|
||||||
|
const date = fromSeconds(1608029846);
|
||||||
|
expect(date).toEqual(fromRfc3339WithNanoseconds("2020-12-15T10:57:26Z"));
|
||||||
|
}
|
||||||
|
{
|
||||||
|
const date = fromSeconds(1608029846, 0);
|
||||||
|
expect(date).toEqual(fromRfc3339WithNanoseconds("2020-12-15T10:57:26Z"));
|
||||||
|
}
|
||||||
|
{
|
||||||
|
const date = fromSeconds(1608029846, 1);
|
||||||
|
expect(date).toEqual(fromRfc3339WithNanoseconds("2020-12-15T10:57:26.000000001Z"));
|
||||||
|
}
|
||||||
|
{
|
||||||
|
const date = fromSeconds(1608029846, 10);
|
||||||
|
expect(date).toEqual(fromRfc3339WithNanoseconds("2020-12-15T10:57:26.000000010Z"));
|
||||||
|
}
|
||||||
|
{
|
||||||
|
const date = fromSeconds(1608029846, 100);
|
||||||
|
expect(date).toEqual(fromRfc3339WithNanoseconds("2020-12-15T10:57:26.000000100Z"));
|
||||||
|
}
|
||||||
|
{
|
||||||
|
const date = fromSeconds(1608029846, 1000);
|
||||||
|
expect(date).toEqual(fromRfc3339WithNanoseconds("2020-12-15T10:57:26.000001000Z"));
|
||||||
|
}
|
||||||
|
{
|
||||||
|
const date = fromSeconds(1608029846, 10000);
|
||||||
|
expect(date).toEqual(fromRfc3339WithNanoseconds("2020-12-15T10:57:26.000010000Z"));
|
||||||
|
}
|
||||||
|
{
|
||||||
|
const date = fromSeconds(1608029846, 100000);
|
||||||
|
expect(date).toEqual(fromRfc3339WithNanoseconds("2020-12-15T10:57:26.000100000Z"));
|
||||||
|
}
|
||||||
|
{
|
||||||
|
const date = fromSeconds(1608029846, 1000000);
|
||||||
|
expect(date).toEqual(fromRfc3339WithNanoseconds("2020-12-15T10:57:26.001000000Z"));
|
||||||
|
}
|
||||||
|
{
|
||||||
|
const date = fromSeconds(1608029846, 10000000);
|
||||||
|
expect(date).toEqual(fromRfc3339WithNanoseconds("2020-12-15T10:57:26.010000000Z"));
|
||||||
|
}
|
||||||
|
{
|
||||||
|
const date = fromSeconds(1608029846, 100000000);
|
||||||
|
expect(date).toEqual(fromRfc3339WithNanoseconds("2020-12-15T10:57:26.100000000Z"));
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
it("throws for nanos out of range", () => {
|
||||||
|
expect(() => fromSeconds(1608029846, 1000000000)).toThrow();
|
||||||
|
expect(() => fromSeconds(1608029846, -1)).toThrow();
|
||||||
|
expect(() => fromSeconds(1608029846, 1.2)).toThrow();
|
||||||
|
expect(() => fromSeconds(1608029846, NaN)).toThrow();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
describe("toSeconds", () => {
|
describe("toSeconds", () => {
|
||||||
it("works", () => {
|
it("works", () => {
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
import { fromRfc3339 } from "@cosmjs/encoding";
|
import { fromRfc3339 } from "@cosmjs/encoding";
|
||||||
|
import { Uint32 } from "@cosmjs/math";
|
||||||
import { ReadonlyDate } from "readonly-date";
|
import { ReadonlyDate } from "readonly-date";
|
||||||
|
|
||||||
export interface ReadonlyDateWithNanoseconds extends ReadonlyDate {
|
export interface ReadonlyDateWithNanoseconds extends ReadonlyDate {
|
||||||
@@ -25,6 +26,16 @@ export function toRfc3339WithNanoseconds(dateTime: ReadonlyDateWithNanoseconds):
|
|||||||
return `${millisecondIso.slice(0, -1)}${nanoseconds.padStart(6, "0")}Z`;
|
return `${millisecondIso.slice(0, -1)}${nanoseconds.padStart(6, "0")}Z`;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function fromSeconds(seconds: number, nanos = 0): DateWithNanoseconds {
|
||||||
|
const checkedNanos = new Uint32(nanos).toNumber();
|
||||||
|
if (checkedNanos > 999_999_999) {
|
||||||
|
throw new Error("Nano seconds must not exceed 999999999");
|
||||||
|
}
|
||||||
|
const out: DateWithNanoseconds = new Date(seconds * 1000 + Math.floor(checkedNanos / 1000000));
|
||||||
|
out.nanoseconds = checkedNanos % 1000000;
|
||||||
|
return out;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Caclulates the UNIX timestamp in seconds as well as the nanoseconds after the given second.
|
* Caclulates the UNIX timestamp in seconds as well as the nanoseconds after the given second.
|
||||||
*
|
*
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ export {
|
|||||||
DateTime,
|
DateTime,
|
||||||
ReadonlyDateWithNanoseconds,
|
ReadonlyDateWithNanoseconds,
|
||||||
fromRfc3339WithNanoseconds,
|
fromRfc3339WithNanoseconds,
|
||||||
|
fromSeconds,
|
||||||
toRfc3339WithNanoseconds,
|
toRfc3339WithNanoseconds,
|
||||||
toSeconds,
|
toSeconds,
|
||||||
} from "./dates";
|
} from "./dates";
|
||||||
|
|||||||
Reference in New Issue
Block a user