correctly handle time protobuf parsing

Signed-off-by: Libo Zeng <liboz@google.com>
This commit is contained in:
Libo Zeng
2022-01-09 18:46:38 -05:00
parent 88a9de34e9
commit 7196378187
2 changed files with 25 additions and 9 deletions

View File

@@ -1,4 +1,5 @@
import HumanizeDuration from 'humanize-duration';
import HumanizeDuration from "humanize-duration";
import { k8s } from "../proto/proto";
/**
* A simple humanizer to merely differentiate between months and minutes.
@@ -21,13 +22,27 @@ const shortEnglishHumanizer = HumanizeDuration.humanizer({
});
/**
* @param epochtimestampMs an epoch timestamp value (in ms)
* @returns a "humanized" duration from now
*
*/
export default function fromNow(epochtimestampMs: number) {
const diff = Date.now() - new Date(epochtimestampMs).getTime();
return formatDuration(diff);
* @param epochtimestampMs an epoch timestamp value (in ms)
* @returns a "humanized" duration from now
*
*/
export default function fromNow(
epochtimestampMs: number | string | k8s.io.apimachinery.pkg.apis.meta.v1.ITime
) {
const diff = Date.now() - safeParseTimeToDate(epochtimestampMs).getTime();
return formatDuration(diff);
}
export function safeParseTimeToDate(
epochtimestampMs: number | string | k8s.io.apimachinery.pkg.apis.meta.v1.ITime
) {
if (
typeof epochtimestampMs !== "number" &&
typeof epochtimestampMs !== "string"
) {
epochtimestampMs = Number(epochtimestampMs.seconds) * 1000;
}
return new Date(epochtimestampMs);
}