@ethisyscore/core-utils 1.21.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +52 -0
- package/dist/date/dayjs.cjs +11 -0
- package/dist/date/dayjs.cjs.map +1 -0
- package/dist/date/dayjs.d.cts +27 -0
- package/dist/date/dayjs.d.ts +27 -0
- package/dist/date/dayjs.js +9 -0
- package/dist/date/dayjs.js.map +1 -0
- package/dist/date/index.cjs +281 -0
- package/dist/date/index.cjs.map +1 -0
- package/dist/date/index.d.cts +206 -0
- package/dist/date/index.d.ts +206 -0
- package/dist/date/index.js +241 -0
- package/dist/date/index.js.map +1 -0
- package/dist/index.cjs +281 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +241 -0
- package/dist/index.js.map +1 -0
- package/package.json +69 -0
package/README.md
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
# @ethisyscore/core-utils
|
|
2
|
+
|
|
3
|
+
Framework-agnostic utilities shared by the EthisysCore monolith and plugins.
|
|
4
|
+
Pure TypeScript — **no React, no MUI**. Utilities are grouped by domain; today
|
|
5
|
+
that is `date`.
|
|
6
|
+
|
|
7
|
+
## Install
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
npm install @ethisyscore/core-utils
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
`date-fns` is a runtime dependency. `dayjs` is an **optional** peer dependency —
|
|
14
|
+
only needed if you import the `./date/dayjs` sub-path.
|
|
15
|
+
|
|
16
|
+
## Usage
|
|
17
|
+
|
|
18
|
+
```ts
|
|
19
|
+
// Everything (umbrella entry)
|
|
20
|
+
import { formatDateSafe, dateOnlyToIsoUtc, formatDurationNumber } from "@ethisyscore/core-utils";
|
|
21
|
+
|
|
22
|
+
// Or import the date group directly
|
|
23
|
+
import { addDaysToIsoDate, toHHmmss } from "@ethisyscore/core-utils/date";
|
|
24
|
+
|
|
25
|
+
// dayjs-backed picker helper (requires dayjs)
|
|
26
|
+
import { dayjsToIsoDayBoundary } from "@ethisyscore/core-utils/date/dayjs";
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
## What's inside (`./date`)
|
|
30
|
+
|
|
31
|
+
| Area | Exports |
|
|
32
|
+
| --- | --- |
|
|
33
|
+
| **Format constants** | `DATE_FORMAT`, `DISPLAY_DATE_FORMAT`, `AUDIT_DATE_FORMAT`, `DAY_DATE_FORMAT`, `DAY_MONTH_FORMAT`, `DAY_MONTH_YEAR_FORMAT`, `TIME_FORMAT`, `SHORT_DATETIME_FORMAT` |
|
|
34
|
+
| **ISO / timezone-safe** | `isDateOnlyString`, `formatDate`, `getTodayIsoDate`, `getMonthStartIso`, `getMonthEndIso`, `parseIsoDateLocal`, `addDaysToIsoDate`, `toDateOnlyString` |
|
|
35
|
+
| **Display formatting** | `formatDateString`, `formatDateSafe`, `formatDateWithOrdinal`, `formatTimeAgo` |
|
|
36
|
+
| **Duration / timespan** | `timespanToMilliseconds`, `millisecondsToHours`, `hoursToMilliseconds`, `formatMillisecondsAsTimeSpent`, `formatDuration`, `formatDurationNumber`, `toHHmm`, `toHHmmss` |
|
|
37
|
+
| **Wire boundary (`<input>` ⇄ .NET `DateTimeOffset`)** | `isoToDateInput`, `dateInputToIso`, `dateOnlyToIsoUtc`, `nowLocalDateTimeInputValue`, `localDateTimeInputToIsoUtc`, `isoUtcToLocalDateTimeInput`, `ensureUtcIso` |
|
|
38
|
+
| **Ranges / timezone** | `getDateRange`, `computeDateRange`, `currentMonthRangeUtc`, `getCurrentOffsetMinutes` |
|
|
39
|
+
| **dayjs sub-path** | `dayjsToIsoDayBoundary` |
|
|
40
|
+
|
|
41
|
+
The wire-boundary helpers exist to bridge the naive local strings emitted by
|
|
42
|
+
`<input type="date">` / `<input type="datetime-local">` to full ISO-8601 UTC,
|
|
43
|
+
avoiding the offset/DST drift that occurs when a .NET `DateTimeOffset` endpoint
|
|
44
|
+
reinterprets a no-suffix timestamp as UTC.
|
|
45
|
+
|
|
46
|
+
## Scripts
|
|
47
|
+
|
|
48
|
+
```bash
|
|
49
|
+
npm run build # tsup → ESM + CJS + d.ts
|
|
50
|
+
npm test # vitest
|
|
51
|
+
npm run lint # tsc --noEmit
|
|
52
|
+
```
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
// src/date/dayjs.ts
|
|
4
|
+
function dayjsToIsoDayBoundary(value, endOfDay = false) {
|
|
5
|
+
if (!value || !value.isValid()) return void 0;
|
|
6
|
+
return endOfDay ? value.endOf("day").toISOString() : value.startOf("day").toISOString();
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
exports.dayjsToIsoDayBoundary = dayjsToIsoDayBoundary;
|
|
10
|
+
//# sourceMappingURL=dayjs.cjs.map
|
|
11
|
+
//# sourceMappingURL=dayjs.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/date/dayjs.ts"],"names":[],"mappings":";;;AAwBO,SAAS,qBAAA,CAAsB,KAAA,EAAiC,QAAA,GAAW,KAAA,EAA2B;AAC3G,EAAA,IAAI,CAAC,KAAA,IAAS,CAAC,KAAA,CAAM,OAAA,IAAW,OAAO,MAAA;AACvC,EAAA,OAAO,QAAA,GAAW,KAAA,CAAM,KAAA,CAAM,KAAK,CAAA,CAAE,WAAA,EAAY,GAAI,KAAA,CAAM,OAAA,CAAQ,KAAK,CAAA,CAAE,WAAA,EAAY;AACxF","file":"dayjs.cjs","sourcesContent":["import type { Dayjs } from \"dayjs\";\n\n/**\n * Converts a Dayjs picker value to a UTC ISO timestamp anchored at the user's\n * LOCAL day boundary.\n *\n * Used by date-range filter bars where the BE expects a full ISO timestamp\n * boundary — `OccurredFrom` is start-of-local-day, `OccurredTo` is\n * end-of-local-day so the filter is inclusive of the user's calendar day\n * regardless of their timezone.\n *\n * Critical: hardcoding `T00:00:00.000Z` on the formatted date treats the picker\n * value as UTC, which shifts the boundary by the user's offset (e.g. a user in\n * Europe/London during BST loses the first hour of their local day).\n * `startOf(\"day\")` / `endOf(\"day\")` give the local boundary; `.toISOString()`\n * then converts to UTC for the wire.\n *\n * Lives behind the `@ethisyscore/core-utils/date/dayjs` sub-path so the core\n * entry stays free of the optional `dayjs` peer dependency.\n *\n * @param value - The Dayjs picker value (or null when the user cleared the field)\n * @param endOfDay - When true, returns the end-of-local-day boundary\n * @returns ISO timestamp string in UTC, or undefined when the value is null/invalid\n */\nexport function dayjsToIsoDayBoundary(value: Dayjs | null | undefined, endOfDay = false): string | undefined {\n if (!value || !value.isValid()) return undefined;\n return endOfDay ? value.endOf(\"day\").toISOString() : value.startOf(\"day\").toISOString();\n}\n"]}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { Dayjs } from 'dayjs';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Converts a Dayjs picker value to a UTC ISO timestamp anchored at the user's
|
|
5
|
+
* LOCAL day boundary.
|
|
6
|
+
*
|
|
7
|
+
* Used by date-range filter bars where the BE expects a full ISO timestamp
|
|
8
|
+
* boundary — `OccurredFrom` is start-of-local-day, `OccurredTo` is
|
|
9
|
+
* end-of-local-day so the filter is inclusive of the user's calendar day
|
|
10
|
+
* regardless of their timezone.
|
|
11
|
+
*
|
|
12
|
+
* Critical: hardcoding `T00:00:00.000Z` on the formatted date treats the picker
|
|
13
|
+
* value as UTC, which shifts the boundary by the user's offset (e.g. a user in
|
|
14
|
+
* Europe/London during BST loses the first hour of their local day).
|
|
15
|
+
* `startOf("day")` / `endOf("day")` give the local boundary; `.toISOString()`
|
|
16
|
+
* then converts to UTC for the wire.
|
|
17
|
+
*
|
|
18
|
+
* Lives behind the `@ethisyscore/core-utils/date/dayjs` sub-path so the core
|
|
19
|
+
* entry stays free of the optional `dayjs` peer dependency.
|
|
20
|
+
*
|
|
21
|
+
* @param value - The Dayjs picker value (or null when the user cleared the field)
|
|
22
|
+
* @param endOfDay - When true, returns the end-of-local-day boundary
|
|
23
|
+
* @returns ISO timestamp string in UTC, or undefined when the value is null/invalid
|
|
24
|
+
*/
|
|
25
|
+
declare function dayjsToIsoDayBoundary(value: Dayjs | null | undefined, endOfDay?: boolean): string | undefined;
|
|
26
|
+
|
|
27
|
+
export { dayjsToIsoDayBoundary };
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { Dayjs } from 'dayjs';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Converts a Dayjs picker value to a UTC ISO timestamp anchored at the user's
|
|
5
|
+
* LOCAL day boundary.
|
|
6
|
+
*
|
|
7
|
+
* Used by date-range filter bars where the BE expects a full ISO timestamp
|
|
8
|
+
* boundary — `OccurredFrom` is start-of-local-day, `OccurredTo` is
|
|
9
|
+
* end-of-local-day so the filter is inclusive of the user's calendar day
|
|
10
|
+
* regardless of their timezone.
|
|
11
|
+
*
|
|
12
|
+
* Critical: hardcoding `T00:00:00.000Z` on the formatted date treats the picker
|
|
13
|
+
* value as UTC, which shifts the boundary by the user's offset (e.g. a user in
|
|
14
|
+
* Europe/London during BST loses the first hour of their local day).
|
|
15
|
+
* `startOf("day")` / `endOf("day")` give the local boundary; `.toISOString()`
|
|
16
|
+
* then converts to UTC for the wire.
|
|
17
|
+
*
|
|
18
|
+
* Lives behind the `@ethisyscore/core-utils/date/dayjs` sub-path so the core
|
|
19
|
+
* entry stays free of the optional `dayjs` peer dependency.
|
|
20
|
+
*
|
|
21
|
+
* @param value - The Dayjs picker value (or null when the user cleared the field)
|
|
22
|
+
* @param endOfDay - When true, returns the end-of-local-day boundary
|
|
23
|
+
* @returns ISO timestamp string in UTC, or undefined when the value is null/invalid
|
|
24
|
+
*/
|
|
25
|
+
declare function dayjsToIsoDayBoundary(value: Dayjs | null | undefined, endOfDay?: boolean): string | undefined;
|
|
26
|
+
|
|
27
|
+
export { dayjsToIsoDayBoundary };
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
// src/date/dayjs.ts
|
|
2
|
+
function dayjsToIsoDayBoundary(value, endOfDay = false) {
|
|
3
|
+
if (!value || !value.isValid()) return void 0;
|
|
4
|
+
return endOfDay ? value.endOf("day").toISOString() : value.startOf("day").toISOString();
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
export { dayjsToIsoDayBoundary };
|
|
8
|
+
//# sourceMappingURL=dayjs.js.map
|
|
9
|
+
//# sourceMappingURL=dayjs.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/date/dayjs.ts"],"names":[],"mappings":";AAwBO,SAAS,qBAAA,CAAsB,KAAA,EAAiC,QAAA,GAAW,KAAA,EAA2B;AAC3G,EAAA,IAAI,CAAC,KAAA,IAAS,CAAC,KAAA,CAAM,OAAA,IAAW,OAAO,MAAA;AACvC,EAAA,OAAO,QAAA,GAAW,KAAA,CAAM,KAAA,CAAM,KAAK,CAAA,CAAE,WAAA,EAAY,GAAI,KAAA,CAAM,OAAA,CAAQ,KAAK,CAAA,CAAE,WAAA,EAAY;AACxF","file":"dayjs.js","sourcesContent":["import type { Dayjs } from \"dayjs\";\n\n/**\n * Converts a Dayjs picker value to a UTC ISO timestamp anchored at the user's\n * LOCAL day boundary.\n *\n * Used by date-range filter bars where the BE expects a full ISO timestamp\n * boundary — `OccurredFrom` is start-of-local-day, `OccurredTo` is\n * end-of-local-day so the filter is inclusive of the user's calendar day\n * regardless of their timezone.\n *\n * Critical: hardcoding `T00:00:00.000Z` on the formatted date treats the picker\n * value as UTC, which shifts the boundary by the user's offset (e.g. a user in\n * Europe/London during BST loses the first hour of their local day).\n * `startOf(\"day\")` / `endOf(\"day\")` give the local boundary; `.toISOString()`\n * then converts to UTC for the wire.\n *\n * Lives behind the `@ethisyscore/core-utils/date/dayjs` sub-path so the core\n * entry stays free of the optional `dayjs` peer dependency.\n *\n * @param value - The Dayjs picker value (or null when the user cleared the field)\n * @param endOfDay - When true, returns the end-of-local-day boundary\n * @returns ISO timestamp string in UTC, or undefined when the value is null/invalid\n */\nexport function dayjsToIsoDayBoundary(value: Dayjs | null | undefined, endOfDay = false): string | undefined {\n if (!value || !value.isValid()) return undefined;\n return endOfDay ? value.endOf(\"day\").toISOString() : value.startOf(\"day\").toISOString();\n}\n"]}
|
|
@@ -0,0 +1,281 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var dateFns = require('date-fns');
|
|
4
|
+
|
|
5
|
+
// src/date/constants.ts
|
|
6
|
+
var DATE_FORMAT = "yyyy-MM-dd";
|
|
7
|
+
var AUDIT_DATE_FORMAT = "d MMMM yyyy, HH:mm";
|
|
8
|
+
var DISPLAY_DATE_FORMAT = "d MMM yyyy";
|
|
9
|
+
var DAY_DATE_FORMAT = "EEEE, d MMMM";
|
|
10
|
+
var DAY_MONTH_FORMAT = "MMM d";
|
|
11
|
+
var DAY_MONTH_YEAR_FORMAT = "MMM d, yyyy";
|
|
12
|
+
var TIME_FORMAT = "h:mm a";
|
|
13
|
+
var SHORT_DATETIME_FORMAT = "MMM d, h:mm a";
|
|
14
|
+
var isDateOnlyString = (value) => {
|
|
15
|
+
return typeof value === "string" && /^\d{4}-\d{2}-\d{2}$/.test(value);
|
|
16
|
+
};
|
|
17
|
+
var formatDate = (date, dateFormat = DATE_FORMAT) => {
|
|
18
|
+
return dateFns.format(date, dateFormat);
|
|
19
|
+
};
|
|
20
|
+
var getTodayIsoDate = () => {
|
|
21
|
+
return formatDate(/* @__PURE__ */ new Date());
|
|
22
|
+
};
|
|
23
|
+
var getMonthStartIso = (date) => formatDate(new Date(date.getFullYear(), date.getMonth(), 1));
|
|
24
|
+
var getMonthEndIso = (date) => formatDate(new Date(date.getFullYear(), date.getMonth() + 1, 0));
|
|
25
|
+
var parseIsoDateLocal = (value) => {
|
|
26
|
+
if (!value || !isDateOnlyString(value)) return null;
|
|
27
|
+
const [year, month, day] = value.split("-").map(Number);
|
|
28
|
+
const d = new Date(year, month - 1, day);
|
|
29
|
+
return dateFns.isValid(d) ? d : null;
|
|
30
|
+
};
|
|
31
|
+
var addDaysToIsoDate = (isoDate, days) => {
|
|
32
|
+
const base = parseIsoDateLocal(isoDate);
|
|
33
|
+
if (!base) return isoDate;
|
|
34
|
+
base.setDate(base.getDate() + days);
|
|
35
|
+
return formatDate(base);
|
|
36
|
+
};
|
|
37
|
+
var toDateOnlyString = (value) => {
|
|
38
|
+
if (!value) return "";
|
|
39
|
+
const datepart = value.split("T")[0];
|
|
40
|
+
return isDateOnlyString(datepart) ? datepart : "";
|
|
41
|
+
};
|
|
42
|
+
var formatDateString = (date, dateFormat = DATE_FORMAT) => {
|
|
43
|
+
if (!date) return "-";
|
|
44
|
+
const parsed = dateFns.parseISO(date);
|
|
45
|
+
if (!dateFns.isValid(parsed)) return "-";
|
|
46
|
+
return dateFns.format(parsed, dateFormat);
|
|
47
|
+
};
|
|
48
|
+
var formatDateSafe = (date, formatStr = DATE_FORMAT, fallback = "\u2014") => {
|
|
49
|
+
if (!date) return fallback;
|
|
50
|
+
try {
|
|
51
|
+
const dateObj = typeof date === "string" ? dateFns.parseISO(date) : date;
|
|
52
|
+
if (!dateFns.isValid(dateObj)) return fallback;
|
|
53
|
+
return dateFns.format(dateObj, formatStr);
|
|
54
|
+
} catch {
|
|
55
|
+
return fallback;
|
|
56
|
+
}
|
|
57
|
+
};
|
|
58
|
+
var getOrdinalSuffix = (day) => {
|
|
59
|
+
if (day > 3 && day < 21) return "th";
|
|
60
|
+
switch (day % 10) {
|
|
61
|
+
case 1:
|
|
62
|
+
return "st";
|
|
63
|
+
case 2:
|
|
64
|
+
return "nd";
|
|
65
|
+
case 3:
|
|
66
|
+
return "rd";
|
|
67
|
+
default:
|
|
68
|
+
return "th";
|
|
69
|
+
}
|
|
70
|
+
};
|
|
71
|
+
var formatDateWithOrdinal = (date) => {
|
|
72
|
+
if (!date) return "Not specified";
|
|
73
|
+
let dateObj;
|
|
74
|
+
if (typeof date === "string") {
|
|
75
|
+
if (isDateOnlyString(date)) {
|
|
76
|
+
const [year2, month2, day2] = date.split("-").map(Number);
|
|
77
|
+
dateObj = new Date(year2, month2 - 1, day2);
|
|
78
|
+
} else {
|
|
79
|
+
dateObj = dateFns.parseISO(date);
|
|
80
|
+
}
|
|
81
|
+
} else {
|
|
82
|
+
dateObj = date;
|
|
83
|
+
}
|
|
84
|
+
if (!dateFns.isValid(dateObj)) return "Invalid date";
|
|
85
|
+
const day = dateObj.getDate();
|
|
86
|
+
const month = dateFns.format(dateObj, "MMM");
|
|
87
|
+
const year = dateObj.getFullYear();
|
|
88
|
+
return `${day}${getOrdinalSuffix(day)} ${month} ${year}`;
|
|
89
|
+
};
|
|
90
|
+
var formatTimeAgo = (date) => {
|
|
91
|
+
if (!date) return "";
|
|
92
|
+
const dateObj = typeof date === "string" ? dateFns.parseISO(date) : date;
|
|
93
|
+
if (!dateFns.isValid(dateObj)) return "";
|
|
94
|
+
const now = /* @__PURE__ */ new Date();
|
|
95
|
+
const seconds = Math.floor((now.getTime() - dateObj.getTime()) / 1e3);
|
|
96
|
+
if (seconds < 60) return "Just now";
|
|
97
|
+
const minutes = Math.floor(seconds / 60);
|
|
98
|
+
if (minutes < 60) return `${minutes}m ago`;
|
|
99
|
+
const hours = Math.floor(minutes / 60);
|
|
100
|
+
if (hours < 24) return `${hours}h ago`;
|
|
101
|
+
const days = Math.floor(hours / 24);
|
|
102
|
+
if (days < 7) return `${days}d ago`;
|
|
103
|
+
return formatDate(dateObj);
|
|
104
|
+
};
|
|
105
|
+
|
|
106
|
+
// src/date/duration.ts
|
|
107
|
+
var timespanToMilliseconds = (timespan) => {
|
|
108
|
+
if (!timespan) return 0;
|
|
109
|
+
const [h = "0", m = "0", s = "0"] = timespan.split(":");
|
|
110
|
+
const hours = parseInt(h || "0", 10);
|
|
111
|
+
const minutes = parseInt(m || "0", 10);
|
|
112
|
+
const seconds = parseInt(s || "0", 10);
|
|
113
|
+
return (isNaN(hours) ? 0 : hours) * 60 * 60 * 1e3 + (isNaN(minutes) ? 0 : minutes) * 60 * 1e3 + (isNaN(seconds) ? 0 : seconds) * 1e3;
|
|
114
|
+
};
|
|
115
|
+
var millisecondsToHours = (ms) => ms / (1e3 * 60 * 60);
|
|
116
|
+
var hoursToMilliseconds = (hours) => hours * (1e3 * 60 * 60);
|
|
117
|
+
var formatMillisecondsAsTimeSpent = (ms) => {
|
|
118
|
+
if (!ms || ms < 0) return "0h 0min";
|
|
119
|
+
const totalMinutes = Math.floor(ms / (1e3 * 60));
|
|
120
|
+
const hours = Math.floor(totalMinutes / 60);
|
|
121
|
+
const minutes = totalMinutes % 60;
|
|
122
|
+
let result = "";
|
|
123
|
+
if (hours > 0) result += `${hours}h`;
|
|
124
|
+
if (minutes > 0 || hours === 0) result += (hours > 0 ? " " : "") + `${minutes}min`;
|
|
125
|
+
return result;
|
|
126
|
+
};
|
|
127
|
+
function formatDuration(duration) {
|
|
128
|
+
if (!duration) return "-";
|
|
129
|
+
const [hours, minutes, seconds] = duration.split(":").map(Number);
|
|
130
|
+
let result = "";
|
|
131
|
+
if (hours) result += `${hours}h`;
|
|
132
|
+
if (minutes) result += (result ? " " : "") + `${minutes}m`;
|
|
133
|
+
if (seconds) result += (result ? " " : "") + `${seconds}s`;
|
|
134
|
+
return result || "0m";
|
|
135
|
+
}
|
|
136
|
+
var formatDurationNumber = (durationHours) => {
|
|
137
|
+
if (durationHours == null || durationHours === 0) return "\u2014";
|
|
138
|
+
if (durationHours > 8760) return "Unknown";
|
|
139
|
+
if (durationHours < 0) return "Error";
|
|
140
|
+
const hours = Math.floor(durationHours);
|
|
141
|
+
const minutes = Math.floor(durationHours % 1 * 60);
|
|
142
|
+
if (durationHours < 1) return `${minutes}m`;
|
|
143
|
+
if (hours < 24) {
|
|
144
|
+
if (minutes === 0) return `${hours}h`;
|
|
145
|
+
return `${hours}h ${minutes}m`;
|
|
146
|
+
}
|
|
147
|
+
const days = Math.floor(hours / 24);
|
|
148
|
+
const remainingHours = hours % 24;
|
|
149
|
+
if (remainingHours === 0) return `${days}d`;
|
|
150
|
+
return `${days}d ${remainingHours}h`;
|
|
151
|
+
};
|
|
152
|
+
function toHHmm(value, fallback) {
|
|
153
|
+
if (!value || value.length < 5) return fallback;
|
|
154
|
+
return value.slice(0, 5);
|
|
155
|
+
}
|
|
156
|
+
function toHHmmss(value) {
|
|
157
|
+
const trimmed = value.trim();
|
|
158
|
+
if (!trimmed) return null;
|
|
159
|
+
return trimmed.length === 5 ? `${trimmed}:00` : trimmed;
|
|
160
|
+
}
|
|
161
|
+
var isoToDateInput = (isoString) => {
|
|
162
|
+
if (!isoString) return "";
|
|
163
|
+
try {
|
|
164
|
+
return new Date(isoString).toISOString().split("T")[0];
|
|
165
|
+
} catch {
|
|
166
|
+
return "";
|
|
167
|
+
}
|
|
168
|
+
};
|
|
169
|
+
var dateInputToIso = (dateString) => {
|
|
170
|
+
if (!dateString) return "";
|
|
171
|
+
const parsed = /* @__PURE__ */ new Date(dateString + "T00:00:00Z");
|
|
172
|
+
return Number.isNaN(parsed.getTime()) ? "" : parsed.toISOString();
|
|
173
|
+
};
|
|
174
|
+
function dateOnlyToIsoUtc(value) {
|
|
175
|
+
if (!value) return void 0;
|
|
176
|
+
const parsed = value.includes("T") ? new Date(value) : /* @__PURE__ */ new Date(`${value}T00:00:00Z`);
|
|
177
|
+
return Number.isNaN(parsed.getTime()) ? void 0 : parsed.toISOString();
|
|
178
|
+
}
|
|
179
|
+
function nowLocalDateTimeInputValue() {
|
|
180
|
+
return dateFns.format(/* @__PURE__ */ new Date(), "yyyy-MM-dd'T'HH:mm");
|
|
181
|
+
}
|
|
182
|
+
function localDateTimeInputToIsoUtc(value) {
|
|
183
|
+
if (!value) return void 0;
|
|
184
|
+
const parsed = new Date(value);
|
|
185
|
+
if (!dateFns.isValid(parsed)) return void 0;
|
|
186
|
+
return parsed.toISOString();
|
|
187
|
+
}
|
|
188
|
+
function ensureUtcIso(value) {
|
|
189
|
+
return /[Zz]|[+-]\d{2}:?\d{2}$/.test(value) ? value : value + "Z";
|
|
190
|
+
}
|
|
191
|
+
function isoUtcToLocalDateTimeInput(value, fallback = "") {
|
|
192
|
+
if (!value) return fallback;
|
|
193
|
+
const parsed = new Date(ensureUtcIso(value));
|
|
194
|
+
if (!dateFns.isValid(parsed)) return fallback;
|
|
195
|
+
return dateFns.format(parsed, "yyyy-MM-dd'T'HH:mm");
|
|
196
|
+
}
|
|
197
|
+
var getDateRange = (timePeriod, dateFormat = DATE_FORMAT) => {
|
|
198
|
+
const today = /* @__PURE__ */ new Date();
|
|
199
|
+
switch (timePeriod) {
|
|
200
|
+
case "month":
|
|
201
|
+
return { startDate: dateFns.format(dateFns.subMonths(today, 1), dateFormat), endDate: dateFns.format(today, dateFormat) };
|
|
202
|
+
case "quarter":
|
|
203
|
+
return { startDate: dateFns.format(dateFns.subMonths(today, 3), dateFormat), endDate: dateFns.format(today, dateFormat) };
|
|
204
|
+
case "year":
|
|
205
|
+
return { startDate: dateFns.format(dateFns.subYears(today, 1), dateFormat), endDate: dateFns.format(today, dateFormat) };
|
|
206
|
+
case "week":
|
|
207
|
+
default:
|
|
208
|
+
return { startDate: dateFns.format(dateFns.subDays(today, 7), dateFormat), endDate: dateFns.format(today, dateFormat) };
|
|
209
|
+
}
|
|
210
|
+
};
|
|
211
|
+
function computeDateRange(days) {
|
|
212
|
+
const to = (/* @__PURE__ */ new Date()).toISOString();
|
|
213
|
+
const from = new Date(Date.now() - days * 864e5).toISOString();
|
|
214
|
+
return { from, to };
|
|
215
|
+
}
|
|
216
|
+
function currentMonthRangeUtc(today = /* @__PURE__ */ new Date()) {
|
|
217
|
+
const year = today.getUTCFullYear();
|
|
218
|
+
const month = today.getUTCMonth();
|
|
219
|
+
return {
|
|
220
|
+
fromUtc: new Date(Date.UTC(year, month, 1)).toISOString(),
|
|
221
|
+
beforeUtc: new Date(Date.UTC(year, month + 1, 1)).toISOString()
|
|
222
|
+
};
|
|
223
|
+
}
|
|
224
|
+
function getCurrentOffsetMinutes(iana) {
|
|
225
|
+
try {
|
|
226
|
+
const parts = new Intl.DateTimeFormat("en", {
|
|
227
|
+
timeZone: iana,
|
|
228
|
+
timeZoneName: "longOffset"
|
|
229
|
+
}).formatToParts(/* @__PURE__ */ new Date());
|
|
230
|
+
const label = parts.find((p) => p.type === "timeZoneName")?.value ?? "";
|
|
231
|
+
if (label === "GMT" || label === "UTC") return 0;
|
|
232
|
+
const m = label.match(/GMT([+-])(\d{2}):(\d{2})/);
|
|
233
|
+
if (!m) return null;
|
|
234
|
+
const sign = m[1] === "+" ? 1 : -1;
|
|
235
|
+
return sign * (parseInt(m[2], 10) * 60 + parseInt(m[3], 10));
|
|
236
|
+
} catch {
|
|
237
|
+
return null;
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
exports.AUDIT_DATE_FORMAT = AUDIT_DATE_FORMAT;
|
|
242
|
+
exports.DATE_FORMAT = DATE_FORMAT;
|
|
243
|
+
exports.DAY_DATE_FORMAT = DAY_DATE_FORMAT;
|
|
244
|
+
exports.DAY_MONTH_FORMAT = DAY_MONTH_FORMAT;
|
|
245
|
+
exports.DAY_MONTH_YEAR_FORMAT = DAY_MONTH_YEAR_FORMAT;
|
|
246
|
+
exports.DISPLAY_DATE_FORMAT = DISPLAY_DATE_FORMAT;
|
|
247
|
+
exports.SHORT_DATETIME_FORMAT = SHORT_DATETIME_FORMAT;
|
|
248
|
+
exports.TIME_FORMAT = TIME_FORMAT;
|
|
249
|
+
exports.addDaysToIsoDate = addDaysToIsoDate;
|
|
250
|
+
exports.computeDateRange = computeDateRange;
|
|
251
|
+
exports.currentMonthRangeUtc = currentMonthRangeUtc;
|
|
252
|
+
exports.dateInputToIso = dateInputToIso;
|
|
253
|
+
exports.dateOnlyToIsoUtc = dateOnlyToIsoUtc;
|
|
254
|
+
exports.ensureUtcIso = ensureUtcIso;
|
|
255
|
+
exports.formatDate = formatDate;
|
|
256
|
+
exports.formatDateSafe = formatDateSafe;
|
|
257
|
+
exports.formatDateString = formatDateString;
|
|
258
|
+
exports.formatDateWithOrdinal = formatDateWithOrdinal;
|
|
259
|
+
exports.formatDuration = formatDuration;
|
|
260
|
+
exports.formatDurationNumber = formatDurationNumber;
|
|
261
|
+
exports.formatMillisecondsAsTimeSpent = formatMillisecondsAsTimeSpent;
|
|
262
|
+
exports.formatTimeAgo = formatTimeAgo;
|
|
263
|
+
exports.getCurrentOffsetMinutes = getCurrentOffsetMinutes;
|
|
264
|
+
exports.getDateRange = getDateRange;
|
|
265
|
+
exports.getMonthEndIso = getMonthEndIso;
|
|
266
|
+
exports.getMonthStartIso = getMonthStartIso;
|
|
267
|
+
exports.getTodayIsoDate = getTodayIsoDate;
|
|
268
|
+
exports.hoursToMilliseconds = hoursToMilliseconds;
|
|
269
|
+
exports.isDateOnlyString = isDateOnlyString;
|
|
270
|
+
exports.isoToDateInput = isoToDateInput;
|
|
271
|
+
exports.isoUtcToLocalDateTimeInput = isoUtcToLocalDateTimeInput;
|
|
272
|
+
exports.localDateTimeInputToIsoUtc = localDateTimeInputToIsoUtc;
|
|
273
|
+
exports.millisecondsToHours = millisecondsToHours;
|
|
274
|
+
exports.nowLocalDateTimeInputValue = nowLocalDateTimeInputValue;
|
|
275
|
+
exports.parseIsoDateLocal = parseIsoDateLocal;
|
|
276
|
+
exports.timespanToMilliseconds = timespanToMilliseconds;
|
|
277
|
+
exports.toDateOnlyString = toDateOnlyString;
|
|
278
|
+
exports.toHHmm = toHHmm;
|
|
279
|
+
exports.toHHmmss = toHHmmss;
|
|
280
|
+
//# sourceMappingURL=index.cjs.map
|
|
281
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/date/constants.ts","../../src/date/iso.ts","../../src/date/format.ts","../../src/date/duration.ts","../../src/date/wire.ts","../../src/date/range.ts"],"names":["format","isValid","parseISO","year","month","day","subMonths","subYears","subDays"],"mappings":";;;;;AASO,IAAM,WAAA,GAAc;AAGpB,IAAM,iBAAA,GAAoB;AAG1B,IAAM,mBAAA,GAAsB;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A,GAAK,QAAA,CAAS,CAAA,CAAE,CAAC,CAAA,EAAG,EAAE,CAAA,CAAA;AAAA,EAC5D,CAAA,CAAA,MAAQ;AACN,IAAA,OAAO,IAAA;AAAA,EACT;AACF","file":"index.cjs","sourcesContent":["/**\n * Shared date/time format tokens (date-fns syntax).\n *\n * These are the canonical formats used across the EthisysCore monolith and\n * plugins. Change a value here for a global locale requirement rather than\n * hardcoding a format string at a call site.\n */\n\n/** ISO wire format `yyyy-MM-dd` — the standard date-only format throughout the app. */\nexport const DATE_FORMAT = \"yyyy-MM-dd\";\n\n/** Audit-log timestamp, e.g. `5 March 2026, 14:30`. */\nexport const AUDIT_DATE_FORMAT = \"d MMMM yyyy, HH:mm\";\n\n/** User-facing date display, e.g. `5 Mar 2026`. */\nexport const DISPLAY_DATE_FORMAT = \"d MMM yyyy\";\n\n/** Weekday + day + month, e.g. `Monday, 5 March`. */\nexport const DAY_DATE_FORMAT = \"EEEE, d MMMM\";\n\n/** Abbreviated day + month, e.g. `Mar 5`. */\nexport const DAY_MONTH_FORMAT = \"MMM d\";\n\n/** Abbreviated day + month + year, e.g. `Mar 5, 2026`. */\nexport const DAY_MONTH_YEAR_FORMAT = \"MMM d, yyyy\";\n\n/** 12-hour time, e.g. `2:30 PM`. */\nexport const TIME_FORMAT = \"h:mm a\";\n\n/** Short datetime, e.g. `Mar 5, 2:30 PM`. */\nexport const SHORT_DATETIME_FORMAT = \"MMM d, h:mm a\";\n","import { format, isValid } from \"date-fns\";\n\nimport { DATE_FORMAT } from \"./constants\";\n\n/** Type guard: is `value` a bare `yyyy-MM-dd` date-only string? */\nexport const isDateOnlyString = (value: unknown): value is string => {\n return typeof value === \"string\" && /^\\d{4}-\\d{2}-\\d{2}$/.test(value);\n};\n\n/**\n * Formats a `Date`, defaulting to the standard application ISO date format\n * (`yyyy-MM-dd`). Pass `dateFormat` to override.\n */\nexport const formatDate = (date: Date, dateFormat: string = DATE_FORMAT): string => {\n return format(date, dateFormat);\n};\n\n/** Today's date as a `yyyy-MM-dd` ISO string (local calendar day). */\nexport const getTodayIsoDate = (): string => {\n return formatDate(new Date());\n};\n\n/** First day of the given date's month, as a `yyyy-MM-dd` ISO string. */\nexport const getMonthStartIso = (date: Date): string =>\n formatDate(new Date(date.getFullYear(), date.getMonth(), 1));\n\n/** Last day of the given date's month, as a `yyyy-MM-dd` ISO string. */\nexport const getMonthEndIso = (date: Date): string =>\n formatDate(new Date(date.getFullYear(), date.getMonth() + 1, 0));\n\n/**\n * Parses a date-only `yyyy-MM-dd` string as a LOCAL date. Returns `null` when\n * the value is not a date-only string or is not a valid calendar date. Avoids\n * the `new Date(\"yyyy-MM-dd\")` UTC parse, which shifts the day in non-UTC zones.\n */\nexport const parseIsoDateLocal = (value: string | null | undefined): Date | null => {\n if (!value || !isDateOnlyString(value)) return null;\n const [year, month, day] = value.split(\"-\").map(Number);\n const d = new Date(year, month - 1, day);\n return isValid(d) ? d : null;\n};\n\n/**\n * Adds `days` to an ISO `yyyy-MM-dd` date and returns the result as a `yyyy-MM-dd`\n * string. Parses and formats in local time (via `parseIsoDateLocal` / `formatDate`)\n * so the arithmetic can't drift across a day boundary in non-UTC timezones/DST.\n * Returns the input unchanged when it is not a valid date-only string.\n */\nexport const addDaysToIsoDate = (isoDate: string, days: number): string => {\n const base = parseIsoDateLocal(isoDate);\n if (!base) return isoDate;\n base.setDate(base.getDate() + days);\n return formatDate(base);\n};\n\n/**\n * Extracts a date-only string (`yyyy-MM-dd`) from a date string.\n * - Date-only strings (`2024-05-15`) are returned as-is.\n * - ISO datetime strings (`2024-05-15T00:00:00+05:30`) have the date portion\n * extracted directly from the string to preserve the original calendar date\n * without timezone conversion.\n * - Returns empty string for null, undefined, or unparseable values.\n */\nexport const toDateOnlyString = (value: string | null | undefined): string => {\n if (!value) return \"\";\n // Extract the yyyy-MM-dd portion before the \"T\" separator to avoid timezone\n // conversion that can shift the date by a day.\n const datepart = value.split(\"T\")[0];\n return isDateOnlyString(datepart) ? datepart : \"\";\n};\n","import { format, isValid, parseISO } from \"date-fns\";\n\nimport { DATE_FORMAT } from \"./constants\";\nimport { formatDate, isDateOnlyString } from \"./iso\";\n\n/**\n * Formats a date string, defaulting to the standard application format\n * (`yyyy-MM-dd`). Uses `parseISO` to safely handle ISO strings. Pass\n * `dateFormat` to override. Returns `\"-\"` for empty, missing, or unparseable\n * input rather than throwing.\n */\nexport const formatDateString = (date?: string | null, dateFormat: string = DATE_FORMAT): string => {\n if (!date) return \"-\";\n const parsed = parseISO(date);\n if (!isValid(parsed)) return \"-\";\n return format(parsed, dateFormat);\n};\n\n/**\n * Safely formats a date string or `Date`. Handles null, undefined, and invalid\n * dates by returning a fallback string.\n * @param date - The date to format (string, Date, null, or undefined)\n * @param formatStr - The desired output format (defaults to `DATE_FORMAT`)\n * @param fallback - Returned when the date is invalid or missing (defaults to `\"—\"`)\n */\nexport const formatDateSafe = (\n date: string | Date | null | undefined,\n formatStr: string = DATE_FORMAT,\n fallback: string = \"—\",\n): string => {\n if (!date) return fallback;\n try {\n const dateObj = typeof date === \"string\" ? parseISO(date) : date;\n if (!isValid(dateObj)) return fallback;\n return format(dateObj, formatStr);\n } catch {\n return fallback;\n }\n};\n\n/** Returns the ordinal suffix for a day of month (`st`, `nd`, `rd`, `th`). */\nconst getOrdinalSuffix = (day: number): string => {\n if (day > 3 && day < 21) return \"th\";\n switch (day % 10) {\n case 1:\n return \"st\";\n case 2:\n return \"nd\";\n case 3:\n return \"rd\";\n default:\n return \"th\";\n }\n};\n\n/**\n * Formats a date string or `Date` to a format like `31st Aug 2025`.\n * Handles date-only strings (`yyyy-MM-dd`) as local dates to avoid timezone shifts.\n * @param date - Date string (`yyyy-MM-dd`) or `Date`\n * @returns Formatted date string with ordinal suffix\n */\nexport const formatDateWithOrdinal = (date: string | Date | null | undefined): string => {\n if (!date) return \"Not specified\";\n\n let dateObj: Date;\n if (typeof date === \"string\") {\n if (isDateOnlyString(date)) {\n const [year, month, day] = date.split(\"-\").map(Number);\n dateObj = new Date(year, month - 1, day);\n } else {\n dateObj = parseISO(date);\n }\n } else {\n dateObj = date;\n }\n\n if (!isValid(dateObj)) return \"Invalid date\";\n\n const day = dateObj.getDate();\n const month = format(dateObj, \"MMM\");\n const year = dateObj.getFullYear();\n return `${day}${getOrdinalSuffix(day)} ${month} ${year}`;\n};\n\n/**\n * Formats a date as a relative time string (e.g. `2h ago`, `Just now`).\n * Optimised for short labels in dropdowns; anything older than a week falls\n * back to the standard date format.\n * @param date - ISO string or `Date`\n */\nexport const formatTimeAgo = (date: string | Date | null | undefined): string => {\n if (!date) return \"\";\n\n const dateObj = typeof date === \"string\" ? parseISO(date) : date;\n if (!isValid(dateObj)) return \"\";\n\n const now = new Date();\n const seconds = Math.floor((now.getTime() - dateObj.getTime()) / 1000);\n\n // Future dates (shouldn't normally happen, but safety first).\n if (seconds < 60) return \"Just now\";\n\n const minutes = Math.floor(seconds / 60);\n if (minutes < 60) return `${minutes}m ago`;\n\n const hours = Math.floor(minutes / 60);\n if (hours < 24) return `${hours}h ago`;\n\n const days = Math.floor(hours / 24);\n if (days < 7) return `${days}d ago`;\n\n return formatDate(dateObj);\n};\n","/**\n * Pure duration / timespan helpers — no external dependencies.\n *\n * Cover the two duration shapes that flow across the EthisysCore wire: a\n * `\"HH:mm:ss\"` timespan string (.NET `TimeSpan`) and a millisecond count.\n */\n\n/** Converts a `\"HH:mm:ss\"` timespan string to milliseconds. */\nexport const timespanToMilliseconds = (timespan: string): number => {\n if (!timespan) return 0;\n\n const [h = \"0\", m = \"0\", s = \"0\"] = timespan.split(\":\");\n const hours = parseInt(h || \"0\", 10);\n const minutes = parseInt(m || \"0\", 10);\n const seconds = parseInt(s || \"0\", 10);\n\n return (\n (isNaN(hours) ? 0 : hours) * 60 * 60 * 1000 +\n (isNaN(minutes) ? 0 : minutes) * 60 * 1000 +\n (isNaN(seconds) ? 0 : seconds) * 1000\n );\n};\n\n/** Converts milliseconds to hours (as a float). */\nexport const millisecondsToHours = (ms: number): number => ms / (1000 * 60 * 60);\n\n/** Converts hours to milliseconds. */\nexport const hoursToMilliseconds = (hours: number): number => hours * (1000 * 60 * 60);\n\n/** Formats a milliseconds count as `4h 15min`. */\nexport const formatMillisecondsAsTimeSpent = (ms: number): string => {\n if (!ms || ms < 0) return \"0h 0min\";\n\n const totalMinutes = Math.floor(ms / (1000 * 60));\n const hours = Math.floor(totalMinutes / 60);\n const minutes = totalMinutes % 60;\n\n let result = \"\";\n if (hours > 0) result += `${hours}h`;\n if (minutes > 0 || hours === 0) result += (hours > 0 ? \" \" : \"\") + `${minutes}min`;\n return result;\n};\n\n/** Formats a `\"HH:mm:ss\"` duration string as human readable (e.g. `2h 30m 0s`). */\nexport function formatDuration(duration?: string): string {\n if (!duration) return \"-\";\n const [hours, minutes, seconds] = duration.split(\":\").map(Number);\n let result = \"\";\n if (hours) result += `${hours}h`;\n if (minutes) result += (result ? \" \" : \"\") + `${minutes}m`;\n if (seconds) result += (result ? \" \" : \"\") + `${seconds}s`;\n return result || \"0m\";\n}\n\n/**\n * Formats a fractional-hours number as a compact human-readable duration\n * (`45m`, `2h 30m`, `3d 4h`). Defensively caps unreasonable values.\n */\nexport const formatDurationNumber = (durationHours?: number | null): string => {\n if (durationHours == null || durationHours === 0) return \"—\";\n\n // Defensive: cap at a reasonable max (1 year = 8760 hours).\n if (durationHours > 8760) return \"Unknown\";\n // Negative duration is also suspicious.\n if (durationHours < 0) return \"Error\";\n\n const hours = Math.floor(durationHours);\n const minutes = Math.floor((durationHours % 1) * 60);\n\n if (durationHours < 1) return `${minutes}m`;\n\n if (hours < 24) {\n if (minutes === 0) return `${hours}h`;\n return `${hours}h ${minutes}m`;\n }\n\n const days = Math.floor(hours / 24);\n const remainingHours = hours % 24;\n if (remainingHours === 0) return `${days}d`;\n return `${days}d ${remainingHours}h`;\n};\n\n/**\n * Trims an API-supplied `\"HH:mm:ss\"` down to the `\"HH:mm\"` string used by\n * MUI `TimePicker`-backed forms. Returns the supplied fallback when the value\n * is missing or shorter than five characters.\n */\nexport function toHHmm(value: string | null | undefined, fallback: string): string {\n if (!value || value.length < 5) return fallback;\n return value.slice(0, 5);\n}\n\n/**\n * Serialises a non-empty `\"HH:mm\"` / `\"HH:mm:ss\"` value back to the canonical\n * `\"HH:mm:ss\"` API format. Returns `null` for empty input so callers can block\n * the save rather than silently persisting midnight — an empty TimePicker\n * (cleared via keyboard) is an unsaved edit, not a legitimate `00:00:00` value.\n */\nexport function toHHmmss(value: string): string | null {\n const trimmed = value.trim();\n if (!trimmed) return null;\n return trimmed.length === 5 ? `${trimmed}:00` : trimmed;\n}\n","import { format, isValid } from \"date-fns\";\n\n// ---------------------------------------------------------------------------\n// Wire-boundary helpers\n// ---------------------------------------------------------------------------\n//\n// HTML's `<input type=\"date\">` and `<input type=\"datetime-local\">` emit naive\n// local strings with NO timezone suffix. Sending those raw to a .NET backend\n// that deserialises into `DateTimeOffset` parses them using the BE process's\n// local offset (UTC on most container hosts), silently misinterpreting the\n// user's wall-clock as UTC and shifting the stored timestamp by the user's\n// offset (e.g. losing the first hour of the local day during BST). These\n// helpers bridge cleanly between the input shapes and full ISO-8601 UTC.\n//\n// All are pure and timezone-aware via the runtime `Date` object.\n\n/** Converts an ISO date string to date-input format (`yyyy-MM-dd`). */\nexport const isoToDateInput = (isoString: string | undefined): string => {\n if (!isoString) return \"\";\n try {\n return new Date(isoString).toISOString().split(\"T\")[0];\n } catch {\n return \"\";\n }\n};\n\n/** Converts date-input format (`yyyy-MM-dd`) to a UTC ISO string at midnight UTC. */\nexport const dateInputToIso = (dateString: string): string => {\n if (!dateString) return \"\";\n const parsed = new Date(dateString + \"T00:00:00Z\");\n return Number.isNaN(parsed.getTime()) ? \"\" : parsed.toISOString();\n};\n\n/**\n * Normalises a date-only value from `<input type=\"date\">` (`yyyy-MM-dd`) into a\n * canonical UTC ISO-8601 timestamp at midnight UTC, e.g.\n * `2026-07-14` → `2026-07-14T00:00:00.000Z`. Use at the API-payload boundary\n * for fields the BE types as `DateTimeOffset` — a bare date-only string risks\n * ambiguous/failed deserialisation. A value that is already a full ISO\n * timestamp is passed through unchanged. Empty/invalid → `undefined` so callers\n * omit the field (BE clears it).\n */\nexport function dateOnlyToIsoUtc(value: string | null | undefined): string | undefined {\n if (!value) return undefined;\n const parsed = value.includes(\"T\") ? new Date(value) : new Date(`${value}T00:00:00Z`);\n return Number.isNaN(parsed.getTime()) ? undefined : parsed.toISOString();\n}\n\n/**\n * Returns the current local wall-clock as a `yyyy-MM-ddTHH:mm` string — the\n * format an `<input type=\"datetime-local\">` element expects. Minute precision;\n * seconds and timezone deliberately omitted.\n */\nexport function nowLocalDateTimeInputValue(): string {\n return format(new Date(), \"yyyy-MM-dd'T'HH:mm\");\n}\n\n/**\n * Converts a naive local-time string from `<input type=\"datetime-local\">`\n * (shape `yyyy-MM-ddTHH:mm` or `yyyy-MM-ddTHH:mm:ss`) into a full ISO-8601 UTC\n * string with `Z` suffix that .NET `DateTimeOffset` cannot ambiguously\n * interpret. Empty / undefined inputs pass through as `undefined`.\n */\nexport function localDateTimeInputToIsoUtc(value: string | null | undefined): string | undefined {\n if (!value) return undefined;\n const parsed = new Date(value);\n if (!isValid(parsed)) return undefined;\n return parsed.toISOString();\n}\n\n/**\n * Ensures an ISO-8601 string is treated as UTC by appending a `Z` suffix when\n * no explicit offset or UTC indicator is present. The backend may emit\n * timestamps like `2026-05-26T06:21:00` (no Z), which JS would otherwise parse\n * as local time.\n */\nexport function ensureUtcIso(value: string): string {\n return /[Zz]|[+-]\\d{2}:?\\d{2}$/.test(value) ? value : value + \"Z\";\n}\n\n/**\n * Converts an ISO-8601 string (with or without explicit offset) into the naive\n * local-time shape `yyyy-MM-ddTHH:mm` expected by `<input type=\"datetime-local\">`.\n * Returns the supplied fallback (default empty string) when the input is\n * missing or unparseable. When `value` carries no offset or `Z` suffix it is\n * treated as UTC before converting to local time, so the edit form displays the\n * correct time regardless of the user's timezone.\n */\nexport function isoUtcToLocalDateTimeInput(value: string | null | undefined, fallback = \"\"): string {\n if (!value) return fallback;\n const parsed = new Date(ensureUtcIso(value));\n if (!isValid(parsed)) return fallback;\n return format(parsed, \"yyyy-MM-dd'T'HH:mm\");\n}\n","import { format, subDays, subMonths, subYears } from \"date-fns\";\n\nimport { DATE_FORMAT } from \"./constants\";\n\nexport interface DateRange {\n startDate: string;\n endDate: string;\n}\n\n/**\n * Returns a `{ startDate, endDate }` date range for a named relative period,\n * formatted with `dateFormat` (defaults to the ISO date-only format). Unknown\n * periods fall back to the last week.\n * @param timePeriod - one of `week` | `month` | `quarter` | `year`\n * @param dateFormat - output format for both bounds (defaults to `DATE_FORMAT`)\n */\nexport const getDateRange = (timePeriod: string, dateFormat: string = DATE_FORMAT): DateRange => {\n const today = new Date();\n switch (timePeriod) {\n case \"month\":\n return { startDate: format(subMonths(today, 1), dateFormat), endDate: format(today, dateFormat) };\n case \"quarter\":\n return { startDate: format(subMonths(today, 3), dateFormat), endDate: format(today, dateFormat) };\n case \"year\":\n return { startDate: format(subYears(today, 1), dateFormat), endDate: format(today, dateFormat) };\n case \"week\":\n default:\n return { startDate: format(subDays(today, 7), dateFormat), endDate: format(today, dateFormat) };\n }\n};\n\n/** Computes an ISO timestamp range from a number of days back to now. */\nexport function computeDateRange(days: number): { from: string; to: string } {\n const to = new Date().toISOString();\n const from = new Date(Date.now() - days * 86_400_000).toISOString();\n return { from, to };\n}\n\n/**\n * Returns the current calendar month as a half-open UTC range `[fromUtc, beforeUtc)`,\n * suitable for \"this month\" count filters. `fromUtc` is the first instant of the\n * month; `beforeUtc` is the first instant of the next month (exclusive).\n */\nexport function currentMonthRangeUtc(today: Date = new Date()): { fromUtc: string; beforeUtc: string } {\n const year = today.getUTCFullYear();\n const month = today.getUTCMonth();\n return {\n fromUtc: new Date(Date.UTC(year, month, 1)).toISOString(),\n beforeUtc: new Date(Date.UTC(year, month + 1, 1)).toISOString(),\n };\n}\n\n/**\n * Computes the current UTC-offset minutes for a given IANA zone name using the\n * runtime's `Intl` implementation. Returns `null` when the zone is unknown.\n *\n * Used by timezone auto-populate fallbacks so a browser reporting an IANA zone\n * that isn't in the backend seed (e.g. `Europe/London` during BST) can still\n * match against a seeded zone sharing the same current offset.\n */\nexport function getCurrentOffsetMinutes(iana: string): number | null {\n try {\n const parts = new Intl.DateTimeFormat(\"en\", {\n timeZone: iana,\n timeZoneName: \"longOffset\",\n }).formatToParts(new Date());\n const label = parts.find((p) => p.type === \"timeZoneName\")?.value ?? \"\";\n if (label === \"GMT\" || label === \"UTC\") return 0;\n const m = label.match(/GMT([+-])(\\d{2}):(\\d{2})/);\n if (!m) return null;\n const sign = m[1] === \"+\" ? 1 : -1;\n return sign * (parseInt(m[2], 10) * 60 + parseInt(m[3], 10));\n } catch {\n return null;\n }\n}\n"]}
|