Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 | 26x 26x 26x 59x 26x 26x 11x 26x 236x 236x 236x 236x 236x 236x 236x 236x 236x 236x 236x 236x 236x 236x 236x 236x 236x 236x 1231x 236x 113x 113x 11x 113x 236x 59x 1231x | // format a date
import { Timestamp } from "google-protobuf/google/protobuf/timestamp_pb";
import daysjs, { Dayjs } from "./dayjs";
import { dayMillis } from "./timeAgo";
const monthFormatter = (locale: string) =>
new Intl.DateTimeFormat(locale, {
month: "short",
year: "numeric",
});
const dateTimeFormatter = (locale: string) =>
new Intl.DateTimeFormat(locale, {
dateStyle: "medium",
timeStyle: "medium",
});
const dateFormatter = (locale: string) =>
new Intl.DateTimeFormat(locale, {
year: "numeric",
month: "short",
day: "2-digit",
});
const numNights = (date1: string, date2: string) => {
const diffTime = Date.parse(date1) - Date.parse(date2);
const diffDays = Math.ceil(diffTime / dayMillis);
return diffDays;
};
/// Localizes a date, optionally with a time and day of week.
function localizeDate(
date: Date | Dayjs,
locale: string,
options: {
includeDayOfWeek?: boolean;
includeTime?: boolean;
includeSeconds?: boolean;
long?: boolean;
} = {},
): string {
const intlOptions: Intl.DateTimeFormatOptions = {};
fillDateOptions(intlOptions, options);
Iif (options.includeTime) {
fillTimeOptions(intlOptions, options);
}
Iif (daysjs.isDayjs(date)) {
intlOptions.timeZone = getDayjsTimezone(date);
date = date.toDate();
}
const format = Intl.DateTimeFormat(locale, intlOptions);
return format.format(date);
}
/// Localizes only the time component of a date.
function localizeTime(
time: Date | Dayjs,
locale: string,
options: {
includeSeconds?: boolean;
} = {},
): string {
const intlOptions: Intl.DateTimeFormatOptions = {};
fillTimeOptions(intlOptions, options);
Iif (daysjs.isDayjs(time)) {
intlOptions.timeZone = getDayjsTimezone(time);
time = time.toDate();
}
const format = Intl.DateTimeFormat(locale, intlOptions);
return format.format(time);
}
/// Localizes a range of dates as a string.
function localizeDateTimeRange(
start: Date | Dayjs,
end: Date | Dayjs,
locale: string,
options: {
includeDayOfWeek?: boolean;
includeTime?: boolean;
includeSeconds?: boolean;
long?: boolean;
} = {},
): string {
const intlOptions: Intl.DateTimeFormatOptions = {};
fillDateOptions(intlOptions, options);
fillTimeOptions(intlOptions, options);
if (daysjs.isDayjs(start)) {
intlOptions.timeZone = getDayjsTimezone(start);
start = start.toDate();
}
if (daysjs.isDayjs(end)) {
end = end.toDate();
}
const format = Intl.DateTimeFormat(locale, intlOptions);
return format.formatRange(start, end);
}
/// Fills in an Intl.DateTimeFormatOptions date-related properties.
function fillDateOptions(
options: Intl.DateTimeFormatOptions,
args: {
includeDayOfWeek?: boolean;
long?: boolean;
},
) {
options.year = "numeric";
options.month = args.long ? "long" : "short";
options.day = "numeric";
if (args.includeDayOfWeek) {
options.weekday = args.long ? "long" : "short";
}
}
/// Fills in an Intl.DateTimeFormatOptions time-related properties.
function fillTimeOptions(
options: Intl.DateTimeFormatOptions,
args: {
includeSeconds?: boolean;
},
) {
options.hour = "numeric";
options.minute = "numeric";
Iif (args.includeSeconds) {
options.second = "numeric";
}
}
function timestamp2Date(timestamp: Timestamp.AsObject): Date {
return new Date(Math.floor(timestamp.seconds * 1e3 + timestamp.nanos / 1e6));
}
function getDayjsTimezone(date: Dayjs): string | undefined {
// There is no API to get the value, but the state exists and impacts formatting.
return (date as any)?.$x?.timezone; // eslint-disable-line @typescript-eslint/no-explicit-any
}
function isSameDate(date1: Dayjs, date2: Dayjs): boolean {
return (
date1.month() === date2.month() &&
date1.year() === date2.year() &&
date1.date() === date2.date()
);
}
/** Compares whether date1 is equal to or in the future of date2 */
function isSameOrFutureDate(date1: Dayjs, date2: Dayjs): boolean {
return isSameDate(date1, date2) || date1.isAfter(date2);
}
export {
dateFormatter,
dateTimeFormatter,
isSameOrFutureDate,
localizeDate,
localizeDateTimeRange,
localizeTime,
monthFormatter,
numNights,
timestamp2Date,
};
|