signal-desktop/ts/util/timestampLongUtils.ts

29 lines
593 B
TypeScript
Raw Normal View History

2021-04-09 16:19:38 +00:00
// Copyright 2021 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
2021-07-13 18:54:53 +00:00
import Long from 'long';
import { normalizeNumber } from './normalizeNumber';
2021-04-09 16:19:38 +00:00
export function getSafeLongFromTimestamp(timestamp = 0): Long {
if (timestamp >= Number.MAX_SAFE_INTEGER) {
2021-07-13 18:54:53 +00:00
return Long.MAX_VALUE;
2021-04-09 16:19:38 +00:00
}
2021-07-13 18:54:53 +00:00
return Long.fromNumber(timestamp);
2021-04-09 16:19:38 +00:00
}
2021-07-13 18:54:53 +00:00
export function getTimestampFromLong(value?: Long | number | null): number {
2021-04-09 16:19:38 +00:00
if (!value) {
return 0;
}
2021-07-13 18:54:53 +00:00
const num = normalizeNumber(value);
2021-04-09 16:19:38 +00:00
if (num >= Number.MAX_SAFE_INTEGER) {
return Number.MAX_SAFE_INTEGER;
}
return num;
}