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 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 | 41x 41x 41x 41x 938x 12x 938x 938x 938x 34x 188x 188x 188x 38x 111x 14x 111x 43x 43x 43x 60x 60x 1x 60x 13x 13x 13x 13x 226x 375x 375x 375x 149x 149x 3x 149x 3x 149x 41x 563x 563x 563x 563x 43x 43x 563x 563x 563x 519x 274x 245x 199x 519x 519x 446x 519x 230x 563x 299x 299x 299x 1x 563x 563x 41x 68x 68x 68x 68x 68x 13x 91x 13x 68x 41x 847x 847x 1x 846x 846x 846x 846x 41x 78x 78x 1x 77x 77x 77x 77x 77x 77x 77x 8x 69x 77x 77x 77x 69x 77x 41x 3x 919x 919x 919x 919x 17x 17x 919x 919x 919x 31x 917x 917x 38x 32x 22x 17x 7x 7x 10x 6x 1x 5x 4x 886x 886x 646x 886x 886x 4x 4x 4x 4x 4x 4x 4x 3x 4x | // format a date
import { Timestamp } from "google-protobuf/google/protobuf/timestamp_pb";
import { capitalizeFirstLetter } from "i18n/casing";
import { TFunction } from "i18next";
import { Temporal } from "temporal-polyfill";
import { approxDateDuration, timestampToInstant, UTC_TIMEZONE } from "utils/date";
import dayjs, { i18nToDayjsLocale } from "utils/dayjs";
/**
* Converts a Temporal date/time object to a Date
* such that it displays as expected in the UTC timezone.
*/
function toDateForUTCDisplay(temporal: Temporal.ZonedDateTime | Temporal.PlainDate | Temporal.PlainDateTime): Date {
if (temporal instanceof Temporal.ZonedDateTime) {
// Discard the timezone, we'll reinterpret it in UTC,
// which results in an incorrect timestamp but correct
// datetime components for displaying.
temporal = temporal.toPlainDateTime();
}
Iif (temporal instanceof Temporal.PlainDate) {
temporal = temporal.toPlainDateTime();
}
const zoned = temporal.toZonedDateTime(UTC_TIMEZONE);
return new Date(zoned.epochMilliseconds);
}
interface LocalizeDateOptions {
/**
* Whether to include the year (defaults to true).
* "auto" omits the year if it matches the current year (browser timezone).
*/
includeYear?: boolean | "auto";
/** Whether to include the day (defaults to true). */
includeDay?: boolean;
/** Whether to include the day of week (defaults to false). */
includeDayOfWeek?: boolean;
/** Whether to abbreviate days of the week and month names (defaults to false). */
abbreviate?: boolean;
/**
* Whether to uppercase the first letter (defaults to false). Set this only when
* the date stands alone (not interpolated into a larger sentence) so it reads
* like the start of a sentence. Day/month names are never otherwise capitalized.
*/
capitalize?: boolean;
}
interface LocalizeTimeOptions {
/** Whether to include seconds (defaults to false). */
includeSeconds?: boolean;
}
interface LocalizeDateTimeOptions extends LocalizeDateOptions, LocalizeTimeOptions {
/** Whether to include the date (defaults to true). */
includeDate?: boolean;
/** Whether to include the time (defaults to true). */
includeTime?: boolean;
}
/**
* Localizes a date and time, in full or partially (specific components),
* optionally with the day of the week and seconds.
*/
export function localizeDateTime(
date: Temporal.PlainDateTime | Temporal.ZonedDateTime,
locale: string,
options?: LocalizeDateTimeOptions,
): string {
const format = getIntlDateTimeFormatUTC(locale, options, { year: date.year });
const formatted = format.format(toDateForUTCDisplay(date));
return options?.capitalize ? capitalizeFirstLetter(formatted, locale) : formatted;
}
/** Localizes a date only (no time), optionally with the day of the week. */
export function localizeDateOnly(
date: Temporal.PlainDate | Temporal.PlainDateTime | Temporal.ZonedDateTime,
locale: string,
options?: LocalizeDateOptions,
): string {
if (date instanceof Temporal.PlainDate) {
date = date.toPlainDateTime();
}
return localizeDateTime(date, locale, { ...options, includeTime: false });
}
/** Localizes a time only (no date), optionally with seconds. */
export function localizeTimeOnly(
time: Temporal.PlainTime | Temporal.PlainDateTime | Temporal.ZonedDateTime,
locale: string,
options?: LocalizeTimeOptions,
): string {
Iif (time instanceof Temporal.PlainTime) {
time = new Temporal.PlainDate(2000, 1, 1).toPlainDateTime(time);
}
return localizeDateTime(time, locale, { ...options, includeDate: false });
}
/** Localizes only the year and month of a date. */
export function localizeYearMonth(
date: Temporal.PlainYearMonth | Temporal.PlainDate | Temporal.PlainDateTime | Temporal.ZonedDateTime,
locale: string,
options?: {
abbreviate?: boolean; // Defaults to false
capitalize?: boolean; // Defaults to false
},
): string {
if (date instanceof Temporal.PlainYearMonth) {
date = date.toPlainDate({ day: 1 });
}
return localizeDateOnly(date, locale, {
...options,
includeDay: false,
});
}
/** Localizes the name of a month (e.g. "Januar", "Mai" in German). */
export function localizeMonthName(
month: number | Temporal.PlainYearMonth | Temporal.PlainDate | Temporal.PlainDateTime | Temporal.ZonedDateTime,
locale: string,
options?: {
abbreviate?: boolean; // Defaults to false
capitalize?: boolean; // Defaults to false
},
): string {
Iif (typeof month !== "number") {
month = month.month;
}
const dummyDate = new Temporal.PlainDate(2000, month, 1);
return localizeDateOnly(dummyDate, locale, {
...options,
includeYear: false,
includeDay: false,
});
}
/** Localizes a range of date and times as a string. */
export function localizeDateTimeRange(
start: Temporal.PlainDateTime | Temporal.ZonedDateTime,
end: Temporal.PlainDateTime | Temporal.ZonedDateTime,
locale: string,
options?: LocalizeDateTimeOptions,
): string {
const format = getIntlDateTimeFormatUTC(locale, options, {
year: start.year == end.year ? start.year : undefined,
});
const formatted = format.formatRange(toDateForUTCDisplay(start), toDateForUTCDisplay(end));
return options?.capitalize ? capitalizeFirstLetter(formatted, locale) : formatted;
}
/** Localizes a range of dates (no times) as a string. */
export function localizeDateRange(
start: Temporal.PlainDate | Temporal.PlainDateTime | Temporal.ZonedDateTime,
end: Temporal.PlainDate | Temporal.PlainDateTime | Temporal.ZonedDateTime,
locale: string,
options?: LocalizeDateOptions,
): string {
if (start instanceof Temporal.PlainDate) {
start = start.toPlainDateTime();
}
if (end instanceof Temporal.PlainDate) {
end = end.toPlainDateTime();
}
return localizeDateTimeRange(start, end, locale, {
...options,
includeTime: false,
});
}
// Creating Intl.DateTimeFormat every time is 40x slower.
// Key: stringified (Intl.DateTimeFormatOptions & { locale: string })
const intlDateTimeFormatCache = new Map<string, Intl.DateTimeFormat>();
/** Gets an Intl.DateTimeFormat based on options. */
function getIntlDateTimeFormatUTC(
locale: string,
options?: LocalizeDateTimeOptions,
dateComponents?: { year?: number },
): Intl.DateTimeFormat {
const intlOptions = getIntlDateTimeFormatOptionsUTC(options, dateComponents);
const cacheKey = JSON.stringify({ ...intlOptions, locale });
let format = intlDateTimeFormatCache.get(cacheKey);
if (!format) {
format = new Intl.DateTimeFormat(locale, intlOptions);
intlDateTimeFormatCache.set(cacheKey, format);
}
return format;
}
/** Creates a new Intl.DateTimeFormat object based on params. */
function getIntlDateTimeFormatOptionsUTC(
options?: LocalizeDateTimeOptions,
dateComponents?: { year?: number },
): Intl.DateTimeFormatOptions {
const intlOptions: Intl.DateTimeFormatOptions = {};
if (options?.includeDate !== false) {
if (options?.includeYear === undefined || options?.includeYear === true) {
intlOptions.year = "numeric";
} else if (
options?.includeYear === "auto" &&
dateComponents?.year !== undefined &&
dateComponents.year != new Date().getFullYear()
) {
intlOptions.year = "numeric";
}
intlOptions.month = options?.abbreviate ? "short" : "long";
if (options?.includeDay !== false) {
intlOptions.day = "numeric";
}
if (options?.includeDayOfWeek) {
intlOptions.weekday = options.abbreviate ? "short" : "long";
}
}
if (options?.includeTime !== false) {
intlOptions.hour = "numeric";
intlOptions.minute = "numeric";
if (options?.includeSeconds) {
intlOptions.second = "numeric";
}
}
intlOptions.timeZone = UTC_TIMEZONE;
return intlOptions;
}
const timeZoneNameCache = new Map<string, string>();
/** Localizes the name of a time zone. */
export function localizeTimeZone(
timeZone: string,
locale: string,
options?: {
short?: boolean;
capitalize?: boolean;
},
) {
const intlOptions: Intl.DateTimeFormatOptions = {
timeZone: timeZone,
timeZoneName: options?.short ? "short" : "long",
};
const cacheKey = JSON.stringify({ ...intlOptions, locale });
let name = timeZoneNameCache.get(cacheKey);
if (!name) {
const format = new Intl.DateTimeFormat(locale, intlOptions);
name = format.formatToParts(Date.now()).find((part) => part.type === "timeZoneName")!.value;
timeZoneNameCache.set(cacheKey, name);
}
return options?.capitalize ? capitalizeFirstLetter(name, locale) : name;
}
const isoMuiDateFormat = "YYYY-MM-DD";
/** Gets the date format for a locale using Material UI placeholders. */
export function getMuiDateFormat(locale: string): string {
if (Intl.DateTimeFormat.supportedLocalesOf(locale).length === 0) {
return isoMuiDateFormat;
}
// Format dummy 3333-11-22 date to figure out how it gets laid out.
const referenceDate = new Intl.DateTimeFormat(locale, {
day: "numeric",
month: "numeric",
year: "numeric",
}).format(new Date(3333, 10, 22));
const format = referenceDate.replace("3333", "YYYY").replace("33", "YY").replace("11", "MM").replace("22", "DD");
// Sanity check: There should be no digits left
Iif (/[0-9]/.test(format)) return isoMuiDateFormat;
return format;
}
const defaultMuiTimeFormat = "HH:mm";
/** Gets a localized time format string compatible with Material UI time pickers. */
export function getMuiTimeFormat(locale: string): string {
if (Intl.DateTimeFormat.supportedLocalesOf(locale).length === 0) {
return defaultMuiTimeFormat;
}
const intlFormat = new Intl.DateTimeFormat(locale, {
hour: "numeric",
minute: "numeric",
hour12: undefined,
});
// Sniff the format using example dates.
// Assume formats only vary by hour-minute separator, 12h vs 24h, and leading zeroes.
const hourMinuteSeparatorMatch = /10(\W+)10/.exec(intlFormat.format(new Date(1970, 0, 1, 10, 10)));
const hourMinuteSeparator = hourMinuteSeparatorMatch ? hourMinuteSeparatorMatch[1] : ":";
const uses24h = intlFormat.format(new Date(1970, 0, 1, 23, 0)).includes("23");
const usesLeadingZeroes = intlFormat.format(new Date(1970, 0, 1, 3, 0)).includes("03");
let format = "";
if (uses24h) {
format += usesLeadingZeroes ? "HH" : "H";
} else {
format += usesLeadingZeroes ? "hh" : "h";
}
format += hourMinuteSeparator;
format += "mm";
if (!uses24h) {
format += " a";
}
return format;
}
export interface LocalizeRelativeTimeOptions {
style?: Intl.RelativeTimeFormatStyle;
/** We override the default to "auto" */
numeric?: Intl.RelativeTimeFormatNumeric;
/** If true, capitalize if the script supports it. By default uses running text capitalization. */
capitalize?: boolean;
}
// Creating Intl objects every time is slow, so cache them.
const relativeTimeFormatCache = new Map<string, Intl.RelativeTimeFormat>();
/** Localizes a time offset expressed in a given unit, e.g. "in 4 minutes". */
export function localizeRelativeTimeUnit(
value: number,
unit: Intl.RelativeTimeFormatUnit,
locale: string,
options?: LocalizeRelativeTimeOptions,
): string {
const intlOptions = {
style: options?.style ?? "long",
numeric: options?.numeric ?? "auto",
};
const cacheKey = JSON.stringify({ locale, ...intlOptions });
let formatter = relativeTimeFormatCache.get(cacheKey);
if (!formatter) {
formatter = new Intl.RelativeTimeFormat(locale, intlOptions);
relativeTimeFormatCache.set(cacheKey, formatter);
}
let result = formatter.format(value, unit);
if (options?.capitalize === true) result = capitalizeFirstLetter(result, locale);
return result;
}
/**
* Localizes a time offset (positive or negative),
* expressing it in the largest possible unit,
* e.g. "in 4 days" for a duration of 4 days and 3 minutes.
*/
export function localizeTimeOffset(
duration: Temporal.Duration,
locale: string,
options?: LocalizeRelativeTimeOptions & {
smallestUnit?: Temporal.PluralizeUnit<Temporal.TimeUnit>;
t?: TFunction<"global", undefined>;
},
) {
duration = approxDateDuration(duration);
if (duration.years != 0) return localizeRelativeTimeUnit(duration.years, "years", locale, options);
if (duration.months != 0) return localizeRelativeTimeUnit(duration.months, "months", locale, options);
if (duration.weeks != 0) return localizeRelativeTimeUnit(duration.weeks, "weeks", locale, options);
if (duration.days != 0) return localizeRelativeTimeUnit(duration.days, "days", locale, options);
// Support "less than one hour ago"
if (duration.hours != 0 || options?.smallestUnit == "hour" || options?.smallestUnit == "hours") {
Iif (duration.hours == 0 && duration.sign <= 0 && options?.t)
return options.t("global:relative_time.less_than_one_hour_ago");
return localizeRelativeTimeUnit(duration.hours, "hours", locale, options);
}
// Support "less than one minute ago"
if (duration.minutes != 0 || options?.smallestUnit == "minute" || options?.smallestUnit == "minutes") {
if (duration.minutes == 0 && duration.sign <= 0 && options?.t)
return options.t("global:relative_time.less_than_a_minute_ago");
return localizeRelativeTimeUnit(duration.minutes, "minutes", locale, options);
}
return localizeRelativeTimeUnit(duration.seconds, "seconds", locale, options);
}
/** Localizes a point in time as a duration relative to some other point in time (by default, now). */
export function localizeRelativeTime(
instant: Temporal.Instant | Timestamp.AsObject,
locale: string,
options?: LocalizeRelativeTimeOptions & {
relativeTo?: Temporal.Instant;
smallestUnit?: Temporal.PluralizeUnit<Temporal.TimeUnit>;
t?: TFunction<"global", undefined>;
},
) {
if (!(instant instanceof Temporal.Instant)) {
instant = timestampToInstant(instant);
}
const duration = instant.since(options?.relativeTo ?? Temporal.Now.instant());
return localizeTimeOffset(duration, locale, options);
}
/**
* Localizes a duration (amount of time), expressing it in the largest possible unit,
* e.g. "4 days" for a duration of 4 days and 3 minutes. (no "in " prefix)
*/
export function localizeDuration(duration: Temporal.Duration, locale: string) {
duration = approxDateDuration(duration);
// Intl.RelativeTimeFormat only supports formatting "in 4 days" / "4 days ago", not "4 days"
// so we have to depend on dayjs.
let dayjsDuration;
Iif (duration.years > 0) dayjsDuration = dayjs.duration(duration.years, "years");
else Iif (duration.months > 0) dayjsDuration = dayjs.duration(duration.months, "months");
else Iif (duration.weeks > 0) dayjsDuration = dayjs.duration(duration.weeks, "weeks");
else Iif (duration.days > 0) dayjsDuration = dayjs.duration(duration.days, "days");
else if (duration.hours > 0) dayjsDuration = dayjs.duration(duration.hours, "hours");
else if (duration.minutes > 0) dayjsDuration = dayjs.duration(duration.minutes, "minutes");
else EdayjsDuration = dayjs.duration(duration.seconds, "seconds");
return dayjsDuration.locale(i18nToDayjsLocale(locale)).humanize();
}
|