blob: d95d1ac88fd681ac4f8016fed607333be342d1e6 [file] [log] [blame]
idillon9e542ca2022-12-15 17:54:07 -05001/*
2 * Copyright (C) 2022 Savoir-faire Linux Inc.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Affero General Public License as
6 * published by the Free Software Foundation; either version 3 of the
7 * License, or (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU Affero General Public License for more details.
13 *
14 * You should have received a copy of the GNU Affero General Public
15 * License along with this program. If not, see
16 * <https://www.gnu.org/licenses/>.
17 */
18import dayjs, { Dayjs } from 'dayjs';
19import { Duration } from 'dayjs/plugin/duration';
20import { i18n } from 'i18next';
21
22export const formatTime = (time: Dayjs, i18n: i18n) => {
23 return dayjs(time).locale(i18n.language).format('LT');
24};
25
26export const formatRelativeDate = (time: Dayjs, i18n: i18n) => {
27 if (time.isToday()) {
28 return new Intl.RelativeTimeFormat(i18n.language, { numeric: 'auto' }).format(0, 'day');
29 } else if (time.isYesterday()) {
30 return new Intl.RelativeTimeFormat(i18n.language, { numeric: 'auto' }).format(-1, 'day');
31 } else {
32 return dayjs(time).locale(i18n.language).format('L');
33 }
34};
35
36export const formatCallDuration = (duration: Duration) => {
37 const minutes = Math.floor(duration.asMinutes()).toString().padStart(2, '0');
38 const seconds = duration.format('ss');
39 return `${minutes}:${seconds}`;
40};