2023-01-03 19:55:46 +00:00
|
|
|
// Copyright 2021 Signal Messenger, LLC
|
2021-03-22 21:08:52 +00:00
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2022-01-26 23:05:26 +00:00
|
|
|
import type { Moment } from 'moment';
|
|
|
|
import moment from 'moment';
|
|
|
|
import type { LocalizerType } from '../types/Util';
|
|
|
|
import * as log from '../logging/log';
|
|
|
|
import { DAY, HOUR, MINUTE, MONTH, WEEK } from './durations';
|
|
|
|
|
|
|
|
const MAX_FORMAT_STRING_LENGTH = 50;
|
|
|
|
|
|
|
|
type RawTimestamp = Readonly<number | Date | Moment>;
|
2021-09-24 17:01:46 +00:00
|
|
|
|
2021-03-22 21:08:52 +00:00
|
|
|
export function isMoreRecentThan(timestamp: number, delta: number): boolean {
|
|
|
|
return timestamp > Date.now() - delta;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function isOlderThan(timestamp: number, delta: number): boolean {
|
|
|
|
return timestamp <= Date.now() - delta;
|
|
|
|
}
|
2021-08-23 22:45:11 +00:00
|
|
|
|
|
|
|
export function isInPast(timestamp: number): boolean {
|
|
|
|
return isOlderThan(timestamp, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function isInFuture(timestamp: number): boolean {
|
|
|
|
return isMoreRecentThan(timestamp, 0);
|
|
|
|
}
|
2021-09-24 17:01:46 +00:00
|
|
|
|
|
|
|
export function toDayMillis(timestamp: number): number {
|
2022-01-26 23:05:26 +00:00
|
|
|
return timestamp - (timestamp % DAY);
|
|
|
|
}
|
|
|
|
|
2022-02-02 18:27:44 +00:00
|
|
|
export const isSameDay = (a: RawTimestamp, b: RawTimestamp): boolean =>
|
2022-01-26 23:05:26 +00:00
|
|
|
moment(a).isSame(b, 'day');
|
|
|
|
|
2022-02-02 18:27:44 +00:00
|
|
|
export const isToday = (rawTimestamp: RawTimestamp): boolean =>
|
2022-01-26 23:05:26 +00:00
|
|
|
isSameDay(rawTimestamp, Date.now());
|
|
|
|
|
|
|
|
const isYesterday = (rawTimestamp: RawTimestamp): boolean =>
|
|
|
|
isSameDay(rawTimestamp, moment().subtract(1, 'day'));
|
|
|
|
|
|
|
|
// This sanitization is probably unnecessary, but we do it just in case someone translates
|
|
|
|
// a super long format string and causes performance issues.
|
|
|
|
function sanitizeFormatString(
|
|
|
|
rawFormatString: string,
|
|
|
|
fallback: string
|
|
|
|
): string {
|
|
|
|
if (rawFormatString.length > MAX_FORMAT_STRING_LENGTH) {
|
|
|
|
log.error(
|
|
|
|
`Format string ${JSON.stringify(
|
|
|
|
rawFormatString
|
|
|
|
)} is too long. Falling back to ${fallback}`
|
|
|
|
);
|
|
|
|
return fallback;
|
|
|
|
}
|
|
|
|
return rawFormatString;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function formatDateTimeShort(
|
|
|
|
i18n: LocalizerType,
|
|
|
|
rawTimestamp: RawTimestamp
|
|
|
|
): string {
|
|
|
|
const timestamp = rawTimestamp.valueOf();
|
|
|
|
|
|
|
|
const now = Date.now();
|
|
|
|
const diff = now - timestamp;
|
|
|
|
|
2022-02-23 18:15:15 +00:00
|
|
|
if (diff < HOUR || isToday(timestamp)) {
|
2022-03-03 20:23:10 +00:00
|
|
|
return formatTime(i18n, rawTimestamp, now);
|
2022-01-26 23:05:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const m = moment(timestamp);
|
|
|
|
|
|
|
|
if (diff < WEEK && m.isSame(now, 'month')) {
|
|
|
|
return m.format('ddd');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m.isSame(now, 'year')) {
|
|
|
|
return m.format(i18n('timestampFormat_M') || 'MMM D');
|
|
|
|
}
|
|
|
|
|
|
|
|
return m.format('ll');
|
|
|
|
}
|
|
|
|
|
|
|
|
export function formatDateTimeLong(
|
|
|
|
i18n: LocalizerType,
|
|
|
|
rawTimestamp: RawTimestamp
|
|
|
|
): string {
|
|
|
|
let rawFormatString: string;
|
|
|
|
if (isToday(rawTimestamp)) {
|
|
|
|
rawFormatString = i18n('timestampFormat__long__today');
|
|
|
|
} else if (isYesterday(rawTimestamp)) {
|
|
|
|
rawFormatString = i18n('timestampFormat__long__yesterday');
|
|
|
|
} else {
|
|
|
|
rawFormatString = 'lll';
|
|
|
|
}
|
|
|
|
const formatString = sanitizeFormatString(rawFormatString, 'lll');
|
|
|
|
|
|
|
|
return moment(rawTimestamp).format(formatString);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function formatTime(
|
|
|
|
i18n: LocalizerType,
|
2022-03-03 20:23:10 +00:00
|
|
|
rawTimestamp: RawTimestamp,
|
2022-05-06 19:02:44 +00:00
|
|
|
now: RawTimestamp,
|
|
|
|
isRelativeTime?: boolean
|
2022-01-26 23:05:26 +00:00
|
|
|
): string {
|
|
|
|
const timestamp = rawTimestamp.valueOf();
|
2022-03-03 20:23:10 +00:00
|
|
|
const diff = now.valueOf() - timestamp;
|
2022-01-26 23:05:26 +00:00
|
|
|
|
|
|
|
if (diff < MINUTE) {
|
|
|
|
return i18n('justNow');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (diff < HOUR) {
|
|
|
|
return i18n('minutesAgo', [Math.floor(diff / MINUTE).toString()]);
|
|
|
|
}
|
|
|
|
|
2022-05-06 19:02:44 +00:00
|
|
|
if (isRelativeTime) {
|
|
|
|
return i18n('hoursAgo', [Math.floor(diff / HOUR).toString()]);
|
|
|
|
}
|
|
|
|
|
2022-02-23 18:15:15 +00:00
|
|
|
return new Date(timestamp).toLocaleTimeString([], {
|
|
|
|
hour: 'numeric',
|
|
|
|
minute: '2-digit',
|
|
|
|
});
|
2022-01-26 23:05:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export function formatDate(
|
|
|
|
i18n: LocalizerType,
|
|
|
|
rawTimestamp: RawTimestamp
|
|
|
|
): string {
|
|
|
|
if (isToday(rawTimestamp)) {
|
|
|
|
return i18n('today');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isYesterday(rawTimestamp)) {
|
|
|
|
return i18n('yesterday');
|
|
|
|
}
|
|
|
|
|
|
|
|
const m = moment(rawTimestamp);
|
|
|
|
|
2023-01-05 22:43:33 +00:00
|
|
|
const rawFormatString =
|
2022-01-26 23:05:26 +00:00
|
|
|
Math.abs(m.diff(Date.now())) < 6 * MONTH
|
2023-01-05 22:43:33 +00:00
|
|
|
? i18n('TimelineDateHeader--date-in-last-6-months')
|
|
|
|
: i18n('TimelineDateHeader--date-older-than-6-months');
|
2022-01-26 23:05:26 +00:00
|
|
|
const formatString = sanitizeFormatString(rawFormatString, 'LL');
|
|
|
|
|
|
|
|
return m.format(formatString);
|
2021-09-24 17:01:46 +00:00
|
|
|
}
|