2015-11-30 19:11:04 +01:00
|
|
|
/*
|
2016-01-07 05:06:39 +01:00
|
|
|
Copyright 2015, 2016 OpenMarket Ltd
|
2017-05-25 19:26:42 +02:00
|
|
|
Copyright 2017 Vector Creations Ltd
|
2022-10-18 03:54:10 +02:00
|
|
|
Copyright 2022 The Matrix.org Foundation C.I.C.
|
2015-11-30 19:11:04 +01:00
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
import { _t } from "./languageHandler";
|
2015-11-30 19:11:04 +01:00
|
|
|
|
2020-10-13 18:39:58 +02:00
|
|
|
function getDaysArray(): string[] {
|
2022-12-12 12:24:14 +01:00
|
|
|
return [_t("Sun"), _t("Mon"), _t("Tue"), _t("Wed"), _t("Thu"), _t("Fri"), _t("Sat")];
|
2017-05-23 16:16:31 +02:00
|
|
|
}
|
|
|
|
|
2020-10-13 18:39:58 +02:00
|
|
|
function getMonthsArray(): string[] {
|
2017-05-25 19:26:42 +02:00
|
|
|
return [
|
2022-12-12 12:24:14 +01:00
|
|
|
_t("Jan"),
|
|
|
|
_t("Feb"),
|
|
|
|
_t("Mar"),
|
|
|
|
_t("Apr"),
|
|
|
|
_t("May"),
|
|
|
|
_t("Jun"),
|
|
|
|
_t("Jul"),
|
|
|
|
_t("Aug"),
|
|
|
|
_t("Sep"),
|
|
|
|
_t("Oct"),
|
|
|
|
_t("Nov"),
|
|
|
|
_t("Dec"),
|
2017-05-25 19:26:42 +02:00
|
|
|
];
|
2017-05-23 16:16:31 +02:00
|
|
|
}
|
2015-11-30 19:11:04 +01:00
|
|
|
|
2020-10-13 18:39:58 +02:00
|
|
|
function pad(n: number): string {
|
2022-12-12 12:24:14 +01:00
|
|
|
return (n < 10 ? "0" : "") + n;
|
2017-05-15 01:25:12 +02:00
|
|
|
}
|
|
|
|
|
2020-10-13 18:39:58 +02:00
|
|
|
function twelveHourTime(date: Date, showSeconds = false): string {
|
2017-05-19 22:31:24 +02:00
|
|
|
let hours = date.getHours() % 12;
|
2017-05-19 23:26:24 +02:00
|
|
|
const minutes = pad(date.getMinutes());
|
2022-12-12 12:24:14 +01:00
|
|
|
const ampm = date.getHours() >= 12 ? _t("PM") : _t("AM");
|
2017-06-30 17:29:40 +02:00
|
|
|
hours = hours ? hours : 12; // convert 0 -> 12
|
2018-02-08 16:58:53 +01:00
|
|
|
if (showSeconds) {
|
|
|
|
const seconds = pad(date.getSeconds());
|
|
|
|
return `${hours}:${minutes}:${seconds}${ampm}`;
|
|
|
|
}
|
2017-05-30 17:31:16 +02:00
|
|
|
return `${hours}:${minutes}${ampm}`;
|
2017-05-18 23:00:44 +02:00
|
|
|
}
|
|
|
|
|
2020-10-13 18:39:58 +02:00
|
|
|
export function formatDate(date: Date, showTwelveHour = false): string {
|
2018-01-10 13:00:11 +01:00
|
|
|
const now = new Date();
|
|
|
|
const days = getDaysArray();
|
|
|
|
const months = getMonthsArray();
|
|
|
|
if (date.toDateString() === now.toDateString()) {
|
|
|
|
return formatTime(date, showTwelveHour);
|
|
|
|
} else if (now.getTime() - date.getTime() < 6 * 24 * 60 * 60 * 1000) {
|
|
|
|
// TODO: use standard date localize function provided in counterpart
|
2022-12-12 12:24:14 +01:00
|
|
|
return _t("%(weekDayName)s %(time)s", {
|
2018-01-10 13:00:11 +01:00
|
|
|
weekDayName: days[date.getDay()],
|
|
|
|
time: formatTime(date, showTwelveHour),
|
|
|
|
});
|
|
|
|
} else if (now.getFullYear() === date.getFullYear()) {
|
|
|
|
// TODO: use standard date localize function provided in counterpart
|
2022-12-12 12:24:14 +01:00
|
|
|
return _t("%(weekDayName)s, %(monthName)s %(day)s %(time)s", {
|
2017-05-26 18:30:02 +02:00
|
|
|
weekDayName: days[date.getDay()],
|
|
|
|
monthName: months[date.getMonth()],
|
|
|
|
day: date.getDate(),
|
2018-01-10 13:00:11 +01:00
|
|
|
time: formatTime(date, showTwelveHour),
|
2017-05-26 18:30:02 +02:00
|
|
|
});
|
2018-01-10 13:00:11 +01:00
|
|
|
}
|
|
|
|
return formatFullDate(date, showTwelveHour);
|
|
|
|
}
|
2016-08-16 16:01:01 +02:00
|
|
|
|
2020-10-13 18:39:58 +02:00
|
|
|
export function formatFullDateNoTime(date: Date): string {
|
2018-01-14 19:32:17 +01:00
|
|
|
const days = getDaysArray();
|
|
|
|
const months = getMonthsArray();
|
2022-12-12 12:24:14 +01:00
|
|
|
return _t("%(weekDayName)s, %(monthName)s %(day)s %(fullYear)s", {
|
2018-01-14 19:32:17 +01:00
|
|
|
weekDayName: days[date.getDay()],
|
|
|
|
monthName: months[date.getMonth()],
|
|
|
|
day: date.getDate(),
|
|
|
|
fullYear: date.getFullYear(),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-04-02 09:25:21 +02:00
|
|
|
export function formatFullDate(date: Date, showTwelveHour = false, showSeconds = true): string {
|
2018-01-10 13:00:11 +01:00
|
|
|
const days = getDaysArray();
|
|
|
|
const months = getMonthsArray();
|
2022-12-12 12:24:14 +01:00
|
|
|
return _t("%(weekDayName)s, %(monthName)s %(day)s %(fullYear)s %(time)s", {
|
2018-01-10 13:00:11 +01:00
|
|
|
weekDayName: days[date.getDay()],
|
|
|
|
monthName: months[date.getMonth()],
|
|
|
|
day: date.getDate(),
|
|
|
|
fullYear: date.getFullYear(),
|
2021-04-02 09:25:21 +02:00
|
|
|
time: showSeconds ? formatFullTime(date, showTwelveHour) : formatTime(date, showTwelveHour),
|
2018-01-10 13:00:11 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-03-22 17:45:44 +01:00
|
|
|
/**
|
|
|
|
* Formats dates to be compatible with attributes of a `<input type="date">`. Dates
|
|
|
|
* should be formatted like "2020-06-23" (formatted according to ISO8601)
|
|
|
|
*
|
|
|
|
* @param date The date to format.
|
|
|
|
* @returns The date string in ISO8601 format ready to be used with an `<input>`
|
|
|
|
*/
|
|
|
|
export function formatDateForInput(date: Date): string {
|
|
|
|
const year = `${date.getFullYear()}`.padStart(4, "0");
|
|
|
|
const month = `${date.getMonth() + 1}`.padStart(2, "0");
|
|
|
|
const day = `${date.getDate()}`.padStart(2, "0");
|
|
|
|
const dateInputValue = `${year}-${month}-${day}`;
|
|
|
|
return dateInputValue;
|
|
|
|
}
|
|
|
|
|
2020-10-13 18:39:58 +02:00
|
|
|
export function formatFullTime(date: Date, showTwelveHour = false): string {
|
2018-02-08 16:58:53 +01:00
|
|
|
if (showTwelveHour) {
|
|
|
|
return twelveHourTime(date, true);
|
|
|
|
}
|
2022-12-12 12:24:14 +01:00
|
|
|
return pad(date.getHours()) + ":" + pad(date.getMinutes()) + ":" + pad(date.getSeconds());
|
2018-02-08 16:58:53 +01:00
|
|
|
}
|
|
|
|
|
2020-10-13 18:39:58 +02:00
|
|
|
export function formatTime(date: Date, showTwelveHour = false): string {
|
2018-01-10 13:00:11 +01:00
|
|
|
if (showTwelveHour) {
|
|
|
|
return twelveHourTime(date);
|
|
|
|
}
|
2022-12-12 12:24:14 +01:00
|
|
|
return pad(date.getHours()) + ":" + pad(date.getMinutes());
|
2018-01-10 13:00:11 +01:00
|
|
|
}
|
2018-01-10 13:06:24 +01:00
|
|
|
|
2021-08-27 16:19:59 +02:00
|
|
|
export function formatSeconds(inSeconds: number): string {
|
2022-11-30 08:47:29 +01:00
|
|
|
const isNegative = inSeconds < 0;
|
|
|
|
inSeconds = Math.abs(inSeconds);
|
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
const hours = Math.floor(inSeconds / (60 * 60))
|
|
|
|
.toFixed(0)
|
|
|
|
.padStart(2, "0");
|
|
|
|
const minutes = Math.floor((inSeconds % (60 * 60)) / 60)
|
|
|
|
.toFixed(0)
|
|
|
|
.padStart(2, "0");
|
|
|
|
const seconds = Math.floor((inSeconds % (60 * 60)) % 60)
|
|
|
|
.toFixed(0)
|
|
|
|
.padStart(2, "0");
|
2021-08-27 16:19:59 +02:00
|
|
|
|
|
|
|
let output = "";
|
|
|
|
if (hours !== "00") output += `${hours}:`;
|
|
|
|
output += `${minutes}:${seconds}`;
|
|
|
|
|
2022-11-30 08:47:29 +01:00
|
|
|
if (isNegative) {
|
|
|
|
output = "-" + output;
|
|
|
|
}
|
|
|
|
|
2021-08-27 16:19:59 +02:00
|
|
|
return output;
|
|
|
|
}
|
|
|
|
|
2022-11-10 11:53:49 +01:00
|
|
|
export function formatTimeLeft(inSeconds: number): string {
|
|
|
|
const hours = Math.floor(inSeconds / (60 * 60)).toFixed(0);
|
|
|
|
const minutes = Math.floor((inSeconds % (60 * 60)) / 60).toFixed(0);
|
2022-12-12 12:24:14 +01:00
|
|
|
const seconds = Math.floor((inSeconds % (60 * 60)) % 60).toFixed(0);
|
2022-11-10 11:53:49 +01:00
|
|
|
|
|
|
|
if (hours !== "0") {
|
|
|
|
return _t("%(hours)sh %(minutes)sm %(seconds)ss left", {
|
|
|
|
hours,
|
|
|
|
minutes,
|
|
|
|
seconds,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (minutes !== "0") {
|
|
|
|
return _t("%(minutes)sm %(seconds)ss left", {
|
|
|
|
minutes,
|
|
|
|
seconds,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return _t("%(seconds)ss left", {
|
|
|
|
seconds,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-01-10 13:06:24 +01:00
|
|
|
const MILLIS_IN_DAY = 86400000;
|
2021-11-03 11:16:50 +01:00
|
|
|
function withinPast24Hours(prevDate: Date, nextDate: Date): boolean {
|
|
|
|
return Math.abs(prevDate.getTime() - nextDate.getTime()) <= MILLIS_IN_DAY;
|
|
|
|
}
|
|
|
|
|
|
|
|
function withinCurrentYear(prevDate: Date, nextDate: Date): boolean {
|
|
|
|
return prevDate.getFullYear() === nextDate.getFullYear();
|
|
|
|
}
|
|
|
|
|
2023-02-20 15:46:07 +01:00
|
|
|
export function wantsDateSeparator(
|
|
|
|
prevEventDate: Date | null | undefined,
|
|
|
|
nextEventDate: Date | null | undefined,
|
|
|
|
): boolean {
|
2018-01-10 13:06:24 +01:00
|
|
|
if (!nextEventDate || !prevEventDate) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// Return early for events that are > 24h apart
|
2021-11-03 11:16:50 +01:00
|
|
|
if (!withinPast24Hours(prevEventDate, nextEventDate)) {
|
2018-01-10 13:06:24 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Compare weekdays
|
|
|
|
return prevEventDate.getDay() !== nextEventDate.getDay();
|
|
|
|
}
|
2021-06-01 09:10:03 +02:00
|
|
|
|
2023-01-12 14:25:14 +01:00
|
|
|
export function formatFullDateNoDay(date: Date): string {
|
2021-06-09 13:10:52 +02:00
|
|
|
return _t("%(date)s at %(time)s", {
|
2022-12-12 12:24:14 +01:00
|
|
|
date: date.toLocaleDateString().replace(/\//g, "-"),
|
|
|
|
time: date.toLocaleTimeString().replace(/:/g, "-"),
|
2021-06-09 12:42:57 +02:00
|
|
|
});
|
2021-06-01 09:10:03 +02:00
|
|
|
}
|
2021-06-03 09:10:00 +02:00
|
|
|
|
2022-10-18 03:54:10 +02:00
|
|
|
/**
|
|
|
|
* Returns an ISO date string without textual description of the date (ie: no "Wednesday" or
|
|
|
|
* similar)
|
|
|
|
* @param date The date to format.
|
|
|
|
* @returns The date string in ISO format.
|
|
|
|
*/
|
|
|
|
export function formatFullDateNoDayISO(date: Date): string {
|
|
|
|
return date.toISOString();
|
|
|
|
}
|
|
|
|
|
2023-01-12 14:25:14 +01:00
|
|
|
export function formatFullDateNoDayNoTime(date: Date): string {
|
2022-12-12 12:24:14 +01:00
|
|
|
return date.getFullYear() + "/" + pad(date.getMonth() + 1) + "/" + pad(date.getDate());
|
2021-06-03 09:10:00 +02:00
|
|
|
}
|
2021-11-03 11:16:50 +01:00
|
|
|
|
|
|
|
export function formatRelativeTime(date: Date, showTwelveHour = false): string {
|
|
|
|
const now = new Date(Date.now());
|
|
|
|
if (withinPast24Hours(date, now)) {
|
|
|
|
return formatTime(date, showTwelveHour);
|
|
|
|
} else {
|
|
|
|
const months = getMonthsArray();
|
|
|
|
let relativeDate = `${months[date.getMonth()]} ${date.getDate()}`;
|
|
|
|
|
|
|
|
if (!withinCurrentYear(date, now)) {
|
|
|
|
relativeDate += `, ${date.getFullYear()}`;
|
|
|
|
}
|
|
|
|
return relativeDate;
|
|
|
|
}
|
|
|
|
}
|
2022-03-21 12:42:58 +01:00
|
|
|
|
2022-11-29 22:21:51 +01:00
|
|
|
const MINUTE_MS = 60000;
|
|
|
|
const HOUR_MS = MINUTE_MS * 60;
|
|
|
|
const DAY_MS = HOUR_MS * 24;
|
|
|
|
|
2022-03-21 12:42:58 +01:00
|
|
|
/**
|
|
|
|
* Formats duration in ms to human readable string
|
|
|
|
* Returns value in biggest possible unit (day, hour, min, second)
|
|
|
|
* Rounds values up until unit threshold
|
|
|
|
* ie. 23:13:57 -> 23h, 24:13:57 -> 1d, 44:56:56 -> 2d
|
|
|
|
*/
|
|
|
|
export function formatDuration(durationMs: number): string {
|
|
|
|
if (durationMs >= DAY_MS) {
|
2022-12-12 12:24:14 +01:00
|
|
|
return _t("%(value)sd", { value: Math.round(durationMs / DAY_MS) });
|
2022-03-21 12:42:58 +01:00
|
|
|
}
|
|
|
|
if (durationMs >= HOUR_MS) {
|
2022-12-12 12:24:14 +01:00
|
|
|
return _t("%(value)sh", { value: Math.round(durationMs / HOUR_MS) });
|
2022-03-21 12:42:58 +01:00
|
|
|
}
|
|
|
|
if (durationMs >= MINUTE_MS) {
|
2022-12-12 12:24:14 +01:00
|
|
|
return _t("%(value)sm", { value: Math.round(durationMs / MINUTE_MS) });
|
2022-03-21 12:42:58 +01:00
|
|
|
}
|
2022-12-12 12:24:14 +01:00
|
|
|
return _t("%(value)ss", { value: Math.round(durationMs / 1000) });
|
2022-03-21 12:42:58 +01:00
|
|
|
}
|
2022-11-29 22:21:51 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Formats duration in ms to human readable string
|
|
|
|
* Returns precise value down to the nearest second
|
|
|
|
* ie. 23:13:57 -> 23h 13m 57s, 44:56:56 -> 1d 20h 56m 56s
|
|
|
|
*/
|
|
|
|
export function formatPreciseDuration(durationMs: number): string {
|
|
|
|
const days = Math.floor(durationMs / DAY_MS);
|
|
|
|
const hours = Math.floor((durationMs % DAY_MS) / HOUR_MS);
|
|
|
|
const minutes = Math.floor((durationMs % HOUR_MS) / MINUTE_MS);
|
|
|
|
const seconds = Math.floor((durationMs % MINUTE_MS) / 1000);
|
|
|
|
|
|
|
|
if (days > 0) {
|
2022-12-12 12:24:14 +01:00
|
|
|
return _t("%(days)sd %(hours)sh %(minutes)sm %(seconds)ss", { days, hours, minutes, seconds });
|
2022-11-29 22:21:51 +01:00
|
|
|
}
|
|
|
|
if (hours > 0) {
|
2022-12-12 12:24:14 +01:00
|
|
|
return _t("%(hours)sh %(minutes)sm %(seconds)ss", { hours, minutes, seconds });
|
2022-11-29 22:21:51 +01:00
|
|
|
}
|
|
|
|
if (minutes > 0) {
|
2022-12-12 12:24:14 +01:00
|
|
|
return _t("%(minutes)sm %(seconds)ss", { minutes, seconds });
|
2022-11-29 22:21:51 +01:00
|
|
|
}
|
2022-12-12 12:24:14 +01:00
|
|
|
return _t("%(value)ss", { value: seconds });
|
2022-11-29 22:21:51 +01:00
|
|
|
}
|
2023-02-02 22:39:23 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Formats a timestamp to a short date
|
|
|
|
* (eg 25/12/22 in uk locale)
|
|
|
|
* localised by system locale
|
|
|
|
* @param timestamp - epoch timestamp
|
|
|
|
* @returns {string} formattedDate
|
|
|
|
*/
|
|
|
|
export const formatLocalDateShort = (timestamp: number): string =>
|
|
|
|
new Intl.DateTimeFormat(
|
|
|
|
undefined, // locales
|
|
|
|
{ day: "2-digit", month: "2-digit", year: "2-digit" },
|
|
|
|
).format(timestamp);
|