-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.js
More file actions
130 lines (110 loc) · 3.37 KB
/
utils.js
File metadata and controls
130 lines (110 loc) · 3.37 KB
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
export function pad2(n) {
return String(n).padStart(2, "0");
}
export function fmtHMS(ms) {
const totalSec = Math.max(0, Math.floor(ms / 1000));
const h = Math.floor(totalSec / 3600);
const m = Math.floor((totalSec % 3600) / 60);
const s = totalSec % 60;
return `${pad2(h)}:${pad2(m)}:${pad2(s)}`;
}
export function fmtHM(ms) {
const totalMin = Math.max(0, Math.round(ms / 60000));
const h = Math.floor(totalMin / 60);
const m = totalMin % 60;
return `${h}:${pad2(m)}`;
}
export function minutesFromMs(ms) {
return Math.max(0, ms / 60000);
}
export function startOfDayMs(d) {
const x = new Date(d);
x.setHours(0, 0, 0, 0);
return x.getTime();
}
export function endOfDayMs(d) {
const x = new Date(d);
x.setHours(23, 59, 59, 999);
return x.getTime();
}
export function startOfWeekMs(d) {
const x = new Date(d);
const day = x.getDay();
const deltaToMonday = (day === 0 ? -6 : 1 - day);
x.setDate(x.getDate() + deltaToMonday);
x.setHours(0, 0, 0, 0);
return x.getTime();
}
export function startOfMonthMs(d) {
const x = new Date(d);
x.setDate(1);
x.setHours(0, 0, 0, 0);
return x.getTime();
}
export function nowMs() {
return Date.now();
}
export function makeLogId() {
const suffix = Math.random().toString(36).slice(2, 8);
return `log_${Date.now()}_${suffix}`;
}
export function parseDateTimeToMs(dateValue, timeValue) {
return new Date(`${dateValue}T${timeValue}`).getTime();
}
export function fmtLogStamp(ms) {
const d = new Date(ms);
const dateStr = d.toLocaleDateString(undefined, {
weekday: "short",
year: "numeric",
month: "numeric",
day: "numeric",
});
const timeStr = d.toLocaleTimeString([], { hour: "2-digit", minute: "2-digit" });
return `${dateStr} ${timeStr}`;
}
export function escapeHtml(value) {
return String(value ?? "")
.replaceAll("&", "&")
.replaceAll("<", "<")
.replaceAll(">", ">")
.replaceAll("\"", """)
.replaceAll("'", "'");
}
export function formatDateInputValue(ms) {
const d = new Date(ms);
return `${d.getFullYear()}-${pad2(d.getMonth() + 1)}-${pad2(d.getDate())}`;
}
export function formatTimeInputValue(ms) {
const d = new Date(ms);
return `${pad2(d.getHours())}:${pad2(d.getMinutes())}`;
}
export function expectedMinutesForType(type) {
if (type === "break_1" || type === "break_3") return 15;
if (type === "break_2") return 30;
return null;
}
export function getManualTypeMeta(type) {
if (type === "break_1") return { kind: "break", sequence: 1, plannedMinutes: 15 };
if (type === "break_2") return { kind: "break", sequence: 2, plannedMinutes: 30 };
if (type === "break_3") return { kind: "break", sequence: 3, plannedMinutes: 15 };
return { kind: "work" };
}
export function clampToRange(msStart, msEnd, rangeStart, rangeEnd) {
const s = Math.max(msStart, rangeStart);
const e = Math.min(msEnd, rangeEnd);
return Math.max(0, e - s);
}
export function formatShortUsDate(date) {
const month = date.getMonth() + 1;
const day = date.getDate();
const year = date.getFullYear() % 100;
return `${month}/${day}/${year}`;
}
export function formatDecimalHours(minutes) {
const decimalHours = minutes / 60;
return decimalHours.toFixed(2).replace(/\.00$/, "").replace(/(\.\d*[1-9])0$/, "$1");
}
export function formatLiveDecimalHours(ms) {
const decimalHours = minutesFromMs(ms) / 60;
return Math.max(0, decimalHours).toFixed(2);
}