2015-12-01 02:11:04 +08:00
|
|
|
/*
|
2016-01-07 12:06:39 +08:00
|
|
|
Copyright 2015, 2016 OpenMarket Ltd
|
2017-05-26 01:26:42 +08:00
|
|
|
Copyright 2017 Vector Creations Ltd
|
2015-12-01 02:11:04 +08: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.
|
|
|
|
*/
|
|
|
|
|
2017-05-25 18:39:08 +08:00
|
|
|
import { _t } from './languageHandler';
|
2015-12-01 02:11:04 +08:00
|
|
|
|
2020-10-14 00:39:58 +08:00
|
|
|
function getDaysArray(): string[] {
|
2017-05-26 01:26:42 +08:00
|
|
|
return [
|
|
|
|
_t('Sun'),
|
|
|
|
_t('Mon'),
|
|
|
|
_t('Tue'),
|
|
|
|
_t('Wed'),
|
|
|
|
_t('Thu'),
|
|
|
|
_t('Fri'),
|
|
|
|
_t('Sat'),
|
|
|
|
];
|
2017-05-23 22:16:31 +08:00
|
|
|
}
|
|
|
|
|
2020-10-14 00:39:58 +08:00
|
|
|
function getMonthsArray(): string[] {
|
2017-05-26 01:26:42 +08:00
|
|
|
return [
|
|
|
|
_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-23 22:16:31 +08:00
|
|
|
}
|
2015-12-01 02:11:04 +08:00
|
|
|
|
2020-10-14 00:39:58 +08:00
|
|
|
function pad(n: number): string {
|
2017-05-15 07:25:12 +08:00
|
|
|
return (n < 10 ? '0' : '') + n;
|
|
|
|
}
|
|
|
|
|
2020-10-14 00:39:58 +08:00
|
|
|
function twelveHourTime(date: Date, showSeconds = false): string {
|
2017-05-20 04:31:24 +08:00
|
|
|
let hours = date.getHours() % 12;
|
2017-05-20 05:26:24 +08:00
|
|
|
const minutes = pad(date.getMinutes());
|
2017-07-03 23:25:03 +08:00
|
|
|
const ampm = date.getHours() >= 12 ? _t('PM') : _t('AM');
|
2017-06-30 23:29:40 +08:00
|
|
|
hours = hours ? hours : 12; // convert 0 -> 12
|
2018-02-08 23:58:53 +08:00
|
|
|
if (showSeconds) {
|
|
|
|
const seconds = pad(date.getSeconds());
|
|
|
|
return `${hours}:${minutes}:${seconds}${ampm}`;
|
|
|
|
}
|
2017-05-30 23:31:16 +08:00
|
|
|
return `${hours}:${minutes}${ampm}`;
|
2017-05-19 05:00:44 +08:00
|
|
|
}
|
|
|
|
|
2020-10-14 00:39:58 +08:00
|
|
|
export function formatDate(date: Date, showTwelveHour = false): string {
|
2018-01-10 20:00:11 +08: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
|
|
|
|
return _t('%(weekDayName)s %(time)s', {
|
|
|
|
weekDayName: days[date.getDay()],
|
|
|
|
time: formatTime(date, showTwelveHour),
|
|
|
|
});
|
|
|
|
} else if (now.getFullYear() === date.getFullYear()) {
|
|
|
|
// TODO: use standard date localize function provided in counterpart
|
|
|
|
return _t('%(weekDayName)s, %(monthName)s %(day)s %(time)s', {
|
2017-05-27 00:30:02 +08:00
|
|
|
weekDayName: days[date.getDay()],
|
|
|
|
monthName: months[date.getMonth()],
|
|
|
|
day: date.getDate(),
|
2018-01-10 20:00:11 +08:00
|
|
|
time: formatTime(date, showTwelveHour),
|
2017-05-27 00:30:02 +08:00
|
|
|
});
|
2018-01-10 20:00:11 +08:00
|
|
|
}
|
|
|
|
return formatFullDate(date, showTwelveHour);
|
|
|
|
}
|
2016-08-16 22:01:01 +08:00
|
|
|
|
2020-10-14 00:39:58 +08:00
|
|
|
export function formatFullDateNoTime(date: Date): string {
|
2018-01-15 02:32:17 +08:00
|
|
|
const days = getDaysArray();
|
|
|
|
const months = getMonthsArray();
|
|
|
|
return _t('%(weekDayName)s, %(monthName)s %(day)s %(fullYear)s', {
|
|
|
|
weekDayName: days[date.getDay()],
|
|
|
|
monthName: months[date.getMonth()],
|
|
|
|
day: date.getDate(),
|
|
|
|
fullYear: date.getFullYear(),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-04-02 15:25:21 +08:00
|
|
|
export function formatFullDate(date: Date, showTwelveHour = false, showSeconds = true): string {
|
2018-01-10 20:00:11 +08:00
|
|
|
const days = getDaysArray();
|
|
|
|
const months = getMonthsArray();
|
|
|
|
return _t('%(weekDayName)s, %(monthName)s %(day)s %(fullYear)s %(time)s', {
|
|
|
|
weekDayName: days[date.getDay()],
|
|
|
|
monthName: months[date.getMonth()],
|
|
|
|
day: date.getDate(),
|
|
|
|
fullYear: date.getFullYear(),
|
2021-04-02 15:25:21 +08:00
|
|
|
time: showSeconds ? formatFullTime(date, showTwelveHour) : formatTime(date, showTwelveHour),
|
2018-01-10 20:00:11 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-10-14 00:39:58 +08:00
|
|
|
export function formatFullTime(date: Date, showTwelveHour = false): string {
|
2018-02-08 23:58:53 +08:00
|
|
|
if (showTwelveHour) {
|
|
|
|
return twelveHourTime(date, true);
|
|
|
|
}
|
|
|
|
return pad(date.getHours()) + ':' + pad(date.getMinutes()) + ':' + pad(date.getSeconds());
|
|
|
|
}
|
|
|
|
|
2020-10-14 00:39:58 +08:00
|
|
|
export function formatTime(date: Date, showTwelveHour = false): string {
|
2018-01-10 20:00:11 +08:00
|
|
|
if (showTwelveHour) {
|
|
|
|
return twelveHourTime(date);
|
|
|
|
}
|
|
|
|
return pad(date.getHours()) + ':' + pad(date.getMinutes());
|
|
|
|
}
|
2018-01-10 20:06:24 +08:00
|
|
|
|
|
|
|
const MILLIS_IN_DAY = 86400000;
|
2020-10-14 00:39:58 +08:00
|
|
|
export function wantsDateSeparator(prevEventDate: Date, nextEventDate: Date): boolean {
|
2018-01-10 20:06:24 +08:00
|
|
|
if (!nextEventDate || !prevEventDate) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// Return early for events that are > 24h apart
|
|
|
|
if (Math.abs(prevEventDate.getTime() - nextEventDate.getTime()) > MILLIS_IN_DAY) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Compare weekdays
|
|
|
|
return prevEventDate.getDay() !== nextEventDate.getDay();
|
|
|
|
}
|
2021-06-01 15:10:03 +08:00
|
|
|
|
|
|
|
|
|
|
|
export function formatFullDateNoDayNoTime(date: Date) {
|
2021-06-01 15:14:05 +08:00
|
|
|
return (
|
|
|
|
date.getFullYear() +
|
|
|
|
"-" +
|
|
|
|
pad(date.getMonth()) +
|
|
|
|
"-" +
|
|
|
|
pad(date.getDate()) +
|
|
|
|
_t(" at ") +
|
|
|
|
pad(date.getHours()) +
|
|
|
|
"." +
|
|
|
|
pad(date.getMinutes()) +
|
|
|
|
"." +
|
|
|
|
pad(date.getSeconds())
|
|
|
|
);
|
2021-06-01 15:10:03 +08:00
|
|
|
}
|