mirror of
https://github.com/l0ng-ai/openidea.git
synced 2026-11-24 16:04:21 +00:00
Pull request lists, timelines, commits and replies show how long ago
something happened ("just now", "8 hours ago", "yesterday", "3 days ago")
and the date after 30 days ("on Oct 12"). Hovering a timeline date shows
the full date and time. With pretty formatting off, the plain date and time
are shown.
60 lines
2.1 KiB
TypeScript
60 lines
2.1 KiB
TypeScript
/**
|
||
* GitHub-style relative dates: "just now", "N minutes ago", "N hours ago",
|
||
* "yesterday", "N days ago", then "on Oct 12" (with the year when it differs).
|
||
* With Settings › Date Formats › pretty formatting off, the plain date and time.
|
||
*/
|
||
import {
|
||
dateFormatOptions,
|
||
formatClock,
|
||
formatDay,
|
||
formatPattern,
|
||
type DateFormatOptions,
|
||
} from "@/platform/dateFormat";
|
||
import { tx } from "@/platform/i18n";
|
||
|
||
const MINUTE = 60_000;
|
||
const HOUR = 60 * MINUTE;
|
||
const DAY = 24 * HOUR;
|
||
|
||
function onDay(d: Date, now: number, o: DateFormatOptions): string {
|
||
if (o.overrideSystem) return tx`on ${formatPattern(d, o.pattern)}`;
|
||
const sameYear = d.getFullYear() === new Date(now).getFullYear();
|
||
const day = d.toLocaleDateString(undefined, {
|
||
month: "short",
|
||
day: "numeric",
|
||
...(sameYear ? {} : { year: "numeric" }),
|
||
});
|
||
return tx`on ${day}`;
|
||
}
|
||
|
||
const toMs = (date: string | number | Date) =>
|
||
typeof date === "number" ? date : date instanceof Date ? date.getTime() : Date.parse(date);
|
||
|
||
/** List rows, timeline headers, commits and replies. Empty for an invalid date. */
|
||
export function prettyDate(date: string | number | Date, now = Date.now(), o = dateFormatOptions()): string {
|
||
const ms = toMs(date);
|
||
if (Number.isNaN(ms)) return "";
|
||
const d = new Date(ms);
|
||
if (!o.pretty) return `${formatDay(d, o)} ${formatClock(d, o)}`;
|
||
const delta = Math.max(0, now - ms);
|
||
if (delta < MINUTE) return tx("just now");
|
||
if (delta < HOUR) {
|
||
const n = Math.floor(delta / MINUTE);
|
||
return n === 1 ? tx("1 minute ago") : tx`${n} minutes ago`;
|
||
}
|
||
if (delta < DAY) {
|
||
const n = Math.floor(delta / HOUR);
|
||
return n === 1 ? tx("1 hour ago") : tx`${n} hours ago`;
|
||
}
|
||
const days = Math.floor(delta / DAY);
|
||
if (days === 1) return tx("yesterday");
|
||
if (days < 30) return tx`${days} days ago`;
|
||
return onDay(d, now, o);
|
||
}
|
||
|
||
/** The full date and time, for a tooltip on a relative date. */
|
||
export function fullDateTime(date: string | number | Date, o = dateFormatOptions()): string {
|
||
const ms = toMs(date);
|
||
return Number.isNaN(ms) ? "" : `${formatDay(new Date(ms), o)} ${formatClock(new Date(ms), o)}`;
|
||
}
|