@wavyx/pdcli 0.3.0 → 0.5.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.
@@ -0,0 +1,33 @@
1
+ import { CliError } from './errors.js'
2
+
3
+ const DAY_MS = 86_400_000
4
+
5
+ /**
6
+ * Parse a trailing period like "90d", "30d", or "3m" (months = 30 days)
7
+ * into the start date of the window.
8
+ * @param {string} period
9
+ * @param {Date} [now]
10
+ * @returns {Date}
11
+ */
12
+ export function parsePeriod(period, now = new Date()) {
13
+ const match = /^(\d+)([dm])$/.exec(period)
14
+ if (!match) {
15
+ throw new CliError(
16
+ `Invalid period "${period}" — use Nd (days) or Nm (months), e.g. 90d`,
17
+ { exitCode: 64 },
18
+ )
19
+ }
20
+ const amount = Number(match[1])
21
+ const days = match[2] === 'm' ? amount * 30 : amount
22
+ return new Date(now.getTime() - days * DAY_MS)
23
+ }
24
+
25
+ /**
26
+ * Format a date the way v2 query params accept it: RFC 3339 seconds
27
+ * precision, no milliseconds (the API rejects fractional seconds).
28
+ * @param {Date} date
29
+ * @returns {string}
30
+ */
31
+ export function formatApiDatetime(date) {
32
+ return date.toISOString().replace(/\.\d{3}Z$/, 'Z')
33
+ }