All files / utils dateTime.js
86.95%
Statements
40/46
66.66%
Branches
16/24
100%
Functions
10/10
94.73%
Lines
36/38
Press n or j to go to the next uncovered block, b, p or k for the previous block.
Filter:
| 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 | 2x 2x 2x 13x 143x 78x 13x 6x 6x 6x 6x 3x 3x 3x 6x 6x 3x 3x 11x 3x 11x 3x 3x 3x 7x 7x 7x 35x 7x 1x 1x 1x 1x 1x 1x 1x | export const APP_TIME_ZONE = import.meta.env.VITE_APP_TIME_ZONE || "Asia/Shanghai";
const SQL_DATE_TIME_PATTERN =
/^(\d{4})-(\d{2})-(\d{2})[ T](\d{2}):(\d{2}):(\d{2})$/;
const zonedPartsFormatter = new Intl.DateTimeFormat("en-CA", {
timeZone: APP_TIME_ZONE,
year: "numeric",
month: "2-digit",
day: "2-digit",
hour: "2-digit",
minute: "2-digit",
second: "2-digit",
hourCycle: "h23",
});
function zonedParts(date) {
const parts = Object.fromEntries(
zonedPartsFormatter
.formatToParts(date)
.filter((part) => part.type !== "literal")
.map((part) => [part.type, Number(part.value)])
);
return {
year: parts.year,
month: parts.month,
day: parts.day,
hour: parts.hour,
minute: parts.minute,
second: parts.second,
};
}
function offsetAt(date) {
const parts = zonedParts(date);
const wallClockAsUtc = Date.UTC(
parts.year,
parts.month - 1,
parts.day,
parts.hour,
parts.minute,
parts.second
);
const instantToSecond = Math.floor(date.getTime() / 1000) * 1000;
return wallClockAsUtc - instantToSecond;
}
function wallClockToInstant(parts) {
const target = Date.UTC(
parts.year,
parts.month - 1,
parts.day,
parts.hour,
parts.minute,
parts.second
);
let candidate = target;
for (let attempt = 0; attempt < 3; attempt += 1) {
const next = target - offsetAt(new Date(candidate));
if (next === candidate) break;
candidate = next;
}
return new Date(candidate);
}
export function parseAppDateTime(value) {
if (value instanceof Date) return new Date(value.getTime());
const text = String(value ?? "").trim();
Iif (!text) return null;
const sqlMatch = text.match(SQL_DATE_TIME_PATTERN);
Eif (sqlMatch) {
return wallClockToInstant({
year: Number(sqlMatch[1]),
month: Number(sqlMatch[2]),
day: Number(sqlMatch[3]),
hour: Number(sqlMatch[4]),
minute: Number(sqlMatch[5]),
second: Number(sqlMatch[6]),
});
}
const parsed = new Date(text);
return Number.isNaN(parsed.getTime()) ? null : parsed;
}
export function toAppSqlDateTime(value = new Date()) {
const date = parseAppDateTime(value);
Iif (!date) return "";
const parts = zonedParts(date);
const pad = (part) => String(part).padStart(2, "0");
return `${parts.year}-${pad(parts.month)}-${pad(parts.day)} `
+ `${pad(parts.hour)}:${pad(parts.minute)}:${pad(parts.second)}`;
}
export function formatAppMonthDayTime(value) {
const date = parseAppDateTime(value);
Iif (!date) return "时间未知";
return new Intl.DateTimeFormat("zh-CN", {
timeZone: APP_TIME_ZONE,
month: "numeric",
day: "numeric",
hour: "2-digit",
minute: "2-digit",
hourCycle: "h23",
}).format(date);
}
export function remainingAppDays(deadline, now = new Date()) {
const end = parseAppDateTime(deadline);
const current = parseAppDateTime(now);
Iif (!end || !current) return null;
return Math.max(1, Math.ceil((end.getTime() - current.getTime()) / 86400000));
}
|