From 9577a02278ae1ee4f6c7f7ebe639898ffef7090d Mon Sep 17 00:00:00 2001 From: blamouche Date: Tue, 19 May 2026 23:08:28 +0200 Subject: [PATCH] Add Automations feature: scheduled prompts from a vault folder MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A new "Automations" affordance reads markdown files from a configurable vault folder, parses a frontmatter schedule (`interval` minutes or 5-field `cron`) plus optional `enabled`/`runtime`/`appendNewline`/`name`, and fires the file body into the running CLI on schedule or manually from a modal. Each fire (or skip / error) is logged to a ring-buffer history capped at 200 entries; the modal exposes the entries with status, last/next run, a per-row Run now button, and a History tab with Clear / Export-as-markdown. The scheduler runs at the plugin level (`activeWindow.setInterval`, 30 s tick) and stays alive whether the panel is open or not. The folder cache live-updates on vault create/modify/delete/rename. Runs skip cleanly when no CLI is running — the plugin never auto-starts a runtime. Implementation: pure logic in automation.ts (28 vitest cases), wiring in main.ts, AutomationsModal in automations-modal.ts. Adds `cron-parser` (runtime) and `yaml` (dev). Co-Authored-By: Claude Opus 4.7 (1M context) --- .prompt-hub/memory.md | 1 + .prompt-hub/releases.md | 7 + ...223408-create-feature-automation-branch.md | 6 +- ...19-225317-automations-recurring-prompts.md | 41 + .prompt-hub/version.md | 2 +- README.md | 49 + automation.ts | 267 + automations-modal.ts | 269 + main.js | 9847 ++++++++++++++++- main.ts | 345 +- package-lock.json | 31 +- package.json | 4 +- styles.css | 221 + tests/automation.test.ts | 276 + 14 files changed, 11327 insertions(+), 39 deletions(-) create mode 100644 .prompt-hub/todo/todo-20260519-225317-automations-recurring-prompts.md create mode 100644 automation.ts create mode 100644 automations-modal.ts create mode 100644 tests/automation.test.ts diff --git a/.prompt-hub/memory.md b/.prompt-hub/memory.md index ba7fefb..a0b2007 100644 --- a/.prompt-hub/memory.md +++ b/.prompt-hub/memory.md @@ -66,3 +66,4 @@ | 2026-04-27 23:09:00 CEST | agent | User reported the community-store auto-install only ships `main.js`, `manifest.json`, `styles.css`, which doesn't include the `pty-proxy.js` / `pty-bridge.py` files the plugin spawns. Fix: embedded both auxiliary file contents into `main.js` at build time via esbuild `define` (`PTY_PROXY_SOURCE`, `PTY_BRIDGE_SOURCE` constants declared in main.ts), and added `ensureProxyFiles(pluginDir)` that writes them lazily before each `spawnPtyProxy` call (only when missing or content drift). Verified embedded markers (`spawnPythonBridge`, `decode_payload`, `TIOCSWINSZ`, `pty.fork`) present in built main.js. README `Required files` reworked accordingly. Bumped to 0.1.46. | `esbuild.config.mjs`, `main.ts`, `main.js`, `manifest.json`, `versions.json`, `package.json`, `package-lock.json`, `README.md`, `.prompt-hub/version.md`, `.prompt-hub/releases.md` | success | Commit, push, annotated tag 0.1.46. User republishes draft so community-store install works for new users. | | 2026-05-12 23:12:00 CEST | agent | Trimmed the release zip staging step to the 3 canonical Obsidian plugin files (`manifest.json`, `main.js`, `styles.css`). The auxiliary `pty-proxy.js` / `pty-bridge.py` are now embedded in main.js so they no longer need to ship; `versions.json`, `package.json`, `package-lock.json` also dropped from the zip (`versions.json` stays as a standalone release asset since Obsidian fetches it directly from the release URL). Updated release notes body + README `Release` and `Required files` sections. Dry-run zip listing confirmed 3 files in the `any-ai-cli/` folder. Bumped to 0.1.47. | `.github/workflows/release.yml`, `README.md`, `manifest.json`, `versions.json`, `package.json`, `package-lock.json`, `.prompt-hub/version.md`, `.prompt-hub/releases.md` | success | Commit, push, annotated tag 0.1.47. | | 2026-05-19 22:34:08 CEST | agent | Created new branch `feature/automation` from `main` for upcoming automation work; bumped minor version 0.1.48 → 0.2.0 per Prompt Hub branch protocol and logged the release entry. | `git checkout -b feature/automation`, `.prompt-hub/version.md`, `.prompt-hub/releases.md`, `.prompt-hub/todo/todo-20260519-223408-create-feature-automation-branch.md` | success | Commit the version/releases bump on `feature/automation` and push the new branch upstream. | +| 2026-05-19 23:08:00 CEST | agent | Implemented the Automations feature on `feature/automation`: new `automation.ts` (pure parser/scheduler logic with 28 vitest cases), plugin-level scheduler in `main.ts` (ticks every 30 s, vault `create/modify/delete/rename` listeners, `triggerAutomation` with `scheduler` vs `manual` source, history ring-buffer capped at 200), `sendAutomationPrompt` method on `ClaudeCliView`, new "Automations" toolbar button, new `AutomationsModal` with **Automations** + **History** tabs (parse errors surfaced, Run now per row, Clear history, Export as markdown), settings section with `Automations folder` + `Reload now`, modal styles in `styles.css`, README updated with feature + format docs. Added `cron-parser` (runtime) and `yaml` (dev). Validated `npm run lint`, `npm run build`, `npm test` (55 tests pass). Bumped 0.2.0 → 0.2.1. | `automation.ts`, `automations-modal.ts`, `tests/automation.test.ts`, `main.ts`, `styles.css`, `README.md`, `package.json`, `package-lock.json`, `.prompt-hub/version.md`, `.prompt-hub/releases.md`, `.prompt-hub/todo/todo-20260519-225317-automations-recurring-prompts.md` | success | Commit and push to `feature/automation`. | diff --git a/.prompt-hub/releases.md b/.prompt-hub/releases.md index 9f8d597..5d1ae71 100644 --- a/.prompt-hub/releases.md +++ b/.prompt-hub/releases.md @@ -1,5 +1,12 @@ # Releases +## 0.2.1 - 2026-05-19 +- Added the Automations feature: pick a vault folder of markdown files (frontmatter `interval` in minutes or 5-field `cron`, optional `enabled`/`runtime`/`appendNewline`/`name`; body = prompt) and the plugin fires them into the running CLI on schedule. Skips runs when no CLI is running (logged, never auto-starts). +- Added a new `Automations` toolbar button that opens a modal with two tabs: **Automations** (list with schedule, last run, next run, status, per-row Run now + Open file; surfaces parse errors) and **History** (chronological log capped at 200 entries with status badges, Clear history, Export as markdown). +- Added plugin-level scheduler (ticks every 30 s) wired via `registerInterval` + vault `create/modify/delete/rename` events so the folder cache live-updates. +- Added new module `automation.ts` (pure logic: frontmatter parser, `computeNextRun`, `isDue`, `pushHistory`, `describeSchedule`, `buildPromptPreview`) with 28 vitest cases covering parsing, scheduling, ring-buffer truncation and preview formatting. +- Added `cron-parser` runtime dep and `yaml` devDep; bumped `package-lock.json`. + ## 0.2.0 - 2026-05-19 - Opened branch `feature/automation` off `main` for upcoming automation work; bumped minor version per Prompt Hub branch protocol. diff --git a/.prompt-hub/todo/todo-20260519-223408-create-feature-automation-branch.md b/.prompt-hub/todo/todo-20260519-223408-create-feature-automation-branch.md index 82379e0..eea069f 100644 --- a/.prompt-hub/todo/todo-20260519-223408-create-feature-automation-branch.md +++ b/.prompt-hub/todo/todo-20260519-223408-create-feature-automation-branch.md @@ -13,4 +13,8 @@ Create a new git branch `feature/automation` off `main` for upcoming automation 7. Push the branch to `origin` with upstream tracking. ## Review -- _to be filled in after execution_ +- Created branch `feature/automation` from `main` (commit `366733d`) and switched to it. +- Bumped `.prompt-hub/version.md` `0.1.48` → `0.2.0`; added a `0.2.0` entry to `.prompt-hub/releases.md`. +- Logged the action in `.prompt-hub/memory.md`. +- Pushed the branch upstream (`origin/feature/automation`, tracking set up). +- Status: **completed**. diff --git a/.prompt-hub/todo/todo-20260519-225317-automations-recurring-prompts.md b/.prompt-hub/todo/todo-20260519-225317-automations-recurring-prompts.md new file mode 100644 index 0000000..802ac3a --- /dev/null +++ b/.prompt-hub/todo/todo-20260519-225317-automations-recurring-prompts.md @@ -0,0 +1,41 @@ +# Task: Automations — prompts récurrents depuis un dossier vault + +Branch: `feature/automation` +Plan: `/Users/benoitlamouche/.claude/plans/j-aimerais-ajouter-une-option-magical-river.md` + +## Objective +Permettre à l'utilisateur de définir un dossier de prompts (markdown + frontmatter) qui sont auto-envoyés au CLI selon une récurrence (interval ou cron) ou manuellement depuis un bouton de toolbar. Historique des exécutions tracké et consultable. + +## Plan d'exécution +1. Ajouter `cron-parser` aux dépendances. +2. Créer `automation.ts` — parser frontmatter, types, `computeNextRun`, `isDue`, `pushHistory` (logique pure). +3. Créer `tests/automation.test.ts` — couverture parser + scheduling + ring buffer. +4. Étendre `ClaudeCliPluginSettings` + `DEFAULT_SETTINGS` + `loadSettings` dans `main.ts` (champs automations). +5. Ajouter scheduler + `triggerAutomation` + `recordHistory` + `loadAutomations` à la classe `ClaudeCliPlugin`. +6. Ajouter méthode publique `sendAutomationPrompt` sur `ClaudeCliView`. +7. Ajouter section "Automations" au settings tab. +8. Ajouter bouton "Automations" dans la toolbar du panel + wire vers modal. +9. Créer `automations-modal.ts` (onglets Automations / History). +10. Ajouter styles modal dans `styles.css`. +11. Lint + build + typecheck + tests. +12. Mettre à jour README. +13. Bumper version + releases + memory + review du todo. +14. Commit + push. + +## Review +- Implémenté la feature complète selon le plan : + - `automation.ts` (parser frontmatter + `computeNextRun` / `isDue` / `pushHistory` / `buildPromptPreview` / `describeSchedule`). + - `tests/automation.test.ts` — 28 cas (parsing OK + erreurs, scheduling interval/cron, ring-buffer truncation, preview formatting). + - `main.ts` — étendu `ClaudeCliPluginSettings` + `loadSettings` (avec `sanitizeLastRun` / `sanitizeHistory`), scheduler plugin-level via `activeWindow.setInterval` + 30 s tick + `onLayoutReady` initial tick, vault `create/modify/delete/rename` events filtrés sur le préfixe de dossier, `triggerAutomation` avec `scheduler`/`manual` source, `recordHistory` + `clearAutomationHistory`, méthodes publiques `isProcessRunning` / `getRunningRuntimeId` / `matchesRuntime` / `sendAutomationPrompt` sur `ClaudeCliView`. + - Nouveau bouton "Automations" dans la toolbar (`secondaryRowEl`) avec icône `calendar-clock`, ouvre `AutomationsModal`. + - Section settings "Automations" (champ `automationsFolder` + bouton `Reload now`). + - `automations-modal.ts` — onglets **Automations** (tableau Name/Schedule/Last run/Next run/Status/Actions, Run now désactivé si CLI off, Open file, erreurs de parsing en haut) et **History** (liste capée, badges colorés par status, `Clear history`, `Export as markdown`). + - `styles.css` — styles modal complets (tabs, table, badges, history list). + - `README.md` — entrée feature + section **Automations** (setup, format, manual runs + history). + - Deps : `cron-parser` (runtime), `yaml` (dev). +- Validations : + - `npm run lint` → clean (corrections : `activeWindow.setInterval`, suppression `console.info/error`, sentence-case "Markdown"/"readme", typage `unknown` du parser YAML dans les tests). + - `npm run build` → OK. + - `npm test` → 55 tests pass (28 nouveaux + 27 existants). +- Bumped version 0.2.0 → 0.2.1, log mémoire ajouté. +- Statut : **completed** (reste : commit + push, et test manuel utilisateur dans un vault de dev). diff --git a/.prompt-hub/version.md b/.prompt-hub/version.md index 0ea3a94..0c62199 100644 --- a/.prompt-hub/version.md +++ b/.prompt-hub/version.md @@ -1 +1 @@ -0.2.0 +0.2.1 diff --git a/README.md b/README.md index ec18886..06003ac 100644 --- a/README.md +++ b/README.md @@ -11,6 +11,7 @@ The plugin embeds a real PTY-backed terminal in the sidebar and lets you declare - Dedicated sidebar view with an embedded `xterm` terminal. - **Customizable runtime list** — declare any number of CLI runtimes from settings (Claude and Codex are pre-populated; add Aider, custom wrappers, anything on `PATH`) and switch between them via a sidebar dropdown. - One-click `@active file` and `@active folder` buttons that insert the current note path (or its parent folder) as a mention in the running CLI's stdin. +- **Automations** — drop markdown files (prompt + frontmatter schedule) in a vault folder and have them fired into the running CLI on an `interval` or `cron`, or run them manually from a modal with a per-run history log. - Process controls in the toolbar: `Start`, `Stop`, `Restart`, `Clear`. - Launches the selected runtime in the **current active vault folder** so the AI sees your notes as the working tree. - Resilient PTY stack with multi-tier fallbacks (`node-pty` → Python PTY bridge → direct pipe → `script`) so it works on macOS, Linux, and Windows. @@ -88,6 +89,49 @@ The community-store auto-install and the release zip both ship only the three ca - Click `@active file` or `@active folder` (second toolbar row) to insert the current note path or its parent folder as a mention. - Click `Restart` to relaunch, `Stop` to terminate, `Clear` to wipe the terminal output. 5. Switching the dropdown to another runtime while a process is running automatically restarts it on the new CLI (configurable in settings). +6. Click `Automations` (second toolbar row) to open the Automations modal: run any prompt manually with **Run now**, or browse the **History** tab to see what fired and when. + +## Automations + +Automations let you store reusable prompts as markdown files in your vault and have the plugin send them to the running CLI either on a schedule (`interval` or `cron`) or manually from the **Automations** modal. + +### Setup + +1. Pick a folder in your vault to hold the prompts (e.g. `Automations`). +2. In plugin settings → **Automations folder**, set that path. Leave empty to disable the feature. +3. Drop one markdown file per automation in that folder. The plugin scans the folder on startup and live-updates on vault changes (create / modify / delete / rename). + +### File format + +Each automation is a regular markdown file with YAML frontmatter that sets the schedule, plus a body containing the prompt that will be sent to the CLI verbatim. + +```markdown +--- +name: Daily summary # optional, defaults to the filename +enabled: true # optional, defaults to true +interval: 60 # minutes — exclusive with `cron` +# cron: "0 9 * * 1-5" # standard 5-field cron — exclusive with `interval` +runtime: claude # optional — only fire if this runtime id or display name is running +appendNewline: true # optional, defaults to true (adds "\n" so the CLI executes the prompt) +--- + +Summarize my notes from the last 24h and propose three priorities for today. +``` + +Rules: + +- Exactly one of `interval` or `cron` must be set. `interval` is in whole minutes (>= 1). `cron` uses standard 5-field syntax (`cron-parser`). +- `enabled: false` keeps the entry visible in the modal but skips scheduling and disables the **Run now** button. +- The prompt is everything after the closing `---` (trimmed). +- If the configured `runtime` does not match the currently running CLI, the run is skipped and logged in History. +- If no CLI is running when a scheduled run is due, the run is skipped (the plugin never auto-starts a CLI for you). + +### Manual runs and history + +The **Automations** toolbar button opens a modal with two tabs: + +- **Automations** — list of parsed entries with schedule, last run, next run, status badge, and a **Run now** button per row (disabled when no CLI is running). Parse errors are shown at the top with file paths and reasons. +- **History** — chronological log of every fired run (or skip / error), capped at 200 entries by default. You can **Clear history** or **Export as markdown** to create a snapshot note in the vault. ## Plugin Settings @@ -106,6 +150,11 @@ Runtimes section (the customizable list of CLIs shown in the sidebar dropdown): - Add as many entries as you need with **Add runtime**. Remove unused ones via the trash icon (the list must keep at least one entry). - Claude and Codex are pre-populated on first install. Old `command` / `codexCommand` settings from earlier versions are migrated automatically. +Automations section: + +- **Automations folder** — vault-relative path to the folder holding automation markdown files. Leave empty to disable. See the [Automations](#automations) section above for the file format. +- **Reload automations** — force a re-scan (otherwise the plugin already refreshes on any vault change inside the folder). + Advanced: - **Node executable** — path to the Node binary used to run the PTY proxy. Leave as `auto` for automatic detection, or override with an explicit path (`/opt/homebrew/bin/node`, `C:\Program Files\nodejs\node.exe`, etc.). diff --git a/automation.ts b/automation.ts new file mode 100644 index 0000000..4199bac --- /dev/null +++ b/automation.ts @@ -0,0 +1,267 @@ +import { CronExpressionParser } from "cron-parser"; + +export interface AutomationFrontmatter { + name?: string; + enabled?: boolean; + interval?: number; + cron?: string; + runtime?: string; + appendNewline?: boolean; +} + +export interface ParsedAutomation { + path: string; + name: string; + enabled: boolean; + interval: number | null; + cron: string | null; + runtime: string | null; + appendNewline: boolean; + body: string; +} + +export interface AutomationParseError { + path: string; + name: string; + reason: string; +} + +export type ParseResult = + | { ok: true; entry: ParsedAutomation } + | { ok: false; error: AutomationParseError }; + +export interface AutomationRunRecord { + ts: number; + path: string; + name: string; + source: "scheduler" | "manual"; + status: "ran" | "skipped" | "error"; + reason?: string; + runtimeId?: string | null; + promptPreview?: string; +} + +const FRONTMATTER_RE = /^---\s*\r?\n([\s\S]*?)\r?\n---\s*(?:\r?\n)?([\s\S]*)$/; + +export interface FrontmatterParser { + (yaml: string): unknown; +} + +export function splitFrontmatter(content: string): { yaml: string | null; body: string } { + const match = FRONTMATTER_RE.exec(content); + if (!match) { + return { yaml: null, body: content }; + } + return { yaml: match[1], body: match[2] ?? "" }; +} + +export function basenameWithoutExt(filePath: string): string { + const lastSlash = filePath.lastIndexOf("/"); + const file = lastSlash >= 0 ? filePath.slice(lastSlash + 1) : filePath; + const dot = file.lastIndexOf("."); + return dot > 0 ? file.slice(0, dot) : file; +} + +export function parseAutomationFile( + content: string, + filePath: string, + parseYaml: FrontmatterParser +): ParseResult { + const fallbackName = basenameWithoutExt(filePath); + const { yaml, body } = splitFrontmatter(content); + + if (yaml === null) { + return { + ok: false, + error: { + path: filePath, + name: fallbackName, + reason: "Missing frontmatter block (expected `---` fenced YAML at top of file)." + } + }; + } + + let parsed: unknown; + try { + parsed = parseYaml(yaml); + } catch (err) { + return { + ok: false, + error: { + path: filePath, + name: fallbackName, + reason: `YAML parse error: ${(err as Error).message}` + } + }; + } + + if (!parsed || typeof parsed !== "object") { + return { + ok: false, + error: { + path: filePath, + name: fallbackName, + reason: "Frontmatter must be a YAML mapping." + } + }; + } + + const fm = parsed as AutomationFrontmatter; + const hasInterval = fm.interval !== undefined && fm.interval !== null; + const hasCron = typeof fm.cron === "string" && fm.cron.trim().length > 0; + + if (hasInterval && hasCron) { + return { + ok: false, + error: { + path: filePath, + name: typeof fm.name === "string" && fm.name.trim() ? fm.name.trim() : fallbackName, + reason: "`interval` and `cron` are mutually exclusive — set only one." + } + }; + } + + if (!hasInterval && !hasCron) { + return { + ok: false, + error: { + path: filePath, + name: typeof fm.name === "string" && fm.name.trim() ? fm.name.trim() : fallbackName, + reason: "Missing schedule: set either `interval` (minutes) or `cron`." + } + }; + } + + let interval: number | null = null; + if (hasInterval) { + const raw = fm.interval; + if (typeof raw !== "number" || !Number.isFinite(raw) || !Number.isInteger(raw) || raw < 1) { + return { + ok: false, + error: { + path: filePath, + name: typeof fm.name === "string" && fm.name.trim() ? fm.name.trim() : fallbackName, + reason: "`interval` must be an integer >= 1 (minutes)." + } + }; + } + interval = raw; + } + + let cron: string | null = null; + if (hasCron) { + const expr = (fm.cron as string).trim(); + try { + CronExpressionParser.parse(expr); + } catch (err) { + return { + ok: false, + error: { + path: filePath, + name: typeof fm.name === "string" && fm.name.trim() ? fm.name.trim() : fallbackName, + reason: `Invalid cron expression: ${(err as Error).message}` + } + }; + } + cron = expr; + } + + const trimmedBody = body.trim(); + if (!trimmedBody) { + return { + ok: false, + error: { + path: filePath, + name: typeof fm.name === "string" && fm.name.trim() ? fm.name.trim() : fallbackName, + reason: "Prompt body is empty (write the prompt below the frontmatter)." + } + }; + } + + return { + ok: true, + entry: { + path: filePath, + name: typeof fm.name === "string" && fm.name.trim() ? fm.name.trim() : fallbackName, + enabled: typeof fm.enabled === "boolean" ? fm.enabled : true, + interval, + cron, + runtime: typeof fm.runtime === "string" && fm.runtime.trim() ? fm.runtime.trim() : null, + appendNewline: typeof fm.appendNewline === "boolean" ? fm.appendNewline : true, + body: trimmedBody + } + }; +} + +/** + * Returns the epoch ms of the next scheduled run, or null if the entry has no + * usable schedule. Cron schedules are advanced from `lastRun` (or `now` if never + * run); interval schedules add `interval * 60_000` to `lastRun`, or fire immediately + * when never run. + */ +export function computeNextRun( + entry: ParsedAutomation, + lastRun: number | null, + now: number +): number | null { + if (entry.interval !== null) { + if (lastRun === null) { + return now; + } + return lastRun + entry.interval * 60_000; + } + if (entry.cron !== null) { + try { + const reference = lastRun ?? now - 1; + const it = CronExpressionParser.parse(entry.cron, { + currentDate: new Date(reference) + }); + return it.next().getTime(); + } catch { + return null; + } + } + return null; +} + +export function isDue( + entry: ParsedAutomation, + lastRun: number | null, + now: number +): boolean { + if (!entry.enabled) { + return false; + } + const next = computeNextRun(entry, lastRun, now); + return next !== null && next <= now; +} + +export function describeSchedule(entry: ParsedAutomation): string { + if (entry.interval !== null) { + return entry.interval === 1 ? "every 1 min" : `every ${entry.interval} min`; + } + if (entry.cron !== null) { + return `cron: ${entry.cron}`; + } + return "no schedule"; +} + +export function pushHistory( + history: AutomationRunRecord[], + record: AutomationRunRecord, + limit: number +): AutomationRunRecord[] { + const next = [record, ...history]; + if (next.length > limit) { + next.length = limit; + } + return next; +} + +export function buildPromptPreview(body: string, maxLen = 120): string { + const oneLine = body.replace(/\s+/g, " ").trim(); + if (oneLine.length <= maxLen) { + return oneLine; + } + return `${oneLine.slice(0, maxLen - 1)}…`; +} diff --git a/automations-modal.ts b/automations-modal.ts new file mode 100644 index 0000000..5709a03 --- /dev/null +++ b/automations-modal.ts @@ -0,0 +1,269 @@ +import { App, Modal, Notice, setIcon } from "obsidian"; +import type ClaudeCliPlugin from "./main"; +import { + computeNextRun, + describeSchedule, + type AutomationRunRecord, + type ParsedAutomation +} from "./automation"; + +type Tab = "automations" | "history"; + +export class AutomationsModal extends Modal { + private plugin: ClaudeCliPlugin; + private currentTab: Tab = "automations"; + private tabsHostEl: HTMLDivElement | null = null; + private bodyEl: HTMLDivElement | null = null; + private unsubscribe: (() => void) | null = null; + + constructor(app: App, plugin: ClaudeCliPlugin) { + super(app); + this.plugin = plugin; + } + + onOpen(): void { + this.modalEl.addClass("any-ai-cli-automations-modal"); + this.titleEl.setText("Automations"); + this.contentEl.empty(); + + this.tabsHostEl = this.contentEl.createDiv({ cls: "any-ai-cli-am-tabs" }); + this.bodyEl = this.contentEl.createDiv({ cls: "any-ai-cli-am-body" }); + + this.renderTabs(); + this.renderBody(); + + this.unsubscribe = this.plugin.onAutomationsChanged(() => { + this.renderBody(); + }); + } + + onClose(): void { + this.unsubscribe?.(); + this.unsubscribe = null; + this.contentEl.empty(); + } + + private renderTabs(): void { + if (!this.tabsHostEl) return; + this.tabsHostEl.empty(); + const tabs: { id: Tab; label: string }[] = [ + { id: "automations", label: "Automations" }, + { id: "history", label: "History" } + ]; + for (const tab of tabs) { + const btn = this.tabsHostEl.createEl("button", { + text: tab.label, + cls: `any-ai-cli-am-tab${this.currentTab === tab.id ? " is-active" : ""}` + }); + btn.addEventListener("click", () => { + if (this.currentTab !== tab.id) { + this.currentTab = tab.id; + this.renderTabs(); + this.renderBody(); + } + }); + } + } + + private renderBody(): void { + if (!this.bodyEl) return; + this.bodyEl.empty(); + if (this.currentTab === "automations") { + this.renderAutomationsTab(this.bodyEl); + } else { + this.renderHistoryTab(this.bodyEl); + } + } + + private renderAutomationsTab(host: HTMLDivElement): void { + const folder = this.plugin.settings.automationsFolder.trim(); + if (!folder) { + host.createEl("p", { + text: "No automations folder configured. Set one in plugin settings to get started.", + cls: "any-ai-cli-am-empty" + }); + return; + } + + const entries = this.plugin.getAutomations(); + const errors = this.plugin.getAutomationErrors(); + const enabledCount = entries.filter((e) => e.enabled).length; + const disabledCount = entries.length - enabledCount; + + const info = host.createEl("p", { cls: "any-ai-cli-am-info" }); + info.setText( + `${entries.length} automation${entries.length === 1 ? "" : "s"} (${enabledCount} enabled, ${disabledCount} disabled) — folder: ${folder}` + ); + + if (errors.length > 0) { + const errBox = host.createDiv({ cls: "any-ai-cli-am-errors" }); + errBox.createEl("strong", { text: `${errors.length} file${errors.length === 1 ? "" : "s"} could not be parsed:` }); + const list = errBox.createEl("ul"); + for (const err of errors) { + const item = list.createEl("li"); + item.createSpan({ text: err.path, cls: "any-ai-cli-am-error-path" }); + item.createSpan({ text: ` — ${err.reason}` }); + } + } + + if (entries.length === 0) { + host.createEl("p", { + text: "No automations found in the configured folder.", + cls: "any-ai-cli-am-empty" + }); + return; + } + + const runnable = this.isAnyViewRunning(); + + const table = host.createEl("table", { cls: "any-ai-cli-am-table" }); + const thead = table.createEl("thead").createEl("tr"); + for (const label of ["Name", "Schedule", "Last run", "Next run", "Status", "Actions"]) { + thead.createEl("th", { text: label }); + } + const tbody = table.createEl("tbody"); + + const now = Date.now(); + for (const entry of entries) { + const tr = tbody.createEl("tr"); + if (!entry.enabled) tr.addClass("is-disabled"); + + tr.createEl("td", { text: entry.name }); + tr.createEl("td", { text: describeSchedule(entry) }); + + const lastRun = this.plugin.settings.automationsLastRun[entry.path] ?? null; + tr.createEl("td", { text: lastRun ? formatRelative(lastRun, now) : "never" }); + + const nextRun = entry.enabled ? computeNextRun(entry, lastRun, now) : null; + tr.createEl("td", { + text: nextRun === null ? "—" : nextRun <= now ? "due now" : formatRelative(nextRun, now) + }); + + const statusCell = tr.createEl("td"); + const badge = statusCell.createSpan({ + text: entry.enabled ? "enabled" : "disabled", + cls: `any-ai-cli-am-badge ${entry.enabled ? "is-enabled" : "is-disabled"}` + }); + if (entry.runtime) { + badge.setAttribute("title", `Requires runtime "${entry.runtime}"`); + } + + const actionsCell = tr.createEl("td", { cls: "any-ai-cli-am-actions" }); + const runBtn = actionsCell.createEl("button", { text: "Run now" }); + runBtn.disabled = !runnable; + if (!runnable) { + runBtn.setAttribute("title", "Start a CLI in the sidebar panel first."); + } + runBtn.addEventListener("click", () => { + void this.plugin.triggerAutomation(entry, "manual"); + }); + + const openBtn = actionsCell.createEl("button", { text: "Open" }); + setIcon(openBtn.createSpan(), "external-link"); + openBtn.addEventListener("click", () => { + void this.app.workspace.openLinkText(entry.path, "", true); + this.close(); + }); + } + } + + private renderHistoryTab(host: HTMLDivElement): void { + const history = this.plugin.settings.automationsHistory; + + const toolbar = host.createDiv({ cls: "any-ai-cli-am-history-toolbar" }); + toolbar.createEl("p", { + text: `${history.length} entr${history.length === 1 ? "y" : "ies"} (most recent first, capped at ${this.plugin.settings.automationsHistoryLimit}).`, + cls: "any-ai-cli-am-info" + }); + + const actions = toolbar.createDiv({ cls: "any-ai-cli-am-history-actions" }); + + const clearBtn = actions.createEl("button", { text: "Clear history" }); + clearBtn.disabled = history.length === 0; + clearBtn.addEventListener("click", () => { + this.plugin.clearAutomationHistory(); + new Notice("Automation history cleared.", 2500); + }); + + const exportBtn = actions.createEl("button", { text: "Export as markdown" }); + exportBtn.disabled = history.length === 0; + exportBtn.addEventListener("click", () => { + void this.exportHistory(history); + }); + + if (history.length === 0) { + host.createEl("p", { text: "No runs recorded yet.", cls: "any-ai-cli-am-empty" }); + return; + } + + const list = host.createEl("ul", { cls: "any-ai-cli-am-history-list" }); + for (const record of history) { + const li = list.createEl("li", { cls: "any-ai-cli-am-history-item" }); + li.createSpan({ text: formatDateTime(record.ts), cls: "any-ai-cli-am-history-ts" }); + li.createSpan({ text: record.name, cls: "any-ai-cli-am-history-name" }); + li.createSpan({ + text: record.status, + cls: `any-ai-cli-am-badge is-${record.status}` + }); + li.createSpan({ text: record.source, cls: "any-ai-cli-am-history-source" }); + const detail = record.reason || record.promptPreview || ""; + if (detail) { + li.createSpan({ text: detail, cls: "any-ai-cli-am-history-detail" }); + } + } + } + + private isAnyViewRunning(): boolean { + const leaves = this.app.workspace.getLeavesOfType("claude-cli-view"); + return leaves.some((leaf) => { + const view = leaf.view as unknown as { isProcessRunning?: () => boolean }; + return typeof view?.isProcessRunning === "function" && view.isProcessRunning(); + }); + } + + private async exportHistory(history: AutomationRunRecord[]): Promise { + const lines: string[] = []; + lines.push(`# Automations history — ${new Date().toISOString()}`, ""); + lines.push("| Time | Name | Status | Source | Detail | Path |"); + lines.push("| --- | --- | --- | --- | --- | --- |"); + for (const r of history) { + const detail = (r.reason || r.promptPreview || "").replace(/\|/g, "\\|"); + lines.push( + `| ${formatDateTime(r.ts)} | ${r.name.replace(/\|/g, "\\|")} | ${r.status} | ${r.source} | ${detail} | ${r.path} |` + ); + } + const fileName = `automations-history-${new Date().toISOString().slice(0, 10)}.md`; + try { + const file = await this.app.vault.create(fileName, lines.join("\n")); + await this.app.workspace.openLinkText(file.path, "", true); + this.close(); + } catch (err) { + new Notice(`Export failed: ${(err as Error).message}`, 5000); + } + } +} + +function pad(n: number): string { + return n < 10 ? `0${n}` : `${n}`; +} + +function formatDateTime(ts: number): string { + const d = new Date(ts); + return `${d.getFullYear()}-${pad(d.getMonth() + 1)}-${pad(d.getDate())} ${pad(d.getHours())}:${pad(d.getMinutes())}:${pad(d.getSeconds())}`; +} + +function formatRelative(ts: number, now: number): string { + const diff = ts - now; + const absMs = Math.abs(diff); + const sec = Math.round(absMs / 1000); + const min = Math.round(sec / 60); + const hr = Math.round(min / 60); + const day = Math.round(hr / 24); + const arrow = diff < 0 ? "ago" : "in"; + let value: string; + if (sec < 60) value = `${sec}s`; + else if (min < 60) value = `${min}m`; + else if (hr < 24) value = `${hr}h`; + else value = `${day}d`; + return diff < 0 ? `${value} ${arrow}` : `${arrow} ${value}`; +} diff --git a/main.js b/main.js index fcf7767..6ea09b0 100644 --- a/main.js +++ b/main.js @@ -9,6 +9,13 @@ var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf; var __hasOwnProp = Object.prototype.hasOwnProperty; +var __typeError = (msg) => { + throw TypeError(msg); +}; +var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value; +var __commonJS = (cb, mod) => function __require() { + return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports; +}; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); @@ -30,6 +37,9110 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge mod )); var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); +var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value); +var __accessCheck = (obj, member, msg) => member.has(obj) || __typeError("Cannot " + msg); +var __privateGet = (obj, member, getter) => (__accessCheck(obj, member, "read from private field"), getter ? getter.call(obj) : member.get(obj)); +var __privateAdd = (obj, member, value) => member.has(obj) ? __typeError("Cannot add the same private member more than once") : member instanceof WeakSet ? member.add(obj) : member.set(obj, value); +var __privateSet = (obj, member, value, setter) => (__accessCheck(obj, member, "write to private field"), setter ? setter.call(obj, value) : member.set(obj, value), value); +var __privateMethod = (obj, member, method) => (__accessCheck(obj, member, "access private method"), method); + +// node_modules/cron-parser/dist/fields/types.js +var require_types = __commonJS({ + "node_modules/cron-parser/dist/fields/types.js"(exports) { + "use strict"; + Object.defineProperty(exports, "__esModule", { value: true }); + } +}); + +// node_modules/cron-parser/dist/fields/CronField.js +var require_CronField = __commonJS({ + "node_modules/cron-parser/dist/fields/CronField.js"(exports) { + "use strict"; + Object.defineProperty(exports, "__esModule", { value: true }); + exports.CronField = void 0; + var _hasLastChar, _hasQuestionMarkChar, _wildcard, _values, _CronField_instances, isWildcardValue_fn; + var _CronField = class _CronField { + /** + * CronField constructor. Initializes the field with the provided values. + * @param {number[] | string[]} values - Values for this field + * @param {CronFieldOptions} [options] - Options provided by the parser + * @throws {TypeError} if the constructor is called directly + * @throws {Error} if validation fails + */ + constructor(values, options = { rawValue: "" }) { + __privateAdd(this, _CronField_instances); + __privateAdd(this, _hasLastChar, false); + __privateAdd(this, _hasQuestionMarkChar, false); + __privateAdd(this, _wildcard, false); + __privateAdd(this, _values, []); + __publicField(this, "options", { rawValue: "" }); + var _a5; + if (!Array.isArray(values)) { + throw new Error(`${this.constructor.name} Validation error, values is not an array`); + } + if (!(values.length > 0)) { + throw new Error(`${this.constructor.name} Validation error, values contains no values`); + } + this.options = { + ...options, + rawValue: (_a5 = options.rawValue) != null ? _a5 : "" + }; + __privateSet(this, _values, values.sort(_CronField.sorter)); + __privateSet(this, _wildcard, this.options.wildcard !== void 0 ? this.options.wildcard : __privateMethod(this, _CronField_instances, isWildcardValue_fn).call(this)); + __privateSet(this, _hasLastChar, this.options.rawValue.includes("L") || values.includes("L")); + __privateSet(this, _hasQuestionMarkChar, this.options.rawValue.includes("?") || values.includes("?")); + } + /** + * Returns the minimum value allowed for this field. + */ + /* istanbul ignore next */ + static get min() { + throw new Error("min must be overridden"); + } + /** + * Returns the maximum value allowed for this field. + */ + /* istanbul ignore next */ + static get max() { + throw new Error("max must be overridden"); + } + /** + * Returns the allowed characters for this field. + */ + /* istanbul ignore next */ + static get chars() { + return Object.freeze([]); + } + /** + * Returns the regular expression used to validate this field. + */ + static get validChars() { + return /^[?,*\dH/-]+$|^.*H\(\d+-\d+\)\/\d+.*$|^.*H\(\d+-\d+\).*$|^.*H\/\d+.*$/; + } + /** + * Returns the constraints for this field. + */ + static get constraints() { + return { min: this.min, max: this.max, chars: this.chars, validChars: this.validChars }; + } + /** + * Returns the minimum value allowed for this field. + * @returns {number} + */ + get min() { + return this.constructor.min; + } + /** + * Returns the maximum value allowed for this field. + * @returns {number} + */ + get max() { + return this.constructor.max; + } + /** + * Returns an array of allowed special characters for this field. + * @returns {string[]} + */ + get chars() { + return this.constructor.chars; + } + /** + * Indicates whether this field has a "last" character. + * @returns {boolean} + */ + get hasLastChar() { + return __privateGet(this, _hasLastChar); + } + /** + * Indicates whether this field has a "question mark" character. + * @returns {boolean} + */ + get hasQuestionMarkChar() { + return __privateGet(this, _hasQuestionMarkChar); + } + /** + * Indicates whether this field is a wildcard. + * @returns {boolean} + */ + get isWildcard() { + return __privateGet(this, _wildcard); + } + /** + * Returns an array of allowed values for this field. + * @returns {CronFieldType} + */ + get values() { + return __privateGet(this, _values); + } + /** + * Helper function to sort values in ascending order. + * @param {number | string} a - First value to compare + * @param {number | string} b - Second value to compare + * @returns {number} - A negative, zero, or positive value, depending on the sort order + */ + static sorter(a, b2) { + const aIsNumber = typeof a === "number"; + const bIsNumber = typeof b2 === "number"; + if (aIsNumber && bIsNumber) + return a - b2; + if (!aIsNumber && !bIsNumber) + return a.localeCompare(b2); + return aIsNumber ? ( + /* istanbul ignore next - A will always be a number until L-2 is supported */ + -1 + ) : 1; + } + /** + * Find the next (or previous when `reverse` is true) numeric value in a sorted list. + * Returns null if there's no value strictly after/before the current one. + * + * @param values - Sorted numeric values + * @param currentValue - Current value to compare against + * @param reverse - When true, search in reverse for previous smaller value + */ + static findNearestValueInList(values, currentValue, reverse = false) { + if (reverse) { + for (let i = values.length - 1; i >= 0; i--) { + if (values[i] < currentValue) + return values[i]; + } + return null; + } + for (let i = 0; i < values.length; i++) { + if (values[i] > currentValue) + return values[i]; + } + return null; + } + /** + * Instance helper that operates on this field's numeric `values`. + * + * @param currentValue - Current value to compare against + * @param reverse - When true, search in reverse for previous smaller value + */ + findNearestValue(currentValue, reverse = false) { + return this.constructor.findNearestValueInList(this.values, currentValue, reverse); + } + /** + * Serializes the field to an object. + * @returns {SerializedCronField} + */ + serialize() { + return { + wildcard: __privateGet(this, _wildcard), + values: __privateGet(this, _values) + }; + } + /** + * Validates the field values against the allowed range and special characters. + * @throws {Error} if validation fails + */ + validate() { + let badValue; + const charsString = this.chars.length > 0 ? ` or chars ${this.chars.join("")}` : ""; + const charTest = (value) => (char) => new RegExp(`^\\d{0,2}${char}$`).test(value); + const rangeTest = (value) => { + badValue = value; + return typeof value === "number" ? value >= this.min && value <= this.max : this.chars.some(charTest(value)); + }; + const isValidRange = __privateGet(this, _values).every(rangeTest); + if (!isValidRange) { + throw new Error(`${this.constructor.name} Validation error, got value ${badValue} expected range ${this.min}-${this.max}${charsString}`); + } + const duplicate = __privateGet(this, _values).find((value, index) => __privateGet(this, _values).indexOf(value) !== index); + if (duplicate) { + throw new Error(`${this.constructor.name} Validation error, duplicate values found: ${duplicate}`); + } + } + }; + _hasLastChar = new WeakMap(); + _hasQuestionMarkChar = new WeakMap(); + _wildcard = new WeakMap(); + _values = new WeakMap(); + _CronField_instances = new WeakSet(); + /** + * Determines if the field is a wildcard based on the values. + * When options.rawValue is not empty, it checks if the raw value is a wildcard, otherwise it checks if all values in the range are included. + * @returns {boolean} + */ + isWildcardValue_fn = function() { + if (this.options.rawValue.length > 0) { + return ["*", "?"].includes(this.options.rawValue); + } + return Array.from({ length: this.max - this.min + 1 }, (_2, i) => i + this.min).every((value) => __privateGet(this, _values).includes(value)); + }; + var CronField = _CronField; + exports.CronField = CronField; + } +}); + +// node_modules/cron-parser/dist/fields/CronDayOfMonth.js +var require_CronDayOfMonth = __commonJS({ + "node_modules/cron-parser/dist/fields/CronDayOfMonth.js"(exports) { + "use strict"; + Object.defineProperty(exports, "__esModule", { value: true }); + exports.CronDayOfMonth = void 0; + var CronField_1 = require_CronField(); + var MIN_DAY = 1; + var MAX_DAY = 31; + var DAY_CHARS = Object.freeze(["L"]); + var CronDayOfMonth = class extends CronField_1.CronField { + static get min() { + return MIN_DAY; + } + static get max() { + return MAX_DAY; + } + static get chars() { + return DAY_CHARS; + } + static get validChars() { + return /^[?,*\dLH/-]+$|^.*H\(\d+-\d+\)\/\d+.*$|^.*H\(\d+-\d+\).*$|^.*H\/\d+.*$/; + } + /** + * CronDayOfMonth constructor. Initializes the "day of the month" field with the provided values. + * @param {DayOfMonthRange[]} values - Values for the "day of the month" field + * @param {CronFieldOptions} [options] - Options provided by the parser + * @throws {Error} if validation fails + */ + constructor(values, options) { + super(values, options); + this.validate(); + } + /** + * Returns an array of allowed values for the "day of the month" field. + * @returns {DayOfMonthRange[]} + */ + get values() { + return super.values; + } + }; + exports.CronDayOfMonth = CronDayOfMonth; + } +}); + +// node_modules/cron-parser/dist/fields/CronDayOfWeek.js +var require_CronDayOfWeek = __commonJS({ + "node_modules/cron-parser/dist/fields/CronDayOfWeek.js"(exports) { + "use strict"; + Object.defineProperty(exports, "__esModule", { value: true }); + exports.CronDayOfWeek = void 0; + var CronField_1 = require_CronField(); + var MIN_DAY = 0; + var MAX_DAY = 7; + var DAY_CHARS = Object.freeze(["L"]); + var CronDayOfWeek = class extends CronField_1.CronField { + static get min() { + return MIN_DAY; + } + static get max() { + return MAX_DAY; + } + static get chars() { + return DAY_CHARS; + } + static get validChars() { + return /^[?,*\dLH#/-]+$|^.*H\(\d+-\d+\)\/\d+.*$|^.*H\(\d+-\d+\).*$|^.*H\/\d+.*$/; + } + /** + * CronDayOfTheWeek constructor. Initializes the "day of the week" field with the provided values. + * @param {DayOfWeekRange[]} values - Values for the "day of the week" field + * @param {CronFieldOptions} [options] - Options provided by the parser + */ + constructor(values, options) { + super(values, options); + this.validate(); + } + /** + * Returns an array of allowed values for the "day of the week" field. + * @returns {DayOfWeekRange[]} + */ + get values() { + return super.values; + } + /** + * Returns the nth day of the week if specified in the cron expression. + * This is used for the '#' character in the cron expression. + * @returns {number} The nth day of the week (1-5) or 0 if not specified. + */ + get nthDay() { + var _a5; + return (_a5 = this.options.nthDayOfWeek) != null ? _a5 : 0; + } + }; + exports.CronDayOfWeek = CronDayOfWeek; + } +}); + +// node_modules/cron-parser/dist/fields/CronHour.js +var require_CronHour = __commonJS({ + "node_modules/cron-parser/dist/fields/CronHour.js"(exports) { + "use strict"; + Object.defineProperty(exports, "__esModule", { value: true }); + exports.CronHour = void 0; + var CronField_1 = require_CronField(); + var MIN_HOUR = 0; + var MAX_HOUR = 23; + var HOUR_CHARS = Object.freeze([]); + var CronHour = class extends CronField_1.CronField { + static get min() { + return MIN_HOUR; + } + static get max() { + return MAX_HOUR; + } + static get chars() { + return HOUR_CHARS; + } + /** + * CronHour constructor. Initializes the "hour" field with the provided values. + * @param {HourRange[]} values - Values for the "hour" field + * @param {CronFieldOptions} [options] - Options provided by the parser + */ + constructor(values, options) { + super(values, options); + this.validate(); + } + /** + * Returns an array of allowed values for the "hour" field. + * @returns {HourRange[]} + */ + get values() { + return super.values; + } + }; + exports.CronHour = CronHour; + } +}); + +// node_modules/cron-parser/dist/fields/CronMinute.js +var require_CronMinute = __commonJS({ + "node_modules/cron-parser/dist/fields/CronMinute.js"(exports) { + "use strict"; + Object.defineProperty(exports, "__esModule", { value: true }); + exports.CronMinute = void 0; + var CronField_1 = require_CronField(); + var MIN_MINUTE = 0; + var MAX_MINUTE = 59; + var MINUTE_CHARS = Object.freeze([]); + var CronMinute = class extends CronField_1.CronField { + static get min() { + return MIN_MINUTE; + } + static get max() { + return MAX_MINUTE; + } + static get chars() { + return MINUTE_CHARS; + } + /** + * CronSecond constructor. Initializes the "second" field with the provided values. + * @param {SixtyRange[]} values - Values for the "second" field + * @param {CronFieldOptions} [options] - Options provided by the parser + */ + constructor(values, options) { + super(values, options); + this.validate(); + } + /** + * Returns an array of allowed values for the "second" field. + * @returns {SixtyRange[]} + */ + get values() { + return super.values; + } + }; + exports.CronMinute = CronMinute; + } +}); + +// node_modules/luxon/build/node/luxon.js +var require_luxon = __commonJS({ + "node_modules/luxon/build/node/luxon.js"(exports) { + "use strict"; + Object.defineProperty(exports, "__esModule", { value: true }); + var LuxonError = class extends Error { + }; + var InvalidDateTimeError = class extends LuxonError { + constructor(reason) { + super(`Invalid DateTime: ${reason.toMessage()}`); + } + }; + var InvalidIntervalError = class extends LuxonError { + constructor(reason) { + super(`Invalid Interval: ${reason.toMessage()}`); + } + }; + var InvalidDurationError = class extends LuxonError { + constructor(reason) { + super(`Invalid Duration: ${reason.toMessage()}`); + } + }; + var ConflictingSpecificationError = class extends LuxonError { + }; + var InvalidUnitError = class extends LuxonError { + constructor(unit) { + super(`Invalid unit ${unit}`); + } + }; + var InvalidArgumentError = class extends LuxonError { + }; + var ZoneIsAbstractError = class extends LuxonError { + constructor() { + super("Zone is an abstract class"); + } + }; + var n = "numeric"; + var s15 = "short"; + var l = "long"; + var DATE_SHORT = { + year: n, + month: n, + day: n + }; + var DATE_MED = { + year: n, + month: s15, + day: n + }; + var DATE_MED_WITH_WEEKDAY = { + year: n, + month: s15, + day: n, + weekday: s15 + }; + var DATE_FULL = { + year: n, + month: l, + day: n + }; + var DATE_HUGE = { + year: n, + month: l, + day: n, + weekday: l + }; + var TIME_SIMPLE = { + hour: n, + minute: n + }; + var TIME_WITH_SECONDS = { + hour: n, + minute: n, + second: n + }; + var TIME_WITH_SHORT_OFFSET = { + hour: n, + minute: n, + second: n, + timeZoneName: s15 + }; + var TIME_WITH_LONG_OFFSET = { + hour: n, + minute: n, + second: n, + timeZoneName: l + }; + var TIME_24_SIMPLE = { + hour: n, + minute: n, + hourCycle: "h23" + }; + var TIME_24_WITH_SECONDS = { + hour: n, + minute: n, + second: n, + hourCycle: "h23" + }; + var TIME_24_WITH_SHORT_OFFSET = { + hour: n, + minute: n, + second: n, + hourCycle: "h23", + timeZoneName: s15 + }; + var TIME_24_WITH_LONG_OFFSET = { + hour: n, + minute: n, + second: n, + hourCycle: "h23", + timeZoneName: l + }; + var DATETIME_SHORT = { + year: n, + month: n, + day: n, + hour: n, + minute: n + }; + var DATETIME_SHORT_WITH_SECONDS = { + year: n, + month: n, + day: n, + hour: n, + minute: n, + second: n + }; + var DATETIME_MED = { + year: n, + month: s15, + day: n, + hour: n, + minute: n + }; + var DATETIME_MED_WITH_SECONDS = { + year: n, + month: s15, + day: n, + hour: n, + minute: n, + second: n + }; + var DATETIME_MED_WITH_WEEKDAY = { + year: n, + month: s15, + day: n, + weekday: s15, + hour: n, + minute: n + }; + var DATETIME_FULL = { + year: n, + month: l, + day: n, + hour: n, + minute: n, + timeZoneName: s15 + }; + var DATETIME_FULL_WITH_SECONDS = { + year: n, + month: l, + day: n, + hour: n, + minute: n, + second: n, + timeZoneName: s15 + }; + var DATETIME_HUGE = { + year: n, + month: l, + day: n, + weekday: l, + hour: n, + minute: n, + timeZoneName: l + }; + var DATETIME_HUGE_WITH_SECONDS = { + year: n, + month: l, + day: n, + weekday: l, + hour: n, + minute: n, + second: n, + timeZoneName: l + }; + var Zone = class { + /** + * The type of zone + * @abstract + * @type {string} + */ + get type() { + throw new ZoneIsAbstractError(); + } + /** + * The name of this zone. + * @abstract + * @type {string} + */ + get name() { + throw new ZoneIsAbstractError(); + } + /** + * The IANA name of this zone. + * Defaults to `name` if not overwritten by a subclass. + * @abstract + * @type {string} + */ + get ianaName() { + return this.name; + } + /** + * Returns whether the offset is known to be fixed for the whole year. + * @abstract + * @type {boolean} + */ + get isUniversal() { + throw new ZoneIsAbstractError(); + } + /** + * Returns the offset's common name (such as EST) at the specified timestamp + * @abstract + * @param {number} ts - Epoch milliseconds for which to get the name + * @param {Object} opts - Options to affect the format + * @param {string} opts.format - What style of offset to return. Accepts 'long' or 'short'. + * @param {string} opts.locale - What locale to return the offset name in. + * @return {string} + */ + offsetName(ts2, opts) { + throw new ZoneIsAbstractError(); + } + /** + * Returns the offset's value as a string + * @abstract + * @param {number} ts - Epoch milliseconds for which to get the offset + * @param {string} format - What style of offset to return. + * Accepts 'narrow', 'short', or 'techie'. Returning '+6', '+06:00', or '+0600' respectively + * @return {string} + */ + formatOffset(ts2, format) { + throw new ZoneIsAbstractError(); + } + /** + * Return the offset in minutes for this zone at the specified timestamp. + * @abstract + * @param {number} ts - Epoch milliseconds for which to compute the offset + * @return {number} + */ + offset(ts2) { + throw new ZoneIsAbstractError(); + } + /** + * Return whether this Zone is equal to another zone + * @abstract + * @param {Zone} otherZone - the zone to compare + * @return {boolean} + */ + equals(otherZone) { + throw new ZoneIsAbstractError(); + } + /** + * Return whether this Zone is valid. + * @abstract + * @type {boolean} + */ + get isValid() { + throw new ZoneIsAbstractError(); + } + }; + var singleton$1 = null; + var SystemZone = class _SystemZone extends Zone { + /** + * Get a singleton instance of the local zone + * @return {SystemZone} + */ + static get instance() { + if (singleton$1 === null) { + singleton$1 = new _SystemZone(); + } + return singleton$1; + } + /** @override **/ + get type() { + return "system"; + } + /** @override **/ + get name() { + return new Intl.DateTimeFormat().resolvedOptions().timeZone; + } + /** @override **/ + get isUniversal() { + return false; + } + /** @override **/ + offsetName(ts2, { + format, + locale + }) { + return parseZoneInfo(ts2, format, locale); + } + /** @override **/ + formatOffset(ts2, format) { + return formatOffset(this.offset(ts2), format); + } + /** @override **/ + offset(ts2) { + return -new Date(ts2).getTimezoneOffset(); + } + /** @override **/ + equals(otherZone) { + return otherZone.type === "system"; + } + /** @override **/ + get isValid() { + return true; + } + }; + var dtfCache = /* @__PURE__ */ new Map(); + function makeDTF(zoneName) { + let dtf = dtfCache.get(zoneName); + if (dtf === void 0) { + dtf = new Intl.DateTimeFormat("en-US", { + hour12: false, + timeZone: zoneName, + year: "numeric", + month: "2-digit", + day: "2-digit", + hour: "2-digit", + minute: "2-digit", + second: "2-digit", + era: "short" + }); + dtfCache.set(zoneName, dtf); + } + return dtf; + } + var typeToPos = { + year: 0, + month: 1, + day: 2, + era: 3, + hour: 4, + minute: 5, + second: 6 + }; + function hackyOffset(dtf, date) { + const formatted = dtf.format(date).replace(/\u200E/g, ""), parsed = /(\d+)\/(\d+)\/(\d+) (AD|BC),? (\d+):(\d+):(\d+)/.exec(formatted), [, fMonth, fDay, fYear, fadOrBc, fHour, fMinute, fSecond] = parsed; + return [fYear, fMonth, fDay, fadOrBc, fHour, fMinute, fSecond]; + } + function partsOffset(dtf, date) { + const formatted = dtf.formatToParts(date); + const filled = []; + for (let i = 0; i < formatted.length; i++) { + const { + type, + value + } = formatted[i]; + const pos = typeToPos[type]; + if (type === "era") { + filled[pos] = value; + } else if (!isUndefined(pos)) { + filled[pos] = parseInt(value, 10); + } + } + return filled; + } + var ianaZoneCache = /* @__PURE__ */ new Map(); + var IANAZone = class _IANAZone extends Zone { + /** + * @param {string} name - Zone name + * @return {IANAZone} + */ + static create(name) { + let zone = ianaZoneCache.get(name); + if (zone === void 0) { + ianaZoneCache.set(name, zone = new _IANAZone(name)); + } + return zone; + } + /** + * Reset local caches. Should only be necessary in testing scenarios. + * @return {void} + */ + static resetCache() { + ianaZoneCache.clear(); + dtfCache.clear(); + } + /** + * Returns whether the provided string is a valid specifier. This only checks the string's format, not that the specifier identifies a known zone; see isValidZone for that. + * @param {string} s - The string to check validity on + * @example IANAZone.isValidSpecifier("America/New_York") //=> true + * @example IANAZone.isValidSpecifier("Sport~~blorp") //=> false + * @deprecated For backward compatibility, this forwards to isValidZone, better use `isValidZone()` directly instead. + * @return {boolean} + */ + static isValidSpecifier(s16) { + return this.isValidZone(s16); + } + /** + * Returns whether the provided string identifies a real zone + * @param {string} zone - The string to check + * @example IANAZone.isValidZone("America/New_York") //=> true + * @example IANAZone.isValidZone("Fantasia/Castle") //=> false + * @example IANAZone.isValidZone("Sport~~blorp") //=> false + * @return {boolean} + */ + static isValidZone(zone) { + if (!zone) { + return false; + } + try { + new Intl.DateTimeFormat("en-US", { + timeZone: zone + }).format(); + return true; + } catch (e) { + return false; + } + } + constructor(name) { + super(); + this.zoneName = name; + this.valid = _IANAZone.isValidZone(name); + } + /** + * The type of zone. `iana` for all instances of `IANAZone`. + * @override + * @type {string} + */ + get type() { + return "iana"; + } + /** + * The name of this zone (i.e. the IANA zone name). + * @override + * @type {string} + */ + get name() { + return this.zoneName; + } + /** + * Returns whether the offset is known to be fixed for the whole year: + * Always returns false for all IANA zones. + * @override + * @type {boolean} + */ + get isUniversal() { + return false; + } + /** + * Returns the offset's common name (such as EST) at the specified timestamp + * @override + * @param {number} ts - Epoch milliseconds for which to get the name + * @param {Object} opts - Options to affect the format + * @param {string} opts.format - What style of offset to return. Accepts 'long' or 'short'. + * @param {string} opts.locale - What locale to return the offset name in. + * @return {string} + */ + offsetName(ts2, { + format, + locale + }) { + return parseZoneInfo(ts2, format, locale, this.name); + } + /** + * Returns the offset's value as a string + * @override + * @param {number} ts - Epoch milliseconds for which to get the offset + * @param {string} format - What style of offset to return. + * Accepts 'narrow', 'short', or 'techie'. Returning '+6', '+06:00', or '+0600' respectively + * @return {string} + */ + formatOffset(ts2, format) { + return formatOffset(this.offset(ts2), format); + } + /** + * Return the offset in minutes for this zone at the specified timestamp. + * @override + * @param {number} ts - Epoch milliseconds for which to compute the offset + * @return {number} + */ + offset(ts2) { + if (!this.valid) return NaN; + const date = new Date(ts2); + if (isNaN(date)) return NaN; + const dtf = makeDTF(this.name); + let [year, month, day, adOrBc, hour, minute, second] = dtf.formatToParts ? partsOffset(dtf, date) : hackyOffset(dtf, date); + if (adOrBc === "BC") { + year = -Math.abs(year) + 1; + } + const adjustedHour = hour === 24 ? 0 : hour; + const asUTC = objToLocalTS({ + year, + month, + day, + hour: adjustedHour, + minute, + second, + millisecond: 0 + }); + let asTS = +date; + const over = asTS % 1e3; + asTS -= over >= 0 ? over : 1e3 + over; + return (asUTC - asTS) / (60 * 1e3); + } + /** + * Return whether this Zone is equal to another zone + * @override + * @param {Zone} otherZone - the zone to compare + * @return {boolean} + */ + equals(otherZone) { + return otherZone.type === "iana" && otherZone.name === this.name; + } + /** + * Return whether this Zone is valid. + * @override + * @type {boolean} + */ + get isValid() { + return this.valid; + } + }; + var intlLFCache = {}; + function getCachedLF(locString, opts = {}) { + const key = JSON.stringify([locString, opts]); + let dtf = intlLFCache[key]; + if (!dtf) { + dtf = new Intl.ListFormat(locString, opts); + intlLFCache[key] = dtf; + } + return dtf; + } + var intlDTCache = /* @__PURE__ */ new Map(); + function getCachedDTF(locString, opts = {}) { + const key = JSON.stringify([locString, opts]); + let dtf = intlDTCache.get(key); + if (dtf === void 0) { + dtf = new Intl.DateTimeFormat(locString, opts); + intlDTCache.set(key, dtf); + } + return dtf; + } + var intlNumCache = /* @__PURE__ */ new Map(); + function getCachedINF(locString, opts = {}) { + const key = JSON.stringify([locString, opts]); + let inf = intlNumCache.get(key); + if (inf === void 0) { + inf = new Intl.NumberFormat(locString, opts); + intlNumCache.set(key, inf); + } + return inf; + } + var intlRelCache = /* @__PURE__ */ new Map(); + function getCachedRTF(locString, opts = {}) { + const { + base, + ...cacheKeyOpts + } = opts; + const key = JSON.stringify([locString, cacheKeyOpts]); + let inf = intlRelCache.get(key); + if (inf === void 0) { + inf = new Intl.RelativeTimeFormat(locString, opts); + intlRelCache.set(key, inf); + } + return inf; + } + var sysLocaleCache = null; + function systemLocale() { + if (sysLocaleCache) { + return sysLocaleCache; + } else { + sysLocaleCache = new Intl.DateTimeFormat().resolvedOptions().locale; + return sysLocaleCache; + } + } + var intlResolvedOptionsCache = /* @__PURE__ */ new Map(); + function getCachedIntResolvedOptions(locString) { + let opts = intlResolvedOptionsCache.get(locString); + if (opts === void 0) { + opts = new Intl.DateTimeFormat(locString).resolvedOptions(); + intlResolvedOptionsCache.set(locString, opts); + } + return opts; + } + var weekInfoCache = /* @__PURE__ */ new Map(); + function getCachedWeekInfo(locString) { + let data = weekInfoCache.get(locString); + if (!data) { + const locale = new Intl.Locale(locString); + data = "getWeekInfo" in locale ? locale.getWeekInfo() : locale.weekInfo; + if (!("minimalDays" in data)) { + data = { + ...fallbackWeekSettings, + ...data + }; + } + weekInfoCache.set(locString, data); + } + return data; + } + function parseLocaleString(localeStr) { + const xIndex = localeStr.indexOf("-x-"); + if (xIndex !== -1) { + localeStr = localeStr.substring(0, xIndex); + } + const uIndex = localeStr.indexOf("-u-"); + if (uIndex === -1) { + return [localeStr]; + } else { + let options; + let selectedStr; + try { + options = getCachedDTF(localeStr).resolvedOptions(); + selectedStr = localeStr; + } catch (e) { + const smaller = localeStr.substring(0, uIndex); + options = getCachedDTF(smaller).resolvedOptions(); + selectedStr = smaller; + } + const { + numberingSystem, + calendar + } = options; + return [selectedStr, numberingSystem, calendar]; + } + } + function intlConfigString(localeStr, numberingSystem, outputCalendar) { + if (outputCalendar || numberingSystem) { + if (!localeStr.includes("-u-")) { + localeStr += "-u"; + } + if (outputCalendar) { + localeStr += `-ca-${outputCalendar}`; + } + if (numberingSystem) { + localeStr += `-nu-${numberingSystem}`; + } + return localeStr; + } else { + return localeStr; + } + } + function mapMonths(f) { + const ms2 = []; + for (let i = 1; i <= 12; i++) { + const dt2 = DateTime.utc(2009, i, 1); + ms2.push(f(dt2)); + } + return ms2; + } + function mapWeekdays(f) { + const ms2 = []; + for (let i = 1; i <= 7; i++) { + const dt2 = DateTime.utc(2016, 11, 13 + i); + ms2.push(f(dt2)); + } + return ms2; + } + function listStuff(loc, length, englishFn, intlFn) { + const mode = loc.listingMode(); + if (mode === "error") { + return null; + } else if (mode === "en") { + return englishFn(length); + } else { + return intlFn(length); + } + } + function supportsFastNumbers(loc) { + if (loc.numberingSystem && loc.numberingSystem !== "latn") { + return false; + } else { + return loc.numberingSystem === "latn" || !loc.locale || loc.locale.startsWith("en") || getCachedIntResolvedOptions(loc.locale).numberingSystem === "latn"; + } + } + var PolyNumberFormatter = class { + constructor(intl, forceSimple, opts) { + this.padTo = opts.padTo || 0; + this.floor = opts.floor || false; + const { + padTo, + floor, + ...otherOpts + } = opts; + if (!forceSimple || Object.keys(otherOpts).length > 0) { + const intlOpts = { + useGrouping: false, + ...opts + }; + if (opts.padTo > 0) intlOpts.minimumIntegerDigits = opts.padTo; + this.inf = getCachedINF(intl, intlOpts); + } + } + format(i) { + if (this.inf) { + const fixed = this.floor ? Math.floor(i) : i; + return this.inf.format(fixed); + } else { + const fixed = this.floor ? Math.floor(i) : roundTo(i, 3); + return padStart(fixed, this.padTo); + } + } + }; + var PolyDateFormatter = class { + constructor(dt2, intl, opts) { + this.opts = opts; + this.originalZone = void 0; + let z2 = void 0; + if (this.opts.timeZone) { + this.dt = dt2; + } else if (dt2.zone.type === "fixed") { + const gmtOffset = -1 * (dt2.offset / 60); + const offsetZ = gmtOffset >= 0 ? `Etc/GMT+${gmtOffset}` : `Etc/GMT${gmtOffset}`; + if (dt2.offset !== 0 && IANAZone.create(offsetZ).valid) { + z2 = offsetZ; + this.dt = dt2; + } else { + z2 = "UTC"; + this.dt = dt2.offset === 0 ? dt2 : dt2.setZone("UTC").plus({ + minutes: dt2.offset + }); + this.originalZone = dt2.zone; + } + } else if (dt2.zone.type === "system") { + this.dt = dt2; + } else if (dt2.zone.type === "iana") { + this.dt = dt2; + z2 = dt2.zone.name; + } else { + z2 = "UTC"; + this.dt = dt2.setZone("UTC").plus({ + minutes: dt2.offset + }); + this.originalZone = dt2.zone; + } + const intlOpts = { + ...this.opts + }; + intlOpts.timeZone = intlOpts.timeZone || z2; + this.dtf = getCachedDTF(intl, intlOpts); + } + format() { + if (this.originalZone) { + return this.formatToParts().map(({ + value + }) => value).join(""); + } + return this.dtf.format(this.dt.toJSDate()); + } + formatToParts() { + const parts = this.dtf.formatToParts(this.dt.toJSDate()); + if (this.originalZone) { + return parts.map((part) => { + if (part.type === "timeZoneName") { + const offsetName = this.originalZone.offsetName(this.dt.ts, { + locale: this.dt.locale, + format: this.opts.timeZoneName + }); + return { + ...part, + value: offsetName + }; + } else { + return part; + } + }); + } + return parts; + } + resolvedOptions() { + return this.dtf.resolvedOptions(); + } + }; + var PolyRelFormatter = class { + constructor(intl, isEnglish, opts) { + this.opts = { + style: "long", + ...opts + }; + if (!isEnglish && hasRelative()) { + this.rtf = getCachedRTF(intl, opts); + } + } + format(count, unit) { + if (this.rtf) { + return this.rtf.format(count, unit); + } else { + return formatRelativeTime(unit, count, this.opts.numeric, this.opts.style !== "long"); + } + } + formatToParts(count, unit) { + if (this.rtf) { + return this.rtf.formatToParts(count, unit); + } else { + return []; + } + } + }; + var fallbackWeekSettings = { + firstDay: 1, + minimalDays: 4, + weekend: [6, 7] + }; + var Locale = class _Locale { + static fromOpts(opts) { + return _Locale.create(opts.locale, opts.numberingSystem, opts.outputCalendar, opts.weekSettings, opts.defaultToEN); + } + static create(locale, numberingSystem, outputCalendar, weekSettings, defaultToEN = false) { + const specifiedLocale = locale || Settings.defaultLocale; + const localeR = specifiedLocale || (defaultToEN ? "en-US" : systemLocale()); + const numberingSystemR = numberingSystem || Settings.defaultNumberingSystem; + const outputCalendarR = outputCalendar || Settings.defaultOutputCalendar; + const weekSettingsR = validateWeekSettings(weekSettings) || Settings.defaultWeekSettings; + return new _Locale(localeR, numberingSystemR, outputCalendarR, weekSettingsR, specifiedLocale); + } + static resetCache() { + sysLocaleCache = null; + intlDTCache.clear(); + intlNumCache.clear(); + intlRelCache.clear(); + intlResolvedOptionsCache.clear(); + weekInfoCache.clear(); + } + static fromObject({ + locale, + numberingSystem, + outputCalendar, + weekSettings + } = {}) { + return _Locale.create(locale, numberingSystem, outputCalendar, weekSettings); + } + constructor(locale, numbering, outputCalendar, weekSettings, specifiedLocale) { + const [parsedLocale, parsedNumberingSystem, parsedOutputCalendar] = parseLocaleString(locale); + this.locale = parsedLocale; + this.numberingSystem = numbering || parsedNumberingSystem || null; + this.outputCalendar = outputCalendar || parsedOutputCalendar || null; + this.weekSettings = weekSettings; + this.intl = intlConfigString(this.locale, this.numberingSystem, this.outputCalendar); + this.weekdaysCache = { + format: {}, + standalone: {} + }; + this.monthsCache = { + format: {}, + standalone: {} + }; + this.meridiemCache = null; + this.eraCache = {}; + this.specifiedLocale = specifiedLocale; + this.fastNumbersCached = null; + } + get fastNumbers() { + if (this.fastNumbersCached == null) { + this.fastNumbersCached = supportsFastNumbers(this); + } + return this.fastNumbersCached; + } + listingMode() { + const isActuallyEn = this.isEnglish(); + const hasNoWeirdness = (this.numberingSystem === null || this.numberingSystem === "latn") && (this.outputCalendar === null || this.outputCalendar === "gregory"); + return isActuallyEn && hasNoWeirdness ? "en" : "intl"; + } + clone(alts) { + if (!alts || Object.getOwnPropertyNames(alts).length === 0) { + return this; + } else { + return _Locale.create(alts.locale || this.specifiedLocale, alts.numberingSystem || this.numberingSystem, alts.outputCalendar || this.outputCalendar, validateWeekSettings(alts.weekSettings) || this.weekSettings, alts.defaultToEN || false); + } + } + redefaultToEN(alts = {}) { + return this.clone({ + ...alts, + defaultToEN: true + }); + } + redefaultToSystem(alts = {}) { + return this.clone({ + ...alts, + defaultToEN: false + }); + } + months(length, format = false) { + return listStuff(this, length, months, () => { + const monthSpecialCase = this.intl === "ja" || this.intl.startsWith("ja-"); + format &= !monthSpecialCase; + const intl = format ? { + month: length, + day: "numeric" + } : { + month: length + }, formatStr = format ? "format" : "standalone"; + if (!this.monthsCache[formatStr][length]) { + const mapper = !monthSpecialCase ? (dt2) => this.extract(dt2, intl, "month") : (dt2) => this.dtFormatter(dt2, intl).format(); + this.monthsCache[formatStr][length] = mapMonths(mapper); + } + return this.monthsCache[formatStr][length]; + }); + } + weekdays(length, format = false) { + return listStuff(this, length, weekdays, () => { + const intl = format ? { + weekday: length, + year: "numeric", + month: "long", + day: "numeric" + } : { + weekday: length + }, formatStr = format ? "format" : "standalone"; + if (!this.weekdaysCache[formatStr][length]) { + this.weekdaysCache[formatStr][length] = mapWeekdays((dt2) => this.extract(dt2, intl, "weekday")); + } + return this.weekdaysCache[formatStr][length]; + }); + } + meridiems() { + return listStuff(this, void 0, () => meridiems, () => { + if (!this.meridiemCache) { + const intl = { + hour: "numeric", + hourCycle: "h12" + }; + this.meridiemCache = [DateTime.utc(2016, 11, 13, 9), DateTime.utc(2016, 11, 13, 19)].map((dt2) => this.extract(dt2, intl, "dayperiod")); + } + return this.meridiemCache; + }); + } + eras(length) { + return listStuff(this, length, eras, () => { + const intl = { + era: length + }; + if (!this.eraCache[length]) { + this.eraCache[length] = [DateTime.utc(-40, 1, 1), DateTime.utc(2017, 1, 1)].map((dt2) => this.extract(dt2, intl, "era")); + } + return this.eraCache[length]; + }); + } + extract(dt2, intlOpts, field) { + const df = this.dtFormatter(dt2, intlOpts), results = df.formatToParts(), matching = results.find((m) => m.type.toLowerCase() === field); + return matching ? matching.value : null; + } + numberFormatter(opts = {}) { + return new PolyNumberFormatter(this.intl, opts.forceSimple || this.fastNumbers, opts); + } + dtFormatter(dt2, intlOpts = {}) { + return new PolyDateFormatter(dt2, this.intl, intlOpts); + } + relFormatter(opts = {}) { + return new PolyRelFormatter(this.intl, this.isEnglish(), opts); + } + listFormatter(opts = {}) { + return getCachedLF(this.intl, opts); + } + isEnglish() { + return this.locale === "en" || this.locale.toLowerCase() === "en-us" || getCachedIntResolvedOptions(this.intl).locale.startsWith("en-us"); + } + getWeekSettings() { + if (this.weekSettings) { + return this.weekSettings; + } else if (!hasLocaleWeekInfo()) { + return fallbackWeekSettings; + } else { + return getCachedWeekInfo(this.locale); + } + } + getStartOfWeek() { + return this.getWeekSettings().firstDay; + } + getMinDaysInFirstWeek() { + return this.getWeekSettings().minimalDays; + } + getWeekendDays() { + return this.getWeekSettings().weekend; + } + equals(other) { + return this.locale === other.locale && this.numberingSystem === other.numberingSystem && this.outputCalendar === other.outputCalendar; + } + toString() { + return `Locale(${this.locale}, ${this.numberingSystem}, ${this.outputCalendar})`; + } + }; + var singleton = null; + var FixedOffsetZone = class _FixedOffsetZone extends Zone { + /** + * Get a singleton instance of UTC + * @return {FixedOffsetZone} + */ + static get utcInstance() { + if (singleton === null) { + singleton = new _FixedOffsetZone(0); + } + return singleton; + } + /** + * Get an instance with a specified offset + * @param {number} offset - The offset in minutes + * @return {FixedOffsetZone} + */ + static instance(offset2) { + return offset2 === 0 ? _FixedOffsetZone.utcInstance : new _FixedOffsetZone(offset2); + } + /** + * Get an instance of FixedOffsetZone from a UTC offset string, like "UTC+6" + * @param {string} s - The offset string to parse + * @example FixedOffsetZone.parseSpecifier("UTC+6") + * @example FixedOffsetZone.parseSpecifier("UTC+06") + * @example FixedOffsetZone.parseSpecifier("UTC-6:00") + * @return {FixedOffsetZone} + */ + static parseSpecifier(s16) { + if (s16) { + const r = s16.match(/^utc(?:([+-]\d{1,2})(?::(\d{2}))?)?$/i); + if (r) { + return new _FixedOffsetZone(signedOffset(r[1], r[2])); + } + } + return null; + } + constructor(offset2) { + super(); + this.fixed = offset2; + } + /** + * The type of zone. `fixed` for all instances of `FixedOffsetZone`. + * @override + * @type {string} + */ + get type() { + return "fixed"; + } + /** + * The name of this zone. + * All fixed zones' names always start with "UTC" (plus optional offset) + * @override + * @type {string} + */ + get name() { + return this.fixed === 0 ? "UTC" : `UTC${formatOffset(this.fixed, "narrow")}`; + } + /** + * The IANA name of this zone, i.e. `Etc/UTC` or `Etc/GMT+/-nn` + * + * @override + * @type {string} + */ + get ianaName() { + if (this.fixed === 0) { + return "Etc/UTC"; + } else { + return `Etc/GMT${formatOffset(-this.fixed, "narrow")}`; + } + } + /** + * Returns the offset's common name at the specified timestamp. + * + * For fixed offset zones this equals to the zone name. + * @override + */ + offsetName() { + return this.name; + } + /** + * Returns the offset's value as a string + * @override + * @param {number} ts - Epoch milliseconds for which to get the offset + * @param {string} format - What style of offset to return. + * Accepts 'narrow', 'short', or 'techie'. Returning '+6', '+06:00', or '+0600' respectively + * @return {string} + */ + formatOffset(ts2, format) { + return formatOffset(this.fixed, format); + } + /** + * Returns whether the offset is known to be fixed for the whole year: + * Always returns true for all fixed offset zones. + * @override + * @type {boolean} + */ + get isUniversal() { + return true; + } + /** + * Return the offset in minutes for this zone at the specified timestamp. + * + * For fixed offset zones, this is constant and does not depend on a timestamp. + * @override + * @return {number} + */ + offset() { + return this.fixed; + } + /** + * Return whether this Zone is equal to another zone (i.e. also fixed and same offset) + * @override + * @param {Zone} otherZone - the zone to compare + * @return {boolean} + */ + equals(otherZone) { + return otherZone.type === "fixed" && otherZone.fixed === this.fixed; + } + /** + * Return whether this Zone is valid: + * All fixed offset zones are valid. + * @override + * @type {boolean} + */ + get isValid() { + return true; + } + }; + var InvalidZone = class extends Zone { + constructor(zoneName) { + super(); + this.zoneName = zoneName; + } + /** @override **/ + get type() { + return "invalid"; + } + /** @override **/ + get name() { + return this.zoneName; + } + /** @override **/ + get isUniversal() { + return false; + } + /** @override **/ + offsetName() { + return null; + } + /** @override **/ + formatOffset() { + return ""; + } + /** @override **/ + offset() { + return NaN; + } + /** @override **/ + equals() { + return false; + } + /** @override **/ + get isValid() { + return false; + } + }; + function normalizeZone(input, defaultZone2) { + if (isUndefined(input) || input === null) { + return defaultZone2; + } else if (input instanceof Zone) { + return input; + } else if (isString(input)) { + const lowered = input.toLowerCase(); + if (lowered === "default") return defaultZone2; + else if (lowered === "local" || lowered === "system") return SystemZone.instance; + else if (lowered === "utc" || lowered === "gmt") return FixedOffsetZone.utcInstance; + else return FixedOffsetZone.parseSpecifier(lowered) || IANAZone.create(input); + } else if (isNumber(input)) { + return FixedOffsetZone.instance(input); + } else if (typeof input === "object" && "offset" in input && typeof input.offset === "function") { + return input; + } else { + return new InvalidZone(input); + } + } + var numberingSystems = { + arab: "[\u0660-\u0669]", + arabext: "[\u06F0-\u06F9]", + bali: "[\u1B50-\u1B59]", + beng: "[\u09E6-\u09EF]", + deva: "[\u0966-\u096F]", + fullwide: "[\uFF10-\uFF19]", + gujr: "[\u0AE6-\u0AEF]", + hanidec: "[\u3007|\u4E00|\u4E8C|\u4E09|\u56DB|\u4E94|\u516D|\u4E03|\u516B|\u4E5D]", + khmr: "[\u17E0-\u17E9]", + knda: "[\u0CE6-\u0CEF]", + laoo: "[\u0ED0-\u0ED9]", + limb: "[\u1946-\u194F]", + mlym: "[\u0D66-\u0D6F]", + mong: "[\u1810-\u1819]", + mymr: "[\u1040-\u1049]", + orya: "[\u0B66-\u0B6F]", + tamldec: "[\u0BE6-\u0BEF]", + telu: "[\u0C66-\u0C6F]", + thai: "[\u0E50-\u0E59]", + tibt: "[\u0F20-\u0F29]", + latn: "\\d" + }; + var numberingSystemsUTF16 = { + arab: [1632, 1641], + arabext: [1776, 1785], + bali: [6992, 7001], + beng: [2534, 2543], + deva: [2406, 2415], + fullwide: [65296, 65303], + gujr: [2790, 2799], + khmr: [6112, 6121], + knda: [3302, 3311], + laoo: [3792, 3801], + limb: [6470, 6479], + mlym: [3430, 3439], + mong: [6160, 6169], + mymr: [4160, 4169], + orya: [2918, 2927], + tamldec: [3046, 3055], + telu: [3174, 3183], + thai: [3664, 3673], + tibt: [3872, 3881] + }; + var hanidecChars = numberingSystems.hanidec.replace(/[\[|\]]/g, "").split(""); + function parseDigits(str) { + let value = parseInt(str, 10); + if (isNaN(value)) { + value = ""; + for (let i = 0; i < str.length; i++) { + const code = str.charCodeAt(i); + if (str[i].search(numberingSystems.hanidec) !== -1) { + value += hanidecChars.indexOf(str[i]); + } else { + for (const key in numberingSystemsUTF16) { + const [min, max] = numberingSystemsUTF16[key]; + if (code >= min && code <= max) { + value += code - min; + } + } + } + } + return parseInt(value, 10); + } else { + return value; + } + } + var digitRegexCache = /* @__PURE__ */ new Map(); + function resetDigitRegexCache() { + digitRegexCache.clear(); + } + function digitRegex({ + numberingSystem + }, append = "") { + const ns2 = numberingSystem || "latn"; + let appendCache = digitRegexCache.get(ns2); + if (appendCache === void 0) { + appendCache = /* @__PURE__ */ new Map(); + digitRegexCache.set(ns2, appendCache); + } + let regex = appendCache.get(append); + if (regex === void 0) { + regex = new RegExp(`${numberingSystems[ns2]}${append}`); + appendCache.set(append, regex); + } + return regex; + } + var now = () => Date.now(); + var defaultZone = "system"; + var defaultLocale = null; + var defaultNumberingSystem = null; + var defaultOutputCalendar = null; + var twoDigitCutoffYear = 60; + var throwOnInvalid; + var defaultWeekSettings = null; + var Settings = class { + /** + * Get the callback for returning the current timestamp. + * @type {function} + */ + static get now() { + return now; + } + /** + * Set the callback for returning the current timestamp. + * The function should return a number, which will be interpreted as an Epoch millisecond count + * @type {function} + * @example Settings.now = () => Date.now() + 3000 // pretend it is 3 seconds in the future + * @example Settings.now = () => 0 // always pretend it's Jan 1, 1970 at midnight in UTC time + */ + static set now(n2) { + now = n2; + } + /** + * Set the default time zone to create DateTimes in. Does not affect existing instances. + * Use the value "system" to reset this value to the system's time zone. + * @type {string} + */ + static set defaultZone(zone) { + defaultZone = zone; + } + /** + * Get the default time zone object currently used to create DateTimes. Does not affect existing instances. + * The default value is the system's time zone (the one set on the machine that runs this code). + * @type {Zone} + */ + static get defaultZone() { + return normalizeZone(defaultZone, SystemZone.instance); + } + /** + * Get the default locale to create DateTimes with. Does not affect existing instances. + * @type {string} + */ + static get defaultLocale() { + return defaultLocale; + } + /** + * Set the default locale to create DateTimes with. Does not affect existing instances. + * @type {string} + */ + static set defaultLocale(locale) { + defaultLocale = locale; + } + /** + * Get the default numbering system to create DateTimes with. Does not affect existing instances. + * @type {string} + */ + static get defaultNumberingSystem() { + return defaultNumberingSystem; + } + /** + * Set the default numbering system to create DateTimes with. Does not affect existing instances. + * @type {string} + */ + static set defaultNumberingSystem(numberingSystem) { + defaultNumberingSystem = numberingSystem; + } + /** + * Get the default output calendar to create DateTimes with. Does not affect existing instances. + * @type {string} + */ + static get defaultOutputCalendar() { + return defaultOutputCalendar; + } + /** + * Set the default output calendar to create DateTimes with. Does not affect existing instances. + * @type {string} + */ + static set defaultOutputCalendar(outputCalendar) { + defaultOutputCalendar = outputCalendar; + } + /** + * @typedef {Object} WeekSettings + * @property {number} firstDay + * @property {number} minimalDays + * @property {number[]} weekend + */ + /** + * @return {WeekSettings|null} + */ + static get defaultWeekSettings() { + return defaultWeekSettings; + } + /** + * Allows overriding the default locale week settings, i.e. the start of the week, the weekend and + * how many days are required in the first week of a year. + * Does not affect existing instances. + * + * @param {WeekSettings|null} weekSettings + */ + static set defaultWeekSettings(weekSettings) { + defaultWeekSettings = validateWeekSettings(weekSettings); + } + /** + * Get the cutoff year for whether a 2-digit year string is interpreted in the current or previous century. Numbers higher than the cutoff will be considered to mean 19xx and numbers lower or equal to the cutoff will be considered 20xx. + * @type {number} + */ + static get twoDigitCutoffYear() { + return twoDigitCutoffYear; + } + /** + * Set the cutoff year for whether a 2-digit year string is interpreted in the current or previous century. Numbers higher than the cutoff will be considered to mean 19xx and numbers lower or equal to the cutoff will be considered 20xx. + * @type {number} + * @example Settings.twoDigitCutoffYear = 0 // all 'yy' are interpreted as 20th century + * @example Settings.twoDigitCutoffYear = 99 // all 'yy' are interpreted as 21st century + * @example Settings.twoDigitCutoffYear = 50 // '49' -> 2049; '50' -> 1950 + * @example Settings.twoDigitCutoffYear = 1950 // interpreted as 50 + * @example Settings.twoDigitCutoffYear = 2050 // ALSO interpreted as 50 + */ + static set twoDigitCutoffYear(cutoffYear) { + twoDigitCutoffYear = cutoffYear % 100; + } + /** + * Get whether Luxon will throw when it encounters invalid DateTimes, Durations, or Intervals + * @type {boolean} + */ + static get throwOnInvalid() { + return throwOnInvalid; + } + /** + * Set whether Luxon will throw when it encounters invalid DateTimes, Durations, or Intervals + * @type {boolean} + */ + static set throwOnInvalid(t) { + throwOnInvalid = t; + } + /** + * Reset Luxon's global caches. Should only be necessary in testing scenarios. + * @return {void} + */ + static resetCaches() { + Locale.resetCache(); + IANAZone.resetCache(); + DateTime.resetCache(); + resetDigitRegexCache(); + } + }; + var Invalid = class { + constructor(reason, explanation) { + this.reason = reason; + this.explanation = explanation; + } + toMessage() { + if (this.explanation) { + return `${this.reason}: ${this.explanation}`; + } else { + return this.reason; + } + } + }; + var nonLeapLadder = [0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334]; + var leapLadder = [0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335]; + function unitOutOfRange(unit, value) { + return new Invalid("unit out of range", `you specified ${value} (of type ${typeof value}) as a ${unit}, which is invalid`); + } + function dayOfWeek(year, month, day) { + const d = new Date(Date.UTC(year, month - 1, day)); + if (year < 100 && year >= 0) { + d.setUTCFullYear(d.getUTCFullYear() - 1900); + } + const js2 = d.getUTCDay(); + return js2 === 0 ? 7 : js2; + } + function computeOrdinal(year, month, day) { + return day + (isLeapYear(year) ? leapLadder : nonLeapLadder)[month - 1]; + } + function uncomputeOrdinal(year, ordinal) { + const table = isLeapYear(year) ? leapLadder : nonLeapLadder, month0 = table.findIndex((i) => i < ordinal), day = ordinal - table[month0]; + return { + month: month0 + 1, + day + }; + } + function isoWeekdayToLocal(isoWeekday, startOfWeek) { + return (isoWeekday - startOfWeek + 7) % 7 + 1; + } + function gregorianToWeek(gregObj, minDaysInFirstWeek = 4, startOfWeek = 1) { + const { + year, + month, + day + } = gregObj, ordinal = computeOrdinal(year, month, day), weekday = isoWeekdayToLocal(dayOfWeek(year, month, day), startOfWeek); + let weekNumber = Math.floor((ordinal - weekday + 14 - minDaysInFirstWeek) / 7), weekYear; + if (weekNumber < 1) { + weekYear = year - 1; + weekNumber = weeksInWeekYear(weekYear, minDaysInFirstWeek, startOfWeek); + } else if (weekNumber > weeksInWeekYear(year, minDaysInFirstWeek, startOfWeek)) { + weekYear = year + 1; + weekNumber = 1; + } else { + weekYear = year; + } + return { + weekYear, + weekNumber, + weekday, + ...timeObject(gregObj) + }; + } + function weekToGregorian(weekData, minDaysInFirstWeek = 4, startOfWeek = 1) { + const { + weekYear, + weekNumber, + weekday + } = weekData, weekdayOfJan4 = isoWeekdayToLocal(dayOfWeek(weekYear, 1, minDaysInFirstWeek), startOfWeek), yearInDays = daysInYear(weekYear); + let ordinal = weekNumber * 7 + weekday - weekdayOfJan4 - 7 + minDaysInFirstWeek, year; + if (ordinal < 1) { + year = weekYear - 1; + ordinal += daysInYear(year); + } else if (ordinal > yearInDays) { + year = weekYear + 1; + ordinal -= daysInYear(weekYear); + } else { + year = weekYear; + } + const { + month, + day + } = uncomputeOrdinal(year, ordinal); + return { + year, + month, + day, + ...timeObject(weekData) + }; + } + function gregorianToOrdinal(gregData) { + const { + year, + month, + day + } = gregData; + const ordinal = computeOrdinal(year, month, day); + return { + year, + ordinal, + ...timeObject(gregData) + }; + } + function ordinalToGregorian(ordinalData) { + const { + year, + ordinal + } = ordinalData; + const { + month, + day + } = uncomputeOrdinal(year, ordinal); + return { + year, + month, + day, + ...timeObject(ordinalData) + }; + } + function usesLocalWeekValues(obj, loc) { + const hasLocaleWeekData = !isUndefined(obj.localWeekday) || !isUndefined(obj.localWeekNumber) || !isUndefined(obj.localWeekYear); + if (hasLocaleWeekData) { + const hasIsoWeekData = !isUndefined(obj.weekday) || !isUndefined(obj.weekNumber) || !isUndefined(obj.weekYear); + if (hasIsoWeekData) { + throw new ConflictingSpecificationError("Cannot mix locale-based week fields with ISO-based week fields"); + } + if (!isUndefined(obj.localWeekday)) obj.weekday = obj.localWeekday; + if (!isUndefined(obj.localWeekNumber)) obj.weekNumber = obj.localWeekNumber; + if (!isUndefined(obj.localWeekYear)) obj.weekYear = obj.localWeekYear; + delete obj.localWeekday; + delete obj.localWeekNumber; + delete obj.localWeekYear; + return { + minDaysInFirstWeek: loc.getMinDaysInFirstWeek(), + startOfWeek: loc.getStartOfWeek() + }; + } else { + return { + minDaysInFirstWeek: 4, + startOfWeek: 1 + }; + } + } + function hasInvalidWeekData(obj, minDaysInFirstWeek = 4, startOfWeek = 1) { + const validYear = isInteger(obj.weekYear), validWeek = integerBetween(obj.weekNumber, 1, weeksInWeekYear(obj.weekYear, minDaysInFirstWeek, startOfWeek)), validWeekday = integerBetween(obj.weekday, 1, 7); + if (!validYear) { + return unitOutOfRange("weekYear", obj.weekYear); + } else if (!validWeek) { + return unitOutOfRange("week", obj.weekNumber); + } else if (!validWeekday) { + return unitOutOfRange("weekday", obj.weekday); + } else return false; + } + function hasInvalidOrdinalData(obj) { + const validYear = isInteger(obj.year), validOrdinal = integerBetween(obj.ordinal, 1, daysInYear(obj.year)); + if (!validYear) { + return unitOutOfRange("year", obj.year); + } else if (!validOrdinal) { + return unitOutOfRange("ordinal", obj.ordinal); + } else return false; + } + function hasInvalidGregorianData(obj) { + const validYear = isInteger(obj.year), validMonth = integerBetween(obj.month, 1, 12), validDay = integerBetween(obj.day, 1, daysInMonth(obj.year, obj.month)); + if (!validYear) { + return unitOutOfRange("year", obj.year); + } else if (!validMonth) { + return unitOutOfRange("month", obj.month); + } else if (!validDay) { + return unitOutOfRange("day", obj.day); + } else return false; + } + function hasInvalidTimeData(obj) { + const { + hour, + minute, + second, + millisecond + } = obj; + const validHour = integerBetween(hour, 0, 23) || hour === 24 && minute === 0 && second === 0 && millisecond === 0, validMinute = integerBetween(minute, 0, 59), validSecond = integerBetween(second, 0, 59), validMillisecond = integerBetween(millisecond, 0, 999); + if (!validHour) { + return unitOutOfRange("hour", hour); + } else if (!validMinute) { + return unitOutOfRange("minute", minute); + } else if (!validSecond) { + return unitOutOfRange("second", second); + } else if (!validMillisecond) { + return unitOutOfRange("millisecond", millisecond); + } else return false; + } + function isUndefined(o2) { + return typeof o2 === "undefined"; + } + function isNumber(o2) { + return typeof o2 === "number"; + } + function isInteger(o2) { + return typeof o2 === "number" && o2 % 1 === 0; + } + function isString(o2) { + return typeof o2 === "string"; + } + function isDate(o2) { + return Object.prototype.toString.call(o2) === "[object Date]"; + } + function hasRelative() { + try { + return typeof Intl !== "undefined" && !!Intl.RelativeTimeFormat; + } catch (e) { + return false; + } + } + function hasLocaleWeekInfo() { + try { + return typeof Intl !== "undefined" && !!Intl.Locale && ("weekInfo" in Intl.Locale.prototype || "getWeekInfo" in Intl.Locale.prototype); + } catch (e) { + return false; + } + } + function maybeArray(thing) { + return Array.isArray(thing) ? thing : [thing]; + } + function bestBy(arr, by, compare) { + if (arr.length === 0) { + return void 0; + } + return arr.reduce((best, next) => { + const pair = [by(next), next]; + if (!best) { + return pair; + } else if (compare(best[0], pair[0]) === best[0]) { + return best; + } else { + return pair; + } + }, null)[1]; + } + function pick(obj, keys) { + return keys.reduce((a, k) => { + a[k] = obj[k]; + return a; + }, {}); + } + function hasOwnProperty(obj, prop) { + return Object.prototype.hasOwnProperty.call(obj, prop); + } + function validateWeekSettings(settings) { + if (settings == null) { + return null; + } else if (typeof settings !== "object") { + throw new InvalidArgumentError("Week settings must be an object"); + } else { + if (!integerBetween(settings.firstDay, 1, 7) || !integerBetween(settings.minimalDays, 1, 7) || !Array.isArray(settings.weekend) || settings.weekend.some((v2) => !integerBetween(v2, 1, 7))) { + throw new InvalidArgumentError("Invalid week settings"); + } + return { + firstDay: settings.firstDay, + minimalDays: settings.minimalDays, + weekend: Array.from(settings.weekend) + }; + } + } + function integerBetween(thing, bottom, top) { + return isInteger(thing) && thing >= bottom && thing <= top; + } + function floorMod(x, n2) { + return x - n2 * Math.floor(x / n2); + } + function padStart(input, n2 = 2) { + const isNeg = input < 0; + let padded; + if (isNeg) { + padded = "-" + ("" + -input).padStart(n2, "0"); + } else { + padded = ("" + input).padStart(n2, "0"); + } + return padded; + } + function parseInteger(string) { + if (isUndefined(string) || string === null || string === "") { + return void 0; + } else { + return parseInt(string, 10); + } + } + function parseFloating(string) { + if (isUndefined(string) || string === null || string === "") { + return void 0; + } else { + return parseFloat(string); + } + } + function parseMillis(fraction) { + if (isUndefined(fraction) || fraction === null || fraction === "") { + return void 0; + } else { + const f = parseFloat("0." + fraction) * 1e3; + return Math.floor(f); + } + } + function roundTo(number, digits, rounding = "round") { + const factor = 10 ** digits; + switch (rounding) { + case "expand": + return number > 0 ? Math.ceil(number * factor) / factor : Math.floor(number * factor) / factor; + case "trunc": + return Math.trunc(number * factor) / factor; + case "round": + return Math.round(number * factor) / factor; + case "floor": + return Math.floor(number * factor) / factor; + case "ceil": + return Math.ceil(number * factor) / factor; + default: + throw new RangeError(`Value rounding ${rounding} is out of range`); + } + } + function isLeapYear(year) { + return year % 4 === 0 && (year % 100 !== 0 || year % 400 === 0); + } + function daysInYear(year) { + return isLeapYear(year) ? 366 : 365; + } + function daysInMonth(year, month) { + const modMonth = floorMod(month - 1, 12) + 1, modYear = year + (month - modMonth) / 12; + if (modMonth === 2) { + return isLeapYear(modYear) ? 29 : 28; + } else { + return [31, null, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31][modMonth - 1]; + } + } + function objToLocalTS(obj) { + let d = Date.UTC(obj.year, obj.month - 1, obj.day, obj.hour, obj.minute, obj.second, obj.millisecond); + if (obj.year < 100 && obj.year >= 0) { + d = new Date(d); + d.setUTCFullYear(obj.year, obj.month - 1, obj.day); + } + return +d; + } + function firstWeekOffset(year, minDaysInFirstWeek, startOfWeek) { + const fwdlw = isoWeekdayToLocal(dayOfWeek(year, 1, minDaysInFirstWeek), startOfWeek); + return -fwdlw + minDaysInFirstWeek - 1; + } + function weeksInWeekYear(weekYear, minDaysInFirstWeek = 4, startOfWeek = 1) { + const weekOffset = firstWeekOffset(weekYear, minDaysInFirstWeek, startOfWeek); + const weekOffsetNext = firstWeekOffset(weekYear + 1, minDaysInFirstWeek, startOfWeek); + return (daysInYear(weekYear) - weekOffset + weekOffsetNext) / 7; + } + function untruncateYear(year) { + if (year > 99) { + return year; + } else return year > Settings.twoDigitCutoffYear ? 1900 + year : 2e3 + year; + } + function parseZoneInfo(ts2, offsetFormat, locale, timeZone = null) { + const date = new Date(ts2), intlOpts = { + hourCycle: "h23", + year: "numeric", + month: "2-digit", + day: "2-digit", + hour: "2-digit", + minute: "2-digit" + }; + if (timeZone) { + intlOpts.timeZone = timeZone; + } + const modified = { + timeZoneName: offsetFormat, + ...intlOpts + }; + const parsed = new Intl.DateTimeFormat(locale, modified).formatToParts(date).find((m) => m.type.toLowerCase() === "timezonename"); + return parsed ? parsed.value : null; + } + function signedOffset(offHourStr, offMinuteStr) { + let offHour = parseInt(offHourStr, 10); + if (Number.isNaN(offHour)) { + offHour = 0; + } + const offMin = parseInt(offMinuteStr, 10) || 0, offMinSigned = offHour < 0 || Object.is(offHour, -0) ? -offMin : offMin; + return offHour * 60 + offMinSigned; + } + function asNumber(value) { + const numericValue = Number(value); + if (typeof value === "boolean" || value === "" || !Number.isFinite(numericValue)) throw new InvalidArgumentError(`Invalid unit value ${value}`); + return numericValue; + } + function normalizeObject(obj, normalizer) { + const normalized = {}; + for (const u in obj) { + if (hasOwnProperty(obj, u)) { + const v2 = obj[u]; + if (v2 === void 0 || v2 === null) continue; + normalized[normalizer(u)] = asNumber(v2); + } + } + return normalized; + } + function formatOffset(offset2, format) { + const hours = Math.trunc(Math.abs(offset2 / 60)), minutes = Math.trunc(Math.abs(offset2 % 60)), sign = offset2 >= 0 ? "+" : "-"; + switch (format) { + case "short": + return `${sign}${padStart(hours, 2)}:${padStart(minutes, 2)}`; + case "narrow": + return `${sign}${hours}${minutes > 0 ? `:${minutes}` : ""}`; + case "techie": + return `${sign}${padStart(hours, 2)}${padStart(minutes, 2)}`; + default: + throw new RangeError(`Value format ${format} is out of range for property format`); + } + } + function timeObject(obj) { + return pick(obj, ["hour", "minute", "second", "millisecond"]); + } + var monthsLong = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]; + var monthsShort = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]; + var monthsNarrow = ["J", "F", "M", "A", "M", "J", "J", "A", "S", "O", "N", "D"]; + function months(length) { + switch (length) { + case "narrow": + return [...monthsNarrow]; + case "short": + return [...monthsShort]; + case "long": + return [...monthsLong]; + case "numeric": + return ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12"]; + case "2-digit": + return ["01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12"]; + default: + return null; + } + } + var weekdaysLong = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]; + var weekdaysShort = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]; + var weekdaysNarrow = ["M", "T", "W", "T", "F", "S", "S"]; + function weekdays(length) { + switch (length) { + case "narrow": + return [...weekdaysNarrow]; + case "short": + return [...weekdaysShort]; + case "long": + return [...weekdaysLong]; + case "numeric": + return ["1", "2", "3", "4", "5", "6", "7"]; + default: + return null; + } + } + var meridiems = ["AM", "PM"]; + var erasLong = ["Before Christ", "Anno Domini"]; + var erasShort = ["BC", "AD"]; + var erasNarrow = ["B", "A"]; + function eras(length) { + switch (length) { + case "narrow": + return [...erasNarrow]; + case "short": + return [...erasShort]; + case "long": + return [...erasLong]; + default: + return null; + } + } + function meridiemForDateTime(dt2) { + return meridiems[dt2.hour < 12 ? 0 : 1]; + } + function weekdayForDateTime(dt2, length) { + return weekdays(length)[dt2.weekday - 1]; + } + function monthForDateTime(dt2, length) { + return months(length)[dt2.month - 1]; + } + function eraForDateTime(dt2, length) { + return eras(length)[dt2.year < 0 ? 0 : 1]; + } + function formatRelativeTime(unit, count, numeric = "always", narrow = false) { + const units = { + years: ["year", "yr."], + quarters: ["quarter", "qtr."], + months: ["month", "mo."], + weeks: ["week", "wk."], + days: ["day", "day", "days"], + hours: ["hour", "hr."], + minutes: ["minute", "min."], + seconds: ["second", "sec."] + }; + const lastable = ["hours", "minutes", "seconds"].indexOf(unit) === -1; + if (numeric === "auto" && lastable) { + const isDay = unit === "days"; + switch (count) { + case 1: + return isDay ? "tomorrow" : `next ${units[unit][0]}`; + case -1: + return isDay ? "yesterday" : `last ${units[unit][0]}`; + case 0: + return isDay ? "today" : `this ${units[unit][0]}`; + } + } + const isInPast = Object.is(count, -0) || count < 0, fmtValue = Math.abs(count), singular = fmtValue === 1, lilUnits = units[unit], fmtUnit = narrow ? singular ? lilUnits[1] : lilUnits[2] || lilUnits[1] : singular ? units[unit][0] : unit; + return isInPast ? `${fmtValue} ${fmtUnit} ago` : `in ${fmtValue} ${fmtUnit}`; + } + function stringifyTokens(splits, tokenToString) { + let s16 = ""; + for (const token of splits) { + if (token.literal) { + s16 += token.val; + } else { + s16 += tokenToString(token.val); + } + } + return s16; + } + var macroTokenToFormatOpts = { + D: DATE_SHORT, + DD: DATE_MED, + DDD: DATE_FULL, + DDDD: DATE_HUGE, + t: TIME_SIMPLE, + tt: TIME_WITH_SECONDS, + ttt: TIME_WITH_SHORT_OFFSET, + tttt: TIME_WITH_LONG_OFFSET, + T: TIME_24_SIMPLE, + TT: TIME_24_WITH_SECONDS, + TTT: TIME_24_WITH_SHORT_OFFSET, + TTTT: TIME_24_WITH_LONG_OFFSET, + f: DATETIME_SHORT, + ff: DATETIME_MED, + fff: DATETIME_FULL, + ffff: DATETIME_HUGE, + F: DATETIME_SHORT_WITH_SECONDS, + FF: DATETIME_MED_WITH_SECONDS, + FFF: DATETIME_FULL_WITH_SECONDS, + FFFF: DATETIME_HUGE_WITH_SECONDS + }; + var Formatter = class _Formatter { + static create(locale, opts = {}) { + return new _Formatter(locale, opts); + } + static parseFormat(fmt) { + let current = null, currentFull = "", bracketed = false; + const splits = []; + for (let i = 0; i < fmt.length; i++) { + const c = fmt.charAt(i); + if (c === "'") { + if (currentFull.length > 0 || bracketed) { + splits.push({ + literal: bracketed || /^\s+$/.test(currentFull), + val: currentFull === "" ? "'" : currentFull + }); + } + current = null; + currentFull = ""; + bracketed = !bracketed; + } else if (bracketed) { + currentFull += c; + } else if (c === current) { + currentFull += c; + } else { + if (currentFull.length > 0) { + splits.push({ + literal: /^\s+$/.test(currentFull), + val: currentFull + }); + } + currentFull = c; + current = c; + } + } + if (currentFull.length > 0) { + splits.push({ + literal: bracketed || /^\s+$/.test(currentFull), + val: currentFull + }); + } + return splits; + } + static macroTokenToFormatOpts(token) { + return macroTokenToFormatOpts[token]; + } + constructor(locale, formatOpts) { + this.opts = formatOpts; + this.loc = locale; + this.systemLoc = null; + } + formatWithSystemDefault(dt2, opts) { + if (this.systemLoc === null) { + this.systemLoc = this.loc.redefaultToSystem(); + } + const df = this.systemLoc.dtFormatter(dt2, { + ...this.opts, + ...opts + }); + return df.format(); + } + dtFormatter(dt2, opts = {}) { + return this.loc.dtFormatter(dt2, { + ...this.opts, + ...opts + }); + } + formatDateTime(dt2, opts) { + return this.dtFormatter(dt2, opts).format(); + } + formatDateTimeParts(dt2, opts) { + return this.dtFormatter(dt2, opts).formatToParts(); + } + formatInterval(interval, opts) { + const df = this.dtFormatter(interval.start, opts); + return df.dtf.formatRange(interval.start.toJSDate(), interval.end.toJSDate()); + } + resolvedOptions(dt2, opts) { + return this.dtFormatter(dt2, opts).resolvedOptions(); + } + num(n2, p = 0, signDisplay = void 0) { + if (this.opts.forceSimple) { + return padStart(n2, p); + } + const opts = { + ...this.opts + }; + if (p > 0) { + opts.padTo = p; + } + if (signDisplay) { + opts.signDisplay = signDisplay; + } + return this.loc.numberFormatter(opts).format(n2); + } + formatDateTimeFromString(dt2, fmt) { + const knownEnglish = this.loc.listingMode() === "en", useDateTimeFormatter = this.loc.outputCalendar && this.loc.outputCalendar !== "gregory", string = (opts, extract) => this.loc.extract(dt2, opts, extract), formatOffset2 = (opts) => { + if (dt2.isOffsetFixed && dt2.offset === 0 && opts.allowZ) { + return "Z"; + } + return dt2.isValid ? dt2.zone.formatOffset(dt2.ts, opts.format) : ""; + }, meridiem = () => knownEnglish ? meridiemForDateTime(dt2) : string({ + hour: "numeric", + hourCycle: "h12" + }, "dayperiod"), month = (length, standalone) => knownEnglish ? monthForDateTime(dt2, length) : string(standalone ? { + month: length + } : { + month: length, + day: "numeric" + }, "month"), weekday = (length, standalone) => knownEnglish ? weekdayForDateTime(dt2, length) : string(standalone ? { + weekday: length + } : { + weekday: length, + month: "long", + day: "numeric" + }, "weekday"), maybeMacro = (token) => { + const formatOpts = _Formatter.macroTokenToFormatOpts(token); + if (formatOpts) { + return this.formatWithSystemDefault(dt2, formatOpts); + } else { + return token; + } + }, era = (length) => knownEnglish ? eraForDateTime(dt2, length) : string({ + era: length + }, "era"), tokenToString = (token) => { + switch (token) { + // ms + case "S": + return this.num(dt2.millisecond); + case "u": + // falls through + case "SSS": + return this.num(dt2.millisecond, 3); + // seconds + case "s": + return this.num(dt2.second); + case "ss": + return this.num(dt2.second, 2); + // fractional seconds + case "uu": + return this.num(Math.floor(dt2.millisecond / 10), 2); + case "uuu": + return this.num(Math.floor(dt2.millisecond / 100)); + // minutes + case "m": + return this.num(dt2.minute); + case "mm": + return this.num(dt2.minute, 2); + // hours + case "h": + return this.num(dt2.hour % 12 === 0 ? 12 : dt2.hour % 12); + case "hh": + return this.num(dt2.hour % 12 === 0 ? 12 : dt2.hour % 12, 2); + case "H": + return this.num(dt2.hour); + case "HH": + return this.num(dt2.hour, 2); + // offset + case "Z": + return formatOffset2({ + format: "narrow", + allowZ: this.opts.allowZ + }); + case "ZZ": + return formatOffset2({ + format: "short", + allowZ: this.opts.allowZ + }); + case "ZZZ": + return formatOffset2({ + format: "techie", + allowZ: this.opts.allowZ + }); + case "ZZZZ": + return dt2.zone.offsetName(dt2.ts, { + format: "short", + locale: this.loc.locale + }); + case "ZZZZZ": + return dt2.zone.offsetName(dt2.ts, { + format: "long", + locale: this.loc.locale + }); + // zone + case "z": + return dt2.zoneName; + // meridiems + case "a": + return meridiem(); + // dates + case "d": + return useDateTimeFormatter ? string({ + day: "numeric" + }, "day") : this.num(dt2.day); + case "dd": + return useDateTimeFormatter ? string({ + day: "2-digit" + }, "day") : this.num(dt2.day, 2); + // weekdays - standalone + case "c": + return this.num(dt2.weekday); + case "ccc": + return weekday("short", true); + case "cccc": + return weekday("long", true); + case "ccccc": + return weekday("narrow", true); + // weekdays - format + case "E": + return this.num(dt2.weekday); + case "EEE": + return weekday("short", false); + case "EEEE": + return weekday("long", false); + case "EEEEE": + return weekday("narrow", false); + // months - standalone + case "L": + return useDateTimeFormatter ? string({ + month: "numeric", + day: "numeric" + }, "month") : this.num(dt2.month); + case "LL": + return useDateTimeFormatter ? string({ + month: "2-digit", + day: "numeric" + }, "month") : this.num(dt2.month, 2); + case "LLL": + return month("short", true); + case "LLLL": + return month("long", true); + case "LLLLL": + return month("narrow", true); + // months - format + case "M": + return useDateTimeFormatter ? string({ + month: "numeric" + }, "month") : this.num(dt2.month); + case "MM": + return useDateTimeFormatter ? string({ + month: "2-digit" + }, "month") : this.num(dt2.month, 2); + case "MMM": + return month("short", false); + case "MMMM": + return month("long", false); + case "MMMMM": + return month("narrow", false); + // years + case "y": + return useDateTimeFormatter ? string({ + year: "numeric" + }, "year") : this.num(dt2.year); + case "yy": + return useDateTimeFormatter ? string({ + year: "2-digit" + }, "year") : this.num(dt2.year.toString().slice(-2), 2); + case "yyyy": + return useDateTimeFormatter ? string({ + year: "numeric" + }, "year") : this.num(dt2.year, 4); + case "yyyyyy": + return useDateTimeFormatter ? string({ + year: "numeric" + }, "year") : this.num(dt2.year, 6); + // eras + case "G": + return era("short"); + case "GG": + return era("long"); + case "GGGGG": + return era("narrow"); + case "kk": + return this.num(dt2.weekYear.toString().slice(-2), 2); + case "kkkk": + return this.num(dt2.weekYear, 4); + case "W": + return this.num(dt2.weekNumber); + case "WW": + return this.num(dt2.weekNumber, 2); + case "n": + return this.num(dt2.localWeekNumber); + case "nn": + return this.num(dt2.localWeekNumber, 2); + case "ii": + return this.num(dt2.localWeekYear.toString().slice(-2), 2); + case "iiii": + return this.num(dt2.localWeekYear, 4); + case "o": + return this.num(dt2.ordinal); + case "ooo": + return this.num(dt2.ordinal, 3); + case "q": + return this.num(dt2.quarter); + case "qq": + return this.num(dt2.quarter, 2); + case "X": + return this.num(Math.floor(dt2.ts / 1e3)); + case "x": + return this.num(dt2.ts); + default: + return maybeMacro(token); + } + }; + return stringifyTokens(_Formatter.parseFormat(fmt), tokenToString); + } + formatDurationFromString(dur, fmt) { + const invertLargest = this.opts.signMode === "negativeLargestOnly" ? -1 : 1; + const tokenToField = (token) => { + switch (token[0]) { + case "S": + return "milliseconds"; + case "s": + return "seconds"; + case "m": + return "minutes"; + case "h": + return "hours"; + case "d": + return "days"; + case "w": + return "weeks"; + case "M": + return "months"; + case "y": + return "years"; + default: + return null; + } + }, tokenToString = (lildur, info) => (token) => { + const mapped = tokenToField(token); + if (mapped) { + const inversionFactor = info.isNegativeDuration && mapped !== info.largestUnit ? invertLargest : 1; + let signDisplay; + if (this.opts.signMode === "negativeLargestOnly" && mapped !== info.largestUnit) { + signDisplay = "never"; + } else if (this.opts.signMode === "all") { + signDisplay = "always"; + } else { + signDisplay = "auto"; + } + return this.num(lildur.get(mapped) * inversionFactor, token.length, signDisplay); + } else { + return token; + } + }, tokens = _Formatter.parseFormat(fmt), realTokens = tokens.reduce((found, { + literal, + val + }) => literal ? found : found.concat(val), []), collapsed = dur.shiftTo(...realTokens.map(tokenToField).filter((t) => t)), durationInfo = { + isNegativeDuration: collapsed < 0, + // this relies on "collapsed" being based on "shiftTo", which builds up the object + // in order + largestUnit: Object.keys(collapsed.values)[0] + }; + return stringifyTokens(tokens, tokenToString(collapsed, durationInfo)); + } + }; + var ianaRegex = /[A-Za-z_+-]{1,256}(?::?\/[A-Za-z0-9_+-]{1,256}(?:\/[A-Za-z0-9_+-]{1,256})?)?/; + function combineRegexes(...regexes) { + const full = regexes.reduce((f, r) => f + r.source, ""); + return RegExp(`^${full}$`); + } + function combineExtractors(...extractors) { + return (m) => extractors.reduce(([mergedVals, mergedZone, cursor], ex) => { + const [val, zone, next] = ex(m, cursor); + return [{ + ...mergedVals, + ...val + }, zone || mergedZone, next]; + }, [{}, null, 1]).slice(0, 2); + } + function parse(s16, ...patterns) { + if (s16 == null) { + return [null, null]; + } + for (const [regex, extractor] of patterns) { + const m = regex.exec(s16); + if (m) { + return extractor(m); + } + } + return [null, null]; + } + function simpleParse(...keys) { + return (match2, cursor) => { + const ret = {}; + let i; + for (i = 0; i < keys.length; i++) { + ret[keys[i]] = parseInteger(match2[cursor + i]); + } + return [ret, null, cursor + i]; + }; + } + var offsetRegex = /(?:([Zz])|([+-]\d\d)(?::?(\d\d))?)/; + var isoExtendedZone = `(?:${offsetRegex.source}?(?:\\[(${ianaRegex.source})\\])?)?`; + var isoTimeBaseRegex = /(\d\d)(?::?(\d\d)(?::?(\d\d)(?:[.,](\d{1,30}))?)?)?/; + var isoTimeRegex = RegExp(`${isoTimeBaseRegex.source}${isoExtendedZone}`); + var isoTimeExtensionRegex = RegExp(`(?:[Tt]${isoTimeRegex.source})?`); + var isoYmdRegex = /([+-]\d{6}|\d{4})(?:-?(\d\d)(?:-?(\d\d))?)?/; + var isoWeekRegex = /(\d{4})-?W(\d\d)(?:-?(\d))?/; + var isoOrdinalRegex = /(\d{4})-?(\d{3})/; + var extractISOWeekData = simpleParse("weekYear", "weekNumber", "weekDay"); + var extractISOOrdinalData = simpleParse("year", "ordinal"); + var sqlYmdRegex = /(\d{4})-(\d\d)-(\d\d)/; + var sqlTimeRegex = RegExp(`${isoTimeBaseRegex.source} ?(?:${offsetRegex.source}|(${ianaRegex.source}))?`); + var sqlTimeExtensionRegex = RegExp(`(?: ${sqlTimeRegex.source})?`); + function int(match2, pos, fallback) { + const m = match2[pos]; + return isUndefined(m) ? fallback : parseInteger(m); + } + function extractISOYmd(match2, cursor) { + const item = { + year: int(match2, cursor), + month: int(match2, cursor + 1, 1), + day: int(match2, cursor + 2, 1) + }; + return [item, null, cursor + 3]; + } + function extractISOTime(match2, cursor) { + const item = { + hours: int(match2, cursor, 0), + minutes: int(match2, cursor + 1, 0), + seconds: int(match2, cursor + 2, 0), + milliseconds: parseMillis(match2[cursor + 3]) + }; + return [item, null, cursor + 4]; + } + function extractISOOffset(match2, cursor) { + const local = !match2[cursor] && !match2[cursor + 1], fullOffset = signedOffset(match2[cursor + 1], match2[cursor + 2]), zone = local ? null : FixedOffsetZone.instance(fullOffset); + return [{}, zone, cursor + 3]; + } + function extractIANAZone(match2, cursor) { + const zone = match2[cursor] ? IANAZone.create(match2[cursor]) : null; + return [{}, zone, cursor + 1]; + } + var isoTimeOnly = RegExp(`^T?${isoTimeBaseRegex.source}$`); + var isoDuration = /^-?P(?:(?:(-?\d{1,20}(?:\.\d{1,20})?)Y)?(?:(-?\d{1,20}(?:\.\d{1,20})?)M)?(?:(-?\d{1,20}(?:\.\d{1,20})?)W)?(?:(-?\d{1,20}(?:\.\d{1,20})?)D)?(?:T(?:(-?\d{1,20}(?:\.\d{1,20})?)H)?(?:(-?\d{1,20}(?:\.\d{1,20})?)M)?(?:(-?\d{1,20})(?:[.,](-?\d{1,20}))?S)?)?)$/; + function extractISODuration(match2) { + const [s16, yearStr, monthStr, weekStr, dayStr, hourStr, minuteStr, secondStr, millisecondsStr] = match2; + const hasNegativePrefix = s16[0] === "-"; + const negativeSeconds = secondStr && secondStr[0] === "-"; + const maybeNegate = (num, force = false) => num !== void 0 && (force || num && hasNegativePrefix) ? -num : num; + return [{ + years: maybeNegate(parseFloating(yearStr)), + months: maybeNegate(parseFloating(monthStr)), + weeks: maybeNegate(parseFloating(weekStr)), + days: maybeNegate(parseFloating(dayStr)), + hours: maybeNegate(parseFloating(hourStr)), + minutes: maybeNegate(parseFloating(minuteStr)), + seconds: maybeNegate(parseFloating(secondStr), secondStr === "-0"), + milliseconds: maybeNegate(parseMillis(millisecondsStr), negativeSeconds) + }]; + } + var obsOffsets = { + GMT: 0, + EDT: -4 * 60, + EST: -5 * 60, + CDT: -5 * 60, + CST: -6 * 60, + MDT: -6 * 60, + MST: -7 * 60, + PDT: -7 * 60, + PST: -8 * 60 + }; + function fromStrings(weekdayStr, yearStr, monthStr, dayStr, hourStr, minuteStr, secondStr) { + const result = { + year: yearStr.length === 2 ? untruncateYear(parseInteger(yearStr)) : parseInteger(yearStr), + month: monthsShort.indexOf(monthStr) + 1, + day: parseInteger(dayStr), + hour: parseInteger(hourStr), + minute: parseInteger(minuteStr) + }; + if (secondStr) result.second = parseInteger(secondStr); + if (weekdayStr) { + result.weekday = weekdayStr.length > 3 ? weekdaysLong.indexOf(weekdayStr) + 1 : weekdaysShort.indexOf(weekdayStr) + 1; + } + return result; + } + var rfc2822 = /^(?:(Mon|Tue|Wed|Thu|Fri|Sat|Sun),\s)?(\d{1,2})\s(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)\s(\d{2,4})\s(\d\d):(\d\d)(?::(\d\d))?\s(?:(UT|GMT|[ECMP][SD]T)|([Zz])|(?:([+-]\d\d)(\d\d)))$/; + function extractRFC2822(match2) { + const [, weekdayStr, dayStr, monthStr, yearStr, hourStr, minuteStr, secondStr, obsOffset, milOffset, offHourStr, offMinuteStr] = match2, result = fromStrings(weekdayStr, yearStr, monthStr, dayStr, hourStr, minuteStr, secondStr); + let offset2; + if (obsOffset) { + offset2 = obsOffsets[obsOffset]; + } else if (milOffset) { + offset2 = 0; + } else { + offset2 = signedOffset(offHourStr, offMinuteStr); + } + return [result, new FixedOffsetZone(offset2)]; + } + function preprocessRFC2822(s16) { + return s16.replace(/\([^()]*\)|[\n\t]/g, " ").replace(/(\s\s+)/g, " ").trim(); + } + var rfc1123 = /^(Mon|Tue|Wed|Thu|Fri|Sat|Sun), (\d\d) (Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec) (\d{4}) (\d\d):(\d\d):(\d\d) GMT$/; + var rfc850 = /^(Monday|Tuesday|Wednesday|Thursday|Friday|Saturday|Sunday), (\d\d)-(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)-(\d\d) (\d\d):(\d\d):(\d\d) GMT$/; + var ascii = /^(Mon|Tue|Wed|Thu|Fri|Sat|Sun) (Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec) ( \d|\d\d) (\d\d):(\d\d):(\d\d) (\d{4})$/; + function extractRFC1123Or850(match2) { + const [, weekdayStr, dayStr, monthStr, yearStr, hourStr, minuteStr, secondStr] = match2, result = fromStrings(weekdayStr, yearStr, monthStr, dayStr, hourStr, minuteStr, secondStr); + return [result, FixedOffsetZone.utcInstance]; + } + function extractASCII(match2) { + const [, weekdayStr, monthStr, dayStr, hourStr, minuteStr, secondStr, yearStr] = match2, result = fromStrings(weekdayStr, yearStr, monthStr, dayStr, hourStr, minuteStr, secondStr); + return [result, FixedOffsetZone.utcInstance]; + } + var isoYmdWithTimeExtensionRegex = combineRegexes(isoYmdRegex, isoTimeExtensionRegex); + var isoWeekWithTimeExtensionRegex = combineRegexes(isoWeekRegex, isoTimeExtensionRegex); + var isoOrdinalWithTimeExtensionRegex = combineRegexes(isoOrdinalRegex, isoTimeExtensionRegex); + var isoTimeCombinedRegex = combineRegexes(isoTimeRegex); + var extractISOYmdTimeAndOffset = combineExtractors(extractISOYmd, extractISOTime, extractISOOffset, extractIANAZone); + var extractISOWeekTimeAndOffset = combineExtractors(extractISOWeekData, extractISOTime, extractISOOffset, extractIANAZone); + var extractISOOrdinalDateAndTime = combineExtractors(extractISOOrdinalData, extractISOTime, extractISOOffset, extractIANAZone); + var extractISOTimeAndOffset = combineExtractors(extractISOTime, extractISOOffset, extractIANAZone); + function parseISODate(s16) { + return parse(s16, [isoYmdWithTimeExtensionRegex, extractISOYmdTimeAndOffset], [isoWeekWithTimeExtensionRegex, extractISOWeekTimeAndOffset], [isoOrdinalWithTimeExtensionRegex, extractISOOrdinalDateAndTime], [isoTimeCombinedRegex, extractISOTimeAndOffset]); + } + function parseRFC2822Date(s16) { + return parse(preprocessRFC2822(s16), [rfc2822, extractRFC2822]); + } + function parseHTTPDate(s16) { + return parse(s16, [rfc1123, extractRFC1123Or850], [rfc850, extractRFC1123Or850], [ascii, extractASCII]); + } + function parseISODuration(s16) { + return parse(s16, [isoDuration, extractISODuration]); + } + var extractISOTimeOnly = combineExtractors(extractISOTime); + function parseISOTimeOnly(s16) { + return parse(s16, [isoTimeOnly, extractISOTimeOnly]); + } + var sqlYmdWithTimeExtensionRegex = combineRegexes(sqlYmdRegex, sqlTimeExtensionRegex); + var sqlTimeCombinedRegex = combineRegexes(sqlTimeRegex); + var extractISOTimeOffsetAndIANAZone = combineExtractors(extractISOTime, extractISOOffset, extractIANAZone); + function parseSQL(s16) { + return parse(s16, [sqlYmdWithTimeExtensionRegex, extractISOYmdTimeAndOffset], [sqlTimeCombinedRegex, extractISOTimeOffsetAndIANAZone]); + } + var INVALID$2 = "Invalid Duration"; + var lowOrderMatrix = { + weeks: { + days: 7, + hours: 7 * 24, + minutes: 7 * 24 * 60, + seconds: 7 * 24 * 60 * 60, + milliseconds: 7 * 24 * 60 * 60 * 1e3 + }, + days: { + hours: 24, + minutes: 24 * 60, + seconds: 24 * 60 * 60, + milliseconds: 24 * 60 * 60 * 1e3 + }, + hours: { + minutes: 60, + seconds: 60 * 60, + milliseconds: 60 * 60 * 1e3 + }, + minutes: { + seconds: 60, + milliseconds: 60 * 1e3 + }, + seconds: { + milliseconds: 1e3 + } + }; + var casualMatrix = { + years: { + quarters: 4, + months: 12, + weeks: 52, + days: 365, + hours: 365 * 24, + minutes: 365 * 24 * 60, + seconds: 365 * 24 * 60 * 60, + milliseconds: 365 * 24 * 60 * 60 * 1e3 + }, + quarters: { + months: 3, + weeks: 13, + days: 91, + hours: 91 * 24, + minutes: 91 * 24 * 60, + seconds: 91 * 24 * 60 * 60, + milliseconds: 91 * 24 * 60 * 60 * 1e3 + }, + months: { + weeks: 4, + days: 30, + hours: 30 * 24, + minutes: 30 * 24 * 60, + seconds: 30 * 24 * 60 * 60, + milliseconds: 30 * 24 * 60 * 60 * 1e3 + }, + ...lowOrderMatrix + }; + var daysInYearAccurate = 146097 / 400; + var daysInMonthAccurate = 146097 / 4800; + var accurateMatrix = { + years: { + quarters: 4, + months: 12, + weeks: daysInYearAccurate / 7, + days: daysInYearAccurate, + hours: daysInYearAccurate * 24, + minutes: daysInYearAccurate * 24 * 60, + seconds: daysInYearAccurate * 24 * 60 * 60, + milliseconds: daysInYearAccurate * 24 * 60 * 60 * 1e3 + }, + quarters: { + months: 3, + weeks: daysInYearAccurate / 28, + days: daysInYearAccurate / 4, + hours: daysInYearAccurate * 24 / 4, + minutes: daysInYearAccurate * 24 * 60 / 4, + seconds: daysInYearAccurate * 24 * 60 * 60 / 4, + milliseconds: daysInYearAccurate * 24 * 60 * 60 * 1e3 / 4 + }, + months: { + weeks: daysInMonthAccurate / 7, + days: daysInMonthAccurate, + hours: daysInMonthAccurate * 24, + minutes: daysInMonthAccurate * 24 * 60, + seconds: daysInMonthAccurate * 24 * 60 * 60, + milliseconds: daysInMonthAccurate * 24 * 60 * 60 * 1e3 + }, + ...lowOrderMatrix + }; + var orderedUnits$1 = ["years", "quarters", "months", "weeks", "days", "hours", "minutes", "seconds", "milliseconds"]; + var reverseUnits = orderedUnits$1.slice(0).reverse(); + function clone$1(dur, alts, clear = false) { + const conf = { + values: clear ? alts.values : { + ...dur.values, + ...alts.values || {} + }, + loc: dur.loc.clone(alts.loc), + conversionAccuracy: alts.conversionAccuracy || dur.conversionAccuracy, + matrix: alts.matrix || dur.matrix + }; + return new Duration(conf); + } + function durationToMillis(matrix, vals) { + var _vals$milliseconds; + let sum = (_vals$milliseconds = vals.milliseconds) != null ? _vals$milliseconds : 0; + for (const unit of reverseUnits.slice(1)) { + if (vals[unit]) { + sum += vals[unit] * matrix[unit]["milliseconds"]; + } + } + return sum; + } + function normalizeValues(matrix, vals) { + const factor = durationToMillis(matrix, vals) < 0 ? -1 : 1; + orderedUnits$1.reduceRight((previous, current) => { + if (!isUndefined(vals[current])) { + if (previous) { + const previousVal = vals[previous] * factor; + const conv = matrix[current][previous]; + const rollUp = Math.floor(previousVal / conv); + vals[current] += rollUp * factor; + vals[previous] -= rollUp * conv * factor; + } + return current; + } else { + return previous; + } + }, null); + orderedUnits$1.reduce((previous, current) => { + if (!isUndefined(vals[current])) { + if (previous) { + const fraction = vals[previous] % 1; + vals[previous] -= fraction; + vals[current] += fraction * matrix[previous][current]; + } + return current; + } else { + return previous; + } + }, null); + } + function removeZeroes(vals) { + const newVals = {}; + for (const [key, value] of Object.entries(vals)) { + if (value !== 0) { + newVals[key] = value; + } + } + return newVals; + } + var Duration = class _Duration { + /** + * @private + */ + constructor(config) { + const accurate = config.conversionAccuracy === "longterm" || false; + let matrix = accurate ? accurateMatrix : casualMatrix; + if (config.matrix) { + matrix = config.matrix; + } + this.values = config.values; + this.loc = config.loc || Locale.create(); + this.conversionAccuracy = accurate ? "longterm" : "casual"; + this.invalid = config.invalid || null; + this.matrix = matrix; + this.isLuxonDuration = true; + } + /** + * Create Duration from a number of milliseconds. + * @param {number} count of milliseconds + * @param {Object} opts - options for parsing + * @param {string} [opts.locale='en-US'] - the locale to use + * @param {string} opts.numberingSystem - the numbering system to use + * @param {string} [opts.conversionAccuracy='casual'] - the conversion system to use + * @return {Duration} + */ + static fromMillis(count, opts) { + return _Duration.fromObject({ + milliseconds: count + }, opts); + } + /** + * Create a Duration from a JavaScript object with keys like 'years' and 'hours'. + * If this object is empty then a zero milliseconds duration is returned. + * @param {Object} obj - the object to create the DateTime from + * @param {number} obj.years + * @param {number} obj.quarters + * @param {number} obj.months + * @param {number} obj.weeks + * @param {number} obj.days + * @param {number} obj.hours + * @param {number} obj.minutes + * @param {number} obj.seconds + * @param {number} obj.milliseconds + * @param {Object} [opts=[]] - options for creating this Duration + * @param {string} [opts.locale='en-US'] - the locale to use + * @param {string} opts.numberingSystem - the numbering system to use + * @param {string} [opts.conversionAccuracy='casual'] - the preset conversion system to use + * @param {string} [opts.matrix=Object] - the custom conversion system to use + * @return {Duration} + */ + static fromObject(obj, opts = {}) { + if (obj == null || typeof obj !== "object") { + throw new InvalidArgumentError(`Duration.fromObject: argument expected to be an object, got ${obj === null ? "null" : typeof obj}`); + } + return new _Duration({ + values: normalizeObject(obj, _Duration.normalizeUnit), + loc: Locale.fromObject(opts), + conversionAccuracy: opts.conversionAccuracy, + matrix: opts.matrix + }); + } + /** + * Create a Duration from DurationLike. + * + * @param {Object | number | Duration} durationLike + * One of: + * - object with keys like 'years' and 'hours'. + * - number representing milliseconds + * - Duration instance + * @return {Duration} + */ + static fromDurationLike(durationLike) { + if (isNumber(durationLike)) { + return _Duration.fromMillis(durationLike); + } else if (_Duration.isDuration(durationLike)) { + return durationLike; + } else if (typeof durationLike === "object") { + return _Duration.fromObject(durationLike); + } else { + throw new InvalidArgumentError(`Unknown duration argument ${durationLike} of type ${typeof durationLike}`); + } + } + /** + * Create a Duration from an ISO 8601 duration string. + * @param {string} text - text to parse + * @param {Object} opts - options for parsing + * @param {string} [opts.locale='en-US'] - the locale to use + * @param {string} opts.numberingSystem - the numbering system to use + * @param {string} [opts.conversionAccuracy='casual'] - the preset conversion system to use + * @param {string} [opts.matrix=Object] - the preset conversion system to use + * @see https://en.wikipedia.org/wiki/ISO_8601#Durations + * @example Duration.fromISO('P3Y6M1W4DT12H30M5S').toObject() //=> { years: 3, months: 6, weeks: 1, days: 4, hours: 12, minutes: 30, seconds: 5 } + * @example Duration.fromISO('PT23H').toObject() //=> { hours: 23 } + * @example Duration.fromISO('P5Y3M').toObject() //=> { years: 5, months: 3 } + * @return {Duration} + */ + static fromISO(text, opts) { + const [parsed] = parseISODuration(text); + if (parsed) { + return _Duration.fromObject(parsed, opts); + } else { + return _Duration.invalid("unparsable", `the input "${text}" can't be parsed as ISO 8601`); + } + } + /** + * Create a Duration from an ISO 8601 time string. + * @param {string} text - text to parse + * @param {Object} opts - options for parsing + * @param {string} [opts.locale='en-US'] - the locale to use + * @param {string} opts.numberingSystem - the numbering system to use + * @param {string} [opts.conversionAccuracy='casual'] - the preset conversion system to use + * @param {string} [opts.matrix=Object] - the conversion system to use + * @see https://en.wikipedia.org/wiki/ISO_8601#Times + * @example Duration.fromISOTime('11:22:33.444').toObject() //=> { hours: 11, minutes: 22, seconds: 33, milliseconds: 444 } + * @example Duration.fromISOTime('11:00').toObject() //=> { hours: 11, minutes: 0, seconds: 0 } + * @example Duration.fromISOTime('T11:00').toObject() //=> { hours: 11, minutes: 0, seconds: 0 } + * @example Duration.fromISOTime('1100').toObject() //=> { hours: 11, minutes: 0, seconds: 0 } + * @example Duration.fromISOTime('T1100').toObject() //=> { hours: 11, minutes: 0, seconds: 0 } + * @return {Duration} + */ + static fromISOTime(text, opts) { + const [parsed] = parseISOTimeOnly(text); + if (parsed) { + return _Duration.fromObject(parsed, opts); + } else { + return _Duration.invalid("unparsable", `the input "${text}" can't be parsed as ISO 8601`); + } + } + /** + * Create an invalid Duration. + * @param {string} reason - simple string of why this datetime is invalid. Should not contain parameters or anything else data-dependent + * @param {string} [explanation=null] - longer explanation, may include parameters and other useful debugging information + * @return {Duration} + */ + static invalid(reason, explanation = null) { + if (!reason) { + throw new InvalidArgumentError("need to specify a reason the Duration is invalid"); + } + const invalid = reason instanceof Invalid ? reason : new Invalid(reason, explanation); + if (Settings.throwOnInvalid) { + throw new InvalidDurationError(invalid); + } else { + return new _Duration({ + invalid + }); + } + } + /** + * @private + */ + static normalizeUnit(unit) { + const normalized = { + year: "years", + years: "years", + quarter: "quarters", + quarters: "quarters", + month: "months", + months: "months", + week: "weeks", + weeks: "weeks", + day: "days", + days: "days", + hour: "hours", + hours: "hours", + minute: "minutes", + minutes: "minutes", + second: "seconds", + seconds: "seconds", + millisecond: "milliseconds", + milliseconds: "milliseconds" + }[unit ? unit.toLowerCase() : unit]; + if (!normalized) throw new InvalidUnitError(unit); + return normalized; + } + /** + * Check if an object is a Duration. Works across context boundaries + * @param {object} o + * @return {boolean} + */ + static isDuration(o2) { + return o2 && o2.isLuxonDuration || false; + } + /** + * Get the locale of a Duration, such 'en-GB' + * @type {string} + */ + get locale() { + return this.isValid ? this.loc.locale : null; + } + /** + * Get the numbering system of a Duration, such 'beng'. The numbering system is used when formatting the Duration + * + * @type {string} + */ + get numberingSystem() { + return this.isValid ? this.loc.numberingSystem : null; + } + /** + * Returns a string representation of this Duration formatted according to the specified format string. You may use these tokens: + * * `S` for milliseconds + * * `s` for seconds + * * `m` for minutes + * * `h` for hours + * * `d` for days + * * `w` for weeks + * * `M` for months + * * `y` for years + * Notes: + * * Add padding by repeating the token, e.g. "yy" pads the years to two digits, "hhhh" pads the hours out to four digits + * * Tokens can be escaped by wrapping with single quotes. + * * The duration will be converted to the set of units in the format string using {@link Duration#shiftTo} and the Durations's conversion accuracy setting. + * @param {string} fmt - the format string + * @param {Object} opts - options + * @param {boolean} [opts.floor=true] - floor numerical values + * @param {'negative'|'all'|'negativeLargestOnly'} [opts.signMode=negative] - How to handle signs + * @example Duration.fromObject({ years: 1, days: 6, seconds: 2 }).toFormat("y d s") //=> "1 6 2" + * @example Duration.fromObject({ years: 1, days: 6, seconds: 2 }).toFormat("yy dd sss") //=> "01 06 002" + * @example Duration.fromObject({ years: 1, days: 6, seconds: 2 }).toFormat("M S") //=> "12 518402000" + * @example Duration.fromObject({ days: 6, seconds: 2 }).toFormat("d s", { signMode: "all" }) //=> "+6 +2" + * @example Duration.fromObject({ days: -6, seconds: -2 }).toFormat("d s", { signMode: "all" }) //=> "-6 -2" + * @example Duration.fromObject({ days: -6, seconds: -2 }).toFormat("d s", { signMode: "negativeLargestOnly" }) //=> "-6 2" + * @return {string} + */ + toFormat(fmt, opts = {}) { + const fmtOpts = { + ...opts, + floor: opts.round !== false && opts.floor !== false + }; + return this.isValid ? Formatter.create(this.loc, fmtOpts).formatDurationFromString(this, fmt) : INVALID$2; + } + /** + * Returns a string representation of a Duration with all units included. + * To modify its behavior, use `listStyle` and any Intl.NumberFormat option, though `unitDisplay` is especially relevant. + * @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat/NumberFormat#options + * @param {Object} opts - Formatting options. Accepts the same keys as the options parameter of the native `Intl.NumberFormat` constructor, as well as `listStyle`. + * @param {string} [opts.listStyle='narrow'] - How to format the merged list. Corresponds to the `style` property of the options parameter of the native `Intl.ListFormat` constructor. + * @param {boolean} [opts.showZeros=true] - Show all units previously used by the duration even if they are zero + * @example + * ```js + * var dur = Duration.fromObject({ months: 1, weeks: 0, hours: 5, minutes: 6 }) + * dur.toHuman() //=> '1 month, 0 weeks, 5 hours, 6 minutes' + * dur.toHuman({ listStyle: "long" }) //=> '1 month, 0 weeks, 5 hours, and 6 minutes' + * dur.toHuman({ unitDisplay: "short" }) //=> '1 mth, 0 wks, 5 hr, 6 min' + * dur.toHuman({ showZeros: false }) //=> '1 month, 5 hours, 6 minutes' + * ``` + */ + toHuman(opts = {}) { + if (!this.isValid) return INVALID$2; + const showZeros = opts.showZeros !== false; + const l2 = orderedUnits$1.map((unit) => { + const val = this.values[unit]; + if (isUndefined(val) || val === 0 && !showZeros) { + return null; + } + return this.loc.numberFormatter({ + style: "unit", + unitDisplay: "long", + ...opts, + unit: unit.slice(0, -1) + }).format(val); + }).filter((n2) => n2); + return this.loc.listFormatter({ + type: "conjunction", + style: opts.listStyle || "narrow", + ...opts + }).format(l2); + } + /** + * Returns a JavaScript object with this Duration's values. + * @example Duration.fromObject({ years: 1, days: 6, seconds: 2 }).toObject() //=> { years: 1, days: 6, seconds: 2 } + * @return {Object} + */ + toObject() { + if (!this.isValid) return {}; + return { + ...this.values + }; + } + /** + * Returns an ISO 8601-compliant string representation of this Duration. + * @see https://en.wikipedia.org/wiki/ISO_8601#Durations + * @example Duration.fromObject({ years: 3, seconds: 45 }).toISO() //=> 'P3YT45S' + * @example Duration.fromObject({ months: 4, seconds: 45 }).toISO() //=> 'P4MT45S' + * @example Duration.fromObject({ months: 5 }).toISO() //=> 'P5M' + * @example Duration.fromObject({ minutes: 5 }).toISO() //=> 'PT5M' + * @example Duration.fromObject({ milliseconds: 6 }).toISO() //=> 'PT0.006S' + * @return {string} + */ + toISO() { + if (!this.isValid) return null; + let s16 = "P"; + if (this.years !== 0) s16 += this.years + "Y"; + if (this.months !== 0 || this.quarters !== 0) s16 += this.months + this.quarters * 3 + "M"; + if (this.weeks !== 0) s16 += this.weeks + "W"; + if (this.days !== 0) s16 += this.days + "D"; + if (this.hours !== 0 || this.minutes !== 0 || this.seconds !== 0 || this.milliseconds !== 0) s16 += "T"; + if (this.hours !== 0) s16 += this.hours + "H"; + if (this.minutes !== 0) s16 += this.minutes + "M"; + if (this.seconds !== 0 || this.milliseconds !== 0) + s16 += roundTo(this.seconds + this.milliseconds / 1e3, 3) + "S"; + if (s16 === "P") s16 += "T0S"; + return s16; + } + /** + * Returns an ISO 8601-compliant string representation of this Duration, formatted as a time of day. + * Note that this will return null if the duration is invalid, negative, or equal to or greater than 24 hours. + * @see https://en.wikipedia.org/wiki/ISO_8601#Times + * @param {Object} opts - options + * @param {boolean} [opts.suppressMilliseconds=false] - exclude milliseconds from the format if they're 0 + * @param {boolean} [opts.suppressSeconds=false] - exclude seconds from the format if they're 0 + * @param {boolean} [opts.includePrefix=false] - include the `T` prefix + * @param {string} [opts.format='extended'] - choose between the basic and extended format + * @example Duration.fromObject({ hours: 11 }).toISOTime() //=> '11:00:00.000' + * @example Duration.fromObject({ hours: 11 }).toISOTime({ suppressMilliseconds: true }) //=> '11:00:00' + * @example Duration.fromObject({ hours: 11 }).toISOTime({ suppressSeconds: true }) //=> '11:00' + * @example Duration.fromObject({ hours: 11 }).toISOTime({ includePrefix: true }) //=> 'T11:00:00.000' + * @example Duration.fromObject({ hours: 11 }).toISOTime({ format: 'basic' }) //=> '110000.000' + * @return {string} + */ + toISOTime(opts = {}) { + if (!this.isValid) return null; + const millis = this.toMillis(); + if (millis < 0 || millis >= 864e5) return null; + opts = { + suppressMilliseconds: false, + suppressSeconds: false, + includePrefix: false, + format: "extended", + ...opts, + includeOffset: false + }; + const dateTime = DateTime.fromMillis(millis, { + zone: "UTC" + }); + return dateTime.toISOTime(opts); + } + /** + * Returns an ISO 8601 representation of this Duration appropriate for use in JSON. + * @return {string} + */ + toJSON() { + return this.toISO(); + } + /** + * Returns an ISO 8601 representation of this Duration appropriate for use in debugging. + * @return {string} + */ + toString() { + return this.toISO(); + } + /** + * Returns a string representation of this Duration appropriate for the REPL. + * @return {string} + */ + [Symbol.for("nodejs.util.inspect.custom")]() { + if (this.isValid) { + return `Duration { values: ${JSON.stringify(this.values)} }`; + } else { + return `Duration { Invalid, reason: ${this.invalidReason} }`; + } + } + /** + * Returns an milliseconds value of this Duration. + * @return {number} + */ + toMillis() { + if (!this.isValid) return NaN; + return durationToMillis(this.matrix, this.values); + } + /** + * Returns an milliseconds value of this Duration. Alias of {@link toMillis} + * @return {number} + */ + valueOf() { + return this.toMillis(); + } + /** + * Make this Duration longer by the specified amount. Return a newly-constructed Duration. + * @param {Duration|Object|number} duration - The amount to add. Either a Luxon Duration, a number of milliseconds, the object argument to Duration.fromObject() + * @return {Duration} + */ + plus(duration) { + if (!this.isValid) return this; + const dur = _Duration.fromDurationLike(duration), result = {}; + for (const k of orderedUnits$1) { + if (hasOwnProperty(dur.values, k) || hasOwnProperty(this.values, k)) { + result[k] = dur.get(k) + this.get(k); + } + } + return clone$1(this, { + values: result + }, true); + } + /** + * Make this Duration shorter by the specified amount. Return a newly-constructed Duration. + * @param {Duration|Object|number} duration - The amount to subtract. Either a Luxon Duration, a number of milliseconds, the object argument to Duration.fromObject() + * @return {Duration} + */ + minus(duration) { + if (!this.isValid) return this; + const dur = _Duration.fromDurationLike(duration); + return this.plus(dur.negate()); + } + /** + * Scale this Duration by the specified amount. Return a newly-constructed Duration. + * @param {function} fn - The function to apply to each unit. Arity is 1 or 2: the value of the unit and, optionally, the unit name. Must return a number. + * @example Duration.fromObject({ hours: 1, minutes: 30 }).mapUnits(x => x * 2) //=> { hours: 2, minutes: 60 } + * @example Duration.fromObject({ hours: 1, minutes: 30 }).mapUnits((x, u) => u === "hours" ? x * 2 : x) //=> { hours: 2, minutes: 30 } + * @return {Duration} + */ + mapUnits(fn2) { + if (!this.isValid) return this; + const result = {}; + for (const k of Object.keys(this.values)) { + result[k] = asNumber(fn2(this.values[k], k)); + } + return clone$1(this, { + values: result + }, true); + } + /** + * Get the value of unit. + * @param {string} unit - a unit such as 'minute' or 'day' + * @example Duration.fromObject({years: 2, days: 3}).get('years') //=> 2 + * @example Duration.fromObject({years: 2, days: 3}).get('months') //=> 0 + * @example Duration.fromObject({years: 2, days: 3}).get('days') //=> 3 + * @return {number} + */ + get(unit) { + return this[_Duration.normalizeUnit(unit)]; + } + /** + * "Set" the values of specified units. Return a newly-constructed Duration. + * @param {Object} values - a mapping of units to numbers + * @example dur.set({ years: 2017 }) + * @example dur.set({ hours: 8, minutes: 30 }) + * @return {Duration} + */ + set(values) { + if (!this.isValid) return this; + const mixed = { + ...this.values, + ...normalizeObject(values, _Duration.normalizeUnit) + }; + return clone$1(this, { + values: mixed + }); + } + /** + * "Set" the locale and/or numberingSystem. Returns a newly-constructed Duration. + * @example dur.reconfigure({ locale: 'en-GB' }) + * @return {Duration} + */ + reconfigure({ + locale, + numberingSystem, + conversionAccuracy, + matrix + } = {}) { + const loc = this.loc.clone({ + locale, + numberingSystem + }); + const opts = { + loc, + matrix, + conversionAccuracy + }; + return clone$1(this, opts); + } + /** + * Return the length of the duration in the specified unit. + * @param {string} unit - a unit such as 'minutes' or 'days' + * @example Duration.fromObject({years: 1}).as('days') //=> 365 + * @example Duration.fromObject({years: 1}).as('months') //=> 12 + * @example Duration.fromObject({hours: 60}).as('days') //=> 2.5 + * @return {number} + */ + as(unit) { + return this.isValid ? this.shiftTo(unit).get(unit) : NaN; + } + /** + * Reduce this Duration to its canonical representation in its current units. + * Assuming the overall value of the Duration is positive, this means: + * - excessive values for lower-order units are converted to higher-order units (if possible, see first and second example) + * - negative lower-order units are converted to higher order units (there must be such a higher order unit, otherwise + * the overall value would be negative, see third example) + * - fractional values for higher-order units are converted to lower-order units (if possible, see fourth example) + * + * If the overall value is negative, the result of this method is equivalent to `this.negate().normalize().negate()`. + * @example Duration.fromObject({ years: 2, days: 5000 }).normalize().toObject() //=> { years: 15, days: 255 } + * @example Duration.fromObject({ days: 5000 }).normalize().toObject() //=> { days: 5000 } + * @example Duration.fromObject({ hours: 12, minutes: -45 }).normalize().toObject() //=> { hours: 11, minutes: 15 } + * @example Duration.fromObject({ years: 2.5, days: 0, hours: 0 }).normalize().toObject() //=> { years: 2, days: 182, hours: 12 } + * @return {Duration} + */ + normalize() { + if (!this.isValid) return this; + const vals = this.toObject(); + normalizeValues(this.matrix, vals); + return clone$1(this, { + values: vals + }, true); + } + /** + * Rescale units to its largest representation + * @example Duration.fromObject({ milliseconds: 90000 }).rescale().toObject() //=> { minutes: 1, seconds: 30 } + * @return {Duration} + */ + rescale() { + if (!this.isValid) return this; + const vals = removeZeroes(this.normalize().shiftToAll().toObject()); + return clone$1(this, { + values: vals + }, true); + } + /** + * Convert this Duration into its representation in a different set of units. + * @example Duration.fromObject({ hours: 1, seconds: 30 }).shiftTo('minutes', 'milliseconds').toObject() //=> { minutes: 60, milliseconds: 30000 } + * @return {Duration} + */ + shiftTo(...units) { + if (!this.isValid) return this; + if (units.length === 0) { + return this; + } + units = units.map((u) => _Duration.normalizeUnit(u)); + const built = {}, accumulated = {}, vals = this.toObject(); + let lastUnit; + for (const k of orderedUnits$1) { + if (units.indexOf(k) >= 0) { + lastUnit = k; + let own = 0; + for (const ak in accumulated) { + own += this.matrix[ak][k] * accumulated[ak]; + accumulated[ak] = 0; + } + if (isNumber(vals[k])) { + own += vals[k]; + } + const i = Math.trunc(own); + built[k] = i; + accumulated[k] = (own * 1e3 - i * 1e3) / 1e3; + } else if (isNumber(vals[k])) { + accumulated[k] = vals[k]; + } + } + for (const key in accumulated) { + if (accumulated[key] !== 0) { + built[lastUnit] += key === lastUnit ? accumulated[key] : accumulated[key] / this.matrix[lastUnit][key]; + } + } + normalizeValues(this.matrix, built); + return clone$1(this, { + values: built + }, true); + } + /** + * Shift this Duration to all available units. + * Same as shiftTo("years", "months", "weeks", "days", "hours", "minutes", "seconds", "milliseconds") + * @return {Duration} + */ + shiftToAll() { + if (!this.isValid) return this; + return this.shiftTo("years", "months", "weeks", "days", "hours", "minutes", "seconds", "milliseconds"); + } + /** + * Return the negative of this Duration. + * @example Duration.fromObject({ hours: 1, seconds: 30 }).negate().toObject() //=> { hours: -1, seconds: -30 } + * @return {Duration} + */ + negate() { + if (!this.isValid) return this; + const negated = {}; + for (const k of Object.keys(this.values)) { + negated[k] = this.values[k] === 0 ? 0 : -this.values[k]; + } + return clone$1(this, { + values: negated + }, true); + } + /** + * Removes all units with values equal to 0 from this Duration. + * @example Duration.fromObject({ years: 2, days: 0, hours: 0, minutes: 0 }).removeZeros().toObject() //=> { years: 2 } + * @return {Duration} + */ + removeZeros() { + if (!this.isValid) return this; + const vals = removeZeroes(this.values); + return clone$1(this, { + values: vals + }, true); + } + /** + * Get the years. + * @type {number} + */ + get years() { + return this.isValid ? this.values.years || 0 : NaN; + } + /** + * Get the quarters. + * @type {number} + */ + get quarters() { + return this.isValid ? this.values.quarters || 0 : NaN; + } + /** + * Get the months. + * @type {number} + */ + get months() { + return this.isValid ? this.values.months || 0 : NaN; + } + /** + * Get the weeks + * @type {number} + */ + get weeks() { + return this.isValid ? this.values.weeks || 0 : NaN; + } + /** + * Get the days. + * @type {number} + */ + get days() { + return this.isValid ? this.values.days || 0 : NaN; + } + /** + * Get the hours. + * @type {number} + */ + get hours() { + return this.isValid ? this.values.hours || 0 : NaN; + } + /** + * Get the minutes. + * @type {number} + */ + get minutes() { + return this.isValid ? this.values.minutes || 0 : NaN; + } + /** + * Get the seconds. + * @return {number} + */ + get seconds() { + return this.isValid ? this.values.seconds || 0 : NaN; + } + /** + * Get the milliseconds. + * @return {number} + */ + get milliseconds() { + return this.isValid ? this.values.milliseconds || 0 : NaN; + } + /** + * Returns whether the Duration is invalid. Invalid durations are returned by diff operations + * on invalid DateTimes or Intervals. + * @return {boolean} + */ + get isValid() { + return this.invalid === null; + } + /** + * Returns an error code if this Duration became invalid, or null if the Duration is valid + * @return {string} + */ + get invalidReason() { + return this.invalid ? this.invalid.reason : null; + } + /** + * Returns an explanation of why this Duration became invalid, or null if the Duration is valid + * @type {string} + */ + get invalidExplanation() { + return this.invalid ? this.invalid.explanation : null; + } + /** + * Equality check + * Two Durations are equal iff they have the same units and the same values for each unit. + * @param {Duration} other + * @return {boolean} + */ + equals(other) { + if (!this.isValid || !other.isValid) { + return false; + } + if (!this.loc.equals(other.loc)) { + return false; + } + function eq(v1, v2) { + if (v1 === void 0 || v1 === 0) return v2 === void 0 || v2 === 0; + return v1 === v2; + } + for (const u of orderedUnits$1) { + if (!eq(this.values[u], other.values[u])) { + return false; + } + } + return true; + } + }; + var INVALID$1 = "Invalid Interval"; + function validateStartEnd(start, end) { + if (!start || !start.isValid) { + return Interval.invalid("missing or invalid start"); + } else if (!end || !end.isValid) { + return Interval.invalid("missing or invalid end"); + } else if (end < start) { + return Interval.invalid("end before start", `The end of an interval must be after its start, but you had start=${start.toISO()} and end=${end.toISO()}`); + } else { + return null; + } + } + var Interval = class _Interval { + /** + * @private + */ + constructor(config) { + this.s = config.start; + this.e = config.end; + this.invalid = config.invalid || null; + this.isLuxonInterval = true; + } + /** + * Create an invalid Interval. + * @param {string} reason - simple string of why this Interval is invalid. Should not contain parameters or anything else data-dependent + * @param {string} [explanation=null] - longer explanation, may include parameters and other useful debugging information + * @return {Interval} + */ + static invalid(reason, explanation = null) { + if (!reason) { + throw new InvalidArgumentError("need to specify a reason the Interval is invalid"); + } + const invalid = reason instanceof Invalid ? reason : new Invalid(reason, explanation); + if (Settings.throwOnInvalid) { + throw new InvalidIntervalError(invalid); + } else { + return new _Interval({ + invalid + }); + } + } + /** + * Create an Interval from a start DateTime and an end DateTime. Inclusive of the start but not the end. + * @param {DateTime|Date|Object} start + * @param {DateTime|Date|Object} end + * @return {Interval} + */ + static fromDateTimes(start, end) { + const builtStart = friendlyDateTime(start), builtEnd = friendlyDateTime(end); + const validateError = validateStartEnd(builtStart, builtEnd); + if (validateError == null) { + return new _Interval({ + start: builtStart, + end: builtEnd + }); + } else { + return validateError; + } + } + /** + * Create an Interval from a start DateTime and a Duration to extend to. + * @param {DateTime|Date|Object} start + * @param {Duration|Object|number} duration - the length of the Interval. + * @return {Interval} + */ + static after(start, duration) { + const dur = Duration.fromDurationLike(duration), dt2 = friendlyDateTime(start); + return _Interval.fromDateTimes(dt2, dt2.plus(dur)); + } + /** + * Create an Interval from an end DateTime and a Duration to extend backwards to. + * @param {DateTime|Date|Object} end + * @param {Duration|Object|number} duration - the length of the Interval. + * @return {Interval} + */ + static before(end, duration) { + const dur = Duration.fromDurationLike(duration), dt2 = friendlyDateTime(end); + return _Interval.fromDateTimes(dt2.minus(dur), dt2); + } + /** + * Create an Interval from an ISO 8601 string. + * Accepts `/`, `/`, and `/` formats. + * @param {string} text - the ISO string to parse + * @param {Object} [opts] - options to pass {@link DateTime#fromISO} and optionally {@link Duration#fromISO} + * @see https://en.wikipedia.org/wiki/ISO_8601#Time_intervals + * @return {Interval} + */ + static fromISO(text, opts) { + const [s16, e] = (text || "").split("/", 2); + if (s16 && e) { + let start, startIsValid; + try { + start = DateTime.fromISO(s16, opts); + startIsValid = start.isValid; + } catch (e2) { + startIsValid = false; + } + let end, endIsValid; + try { + end = DateTime.fromISO(e, opts); + endIsValid = end.isValid; + } catch (e2) { + endIsValid = false; + } + if (startIsValid && endIsValid) { + return _Interval.fromDateTimes(start, end); + } + if (startIsValid) { + const dur = Duration.fromISO(e, opts); + if (dur.isValid) { + return _Interval.after(start, dur); + } + } else if (endIsValid) { + const dur = Duration.fromISO(s16, opts); + if (dur.isValid) { + return _Interval.before(end, dur); + } + } + } + return _Interval.invalid("unparsable", `the input "${text}" can't be parsed as ISO 8601`); + } + /** + * Check if an object is an Interval. Works across context boundaries + * @param {object} o + * @return {boolean} + */ + static isInterval(o2) { + return o2 && o2.isLuxonInterval || false; + } + /** + * Returns the start of the Interval + * @type {DateTime} + */ + get start() { + return this.isValid ? this.s : null; + } + /** + * Returns the end of the Interval. This is the first instant which is not part of the interval + * (Interval is half-open). + * @type {DateTime} + */ + get end() { + return this.isValid ? this.e : null; + } + /** + * Returns the last DateTime included in the interval (since end is not part of the interval) + * @type {DateTime} + */ + get lastDateTime() { + return this.isValid ? this.e ? this.e.minus(1) : null : null; + } + /** + * Returns whether this Interval's end is at least its start, meaning that the Interval isn't 'backwards'. + * @type {boolean} + */ + get isValid() { + return this.invalidReason === null; + } + /** + * Returns an error code if this Interval is invalid, or null if the Interval is valid + * @type {string} + */ + get invalidReason() { + return this.invalid ? this.invalid.reason : null; + } + /** + * Returns an explanation of why this Interval became invalid, or null if the Interval is valid + * @type {string} + */ + get invalidExplanation() { + return this.invalid ? this.invalid.explanation : null; + } + /** + * Returns the length of the Interval in the specified unit. + * @param {string} unit - the unit (such as 'hours' or 'days') to return the length in. + * @return {number} + */ + length(unit = "milliseconds") { + return this.isValid ? this.toDuration(...[unit]).get(unit) : NaN; + } + /** + * Returns the count of minutes, hours, days, months, or years included in the Interval, even in part. + * Unlike {@link Interval#length} this counts sections of the calendar, not periods of time, e.g. specifying 'day' + * asks 'what dates are included in this interval?', not 'how many days long is this interval?' + * @param {string} [unit='milliseconds'] - the unit of time to count. + * @param {Object} opts - options + * @param {boolean} [opts.useLocaleWeeks=false] - If true, use weeks based on the locale, i.e. use the locale-dependent start of the week; this operation will always use the locale of the start DateTime + * @return {number} + */ + count(unit = "milliseconds", opts) { + if (!this.isValid) return NaN; + const start = this.start.startOf(unit, opts); + let end; + if (opts != null && opts.useLocaleWeeks) { + end = this.end.reconfigure({ + locale: start.locale + }); + } else { + end = this.end; + } + end = end.startOf(unit, opts); + return Math.floor(end.diff(start, unit).get(unit)) + (end.valueOf() !== this.end.valueOf()); + } + /** + * Returns whether this Interval's start and end are both in the same unit of time + * @param {string} unit - the unit of time to check sameness on + * @return {boolean} + */ + hasSame(unit) { + return this.isValid ? this.isEmpty() || this.e.minus(1).hasSame(this.s, unit) : false; + } + /** + * Return whether this Interval has the same start and end DateTimes. + * @return {boolean} + */ + isEmpty() { + return this.s.valueOf() === this.e.valueOf(); + } + /** + * Return whether this Interval's start is after the specified DateTime. + * @param {DateTime} dateTime + * @return {boolean} + */ + isAfter(dateTime) { + if (!this.isValid) return false; + return this.s > dateTime; + } + /** + * Return whether this Interval's end is before the specified DateTime. + * @param {DateTime} dateTime + * @return {boolean} + */ + isBefore(dateTime) { + if (!this.isValid) return false; + return this.e <= dateTime; + } + /** + * Return whether this Interval contains the specified DateTime. + * @param {DateTime} dateTime + * @return {boolean} + */ + contains(dateTime) { + if (!this.isValid) return false; + return this.s <= dateTime && this.e > dateTime; + } + /** + * "Sets" the start and/or end dates. Returns a newly-constructed Interval. + * @param {Object} values - the values to set + * @param {DateTime} values.start - the starting DateTime + * @param {DateTime} values.end - the ending DateTime + * @return {Interval} + */ + set({ + start, + end + } = {}) { + if (!this.isValid) return this; + return _Interval.fromDateTimes(start || this.s, end || this.e); + } + /** + * Split this Interval at each of the specified DateTimes + * @param {...DateTime} dateTimes - the unit of time to count. + * @return {Array} + */ + splitAt(...dateTimes) { + if (!this.isValid) return []; + const sorted = dateTimes.map(friendlyDateTime).filter((d) => this.contains(d)).sort((a, b2) => a.toMillis() - b2.toMillis()), results = []; + let { + s: s16 + } = this, i = 0; + while (s16 < this.e) { + const added = sorted[i] || this.e, next = +added > +this.e ? this.e : added; + results.push(_Interval.fromDateTimes(s16, next)); + s16 = next; + i += 1; + } + return results; + } + /** + * Split this Interval into smaller Intervals, each of the specified length. + * Left over time is grouped into a smaller interval + * @param {Duration|Object|number} duration - The length of each resulting interval. + * @return {Array} + */ + splitBy(duration) { + const dur = Duration.fromDurationLike(duration); + if (!this.isValid || !dur.isValid || dur.as("milliseconds") === 0) { + return []; + } + let { + s: s16 + } = this, idx = 1, next; + const results = []; + while (s16 < this.e) { + const added = this.start.plus(dur.mapUnits((x) => x * idx)); + next = +added > +this.e ? this.e : added; + results.push(_Interval.fromDateTimes(s16, next)); + s16 = next; + idx += 1; + } + return results; + } + /** + * Split this Interval into the specified number of smaller intervals. + * @param {number} numberOfParts - The number of Intervals to divide the Interval into. + * @return {Array} + */ + divideEqually(numberOfParts) { + if (!this.isValid) return []; + return this.splitBy(this.length() / numberOfParts).slice(0, numberOfParts); + } + /** + * Return whether this Interval overlaps with the specified Interval + * @param {Interval} other + * @return {boolean} + */ + overlaps(other) { + return this.e > other.s && this.s < other.e; + } + /** + * Return whether this Interval's end is adjacent to the specified Interval's start. + * @param {Interval} other + * @return {boolean} + */ + abutsStart(other) { + if (!this.isValid) return false; + return +this.e === +other.s; + } + /** + * Return whether this Interval's start is adjacent to the specified Interval's end. + * @param {Interval} other + * @return {boolean} + */ + abutsEnd(other) { + if (!this.isValid) return false; + return +other.e === +this.s; + } + /** + * Returns true if this Interval fully contains the specified Interval, specifically if the intersect (of this Interval and the other Interval) is equal to the other Interval; false otherwise. + * @param {Interval} other + * @return {boolean} + */ + engulfs(other) { + if (!this.isValid) return false; + return this.s <= other.s && this.e >= other.e; + } + /** + * Return whether this Interval has the same start and end as the specified Interval. + * @param {Interval} other + * @return {boolean} + */ + equals(other) { + if (!this.isValid || !other.isValid) { + return false; + } + return this.s.equals(other.s) && this.e.equals(other.e); + } + /** + * Return an Interval representing the intersection of this Interval and the specified Interval. + * Specifically, the resulting Interval has the maximum start time and the minimum end time of the two Intervals. + * Returns null if the intersection is empty, meaning, the intervals don't intersect. + * @param {Interval} other + * @return {Interval} + */ + intersection(other) { + if (!this.isValid) return this; + const s16 = this.s > other.s ? this.s : other.s, e = this.e < other.e ? this.e : other.e; + if (s16 >= e) { + return null; + } else { + return _Interval.fromDateTimes(s16, e); + } + } + /** + * Return an Interval representing the union of this Interval and the specified Interval. + * Specifically, the resulting Interval has the minimum start time and the maximum end time of the two Intervals. + * @param {Interval} other + * @return {Interval} + */ + union(other) { + if (!this.isValid) return this; + const s16 = this.s < other.s ? this.s : other.s, e = this.e > other.e ? this.e : other.e; + return _Interval.fromDateTimes(s16, e); + } + /** + * Merge an array of Intervals into an equivalent minimal set of Intervals. + * Combines overlapping and adjacent Intervals. + * The resulting array will contain the Intervals in ascending order, that is, starting with the earliest Interval + * and ending with the latest. + * + * @param {Array} intervals + * @return {Array} + */ + static merge(intervals) { + const [found, final] = intervals.sort((a, b2) => a.s - b2.s).reduce(([sofar, current], item) => { + if (!current) { + return [sofar, item]; + } else if (current.overlaps(item) || current.abutsStart(item)) { + return [sofar, current.union(item)]; + } else { + return [sofar.concat([current]), item]; + } + }, [[], null]); + if (final) { + found.push(final); + } + return found; + } + /** + * Return an array of Intervals representing the spans of time that only appear in one of the specified Intervals. + * @param {Array} intervals + * @return {Array} + */ + static xor(intervals) { + let start = null, currentCount = 0; + const results = [], ends = intervals.map((i) => [{ + time: i.s, + type: "s" + }, { + time: i.e, + type: "e" + }]), flattened = Array.prototype.concat(...ends), arr = flattened.sort((a, b2) => a.time - b2.time); + for (const i of arr) { + currentCount += i.type === "s" ? 1 : -1; + if (currentCount === 1) { + start = i.time; + } else { + if (start && +start !== +i.time) { + results.push(_Interval.fromDateTimes(start, i.time)); + } + start = null; + } + } + return _Interval.merge(results); + } + /** + * Return an Interval representing the span of time in this Interval that doesn't overlap with any of the specified Intervals. + * @param {...Interval} intervals + * @return {Array} + */ + difference(...intervals) { + return _Interval.xor([this].concat(intervals)).map((i) => this.intersection(i)).filter((i) => i && !i.isEmpty()); + } + /** + * Returns a string representation of this Interval appropriate for debugging. + * @return {string} + */ + toString() { + if (!this.isValid) return INVALID$1; + return `[${this.s.toISO()} \u2013 ${this.e.toISO()})`; + } + /** + * Returns a string representation of this Interval appropriate for the REPL. + * @return {string} + */ + [Symbol.for("nodejs.util.inspect.custom")]() { + if (this.isValid) { + return `Interval { start: ${this.s.toISO()}, end: ${this.e.toISO()} }`; + } else { + return `Interval { Invalid, reason: ${this.invalidReason} }`; + } + } + /** + * Returns a localized string representing this Interval. Accepts the same options as the + * Intl.DateTimeFormat constructor and any presets defined by Luxon, such as + * {@link DateTime.DATE_FULL} or {@link DateTime.TIME_SIMPLE}. The exact behavior of this method + * is browser-specific, but in general it will return an appropriate representation of the + * Interval in the assigned locale. Defaults to the system's locale if no locale has been + * specified. + * @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DateTimeFormat + * @param {Object} [formatOpts=DateTime.DATE_SHORT] - Either a DateTime preset or + * Intl.DateTimeFormat constructor options. + * @param {Object} opts - Options to override the configuration of the start DateTime. + * @example Interval.fromISO('2022-11-07T09:00Z/2022-11-08T09:00Z').toLocaleString(); //=> 11/7/2022 – 11/8/2022 + * @example Interval.fromISO('2022-11-07T09:00Z/2022-11-08T09:00Z').toLocaleString(DateTime.DATE_FULL); //=> November 7 – 8, 2022 + * @example Interval.fromISO('2022-11-07T09:00Z/2022-11-08T09:00Z').toLocaleString(DateTime.DATE_FULL, { locale: 'fr-FR' }); //=> 7–8 novembre 2022 + * @example Interval.fromISO('2022-11-07T17:00Z/2022-11-07T19:00Z').toLocaleString(DateTime.TIME_SIMPLE); //=> 6:00 – 8:00 PM + * @example Interval.fromISO('2022-11-07T17:00Z/2022-11-07T19:00Z').toLocaleString({ weekday: 'short', month: 'short', day: '2-digit', hour: '2-digit', minute: '2-digit' }); //=> Mon, Nov 07, 6:00 – 8:00 p + * @return {string} + */ + toLocaleString(formatOpts = DATE_SHORT, opts = {}) { + return this.isValid ? Formatter.create(this.s.loc.clone(opts), formatOpts).formatInterval(this) : INVALID$1; + } + /** + * Returns an ISO 8601-compliant string representation of this Interval. + * @see https://en.wikipedia.org/wiki/ISO_8601#Time_intervals + * @param {Object} opts - The same options as {@link DateTime#toISO} + * @return {string} + */ + toISO(opts) { + if (!this.isValid) return INVALID$1; + return `${this.s.toISO(opts)}/${this.e.toISO(opts)}`; + } + /** + * Returns an ISO 8601-compliant string representation of date of this Interval. + * The time components are ignored. + * @see https://en.wikipedia.org/wiki/ISO_8601#Time_intervals + * @return {string} + */ + toISODate() { + if (!this.isValid) return INVALID$1; + return `${this.s.toISODate()}/${this.e.toISODate()}`; + } + /** + * Returns an ISO 8601-compliant string representation of time of this Interval. + * The date components are ignored. + * @see https://en.wikipedia.org/wiki/ISO_8601#Time_intervals + * @param {Object} opts - The same options as {@link DateTime#toISO} + * @return {string} + */ + toISOTime(opts) { + if (!this.isValid) return INVALID$1; + return `${this.s.toISOTime(opts)}/${this.e.toISOTime(opts)}`; + } + /** + * Returns a string representation of this Interval formatted according to the specified format + * string. **You may not want this.** See {@link Interval#toLocaleString} for a more flexible + * formatting tool. + * @param {string} dateFormat - The format string. This string formats the start and end time. + * See {@link DateTime#toFormat} for details. + * @param {Object} opts - Options. + * @param {string} [opts.separator = ' – '] - A separator to place between the start and end + * representations. + * @return {string} + */ + toFormat(dateFormat, { + separator = " \u2013 " + } = {}) { + if (!this.isValid) return INVALID$1; + return `${this.s.toFormat(dateFormat)}${separator}${this.e.toFormat(dateFormat)}`; + } + /** + * Return a Duration representing the time spanned by this interval. + * @param {string|string[]} [unit=['milliseconds']] - the unit or units (such as 'hours' or 'days') to include in the duration. + * @param {Object} opts - options that affect the creation of the Duration + * @param {string} [opts.conversionAccuracy='casual'] - the conversion system to use + * @example Interval.fromDateTimes(dt1, dt2).toDuration().toObject() //=> { milliseconds: 88489257 } + * @example Interval.fromDateTimes(dt1, dt2).toDuration('days').toObject() //=> { days: 1.0241812152777778 } + * @example Interval.fromDateTimes(dt1, dt2).toDuration(['hours', 'minutes']).toObject() //=> { hours: 24, minutes: 34.82095 } + * @example Interval.fromDateTimes(dt1, dt2).toDuration(['hours', 'minutes', 'seconds']).toObject() //=> { hours: 24, minutes: 34, seconds: 49.257 } + * @example Interval.fromDateTimes(dt1, dt2).toDuration('seconds').toObject() //=> { seconds: 88489.257 } + * @return {Duration} + */ + toDuration(unit, opts) { + if (!this.isValid) { + return Duration.invalid(this.invalidReason); + } + return this.e.diff(this.s, unit, opts); + } + /** + * Run mapFn on the interval start and end, returning a new Interval from the resulting DateTimes + * @param {function} mapFn + * @return {Interval} + * @example Interval.fromDateTimes(dt1, dt2).mapEndpoints(endpoint => endpoint.toUTC()) + * @example Interval.fromDateTimes(dt1, dt2).mapEndpoints(endpoint => endpoint.plus({ hours: 2 })) + */ + mapEndpoints(mapFn) { + return _Interval.fromDateTimes(mapFn(this.s), mapFn(this.e)); + } + }; + var Info = class { + /** + * Return whether the specified zone contains a DST. + * @param {string|Zone} [zone='local'] - Zone to check. Defaults to the environment's local zone. + * @return {boolean} + */ + static hasDST(zone = Settings.defaultZone) { + const proto = DateTime.now().setZone(zone).set({ + month: 12 + }); + return !zone.isUniversal && proto.offset !== proto.set({ + month: 6 + }).offset; + } + /** + * Return whether the specified zone is a valid IANA specifier. + * @param {string} zone - Zone to check + * @return {boolean} + */ + static isValidIANAZone(zone) { + return IANAZone.isValidZone(zone); + } + /** + * Converts the input into a {@link Zone} instance. + * + * * If `input` is already a Zone instance, it is returned unchanged. + * * If `input` is a string containing a valid time zone name, a Zone instance + * with that name is returned. + * * If `input` is a string that doesn't refer to a known time zone, a Zone + * instance with {@link Zone#isValid} == false is returned. + * * If `input is a number, a Zone instance with the specified fixed offset + * in minutes is returned. + * * If `input` is `null` or `undefined`, the default zone is returned. + * @param {string|Zone|number} [input] - the value to be converted + * @return {Zone} + */ + static normalizeZone(input) { + return normalizeZone(input, Settings.defaultZone); + } + /** + * Get the weekday on which the week starts according to the given locale. + * @param {Object} opts - options + * @param {string} [opts.locale] - the locale code + * @param {string} [opts.locObj=null] - an existing locale object to use + * @returns {number} the start of the week, 1 for Monday through 7 for Sunday + */ + static getStartOfWeek({ + locale = null, + locObj = null + } = {}) { + return (locObj || Locale.create(locale)).getStartOfWeek(); + } + /** + * Get the minimum number of days necessary in a week before it is considered part of the next year according + * to the given locale. + * @param {Object} opts - options + * @param {string} [opts.locale] - the locale code + * @param {string} [opts.locObj=null] - an existing locale object to use + * @returns {number} + */ + static getMinimumDaysInFirstWeek({ + locale = null, + locObj = null + } = {}) { + return (locObj || Locale.create(locale)).getMinDaysInFirstWeek(); + } + /** + * Get the weekdays, which are considered the weekend according to the given locale + * @param {Object} opts - options + * @param {string} [opts.locale] - the locale code + * @param {string} [opts.locObj=null] - an existing locale object to use + * @returns {number[]} an array of weekdays, 1 for Monday through 7 for Sunday + */ + static getWeekendWeekdays({ + locale = null, + locObj = null + } = {}) { + return (locObj || Locale.create(locale)).getWeekendDays().slice(); + } + /** + * Return an array of standalone month names. + * @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DateTimeFormat + * @param {string} [length='long'] - the length of the month representation, such as "numeric", "2-digit", "narrow", "short", "long" + * @param {Object} opts - options + * @param {string} [opts.locale] - the locale code + * @param {string} [opts.numberingSystem=null] - the numbering system + * @param {string} [opts.locObj=null] - an existing locale object to use + * @param {string} [opts.outputCalendar='gregory'] - the calendar + * @example Info.months()[0] //=> 'January' + * @example Info.months('short')[0] //=> 'Jan' + * @example Info.months('numeric')[0] //=> '1' + * @example Info.months('short', { locale: 'fr-CA' } )[0] //=> 'janv.' + * @example Info.months('numeric', { locale: 'ar' })[0] //=> '١' + * @example Info.months('long', { outputCalendar: 'islamic' })[0] //=> 'Rabiʻ I' + * @return {Array} + */ + static months(length = "long", { + locale = null, + numberingSystem = null, + locObj = null, + outputCalendar = "gregory" + } = {}) { + return (locObj || Locale.create(locale, numberingSystem, outputCalendar)).months(length); + } + /** + * Return an array of format month names. + * Format months differ from standalone months in that they're meant to appear next to the day of the month. In some languages, that + * changes the string. + * See {@link Info#months} + * @param {string} [length='long'] - the length of the month representation, such as "numeric", "2-digit", "narrow", "short", "long" + * @param {Object} opts - options + * @param {string} [opts.locale] - the locale code + * @param {string} [opts.numberingSystem=null] - the numbering system + * @param {string} [opts.locObj=null] - an existing locale object to use + * @param {string} [opts.outputCalendar='gregory'] - the calendar + * @return {Array} + */ + static monthsFormat(length = "long", { + locale = null, + numberingSystem = null, + locObj = null, + outputCalendar = "gregory" + } = {}) { + return (locObj || Locale.create(locale, numberingSystem, outputCalendar)).months(length, true); + } + /** + * Return an array of standalone week names. + * @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DateTimeFormat + * @param {string} [length='long'] - the length of the weekday representation, such as "narrow", "short", "long". + * @param {Object} opts - options + * @param {string} [opts.locale] - the locale code + * @param {string} [opts.numberingSystem=null] - the numbering system + * @param {string} [opts.locObj=null] - an existing locale object to use + * @example Info.weekdays()[0] //=> 'Monday' + * @example Info.weekdays('short')[0] //=> 'Mon' + * @example Info.weekdays('short', { locale: 'fr-CA' })[0] //=> 'lun.' + * @example Info.weekdays('short', { locale: 'ar' })[0] //=> 'الاثنين' + * @return {Array} + */ + static weekdays(length = "long", { + locale = null, + numberingSystem = null, + locObj = null + } = {}) { + return (locObj || Locale.create(locale, numberingSystem, null)).weekdays(length); + } + /** + * Return an array of format week names. + * Format weekdays differ from standalone weekdays in that they're meant to appear next to more date information. In some languages, that + * changes the string. + * See {@link Info#weekdays} + * @param {string} [length='long'] - the length of the month representation, such as "narrow", "short", "long". + * @param {Object} opts - options + * @param {string} [opts.locale=null] - the locale code + * @param {string} [opts.numberingSystem=null] - the numbering system + * @param {string} [opts.locObj=null] - an existing locale object to use + * @return {Array} + */ + static weekdaysFormat(length = "long", { + locale = null, + numberingSystem = null, + locObj = null + } = {}) { + return (locObj || Locale.create(locale, numberingSystem, null)).weekdays(length, true); + } + /** + * Return an array of meridiems. + * @param {Object} opts - options + * @param {string} [opts.locale] - the locale code + * @example Info.meridiems() //=> [ 'AM', 'PM' ] + * @example Info.meridiems({ locale: 'my' }) //=> [ 'နံနက်', 'ညနေ' ] + * @return {Array} + */ + static meridiems({ + locale = null + } = {}) { + return Locale.create(locale).meridiems(); + } + /** + * Return an array of eras, such as ['BC', 'AD']. The locale can be specified, but the calendar system is always Gregorian. + * @param {string} [length='short'] - the length of the era representation, such as "short" or "long". + * @param {Object} opts - options + * @param {string} [opts.locale] - the locale code + * @example Info.eras() //=> [ 'BC', 'AD' ] + * @example Info.eras('long') //=> [ 'Before Christ', 'Anno Domini' ] + * @example Info.eras('long', { locale: 'fr' }) //=> [ 'avant Jésus-Christ', 'après Jésus-Christ' ] + * @return {Array} + */ + static eras(length = "short", { + locale = null + } = {}) { + return Locale.create(locale, null, "gregory").eras(length); + } + /** + * Return the set of available features in this environment. + * Some features of Luxon are not available in all environments. For example, on older browsers, relative time formatting support is not available. Use this function to figure out if that's the case. + * Keys: + * * `relative`: whether this environment supports relative time formatting + * * `localeWeek`: whether this environment supports different weekdays for the start of the week based on the locale + * @example Info.features() //=> { relative: false, localeWeek: true } + * @return {Object} + */ + static features() { + return { + relative: hasRelative(), + localeWeek: hasLocaleWeekInfo() + }; + } + }; + function dayDiff(earlier, later) { + const utcDayStart = (dt2) => dt2.toUTC(0, { + keepLocalTime: true + }).startOf("day").valueOf(), ms2 = utcDayStart(later) - utcDayStart(earlier); + return Math.floor(Duration.fromMillis(ms2).as("days")); + } + function highOrderDiffs(cursor, later, units) { + const differs = [["years", (a, b2) => b2.year - a.year], ["quarters", (a, b2) => b2.quarter - a.quarter + (b2.year - a.year) * 4], ["months", (a, b2) => b2.month - a.month + (b2.year - a.year) * 12], ["weeks", (a, b2) => { + const days = dayDiff(a, b2); + return (days - days % 7) / 7; + }], ["days", dayDiff]]; + const results = {}; + const earlier = cursor; + let lowestOrder, highWater; + for (const [unit, differ] of differs) { + if (units.indexOf(unit) >= 0) { + lowestOrder = unit; + results[unit] = differ(cursor, later); + highWater = earlier.plus(results); + if (highWater > later) { + results[unit]--; + cursor = earlier.plus(results); + if (cursor > later) { + highWater = cursor; + results[unit]--; + cursor = earlier.plus(results); + } + } else { + cursor = highWater; + } + } + } + return [cursor, results, highWater, lowestOrder]; + } + function diff(earlier, later, units, opts) { + let [cursor, results, highWater, lowestOrder] = highOrderDiffs(earlier, later, units); + const remainingMillis = later - cursor; + const lowerOrderUnits = units.filter((u) => ["hours", "minutes", "seconds", "milliseconds"].indexOf(u) >= 0); + if (lowerOrderUnits.length === 0) { + if (highWater < later) { + highWater = cursor.plus({ + [lowestOrder]: 1 + }); + } + if (highWater !== cursor) { + results[lowestOrder] = (results[lowestOrder] || 0) + remainingMillis / (highWater - cursor); + } + } + const duration = Duration.fromObject(results, opts); + if (lowerOrderUnits.length > 0) { + return Duration.fromMillis(remainingMillis, opts).shiftTo(...lowerOrderUnits).plus(duration); + } else { + return duration; + } + } + var MISSING_FTP = "missing Intl.DateTimeFormat.formatToParts support"; + function intUnit(regex, post = (i) => i) { + return { + regex, + deser: ([s16]) => post(parseDigits(s16)) + }; + } + var NBSP = String.fromCharCode(160); + var spaceOrNBSP = `[ ${NBSP}]`; + var spaceOrNBSPRegExp = new RegExp(spaceOrNBSP, "g"); + function fixListRegex(s16) { + return s16.replace(/\./g, "\\.?").replace(spaceOrNBSPRegExp, spaceOrNBSP); + } + function stripInsensitivities(s16) { + return s16.replace(/\./g, "").replace(spaceOrNBSPRegExp, " ").toLowerCase(); + } + function oneOf(strings, startIndex) { + if (strings === null) { + return null; + } else { + return { + regex: RegExp(strings.map(fixListRegex).join("|")), + deser: ([s16]) => strings.findIndex((i) => stripInsensitivities(s16) === stripInsensitivities(i)) + startIndex + }; + } + } + function offset(regex, groups) { + return { + regex, + deser: ([, h2, m]) => signedOffset(h2, m), + groups + }; + } + function simple(regex) { + return { + regex, + deser: ([s16]) => s16 + }; + } + function escapeToken(value) { + return value.replace(/[\-\[\]{}()*+?.,\\\^$|#\s]/g, "\\$&"); + } + function unitForToken(token, loc) { + const one = digitRegex(loc), two = digitRegex(loc, "{2}"), three = digitRegex(loc, "{3}"), four = digitRegex(loc, "{4}"), six = digitRegex(loc, "{6}"), oneOrTwo = digitRegex(loc, "{1,2}"), oneToThree = digitRegex(loc, "{1,3}"), oneToSix = digitRegex(loc, "{1,6}"), oneToNine = digitRegex(loc, "{1,9}"), twoToFour = digitRegex(loc, "{2,4}"), fourToSix = digitRegex(loc, "{4,6}"), literal = (t) => ({ + regex: RegExp(escapeToken(t.val)), + deser: ([s16]) => s16, + literal: true + }), unitate = (t) => { + if (token.literal) { + return literal(t); + } + switch (t.val) { + // era + case "G": + return oneOf(loc.eras("short"), 0); + case "GG": + return oneOf(loc.eras("long"), 0); + // years + case "y": + return intUnit(oneToSix); + case "yy": + return intUnit(twoToFour, untruncateYear); + case "yyyy": + return intUnit(four); + case "yyyyy": + return intUnit(fourToSix); + case "yyyyyy": + return intUnit(six); + // months + case "M": + return intUnit(oneOrTwo); + case "MM": + return intUnit(two); + case "MMM": + return oneOf(loc.months("short", true), 1); + case "MMMM": + return oneOf(loc.months("long", true), 1); + case "L": + return intUnit(oneOrTwo); + case "LL": + return intUnit(two); + case "LLL": + return oneOf(loc.months("short", false), 1); + case "LLLL": + return oneOf(loc.months("long", false), 1); + // dates + case "d": + return intUnit(oneOrTwo); + case "dd": + return intUnit(two); + // ordinals + case "o": + return intUnit(oneToThree); + case "ooo": + return intUnit(three); + // time + case "HH": + return intUnit(two); + case "H": + return intUnit(oneOrTwo); + case "hh": + return intUnit(two); + case "h": + return intUnit(oneOrTwo); + case "mm": + return intUnit(two); + case "m": + return intUnit(oneOrTwo); + case "q": + return intUnit(oneOrTwo); + case "qq": + return intUnit(two); + case "s": + return intUnit(oneOrTwo); + case "ss": + return intUnit(two); + case "S": + return intUnit(oneToThree); + case "SSS": + return intUnit(three); + case "u": + return simple(oneToNine); + case "uu": + return simple(oneOrTwo); + case "uuu": + return intUnit(one); + // meridiem + case "a": + return oneOf(loc.meridiems(), 0); + // weekYear (k) + case "kkkk": + return intUnit(four); + case "kk": + return intUnit(twoToFour, untruncateYear); + // weekNumber (W) + case "W": + return intUnit(oneOrTwo); + case "WW": + return intUnit(two); + // weekdays + case "E": + case "c": + return intUnit(one); + case "EEE": + return oneOf(loc.weekdays("short", false), 1); + case "EEEE": + return oneOf(loc.weekdays("long", false), 1); + case "ccc": + return oneOf(loc.weekdays("short", true), 1); + case "cccc": + return oneOf(loc.weekdays("long", true), 1); + // offset/zone + case "Z": + case "ZZ": + return offset(new RegExp(`([+-]${oneOrTwo.source})(?::(${two.source}))?`), 2); + case "ZZZ": + return offset(new RegExp(`([+-]${oneOrTwo.source})(${two.source})?`), 2); + // we don't support ZZZZ (PST) or ZZZZZ (Pacific Standard Time) in parsing + // because we don't have any way to figure out what they are + case "z": + return simple(/[a-z_+-/]{1,256}?/i); + // this special-case "token" represents a place where a macro-token expanded into a white-space literal + // in this case we accept any non-newline white-space + case " ": + return simple(/[^\S\n\r]/); + default: + return literal(t); + } + }; + const unit = unitate(token) || { + invalidReason: MISSING_FTP + }; + unit.token = token; + return unit; + } + var partTypeStyleToTokenVal = { + year: { + "2-digit": "yy", + numeric: "yyyyy" + }, + month: { + numeric: "M", + "2-digit": "MM", + short: "MMM", + long: "MMMM" + }, + day: { + numeric: "d", + "2-digit": "dd" + }, + weekday: { + short: "EEE", + long: "EEEE" + }, + dayperiod: "a", + dayPeriod: "a", + hour12: { + numeric: "h", + "2-digit": "hh" + }, + hour24: { + numeric: "H", + "2-digit": "HH" + }, + minute: { + numeric: "m", + "2-digit": "mm" + }, + second: { + numeric: "s", + "2-digit": "ss" + }, + timeZoneName: { + long: "ZZZZZ", + short: "ZZZ" + } + }; + function tokenForPart(part, formatOpts, resolvedOpts) { + const { + type, + value + } = part; + if (type === "literal") { + const isSpace = /^\s+$/.test(value); + return { + literal: !isSpace, + val: isSpace ? " " : value + }; + } + const style = formatOpts[type]; + let actualType = type; + if (type === "hour") { + if (formatOpts.hour12 != null) { + actualType = formatOpts.hour12 ? "hour12" : "hour24"; + } else if (formatOpts.hourCycle != null) { + if (formatOpts.hourCycle === "h11" || formatOpts.hourCycle === "h12") { + actualType = "hour12"; + } else { + actualType = "hour24"; + } + } else { + actualType = resolvedOpts.hour12 ? "hour12" : "hour24"; + } + } + let val = partTypeStyleToTokenVal[actualType]; + if (typeof val === "object") { + val = val[style]; + } + if (val) { + return { + literal: false, + val + }; + } + return void 0; + } + function buildRegex(units) { + const re2 = units.map((u) => u.regex).reduce((f, r) => `${f}(${r.source})`, ""); + return [`^${re2}$`, units]; + } + function match(input, regex, handlers) { + const matches = input.match(regex); + if (matches) { + const all = {}; + let matchIndex = 1; + for (const i in handlers) { + if (hasOwnProperty(handlers, i)) { + const h2 = handlers[i], groups = h2.groups ? h2.groups + 1 : 1; + if (!h2.literal && h2.token) { + all[h2.token.val[0]] = h2.deser(matches.slice(matchIndex, matchIndex + groups)); + } + matchIndex += groups; + } + } + return [matches, all]; + } else { + return [matches, {}]; + } + } + function dateTimeFromMatches(matches) { + const toField = (token) => { + switch (token) { + case "S": + return "millisecond"; + case "s": + return "second"; + case "m": + return "minute"; + case "h": + case "H": + return "hour"; + case "d": + return "day"; + case "o": + return "ordinal"; + case "L": + case "M": + return "month"; + case "y": + return "year"; + case "E": + case "c": + return "weekday"; + case "W": + return "weekNumber"; + case "k": + return "weekYear"; + case "q": + return "quarter"; + default: + return null; + } + }; + let zone = null; + let specificOffset; + if (!isUndefined(matches.z)) { + zone = IANAZone.create(matches.z); + } + if (!isUndefined(matches.Z)) { + if (!zone) { + zone = new FixedOffsetZone(matches.Z); + } + specificOffset = matches.Z; + } + if (!isUndefined(matches.q)) { + matches.M = (matches.q - 1) * 3 + 1; + } + if (!isUndefined(matches.h)) { + if (matches.h < 12 && matches.a === 1) { + matches.h += 12; + } else if (matches.h === 12 && matches.a === 0) { + matches.h = 0; + } + } + if (matches.G === 0 && matches.y) { + matches.y = -matches.y; + } + if (!isUndefined(matches.u)) { + matches.S = parseMillis(matches.u); + } + const vals = Object.keys(matches).reduce((r, k) => { + const f = toField(k); + if (f) { + r[f] = matches[k]; + } + return r; + }, {}); + return [vals, zone, specificOffset]; + } + var dummyDateTimeCache = null; + function getDummyDateTime() { + if (!dummyDateTimeCache) { + dummyDateTimeCache = DateTime.fromMillis(1555555555555); + } + return dummyDateTimeCache; + } + function maybeExpandMacroToken(token, locale) { + if (token.literal) { + return token; + } + const formatOpts = Formatter.macroTokenToFormatOpts(token.val); + const tokens = formatOptsToTokens(formatOpts, locale); + if (tokens == null || tokens.includes(void 0)) { + return token; + } + return tokens; + } + function expandMacroTokens(tokens, locale) { + return Array.prototype.concat(...tokens.map((t) => maybeExpandMacroToken(t, locale))); + } + var TokenParser = class { + constructor(locale, format) { + this.locale = locale; + this.format = format; + this.tokens = expandMacroTokens(Formatter.parseFormat(format), locale); + this.units = this.tokens.map((t) => unitForToken(t, locale)); + this.disqualifyingUnit = this.units.find((t) => t.invalidReason); + if (!this.disqualifyingUnit) { + const [regexString, handlers] = buildRegex(this.units); + this.regex = RegExp(regexString, "i"); + this.handlers = handlers; + } + } + explainFromTokens(input) { + if (!this.isValid) { + return { + input, + tokens: this.tokens, + invalidReason: this.invalidReason + }; + } else { + const [rawMatches, matches] = match(input, this.regex, this.handlers), [result, zone, specificOffset] = matches ? dateTimeFromMatches(matches) : [null, null, void 0]; + if (hasOwnProperty(matches, "a") && hasOwnProperty(matches, "H")) { + throw new ConflictingSpecificationError("Can't include meridiem when specifying 24-hour format"); + } + return { + input, + tokens: this.tokens, + regex: this.regex, + rawMatches, + matches, + result, + zone, + specificOffset + }; + } + } + get isValid() { + return !this.disqualifyingUnit; + } + get invalidReason() { + return this.disqualifyingUnit ? this.disqualifyingUnit.invalidReason : null; + } + }; + function explainFromTokens(locale, input, format) { + const parser = new TokenParser(locale, format); + return parser.explainFromTokens(input); + } + function parseFromTokens(locale, input, format) { + const { + result, + zone, + specificOffset, + invalidReason + } = explainFromTokens(locale, input, format); + return [result, zone, specificOffset, invalidReason]; + } + function formatOptsToTokens(formatOpts, locale) { + if (!formatOpts) { + return null; + } + const formatter = Formatter.create(locale, formatOpts); + const df = formatter.dtFormatter(getDummyDateTime()); + const parts = df.formatToParts(); + const resolvedOpts = df.resolvedOptions(); + return parts.map((p) => tokenForPart(p, formatOpts, resolvedOpts)); + } + var INVALID = "Invalid DateTime"; + var MAX_DATE = 864e13; + function unsupportedZone(zone) { + return new Invalid("unsupported zone", `the zone "${zone.name}" is not supported`); + } + function possiblyCachedWeekData(dt2) { + if (dt2.weekData === null) { + dt2.weekData = gregorianToWeek(dt2.c); + } + return dt2.weekData; + } + function possiblyCachedLocalWeekData(dt2) { + if (dt2.localWeekData === null) { + dt2.localWeekData = gregorianToWeek(dt2.c, dt2.loc.getMinDaysInFirstWeek(), dt2.loc.getStartOfWeek()); + } + return dt2.localWeekData; + } + function clone(inst, alts) { + const current = { + ts: inst.ts, + zone: inst.zone, + c: inst.c, + o: inst.o, + loc: inst.loc, + invalid: inst.invalid + }; + return new DateTime({ + ...current, + ...alts, + old: current + }); + } + function fixOffset(localTS, o2, tz) { + let utcGuess = localTS - o2 * 60 * 1e3; + const o22 = tz.offset(utcGuess); + if (o2 === o22) { + return [utcGuess, o2]; + } + utcGuess -= (o22 - o2) * 60 * 1e3; + const o3 = tz.offset(utcGuess); + if (o22 === o3) { + return [utcGuess, o22]; + } + return [localTS - Math.min(o22, o3) * 60 * 1e3, Math.max(o22, o3)]; + } + function tsToObj(ts2, offset2) { + ts2 += offset2 * 60 * 1e3; + const d = new Date(ts2); + return { + year: d.getUTCFullYear(), + month: d.getUTCMonth() + 1, + day: d.getUTCDate(), + hour: d.getUTCHours(), + minute: d.getUTCMinutes(), + second: d.getUTCSeconds(), + millisecond: d.getUTCMilliseconds() + }; + } + function objToTS(obj, offset2, zone) { + return fixOffset(objToLocalTS(obj), offset2, zone); + } + function adjustTime(inst, dur) { + const oPre = inst.o, year = inst.c.year + Math.trunc(dur.years), month = inst.c.month + Math.trunc(dur.months) + Math.trunc(dur.quarters) * 3, c = { + ...inst.c, + year, + month, + day: Math.min(inst.c.day, daysInMonth(year, month)) + Math.trunc(dur.days) + Math.trunc(dur.weeks) * 7 + }, millisToAdd = Duration.fromObject({ + years: dur.years - Math.trunc(dur.years), + quarters: dur.quarters - Math.trunc(dur.quarters), + months: dur.months - Math.trunc(dur.months), + weeks: dur.weeks - Math.trunc(dur.weeks), + days: dur.days - Math.trunc(dur.days), + hours: dur.hours, + minutes: dur.minutes, + seconds: dur.seconds, + milliseconds: dur.milliseconds + }).as("milliseconds"), localTS = objToLocalTS(c); + let [ts2, o2] = fixOffset(localTS, oPre, inst.zone); + if (millisToAdd !== 0) { + ts2 += millisToAdd; + o2 = inst.zone.offset(ts2); + } + return { + ts: ts2, + o: o2 + }; + } + function parseDataToDateTime(parsed, parsedZone, opts, format, text, specificOffset) { + const { + setZone, + zone + } = opts; + if (parsed && Object.keys(parsed).length !== 0 || parsedZone) { + const interpretationZone = parsedZone || zone, inst = DateTime.fromObject(parsed, { + ...opts, + zone: interpretationZone, + specificOffset + }); + return setZone ? inst : inst.setZone(zone); + } else { + return DateTime.invalid(new Invalid("unparsable", `the input "${text}" can't be parsed as ${format}`)); + } + } + function toTechFormat(dt2, format, allowZ = true) { + return dt2.isValid ? Formatter.create(Locale.create("en-US"), { + allowZ, + forceSimple: true + }).formatDateTimeFromString(dt2, format) : null; + } + function toISODate(o2, extended, precision) { + const longFormat = o2.c.year > 9999 || o2.c.year < 0; + let c = ""; + if (longFormat && o2.c.year >= 0) c += "+"; + c += padStart(o2.c.year, longFormat ? 6 : 4); + if (precision === "year") return c; + if (extended) { + c += "-"; + c += padStart(o2.c.month); + if (precision === "month") return c; + c += "-"; + } else { + c += padStart(o2.c.month); + if (precision === "month") return c; + } + c += padStart(o2.c.day); + return c; + } + function toISOTime(o2, extended, suppressSeconds, suppressMilliseconds, includeOffset, extendedZone, precision) { + let showSeconds = !suppressSeconds || o2.c.millisecond !== 0 || o2.c.second !== 0, c = ""; + switch (precision) { + case "day": + case "month": + case "year": + break; + default: + c += padStart(o2.c.hour); + if (precision === "hour") break; + if (extended) { + c += ":"; + c += padStart(o2.c.minute); + if (precision === "minute") break; + if (showSeconds) { + c += ":"; + c += padStart(o2.c.second); + } + } else { + c += padStart(o2.c.minute); + if (precision === "minute") break; + if (showSeconds) { + c += padStart(o2.c.second); + } + } + if (precision === "second") break; + if (showSeconds && (!suppressMilliseconds || o2.c.millisecond !== 0)) { + c += "."; + c += padStart(o2.c.millisecond, 3); + } + } + if (includeOffset) { + if (o2.isOffsetFixed && o2.offset === 0 && !extendedZone) { + c += "Z"; + } else if (o2.o < 0) { + c += "-"; + c += padStart(Math.trunc(-o2.o / 60)); + c += ":"; + c += padStart(Math.trunc(-o2.o % 60)); + } else { + c += "+"; + c += padStart(Math.trunc(o2.o / 60)); + c += ":"; + c += padStart(Math.trunc(o2.o % 60)); + } + } + if (extendedZone) { + c += "[" + o2.zone.ianaName + "]"; + } + return c; + } + var defaultUnitValues = { + month: 1, + day: 1, + hour: 0, + minute: 0, + second: 0, + millisecond: 0 + }; + var defaultWeekUnitValues = { + weekNumber: 1, + weekday: 1, + hour: 0, + minute: 0, + second: 0, + millisecond: 0 + }; + var defaultOrdinalUnitValues = { + ordinal: 1, + hour: 0, + minute: 0, + second: 0, + millisecond: 0 + }; + var orderedUnits = ["year", "month", "day", "hour", "minute", "second", "millisecond"]; + var orderedWeekUnits = ["weekYear", "weekNumber", "weekday", "hour", "minute", "second", "millisecond"]; + var orderedOrdinalUnits = ["year", "ordinal", "hour", "minute", "second", "millisecond"]; + function normalizeUnit(unit) { + const normalized = { + year: "year", + years: "year", + month: "month", + months: "month", + day: "day", + days: "day", + hour: "hour", + hours: "hour", + minute: "minute", + minutes: "minute", + quarter: "quarter", + quarters: "quarter", + second: "second", + seconds: "second", + millisecond: "millisecond", + milliseconds: "millisecond", + weekday: "weekday", + weekdays: "weekday", + weeknumber: "weekNumber", + weeksnumber: "weekNumber", + weeknumbers: "weekNumber", + weekyear: "weekYear", + weekyears: "weekYear", + ordinal: "ordinal" + }[unit.toLowerCase()]; + if (!normalized) throw new InvalidUnitError(unit); + return normalized; + } + function normalizeUnitWithLocalWeeks(unit) { + switch (unit.toLowerCase()) { + case "localweekday": + case "localweekdays": + return "localWeekday"; + case "localweeknumber": + case "localweeknumbers": + return "localWeekNumber"; + case "localweekyear": + case "localweekyears": + return "localWeekYear"; + default: + return normalizeUnit(unit); + } + } + function guessOffsetForZone(zone) { + if (zoneOffsetTs === void 0) { + zoneOffsetTs = Settings.now(); + } + if (zone.type !== "iana") { + return zone.offset(zoneOffsetTs); + } + const zoneName = zone.name; + let offsetGuess = zoneOffsetGuessCache.get(zoneName); + if (offsetGuess === void 0) { + offsetGuess = zone.offset(zoneOffsetTs); + zoneOffsetGuessCache.set(zoneName, offsetGuess); + } + return offsetGuess; + } + function quickDT(obj, opts) { + const zone = normalizeZone(opts.zone, Settings.defaultZone); + if (!zone.isValid) { + return DateTime.invalid(unsupportedZone(zone)); + } + const loc = Locale.fromObject(opts); + let ts2, o2; + if (!isUndefined(obj.year)) { + for (const u of orderedUnits) { + if (isUndefined(obj[u])) { + obj[u] = defaultUnitValues[u]; + } + } + const invalid = hasInvalidGregorianData(obj) || hasInvalidTimeData(obj); + if (invalid) { + return DateTime.invalid(invalid); + } + const offsetProvis = guessOffsetForZone(zone); + [ts2, o2] = objToTS(obj, offsetProvis, zone); + } else { + ts2 = Settings.now(); + } + return new DateTime({ + ts: ts2, + zone, + loc, + o: o2 + }); + } + function diffRelative(start, end, opts) { + const round = isUndefined(opts.round) ? true : opts.round, rounding = isUndefined(opts.rounding) ? "trunc" : opts.rounding, format = (c, unit) => { + c = roundTo(c, round || opts.calendary ? 0 : 2, opts.calendary ? "round" : rounding); + const formatter = end.loc.clone(opts).relFormatter(opts); + return formatter.format(c, unit); + }, differ = (unit) => { + if (opts.calendary) { + if (!end.hasSame(start, unit)) { + return end.startOf(unit).diff(start.startOf(unit), unit).get(unit); + } else return 0; + } else { + return end.diff(start, unit).get(unit); + } + }; + if (opts.unit) { + return format(differ(opts.unit), opts.unit); + } + for (const unit of opts.units) { + const count = differ(unit); + if (Math.abs(count) >= 1) { + return format(count, unit); + } + } + return format(start > end ? -0 : 0, opts.units[opts.units.length - 1]); + } + function lastOpts(argList) { + let opts = {}, args; + if (argList.length > 0 && typeof argList[argList.length - 1] === "object") { + opts = argList[argList.length - 1]; + args = Array.from(argList).slice(0, argList.length - 1); + } else { + args = Array.from(argList); + } + return [opts, args]; + } + var zoneOffsetTs; + var zoneOffsetGuessCache = /* @__PURE__ */ new Map(); + var DateTime = class _DateTime { + /** + * @access private + */ + constructor(config) { + const zone = config.zone || Settings.defaultZone; + let invalid = config.invalid || (Number.isNaN(config.ts) ? new Invalid("invalid input") : null) || (!zone.isValid ? unsupportedZone(zone) : null); + this.ts = isUndefined(config.ts) ? Settings.now() : config.ts; + let c = null, o2 = null; + if (!invalid) { + const unchanged = config.old && config.old.ts === this.ts && config.old.zone.equals(zone); + if (unchanged) { + [c, o2] = [config.old.c, config.old.o]; + } else { + const ot2 = isNumber(config.o) && !config.old ? config.o : zone.offset(this.ts); + c = tsToObj(this.ts, ot2); + invalid = Number.isNaN(c.year) ? new Invalid("invalid input") : null; + c = invalid ? null : c; + o2 = invalid ? null : ot2; + } + } + this._zone = zone; + this.loc = config.loc || Locale.create(); + this.invalid = invalid; + this.weekData = null; + this.localWeekData = null; + this.c = c; + this.o = o2; + this.isLuxonDateTime = true; + } + // CONSTRUCT + /** + * Create a DateTime for the current instant, in the system's time zone. + * + * Use Settings to override these default values if needed. + * @example DateTime.now().toISO() //~> now in the ISO format + * @return {DateTime} + */ + static now() { + return new _DateTime({}); + } + /** + * Create a local DateTime + * @param {number} [year] - The calendar year. If omitted (as in, call `local()` with no arguments), the current time will be used + * @param {number} [month=1] - The month, 1-indexed + * @param {number} [day=1] - The day of the month, 1-indexed + * @param {number} [hour=0] - The hour of the day, in 24-hour time + * @param {number} [minute=0] - The minute of the hour, meaning a number between 0 and 59 + * @param {number} [second=0] - The second of the minute, meaning a number between 0 and 59 + * @param {number} [millisecond=0] - The millisecond of the second, meaning a number between 0 and 999 + * @example DateTime.local() //~> now + * @example DateTime.local({ zone: "America/New_York" }) //~> now, in US east coast time + * @example DateTime.local(2017) //~> 2017-01-01T00:00:00 + * @example DateTime.local(2017, 3) //~> 2017-03-01T00:00:00 + * @example DateTime.local(2017, 3, 12, { locale: "fr" }) //~> 2017-03-12T00:00:00, with a French locale + * @example DateTime.local(2017, 3, 12, 5) //~> 2017-03-12T05:00:00 + * @example DateTime.local(2017, 3, 12, 5, { zone: "utc" }) //~> 2017-03-12T05:00:00, in UTC + * @example DateTime.local(2017, 3, 12, 5, 45) //~> 2017-03-12T05:45:00 + * @example DateTime.local(2017, 3, 12, 5, 45, 10) //~> 2017-03-12T05:45:10 + * @example DateTime.local(2017, 3, 12, 5, 45, 10, 765) //~> 2017-03-12T05:45:10.765 + * @return {DateTime} + */ + static local() { + const [opts, args] = lastOpts(arguments), [year, month, day, hour, minute, second, millisecond] = args; + return quickDT({ + year, + month, + day, + hour, + minute, + second, + millisecond + }, opts); + } + /** + * Create a DateTime in UTC + * @param {number} [year] - The calendar year. If omitted (as in, call `utc()` with no arguments), the current time will be used + * @param {number} [month=1] - The month, 1-indexed + * @param {number} [day=1] - The day of the month + * @param {number} [hour=0] - The hour of the day, in 24-hour time + * @param {number} [minute=0] - The minute of the hour, meaning a number between 0 and 59 + * @param {number} [second=0] - The second of the minute, meaning a number between 0 and 59 + * @param {number} [millisecond=0] - The millisecond of the second, meaning a number between 0 and 999 + * @param {Object} options - configuration options for the DateTime + * @param {string} [options.locale] - a locale to set on the resulting DateTime instance + * @param {string} [options.outputCalendar] - the output calendar to set on the resulting DateTime instance + * @param {string} [options.numberingSystem] - the numbering system to set on the resulting DateTime instance + * @param {string} [options.weekSettings] - the week settings to set on the resulting DateTime instance + * @example DateTime.utc() //~> now + * @example DateTime.utc(2017) //~> 2017-01-01T00:00:00Z + * @example DateTime.utc(2017, 3) //~> 2017-03-01T00:00:00Z + * @example DateTime.utc(2017, 3, 12) //~> 2017-03-12T00:00:00Z + * @example DateTime.utc(2017, 3, 12, 5) //~> 2017-03-12T05:00:00Z + * @example DateTime.utc(2017, 3, 12, 5, 45) //~> 2017-03-12T05:45:00Z + * @example DateTime.utc(2017, 3, 12, 5, 45, { locale: "fr" }) //~> 2017-03-12T05:45:00Z with a French locale + * @example DateTime.utc(2017, 3, 12, 5, 45, 10) //~> 2017-03-12T05:45:10Z + * @example DateTime.utc(2017, 3, 12, 5, 45, 10, 765, { locale: "fr" }) //~> 2017-03-12T05:45:10.765Z with a French locale + * @return {DateTime} + */ + static utc() { + const [opts, args] = lastOpts(arguments), [year, month, day, hour, minute, second, millisecond] = args; + opts.zone = FixedOffsetZone.utcInstance; + return quickDT({ + year, + month, + day, + hour, + minute, + second, + millisecond + }, opts); + } + /** + * Create a DateTime from a JavaScript Date object. Uses the default zone. + * @param {Date} date - a JavaScript Date object + * @param {Object} options - configuration options for the DateTime + * @param {string|Zone} [options.zone='local'] - the zone to place the DateTime into + * @return {DateTime} + */ + static fromJSDate(date, options = {}) { + const ts2 = isDate(date) ? date.valueOf() : NaN; + if (Number.isNaN(ts2)) { + return _DateTime.invalid("invalid input"); + } + const zoneToUse = normalizeZone(options.zone, Settings.defaultZone); + if (!zoneToUse.isValid) { + return _DateTime.invalid(unsupportedZone(zoneToUse)); + } + return new _DateTime({ + ts: ts2, + zone: zoneToUse, + loc: Locale.fromObject(options) + }); + } + /** + * Create a DateTime from a number of milliseconds since the epoch (meaning since 1 January 1970 00:00:00 UTC). Uses the default zone. + * @param {number} milliseconds - a number of milliseconds since 1970 UTC + * @param {Object} options - configuration options for the DateTime + * @param {string|Zone} [options.zone='local'] - the zone to place the DateTime into + * @param {string} [options.locale] - a locale to set on the resulting DateTime instance + * @param {string} options.outputCalendar - the output calendar to set on the resulting DateTime instance + * @param {string} options.numberingSystem - the numbering system to set on the resulting DateTime instance + * @param {string} options.weekSettings - the week settings to set on the resulting DateTime instance + * @return {DateTime} + */ + static fromMillis(milliseconds, options = {}) { + if (!isNumber(milliseconds)) { + throw new InvalidArgumentError(`fromMillis requires a numerical input, but received a ${typeof milliseconds} with value ${milliseconds}`); + } else if (milliseconds < -MAX_DATE || milliseconds > MAX_DATE) { + return _DateTime.invalid("Timestamp out of range"); + } else { + return new _DateTime({ + ts: milliseconds, + zone: normalizeZone(options.zone, Settings.defaultZone), + loc: Locale.fromObject(options) + }); + } + } + /** + * Create a DateTime from a number of seconds since the epoch (meaning since 1 January 1970 00:00:00 UTC). Uses the default zone. + * @param {number} seconds - a number of seconds since 1970 UTC + * @param {Object} options - configuration options for the DateTime + * @param {string|Zone} [options.zone='local'] - the zone to place the DateTime into + * @param {string} [options.locale] - a locale to set on the resulting DateTime instance + * @param {string} options.outputCalendar - the output calendar to set on the resulting DateTime instance + * @param {string} options.numberingSystem - the numbering system to set on the resulting DateTime instance + * @param {string} options.weekSettings - the week settings to set on the resulting DateTime instance + * @return {DateTime} + */ + static fromSeconds(seconds, options = {}) { + if (!isNumber(seconds)) { + throw new InvalidArgumentError("fromSeconds requires a numerical input"); + } else { + return new _DateTime({ + ts: seconds * 1e3, + zone: normalizeZone(options.zone, Settings.defaultZone), + loc: Locale.fromObject(options) + }); + } + } + /** + * Create a DateTime from a JavaScript object with keys like 'year' and 'hour' with reasonable defaults. + * @param {Object} obj - the object to create the DateTime from + * @param {number} obj.year - a year, such as 1987 + * @param {number} obj.month - a month, 1-12 + * @param {number} obj.day - a day of the month, 1-31, depending on the month + * @param {number} obj.ordinal - day of the year, 1-365 or 366 + * @param {number} obj.weekYear - an ISO week year + * @param {number} obj.weekNumber - an ISO week number, between 1 and 52 or 53, depending on the year + * @param {number} obj.weekday - an ISO weekday, 1-7, where 1 is Monday and 7 is Sunday + * @param {number} obj.localWeekYear - a week year, according to the locale + * @param {number} obj.localWeekNumber - a week number, between 1 and 52 or 53, depending on the year, according to the locale + * @param {number} obj.localWeekday - a weekday, 1-7, where 1 is the first and 7 is the last day of the week, according to the locale + * @param {number} obj.hour - hour of the day, 0-23 + * @param {number} obj.minute - minute of the hour, 0-59 + * @param {number} obj.second - second of the minute, 0-59 + * @param {number} obj.millisecond - millisecond of the second, 0-999 + * @param {Object} opts - options for creating this DateTime + * @param {string|Zone} [opts.zone='local'] - interpret the numbers in the context of a particular zone. Can take any value taken as the first argument to setZone() + * @param {string} [opts.locale='system\'s locale'] - a locale to set on the resulting DateTime instance + * @param {string} opts.outputCalendar - the output calendar to set on the resulting DateTime instance + * @param {string} opts.numberingSystem - the numbering system to set on the resulting DateTime instance + * @param {string} opts.weekSettings - the week settings to set on the resulting DateTime instance + * @example DateTime.fromObject({ year: 1982, month: 5, day: 25}).toISODate() //=> '1982-05-25' + * @example DateTime.fromObject({ year: 1982 }).toISODate() //=> '1982-01-01' + * @example DateTime.fromObject({ hour: 10, minute: 26, second: 6 }) //~> today at 10:26:06 + * @example DateTime.fromObject({ hour: 10, minute: 26, second: 6 }, { zone: 'utc' }), + * @example DateTime.fromObject({ hour: 10, minute: 26, second: 6 }, { zone: 'local' }) + * @example DateTime.fromObject({ hour: 10, minute: 26, second: 6 }, { zone: 'America/New_York' }) + * @example DateTime.fromObject({ weekYear: 2016, weekNumber: 2, weekday: 3 }).toISODate() //=> '2016-01-13' + * @example DateTime.fromObject({ localWeekYear: 2022, localWeekNumber: 1, localWeekday: 1 }, { locale: "en-US" }).toISODate() //=> '2021-12-26' + * @return {DateTime} + */ + static fromObject(obj, opts = {}) { + obj = obj || {}; + const zoneToUse = normalizeZone(opts.zone, Settings.defaultZone); + if (!zoneToUse.isValid) { + return _DateTime.invalid(unsupportedZone(zoneToUse)); + } + const loc = Locale.fromObject(opts); + const normalized = normalizeObject(obj, normalizeUnitWithLocalWeeks); + const { + minDaysInFirstWeek, + startOfWeek + } = usesLocalWeekValues(normalized, loc); + const tsNow = Settings.now(), offsetProvis = !isUndefined(opts.specificOffset) ? opts.specificOffset : zoneToUse.offset(tsNow), containsOrdinal = !isUndefined(normalized.ordinal), containsGregorYear = !isUndefined(normalized.year), containsGregorMD = !isUndefined(normalized.month) || !isUndefined(normalized.day), containsGregor = containsGregorYear || containsGregorMD, definiteWeekDef = normalized.weekYear || normalized.weekNumber; + if ((containsGregor || containsOrdinal) && definiteWeekDef) { + throw new ConflictingSpecificationError("Can't mix weekYear/weekNumber units with year/month/day or ordinals"); + } + if (containsGregorMD && containsOrdinal) { + throw new ConflictingSpecificationError("Can't mix ordinal dates with month/day"); + } + const useWeekData = definiteWeekDef || normalized.weekday && !containsGregor; + let units, defaultValues, objNow = tsToObj(tsNow, offsetProvis); + if (useWeekData) { + units = orderedWeekUnits; + defaultValues = defaultWeekUnitValues; + objNow = gregorianToWeek(objNow, minDaysInFirstWeek, startOfWeek); + } else if (containsOrdinal) { + units = orderedOrdinalUnits; + defaultValues = defaultOrdinalUnitValues; + objNow = gregorianToOrdinal(objNow); + } else { + units = orderedUnits; + defaultValues = defaultUnitValues; + } + let foundFirst = false; + for (const u of units) { + const v2 = normalized[u]; + if (!isUndefined(v2)) { + foundFirst = true; + } else if (foundFirst) { + normalized[u] = defaultValues[u]; + } else { + normalized[u] = objNow[u]; + } + } + const higherOrderInvalid = useWeekData ? hasInvalidWeekData(normalized, minDaysInFirstWeek, startOfWeek) : containsOrdinal ? hasInvalidOrdinalData(normalized) : hasInvalidGregorianData(normalized), invalid = higherOrderInvalid || hasInvalidTimeData(normalized); + if (invalid) { + return _DateTime.invalid(invalid); + } + const gregorian = useWeekData ? weekToGregorian(normalized, minDaysInFirstWeek, startOfWeek) : containsOrdinal ? ordinalToGregorian(normalized) : normalized, [tsFinal, offsetFinal] = objToTS(gregorian, offsetProvis, zoneToUse), inst = new _DateTime({ + ts: tsFinal, + zone: zoneToUse, + o: offsetFinal, + loc + }); + if (normalized.weekday && containsGregor && obj.weekday !== inst.weekday) { + return _DateTime.invalid("mismatched weekday", `you can't specify both a weekday of ${normalized.weekday} and a date of ${inst.toISO()}`); + } + if (!inst.isValid) { + return _DateTime.invalid(inst.invalid); + } + return inst; + } + /** + * Create a DateTime from an ISO 8601 string + * @param {string} text - the ISO string + * @param {Object} opts - options to affect the creation + * @param {string|Zone} [opts.zone='local'] - use this zone if no offset is specified in the input string itself. Will also convert the time to this zone + * @param {boolean} [opts.setZone=false] - override the zone with a fixed-offset zone specified in the string itself, if it specifies one + * @param {string} [opts.locale='system's locale'] - a locale to set on the resulting DateTime instance + * @param {string} [opts.outputCalendar] - the output calendar to set on the resulting DateTime instance + * @param {string} [opts.numberingSystem] - the numbering system to set on the resulting DateTime instance + * @param {string} [opts.weekSettings] - the week settings to set on the resulting DateTime instance + * @example DateTime.fromISO('2016-05-25T09:08:34.123') + * @example DateTime.fromISO('2016-05-25T09:08:34.123+06:00') + * @example DateTime.fromISO('2016-05-25T09:08:34.123+06:00', {setZone: true}) + * @example DateTime.fromISO('2016-05-25T09:08:34.123', {zone: 'utc'}) + * @example DateTime.fromISO('2016-W05-4') + * @return {DateTime} + */ + static fromISO(text, opts = {}) { + const [vals, parsedZone] = parseISODate(text); + return parseDataToDateTime(vals, parsedZone, opts, "ISO 8601", text); + } + /** + * Create a DateTime from an RFC 2822 string + * @param {string} text - the RFC 2822 string + * @param {Object} opts - options to affect the creation + * @param {string|Zone} [opts.zone='local'] - convert the time to this zone. Since the offset is always specified in the string itself, this has no effect on the interpretation of string, merely the zone the resulting DateTime is expressed in. + * @param {boolean} [opts.setZone=false] - override the zone with a fixed-offset zone specified in the string itself, if it specifies one + * @param {string} [opts.locale='system's locale'] - a locale to set on the resulting DateTime instance + * @param {string} opts.outputCalendar - the output calendar to set on the resulting DateTime instance + * @param {string} opts.numberingSystem - the numbering system to set on the resulting DateTime instance + * @param {string} opts.weekSettings - the week settings to set on the resulting DateTime instance + * @example DateTime.fromRFC2822('25 Nov 2016 13:23:12 GMT') + * @example DateTime.fromRFC2822('Fri, 25 Nov 2016 13:23:12 +0600') + * @example DateTime.fromRFC2822('25 Nov 2016 13:23 Z') + * @return {DateTime} + */ + static fromRFC2822(text, opts = {}) { + const [vals, parsedZone] = parseRFC2822Date(text); + return parseDataToDateTime(vals, parsedZone, opts, "RFC 2822", text); + } + /** + * Create a DateTime from an HTTP header date + * @see https://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.3.1 + * @param {string} text - the HTTP header date + * @param {Object} opts - options to affect the creation + * @param {string|Zone} [opts.zone='local'] - convert the time to this zone. Since HTTP dates are always in UTC, this has no effect on the interpretation of string, merely the zone the resulting DateTime is expressed in. + * @param {boolean} [opts.setZone=false] - override the zone with the fixed-offset zone specified in the string. For HTTP dates, this is always UTC, so this option is equivalent to setting the `zone` option to 'utc', but this option is included for consistency with similar methods. + * @param {string} [opts.locale='system's locale'] - a locale to set on the resulting DateTime instance + * @param {string} opts.outputCalendar - the output calendar to set on the resulting DateTime instance + * @param {string} opts.numberingSystem - the numbering system to set on the resulting DateTime instance + * @param {string} opts.weekSettings - the week settings to set on the resulting DateTime instance + * @example DateTime.fromHTTP('Sun, 06 Nov 1994 08:49:37 GMT') + * @example DateTime.fromHTTP('Sunday, 06-Nov-94 08:49:37 GMT') + * @example DateTime.fromHTTP('Sun Nov 6 08:49:37 1994') + * @return {DateTime} + */ + static fromHTTP(text, opts = {}) { + const [vals, parsedZone] = parseHTTPDate(text); + return parseDataToDateTime(vals, parsedZone, opts, "HTTP", opts); + } + /** + * Create a DateTime from an input string and format string. + * Defaults to en-US if no locale has been specified, regardless of the system's locale. For a table of tokens and their interpretations, see [here](https://moment.github.io/luxon/#/parsing?id=table-of-tokens). + * @param {string} text - the string to parse + * @param {string} fmt - the format the string is expected to be in (see the link below for the formats) + * @param {Object} opts - options to affect the creation + * @param {string|Zone} [opts.zone='local'] - use this zone if no offset is specified in the input string itself. Will also convert the DateTime to this zone + * @param {boolean} [opts.setZone=false] - override the zone with a zone specified in the string itself, if it specifies one + * @param {string} [opts.locale='en-US'] - a locale string to use when parsing. Will also set the DateTime to this locale + * @param {string} opts.numberingSystem - the numbering system to use when parsing. Will also set the resulting DateTime to this numbering system + * @param {string} opts.weekSettings - the week settings to set on the resulting DateTime instance + * @param {string} opts.outputCalendar - the output calendar to set on the resulting DateTime instance + * @return {DateTime} + */ + static fromFormat(text, fmt, opts = {}) { + if (isUndefined(text) || isUndefined(fmt)) { + throw new InvalidArgumentError("fromFormat requires an input string and a format"); + } + const { + locale = null, + numberingSystem = null + } = opts, localeToUse = Locale.fromOpts({ + locale, + numberingSystem, + defaultToEN: true + }), [vals, parsedZone, specificOffset, invalid] = parseFromTokens(localeToUse, text, fmt); + if (invalid) { + return _DateTime.invalid(invalid); + } else { + return parseDataToDateTime(vals, parsedZone, opts, `format ${fmt}`, text, specificOffset); + } + } + /** + * @deprecated use fromFormat instead + */ + static fromString(text, fmt, opts = {}) { + return _DateTime.fromFormat(text, fmt, opts); + } + /** + * Create a DateTime from a SQL date, time, or datetime + * Defaults to en-US if no locale has been specified, regardless of the system's locale + * @param {string} text - the string to parse + * @param {Object} opts - options to affect the creation + * @param {string|Zone} [opts.zone='local'] - use this zone if no offset is specified in the input string itself. Will also convert the DateTime to this zone + * @param {boolean} [opts.setZone=false] - override the zone with a zone specified in the string itself, if it specifies one + * @param {string} [opts.locale='en-US'] - a locale string to use when parsing. Will also set the DateTime to this locale + * @param {string} opts.numberingSystem - the numbering system to use when parsing. Will also set the resulting DateTime to this numbering system + * @param {string} opts.weekSettings - the week settings to set on the resulting DateTime instance + * @param {string} opts.outputCalendar - the output calendar to set on the resulting DateTime instance + * @example DateTime.fromSQL('2017-05-15') + * @example DateTime.fromSQL('2017-05-15 09:12:34') + * @example DateTime.fromSQL('2017-05-15 09:12:34.342') + * @example DateTime.fromSQL('2017-05-15 09:12:34.342+06:00') + * @example DateTime.fromSQL('2017-05-15 09:12:34.342 America/Los_Angeles') + * @example DateTime.fromSQL('2017-05-15 09:12:34.342 America/Los_Angeles', { setZone: true }) + * @example DateTime.fromSQL('2017-05-15 09:12:34.342', { zone: 'America/Los_Angeles' }) + * @example DateTime.fromSQL('09:12:34.342') + * @return {DateTime} + */ + static fromSQL(text, opts = {}) { + const [vals, parsedZone] = parseSQL(text); + return parseDataToDateTime(vals, parsedZone, opts, "SQL", text); + } + /** + * Create an invalid DateTime. + * @param {string} reason - simple string of why this DateTime is invalid. Should not contain parameters or anything else data-dependent. + * @param {string} [explanation=null] - longer explanation, may include parameters and other useful debugging information + * @return {DateTime} + */ + static invalid(reason, explanation = null) { + if (!reason) { + throw new InvalidArgumentError("need to specify a reason the DateTime is invalid"); + } + const invalid = reason instanceof Invalid ? reason : new Invalid(reason, explanation); + if (Settings.throwOnInvalid) { + throw new InvalidDateTimeError(invalid); + } else { + return new _DateTime({ + invalid + }); + } + } + /** + * Check if an object is an instance of DateTime. Works across context boundaries + * @param {object} o + * @return {boolean} + */ + static isDateTime(o2) { + return o2 && o2.isLuxonDateTime || false; + } + /** + * Produce the format string for a set of options + * @param formatOpts + * @param localeOpts + * @returns {string} + */ + static parseFormatForOpts(formatOpts, localeOpts = {}) { + const tokenList = formatOptsToTokens(formatOpts, Locale.fromObject(localeOpts)); + return !tokenList ? null : tokenList.map((t) => t ? t.val : null).join(""); + } + /** + * Produce the the fully expanded format token for the locale + * Does NOT quote characters, so quoted tokens will not round trip correctly + * @param fmt + * @param localeOpts + * @returns {string} + */ + static expandFormat(fmt, localeOpts = {}) { + const expanded = expandMacroTokens(Formatter.parseFormat(fmt), Locale.fromObject(localeOpts)); + return expanded.map((t) => t.val).join(""); + } + static resetCache() { + zoneOffsetTs = void 0; + zoneOffsetGuessCache.clear(); + } + // INFO + /** + * Get the value of unit. + * @param {string} unit - a unit such as 'minute' or 'day' + * @example DateTime.local(2017, 7, 4).get('month'); //=> 7 + * @example DateTime.local(2017, 7, 4).get('day'); //=> 4 + * @return {number} + */ + get(unit) { + return this[unit]; + } + /** + * Returns whether the DateTime is valid. Invalid DateTimes occur when: + * * The DateTime was created from invalid calendar information, such as the 13th month or February 30 + * * The DateTime was created by an operation on another invalid date + * @type {boolean} + */ + get isValid() { + return this.invalid === null; + } + /** + * Returns an error code if this DateTime is invalid, or null if the DateTime is valid + * @type {string} + */ + get invalidReason() { + return this.invalid ? this.invalid.reason : null; + } + /** + * Returns an explanation of why this DateTime became invalid, or null if the DateTime is valid + * @type {string} + */ + get invalidExplanation() { + return this.invalid ? this.invalid.explanation : null; + } + /** + * Get the locale of a DateTime, such 'en-GB'. The locale is used when formatting the DateTime + * + * @type {string} + */ + get locale() { + return this.isValid ? this.loc.locale : null; + } + /** + * Get the numbering system of a DateTime, such 'beng'. The numbering system is used when formatting the DateTime + * + * @type {string} + */ + get numberingSystem() { + return this.isValid ? this.loc.numberingSystem : null; + } + /** + * Get the output calendar of a DateTime, such 'islamic'. The output calendar is used when formatting the DateTime + * + * @type {string} + */ + get outputCalendar() { + return this.isValid ? this.loc.outputCalendar : null; + } + /** + * Get the time zone associated with this DateTime. + * @type {Zone} + */ + get zone() { + return this._zone; + } + /** + * Get the name of the time zone. + * @type {string} + */ + get zoneName() { + return this.isValid ? this.zone.name : null; + } + /** + * Get the year + * @example DateTime.local(2017, 5, 25).year //=> 2017 + * @type {number} + */ + get year() { + return this.isValid ? this.c.year : NaN; + } + /** + * Get the quarter + * @example DateTime.local(2017, 5, 25).quarter //=> 2 + * @type {number} + */ + get quarter() { + return this.isValid ? Math.ceil(this.c.month / 3) : NaN; + } + /** + * Get the month (1-12). + * @example DateTime.local(2017, 5, 25).month //=> 5 + * @type {number} + */ + get month() { + return this.isValid ? this.c.month : NaN; + } + /** + * Get the day of the month (1-30ish). + * @example DateTime.local(2017, 5, 25).day //=> 25 + * @type {number} + */ + get day() { + return this.isValid ? this.c.day : NaN; + } + /** + * Get the hour of the day (0-23). + * @example DateTime.local(2017, 5, 25, 9).hour //=> 9 + * @type {number} + */ + get hour() { + return this.isValid ? this.c.hour : NaN; + } + /** + * Get the minute of the hour (0-59). + * @example DateTime.local(2017, 5, 25, 9, 30).minute //=> 30 + * @type {number} + */ + get minute() { + return this.isValid ? this.c.minute : NaN; + } + /** + * Get the second of the minute (0-59). + * @example DateTime.local(2017, 5, 25, 9, 30, 52).second //=> 52 + * @type {number} + */ + get second() { + return this.isValid ? this.c.second : NaN; + } + /** + * Get the millisecond of the second (0-999). + * @example DateTime.local(2017, 5, 25, 9, 30, 52, 654).millisecond //=> 654 + * @type {number} + */ + get millisecond() { + return this.isValid ? this.c.millisecond : NaN; + } + /** + * Get the week year + * @see https://en.wikipedia.org/wiki/ISO_week_date + * @example DateTime.local(2014, 12, 31).weekYear //=> 2015 + * @type {number} + */ + get weekYear() { + return this.isValid ? possiblyCachedWeekData(this).weekYear : NaN; + } + /** + * Get the week number of the week year (1-52ish). + * @see https://en.wikipedia.org/wiki/ISO_week_date + * @example DateTime.local(2017, 5, 25).weekNumber //=> 21 + * @type {number} + */ + get weekNumber() { + return this.isValid ? possiblyCachedWeekData(this).weekNumber : NaN; + } + /** + * Get the day of the week. + * 1 is Monday and 7 is Sunday + * @see https://en.wikipedia.org/wiki/ISO_week_date + * @example DateTime.local(2014, 11, 31).weekday //=> 4 + * @type {number} + */ + get weekday() { + return this.isValid ? possiblyCachedWeekData(this).weekday : NaN; + } + /** + * Returns true if this date is on a weekend according to the locale, false otherwise + * @returns {boolean} + */ + get isWeekend() { + return this.isValid && this.loc.getWeekendDays().includes(this.weekday); + } + /** + * Get the day of the week according to the locale. + * 1 is the first day of the week and 7 is the last day of the week. + * If the locale assigns Sunday as the first day of the week, then a date which is a Sunday will return 1, + * @returns {number} + */ + get localWeekday() { + return this.isValid ? possiblyCachedLocalWeekData(this).weekday : NaN; + } + /** + * Get the week number of the week year according to the locale. Different locales assign week numbers differently, + * because the week can start on different days of the week (see localWeekday) and because a different number of days + * is required for a week to count as the first week of a year. + * @returns {number} + */ + get localWeekNumber() { + return this.isValid ? possiblyCachedLocalWeekData(this).weekNumber : NaN; + } + /** + * Get the week year according to the locale. Different locales assign week numbers (and therefor week years) + * differently, see localWeekNumber. + * @returns {number} + */ + get localWeekYear() { + return this.isValid ? possiblyCachedLocalWeekData(this).weekYear : NaN; + } + /** + * Get the ordinal (meaning the day of the year) + * @example DateTime.local(2017, 5, 25).ordinal //=> 145 + * @type {number|DateTime} + */ + get ordinal() { + return this.isValid ? gregorianToOrdinal(this.c).ordinal : NaN; + } + /** + * Get the human readable short month name, such as 'Oct'. + * Defaults to the system's locale if no locale has been specified + * @example DateTime.local(2017, 10, 30).monthShort //=> Oct + * @type {string} + */ + get monthShort() { + return this.isValid ? Info.months("short", { + locObj: this.loc + })[this.month - 1] : null; + } + /** + * Get the human readable long month name, such as 'October'. + * Defaults to the system's locale if no locale has been specified + * @example DateTime.local(2017, 10, 30).monthLong //=> October + * @type {string} + */ + get monthLong() { + return this.isValid ? Info.months("long", { + locObj: this.loc + })[this.month - 1] : null; + } + /** + * Get the human readable short weekday, such as 'Mon'. + * Defaults to the system's locale if no locale has been specified + * @example DateTime.local(2017, 10, 30).weekdayShort //=> Mon + * @type {string} + */ + get weekdayShort() { + return this.isValid ? Info.weekdays("short", { + locObj: this.loc + })[this.weekday - 1] : null; + } + /** + * Get the human readable long weekday, such as 'Monday'. + * Defaults to the system's locale if no locale has been specified + * @example DateTime.local(2017, 10, 30).weekdayLong //=> Monday + * @type {string} + */ + get weekdayLong() { + return this.isValid ? Info.weekdays("long", { + locObj: this.loc + })[this.weekday - 1] : null; + } + /** + * Get the UTC offset of this DateTime in minutes + * @example DateTime.now().offset //=> -240 + * @example DateTime.utc().offset //=> 0 + * @type {number} + */ + get offset() { + return this.isValid ? +this.o : NaN; + } + /** + * Get the short human name for the zone's current offset, for example "EST" or "EDT". + * Defaults to the system's locale if no locale has been specified + * @type {string} + */ + get offsetNameShort() { + if (this.isValid) { + return this.zone.offsetName(this.ts, { + format: "short", + locale: this.locale + }); + } else { + return null; + } + } + /** + * Get the long human name for the zone's current offset, for example "Eastern Standard Time" or "Eastern Daylight Time". + * Defaults to the system's locale if no locale has been specified + * @type {string} + */ + get offsetNameLong() { + if (this.isValid) { + return this.zone.offsetName(this.ts, { + format: "long", + locale: this.locale + }); + } else { + return null; + } + } + /** + * Get whether this zone's offset ever changes, as in a DST. + * @type {boolean} + */ + get isOffsetFixed() { + return this.isValid ? this.zone.isUniversal : null; + } + /** + * Get whether the DateTime is in a DST. + * @type {boolean} + */ + get isInDST() { + if (this.isOffsetFixed) { + return false; + } else { + return this.offset > this.set({ + month: 1, + day: 1 + }).offset || this.offset > this.set({ + month: 5 + }).offset; + } + } + /** + * Get those DateTimes which have the same local time as this DateTime, but a different offset from UTC + * in this DateTime's zone. During DST changes local time can be ambiguous, for example + * `2023-10-29T02:30:00` in `Europe/Berlin` can have offset `+01:00` or `+02:00`. + * This method will return both possible DateTimes if this DateTime's local time is ambiguous. + * @returns {DateTime[]} + */ + getPossibleOffsets() { + if (!this.isValid || this.isOffsetFixed) { + return [this]; + } + const dayMs = 864e5; + const minuteMs = 6e4; + const localTS = objToLocalTS(this.c); + const oEarlier = this.zone.offset(localTS - dayMs); + const oLater = this.zone.offset(localTS + dayMs); + const o1 = this.zone.offset(localTS - oEarlier * minuteMs); + const o2 = this.zone.offset(localTS - oLater * minuteMs); + if (o1 === o2) { + return [this]; + } + const ts1 = localTS - o1 * minuteMs; + const ts2 = localTS - o2 * minuteMs; + const c1 = tsToObj(ts1, o1); + const c2 = tsToObj(ts2, o2); + if (c1.hour === c2.hour && c1.minute === c2.minute && c1.second === c2.second && c1.millisecond === c2.millisecond) { + return [clone(this, { + ts: ts1 + }), clone(this, { + ts: ts2 + })]; + } + return [this]; + } + /** + * Returns true if this DateTime is in a leap year, false otherwise + * @example DateTime.local(2016).isInLeapYear //=> true + * @example DateTime.local(2013).isInLeapYear //=> false + * @type {boolean} + */ + get isInLeapYear() { + return isLeapYear(this.year); + } + /** + * Returns the number of days in this DateTime's month + * @example DateTime.local(2016, 2).daysInMonth //=> 29 + * @example DateTime.local(2016, 3).daysInMonth //=> 31 + * @type {number} + */ + get daysInMonth() { + return daysInMonth(this.year, this.month); + } + /** + * Returns the number of days in this DateTime's year + * @example DateTime.local(2016).daysInYear //=> 366 + * @example DateTime.local(2013).daysInYear //=> 365 + * @type {number} + */ + get daysInYear() { + return this.isValid ? daysInYear(this.year) : NaN; + } + /** + * Returns the number of weeks in this DateTime's year + * @see https://en.wikipedia.org/wiki/ISO_week_date + * @example DateTime.local(2004).weeksInWeekYear //=> 53 + * @example DateTime.local(2013).weeksInWeekYear //=> 52 + * @type {number} + */ + get weeksInWeekYear() { + return this.isValid ? weeksInWeekYear(this.weekYear) : NaN; + } + /** + * Returns the number of weeks in this DateTime's local week year + * @example DateTime.local(2020, 6, {locale: 'en-US'}).weeksInLocalWeekYear //=> 52 + * @example DateTime.local(2020, 6, {locale: 'de-DE'}).weeksInLocalWeekYear //=> 53 + * @type {number} + */ + get weeksInLocalWeekYear() { + return this.isValid ? weeksInWeekYear(this.localWeekYear, this.loc.getMinDaysInFirstWeek(), this.loc.getStartOfWeek()) : NaN; + } + /** + * Returns the resolved Intl options for this DateTime. + * This is useful in understanding the behavior of formatting methods + * @param {Object} opts - the same options as toLocaleString + * @return {Object} + */ + resolvedLocaleOptions(opts = {}) { + const { + locale, + numberingSystem, + calendar + } = Formatter.create(this.loc.clone(opts), opts).resolvedOptions(this); + return { + locale, + numberingSystem, + outputCalendar: calendar + }; + } + // TRANSFORM + /** + * "Set" the DateTime's zone to UTC. Returns a newly-constructed DateTime. + * + * Equivalent to {@link DateTime#setZone}('utc') + * @param {number} [offset=0] - optionally, an offset from UTC in minutes + * @param {Object} [opts={}] - options to pass to `setZone()` + * @return {DateTime} + */ + toUTC(offset2 = 0, opts = {}) { + return this.setZone(FixedOffsetZone.instance(offset2), opts); + } + /** + * "Set" the DateTime's zone to the host's local zone. Returns a newly-constructed DateTime. + * + * Equivalent to `setZone('local')` + * @return {DateTime} + */ + toLocal() { + return this.setZone(Settings.defaultZone); + } + /** + * "Set" the DateTime's zone to specified zone. Returns a newly-constructed DateTime. + * + * By default, the setter keeps the underlying time the same (as in, the same timestamp), but the new instance will report different local times and consider DSTs when making computations, as with {@link DateTime#plus}. You may wish to use {@link DateTime#toLocal} and {@link DateTime#toUTC} which provide simple convenience wrappers for commonly used zones. + * @param {string|Zone} [zone='local'] - a zone identifier. As a string, that can be any IANA zone supported by the host environment, or a fixed-offset name of the form 'UTC+3', or the strings 'local' or 'utc'. You may also supply an instance of a {@link DateTime#Zone} class. + * @param {Object} opts - options + * @param {boolean} [opts.keepLocalTime=false] - If true, adjust the underlying time so that the local time stays the same, but in the target zone. You should rarely need this. + * @return {DateTime} + */ + setZone(zone, { + keepLocalTime = false, + keepCalendarTime = false + } = {}) { + zone = normalizeZone(zone, Settings.defaultZone); + if (zone.equals(this.zone)) { + return this; + } else if (!zone.isValid) { + return _DateTime.invalid(unsupportedZone(zone)); + } else { + let newTS = this.ts; + if (keepLocalTime || keepCalendarTime) { + const offsetGuess = zone.offset(this.ts); + const asObj = this.toObject(); + [newTS] = objToTS(asObj, offsetGuess, zone); + } + return clone(this, { + ts: newTS, + zone + }); + } + } + /** + * "Set" the locale, numberingSystem, or outputCalendar. Returns a newly-constructed DateTime. + * @param {Object} properties - the properties to set + * @example DateTime.local(2017, 5, 25).reconfigure({ locale: 'en-GB' }) + * @return {DateTime} + */ + reconfigure({ + locale, + numberingSystem, + outputCalendar + } = {}) { + const loc = this.loc.clone({ + locale, + numberingSystem, + outputCalendar + }); + return clone(this, { + loc + }); + } + /** + * "Set" the locale. Returns a newly-constructed DateTime. + * Just a convenient alias for reconfigure({ locale }) + * @example DateTime.local(2017, 5, 25).setLocale('en-GB') + * @return {DateTime} + */ + setLocale(locale) { + return this.reconfigure({ + locale + }); + } + /** + * "Set" the values of specified units. Returns a newly-constructed DateTime. + * You can only set units with this method; for "setting" metadata, see {@link DateTime#reconfigure} and {@link DateTime#setZone}. + * + * This method also supports setting locale-based week units, i.e. `localWeekday`, `localWeekNumber` and `localWeekYear`. + * They cannot be mixed with ISO-week units like `weekday`. + * @param {Object} values - a mapping of units to numbers + * @example dt.set({ year: 2017 }) + * @example dt.set({ hour: 8, minute: 30 }) + * @example dt.set({ weekday: 5 }) + * @example dt.set({ year: 2005, ordinal: 234 }) + * @return {DateTime} + */ + set(values) { + if (!this.isValid) return this; + const normalized = normalizeObject(values, normalizeUnitWithLocalWeeks); + const { + minDaysInFirstWeek, + startOfWeek + } = usesLocalWeekValues(normalized, this.loc); + const settingWeekStuff = !isUndefined(normalized.weekYear) || !isUndefined(normalized.weekNumber) || !isUndefined(normalized.weekday), containsOrdinal = !isUndefined(normalized.ordinal), containsGregorYear = !isUndefined(normalized.year), containsGregorMD = !isUndefined(normalized.month) || !isUndefined(normalized.day), containsGregor = containsGregorYear || containsGregorMD, definiteWeekDef = normalized.weekYear || normalized.weekNumber; + if ((containsGregor || containsOrdinal) && definiteWeekDef) { + throw new ConflictingSpecificationError("Can't mix weekYear/weekNumber units with year/month/day or ordinals"); + } + if (containsGregorMD && containsOrdinal) { + throw new ConflictingSpecificationError("Can't mix ordinal dates with month/day"); + } + let mixed; + if (settingWeekStuff) { + mixed = weekToGregorian({ + ...gregorianToWeek(this.c, minDaysInFirstWeek, startOfWeek), + ...normalized + }, minDaysInFirstWeek, startOfWeek); + } else if (!isUndefined(normalized.ordinal)) { + mixed = ordinalToGregorian({ + ...gregorianToOrdinal(this.c), + ...normalized + }); + } else { + mixed = { + ...this.toObject(), + ...normalized + }; + if (isUndefined(normalized.day)) { + mixed.day = Math.min(daysInMonth(mixed.year, mixed.month), mixed.day); + } + } + const [ts2, o2] = objToTS(mixed, this.o, this.zone); + return clone(this, { + ts: ts2, + o: o2 + }); + } + /** + * Add a period of time to this DateTime and return the resulting DateTime + * + * Adding hours, minutes, seconds, or milliseconds increases the timestamp by the right number of milliseconds. Adding days, months, or years shifts the calendar, accounting for DSTs and leap years along the way. Thus, `dt.plus({ hours: 24 })` may result in a different time than `dt.plus({ days: 1 })` if there's a DST shift in between. + * @param {Duration|Object|number} duration - The amount to add. Either a Luxon Duration, a number of milliseconds, the object argument to Duration.fromObject() + * @example DateTime.now().plus(123) //~> in 123 milliseconds + * @example DateTime.now().plus({ minutes: 15 }) //~> in 15 minutes + * @example DateTime.now().plus({ days: 1 }) //~> this time tomorrow + * @example DateTime.now().plus({ days: -1 }) //~> this time yesterday + * @example DateTime.now().plus({ hours: 3, minutes: 13 }) //~> in 3 hr, 13 min + * @example DateTime.now().plus(Duration.fromObject({ hours: 3, minutes: 13 })) //~> in 3 hr, 13 min + * @return {DateTime} + */ + plus(duration) { + if (!this.isValid) return this; + const dur = Duration.fromDurationLike(duration); + return clone(this, adjustTime(this, dur)); + } + /** + * Subtract a period of time to this DateTime and return the resulting DateTime + * See {@link DateTime#plus} + * @param {Duration|Object|number} duration - The amount to subtract. Either a Luxon Duration, a number of milliseconds, the object argument to Duration.fromObject() + @return {DateTime} + */ + minus(duration) { + if (!this.isValid) return this; + const dur = Duration.fromDurationLike(duration).negate(); + return clone(this, adjustTime(this, dur)); + } + /** + * "Set" this DateTime to the beginning of a unit of time. + * @param {string} unit - The unit to go to the beginning of. Can be 'year', 'quarter', 'month', 'week', 'day', 'hour', 'minute', 'second', or 'millisecond'. + * @param {Object} opts - options + * @param {boolean} [opts.useLocaleWeeks=false] - If true, use weeks based on the locale, i.e. use the locale-dependent start of the week + * @example DateTime.local(2014, 3, 3).startOf('month').toISODate(); //=> '2014-03-01' + * @example DateTime.local(2014, 3, 3).startOf('year').toISODate(); //=> '2014-01-01' + * @example DateTime.local(2014, 3, 3).startOf('week').toISODate(); //=> '2014-03-03', weeks always start on Mondays + * @example DateTime.local(2014, 3, 3, 5, 30).startOf('day').toISOTime(); //=> '00:00.000-05:00' + * @example DateTime.local(2014, 3, 3, 5, 30).startOf('hour').toISOTime(); //=> '05:00:00.000-05:00' + * @return {DateTime} + */ + startOf(unit, { + useLocaleWeeks = false + } = {}) { + if (!this.isValid) return this; + const o2 = {}, normalizedUnit = Duration.normalizeUnit(unit); + switch (normalizedUnit) { + case "years": + o2.month = 1; + // falls through + case "quarters": + case "months": + o2.day = 1; + // falls through + case "weeks": + case "days": + o2.hour = 0; + // falls through + case "hours": + o2.minute = 0; + // falls through + case "minutes": + o2.second = 0; + // falls through + case "seconds": + o2.millisecond = 0; + break; + } + if (normalizedUnit === "weeks") { + if (useLocaleWeeks) { + const startOfWeek = this.loc.getStartOfWeek(); + const { + weekday + } = this; + if (weekday < startOfWeek) { + o2.weekNumber = this.weekNumber - 1; + } + o2.weekday = startOfWeek; + } else { + o2.weekday = 1; + } + } + if (normalizedUnit === "quarters") { + const q2 = Math.ceil(this.month / 3); + o2.month = (q2 - 1) * 3 + 1; + } + return this.set(o2); + } + /** + * "Set" this DateTime to the end (meaning the last millisecond) of a unit of time + * @param {string} unit - The unit to go to the end of. Can be 'year', 'quarter', 'month', 'week', 'day', 'hour', 'minute', 'second', or 'millisecond'. + * @param {Object} opts - options + * @param {boolean} [opts.useLocaleWeeks=false] - If true, use weeks based on the locale, i.e. use the locale-dependent start of the week + * @example DateTime.local(2014, 3, 3).endOf('month').toISO(); //=> '2014-03-31T23:59:59.999-05:00' + * @example DateTime.local(2014, 3, 3).endOf('year').toISO(); //=> '2014-12-31T23:59:59.999-05:00' + * @example DateTime.local(2014, 3, 3).endOf('week').toISO(); // => '2014-03-09T23:59:59.999-05:00', weeks start on Mondays + * @example DateTime.local(2014, 3, 3, 5, 30).endOf('day').toISO(); //=> '2014-03-03T23:59:59.999-05:00' + * @example DateTime.local(2014, 3, 3, 5, 30).endOf('hour').toISO(); //=> '2014-03-03T05:59:59.999-05:00' + * @return {DateTime} + */ + endOf(unit, opts) { + return this.isValid ? this.plus({ + [unit]: 1 + }).startOf(unit, opts).minus(1) : this; + } + // OUTPUT + /** + * Returns a string representation of this DateTime formatted according to the specified format string. + * **You may not want this.** See {@link DateTime#toLocaleString} for a more flexible formatting tool. For a table of tokens and their interpretations, see [here](https://moment.github.io/luxon/#/formatting?id=table-of-tokens). + * Defaults to en-US if no locale has been specified, regardless of the system's locale. + * @param {string} fmt - the format string + * @param {Object} opts - opts to override the configuration options on this DateTime + * @example DateTime.now().toFormat('yyyy LLL dd') //=> '2017 Apr 22' + * @example DateTime.now().setLocale('fr').toFormat('yyyy LLL dd') //=> '2017 avr. 22' + * @example DateTime.now().toFormat('yyyy LLL dd', { locale: "fr" }) //=> '2017 avr. 22' + * @example DateTime.now().toFormat("HH 'hours and' mm 'minutes'") //=> '20 hours and 55 minutes' + * @return {string} + */ + toFormat(fmt, opts = {}) { + return this.isValid ? Formatter.create(this.loc.redefaultToEN(opts)).formatDateTimeFromString(this, fmt) : INVALID; + } + /** + * Returns a localized string representing this date. Accepts the same options as the Intl.DateTimeFormat constructor and any presets defined by Luxon, such as `DateTime.DATE_FULL` or `DateTime.TIME_SIMPLE`. + * The exact behavior of this method is browser-specific, but in general it will return an appropriate representation + * of the DateTime in the assigned locale. + * Defaults to the system's locale if no locale has been specified + * @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DateTimeFormat + * @param formatOpts {Object} - Intl.DateTimeFormat constructor options and configuration options + * @param {Object} opts - opts to override the configuration options on this DateTime + * @example DateTime.now().toLocaleString(); //=> 4/20/2017 + * @example DateTime.now().setLocale('en-gb').toLocaleString(); //=> '20/04/2017' + * @example DateTime.now().toLocaleString(DateTime.DATE_FULL); //=> 'April 20, 2017' + * @example DateTime.now().toLocaleString(DateTime.DATE_FULL, { locale: 'fr' }); //=> '28 août 2022' + * @example DateTime.now().toLocaleString(DateTime.TIME_SIMPLE); //=> '11:32 AM' + * @example DateTime.now().toLocaleString(DateTime.DATETIME_SHORT); //=> '4/20/2017, 11:32 AM' + * @example DateTime.now().toLocaleString({ weekday: 'long', month: 'long', day: '2-digit' }); //=> 'Thursday, April 20' + * @example DateTime.now().toLocaleString({ weekday: 'short', month: 'short', day: '2-digit', hour: '2-digit', minute: '2-digit' }); //=> 'Thu, Apr 20, 11:27 AM' + * @example DateTime.now().toLocaleString({ hour: '2-digit', minute: '2-digit', hourCycle: 'h23' }); //=> '11:32' + * @return {string} + */ + toLocaleString(formatOpts = DATE_SHORT, opts = {}) { + return this.isValid ? Formatter.create(this.loc.clone(opts), formatOpts).formatDateTime(this) : INVALID; + } + /** + * Returns an array of format "parts", meaning individual tokens along with metadata. This is allows callers to post-process individual sections of the formatted output. + * Defaults to the system's locale if no locale has been specified + * @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DateTimeFormat/formatToParts + * @param opts {Object} - Intl.DateTimeFormat constructor options, same as `toLocaleString`. + * @example DateTime.now().toLocaleParts(); //=> [ + * //=> { type: 'day', value: '25' }, + * //=> { type: 'literal', value: '/' }, + * //=> { type: 'month', value: '05' }, + * //=> { type: 'literal', value: '/' }, + * //=> { type: 'year', value: '1982' } + * //=> ] + */ + toLocaleParts(opts = {}) { + return this.isValid ? Formatter.create(this.loc.clone(opts), opts).formatDateTimeParts(this) : []; + } + /** + * Returns an ISO 8601-compliant string representation of this DateTime + * @param {Object} opts - options + * @param {boolean} [opts.suppressMilliseconds=false] - exclude milliseconds from the format if they're 0 + * @param {boolean} [opts.suppressSeconds=false] - exclude seconds from the format if they're 0 + * @param {boolean} [opts.includeOffset=true] - include the offset, such as 'Z' or '-04:00' + * @param {boolean} [opts.extendedZone=false] - add the time zone format extension + * @param {string} [opts.format='extended'] - choose between the basic and extended format + * @param {string} [opts.precision='milliseconds'] - truncate output to desired presicion: 'years', 'months', 'days', 'hours', 'minutes', 'seconds' or 'milliseconds'. When precision and suppressSeconds or suppressMilliseconds are used together, precision sets the maximum unit shown in the output, however seconds or milliseconds will still be suppressed if they are 0. + * @example DateTime.utc(1983, 5, 25).toISO() //=> '1982-05-25T00:00:00.000Z' + * @example DateTime.now().toISO() //=> '2017-04-22T20:47:05.335-04:00' + * @example DateTime.now().toISO({ includeOffset: false }) //=> '2017-04-22T20:47:05.335' + * @example DateTime.now().toISO({ format: 'basic' }) //=> '20170422T204705.335-0400' + * @example DateTime.now().toISO({ precision: 'day' }) //=> '2017-04-22Z' + * @example DateTime.now().toISO({ precision: 'minute' }) //=> '2017-04-22T20:47Z' + * @return {string|null} + */ + toISO({ + format = "extended", + suppressSeconds = false, + suppressMilliseconds = false, + includeOffset = true, + extendedZone = false, + precision = "milliseconds" + } = {}) { + if (!this.isValid) { + return null; + } + precision = normalizeUnit(precision); + const ext = format === "extended"; + let c = toISODate(this, ext, precision); + if (orderedUnits.indexOf(precision) >= 3) c += "T"; + c += toISOTime(this, ext, suppressSeconds, suppressMilliseconds, includeOffset, extendedZone, precision); + return c; + } + /** + * Returns an ISO 8601-compliant string representation of this DateTime's date component + * @param {Object} opts - options + * @param {string} [opts.format='extended'] - choose between the basic and extended format + * @param {string} [opts.precision='day'] - truncate output to desired precision: 'years', 'months', or 'days'. + * @example DateTime.utc(1982, 5, 25).toISODate() //=> '1982-05-25' + * @example DateTime.utc(1982, 5, 25).toISODate({ format: 'basic' }) //=> '19820525' + * @example DateTime.utc(1982, 5, 25).toISODate({ precision: 'month' }) //=> '1982-05' + * @return {string|null} + */ + toISODate({ + format = "extended", + precision = "day" + } = {}) { + if (!this.isValid) { + return null; + } + return toISODate(this, format === "extended", normalizeUnit(precision)); + } + /** + * Returns an ISO 8601-compliant string representation of this DateTime's week date + * @example DateTime.utc(1982, 5, 25).toISOWeekDate() //=> '1982-W21-2' + * @return {string} + */ + toISOWeekDate() { + return toTechFormat(this, "kkkk-'W'WW-c"); + } + /** + * Returns an ISO 8601-compliant string representation of this DateTime's time component + * @param {Object} opts - options + * @param {boolean} [opts.suppressMilliseconds=false] - exclude milliseconds from the format if they're 0 + * @param {boolean} [opts.suppressSeconds=false] - exclude seconds from the format if they're 0 + * @param {boolean} [opts.includeOffset=true] - include the offset, such as 'Z' or '-04:00' + * @param {boolean} [opts.extendedZone=true] - add the time zone format extension + * @param {boolean} [opts.includePrefix=false] - include the `T` prefix + * @param {string} [opts.format='extended'] - choose between the basic and extended format + * @param {string} [opts.precision='milliseconds'] - truncate output to desired presicion: 'hours', 'minutes', 'seconds' or 'milliseconds'. When precision and suppressSeconds or suppressMilliseconds are used together, precision sets the maximum unit shown in the output, however seconds or milliseconds will still be suppressed if they are 0. + * @example DateTime.utc().set({ hour: 7, minute: 34 }).toISOTime() //=> '07:34:19.361Z' + * @example DateTime.utc().set({ hour: 7, minute: 34, seconds: 0, milliseconds: 0 }).toISOTime({ suppressSeconds: true }) //=> '07:34Z' + * @example DateTime.utc().set({ hour: 7, minute: 34 }).toISOTime({ format: 'basic' }) //=> '073419.361Z' + * @example DateTime.utc().set({ hour: 7, minute: 34 }).toISOTime({ includePrefix: true }) //=> 'T07:34:19.361Z' + * @example DateTime.utc().set({ hour: 7, minute: 34, second: 56 }).toISOTime({ precision: 'minute' }) //=> '07:34Z' + * @return {string} + */ + toISOTime({ + suppressMilliseconds = false, + suppressSeconds = false, + includeOffset = true, + includePrefix = false, + extendedZone = false, + format = "extended", + precision = "milliseconds" + } = {}) { + if (!this.isValid) { + return null; + } + precision = normalizeUnit(precision); + let c = includePrefix && orderedUnits.indexOf(precision) >= 3 ? "T" : ""; + return c + toISOTime(this, format === "extended", suppressSeconds, suppressMilliseconds, includeOffset, extendedZone, precision); + } + /** + * Returns an RFC 2822-compatible string representation of this DateTime + * @example DateTime.utc(2014, 7, 13).toRFC2822() //=> 'Sun, 13 Jul 2014 00:00:00 +0000' + * @example DateTime.local(2014, 7, 13).toRFC2822() //=> 'Sun, 13 Jul 2014 00:00:00 -0400' + * @return {string} + */ + toRFC2822() { + return toTechFormat(this, "EEE, dd LLL yyyy HH:mm:ss ZZZ", false); + } + /** + * Returns a string representation of this DateTime appropriate for use in HTTP headers. The output is always expressed in GMT. + * Specifically, the string conforms to RFC 1123. + * @see https://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.3.1 + * @example DateTime.utc(2014, 7, 13).toHTTP() //=> 'Sun, 13 Jul 2014 00:00:00 GMT' + * @example DateTime.utc(2014, 7, 13, 19).toHTTP() //=> 'Sun, 13 Jul 2014 19:00:00 GMT' + * @return {string} + */ + toHTTP() { + return toTechFormat(this.toUTC(), "EEE, dd LLL yyyy HH:mm:ss 'GMT'"); + } + /** + * Returns a string representation of this DateTime appropriate for use in SQL Date + * @example DateTime.utc(2014, 7, 13).toSQLDate() //=> '2014-07-13' + * @return {string|null} + */ + toSQLDate() { + if (!this.isValid) { + return null; + } + return toISODate(this, true); + } + /** + * Returns a string representation of this DateTime appropriate for use in SQL Time + * @param {Object} opts - options + * @param {boolean} [opts.includeZone=false] - include the zone, such as 'America/New_York'. Overrides includeOffset. + * @param {boolean} [opts.includeOffset=true] - include the offset, such as 'Z' or '-04:00' + * @param {boolean} [opts.includeOffsetSpace=true] - include the space between the time and the offset, such as '05:15:16.345 -04:00' + * @example DateTime.utc().toSQL() //=> '05:15:16.345' + * @example DateTime.now().toSQL() //=> '05:15:16.345 -04:00' + * @example DateTime.now().toSQL({ includeOffset: false }) //=> '05:15:16.345' + * @example DateTime.now().toSQL({ includeZone: false }) //=> '05:15:16.345 America/New_York' + * @return {string} + */ + toSQLTime({ + includeOffset = true, + includeZone = false, + includeOffsetSpace = true + } = {}) { + let fmt = "HH:mm:ss.SSS"; + if (includeZone || includeOffset) { + if (includeOffsetSpace) { + fmt += " "; + } + if (includeZone) { + fmt += "z"; + } else if (includeOffset) { + fmt += "ZZ"; + } + } + return toTechFormat(this, fmt, true); + } + /** + * Returns a string representation of this DateTime appropriate for use in SQL DateTime + * @param {Object} opts - options + * @param {boolean} [opts.includeZone=false] - include the zone, such as 'America/New_York'. Overrides includeOffset. + * @param {boolean} [opts.includeOffset=true] - include the offset, such as 'Z' or '-04:00' + * @param {boolean} [opts.includeOffsetSpace=true] - include the space between the time and the offset, such as '05:15:16.345 -04:00' + * @example DateTime.utc(2014, 7, 13).toSQL() //=> '2014-07-13 00:00:00.000 Z' + * @example DateTime.local(2014, 7, 13).toSQL() //=> '2014-07-13 00:00:00.000 -04:00' + * @example DateTime.local(2014, 7, 13).toSQL({ includeOffset: false }) //=> '2014-07-13 00:00:00.000' + * @example DateTime.local(2014, 7, 13).toSQL({ includeZone: true }) //=> '2014-07-13 00:00:00.000 America/New_York' + * @return {string} + */ + toSQL(opts = {}) { + if (!this.isValid) { + return null; + } + return `${this.toSQLDate()} ${this.toSQLTime(opts)}`; + } + /** + * Returns a string representation of this DateTime appropriate for debugging + * @return {string} + */ + toString() { + return this.isValid ? this.toISO() : INVALID; + } + /** + * Returns a string representation of this DateTime appropriate for the REPL. + * @return {string} + */ + [Symbol.for("nodejs.util.inspect.custom")]() { + if (this.isValid) { + return `DateTime { ts: ${this.toISO()}, zone: ${this.zone.name}, locale: ${this.locale} }`; + } else { + return `DateTime { Invalid, reason: ${this.invalidReason} }`; + } + } + /** + * Returns the epoch milliseconds of this DateTime. Alias of {@link DateTime#toMillis} + * @return {number} + */ + valueOf() { + return this.toMillis(); + } + /** + * Returns the epoch milliseconds of this DateTime. + * @return {number} + */ + toMillis() { + return this.isValid ? this.ts : NaN; + } + /** + * Returns the epoch seconds (including milliseconds in the fractional part) of this DateTime. + * @return {number} + */ + toSeconds() { + return this.isValid ? this.ts / 1e3 : NaN; + } + /** + * Returns the epoch seconds (as a whole number) of this DateTime. + * @return {number} + */ + toUnixInteger() { + return this.isValid ? Math.floor(this.ts / 1e3) : NaN; + } + /** + * Returns an ISO 8601 representation of this DateTime appropriate for use in JSON. + * @return {string} + */ + toJSON() { + return this.toISO(); + } + /** + * Returns a BSON serializable equivalent to this DateTime. + * @return {Date} + */ + toBSON() { + return this.toJSDate(); + } + /** + * Returns a JavaScript object with this DateTime's year, month, day, and so on. + * @param opts - options for generating the object + * @param {boolean} [opts.includeConfig=false] - include configuration attributes in the output + * @example DateTime.now().toObject() //=> { year: 2017, month: 4, day: 22, hour: 20, minute: 49, second: 42, millisecond: 268 } + * @return {Object} + */ + toObject(opts = {}) { + if (!this.isValid) return {}; + const base = { + ...this.c + }; + if (opts.includeConfig) { + base.outputCalendar = this.outputCalendar; + base.numberingSystem = this.loc.numberingSystem; + base.locale = this.loc.locale; + } + return base; + } + /** + * Returns a JavaScript Date equivalent to this DateTime. + * @return {Date} + */ + toJSDate() { + return new Date(this.isValid ? this.ts : NaN); + } + // COMPARE + /** + * Return the difference between two DateTimes as a Duration. + * @param {DateTime} otherDateTime - the DateTime to compare this one to + * @param {string|string[]} [unit=['milliseconds']] - the unit or array of units (such as 'hours' or 'days') to include in the duration. + * @param {Object} opts - options that affect the creation of the Duration + * @param {string} [opts.conversionAccuracy='casual'] - the conversion system to use + * @example + * var i1 = DateTime.fromISO('1982-05-25T09:45'), + * i2 = DateTime.fromISO('1983-10-14T10:30'); + * i2.diff(i1).toObject() //=> { milliseconds: 43807500000 } + * i2.diff(i1, 'hours').toObject() //=> { hours: 12168.75 } + * i2.diff(i1, ['months', 'days']).toObject() //=> { months: 16, days: 19.03125 } + * i2.diff(i1, ['months', 'days', 'hours']).toObject() //=> { months: 16, days: 19, hours: 0.75 } + * @return {Duration} + */ + diff(otherDateTime, unit = "milliseconds", opts = {}) { + if (!this.isValid || !otherDateTime.isValid) { + return Duration.invalid("created by diffing an invalid DateTime"); + } + const durOpts = { + locale: this.locale, + numberingSystem: this.numberingSystem, + ...opts + }; + const units = maybeArray(unit).map(Duration.normalizeUnit), otherIsLater = otherDateTime.valueOf() > this.valueOf(), earlier = otherIsLater ? this : otherDateTime, later = otherIsLater ? otherDateTime : this, diffed = diff(earlier, later, units, durOpts); + return otherIsLater ? diffed.negate() : diffed; + } + /** + * Return the difference between this DateTime and right now. + * See {@link DateTime#diff} + * @param {string|string[]} [unit=['milliseconds']] - the unit or units units (such as 'hours' or 'days') to include in the duration + * @param {Object} opts - options that affect the creation of the Duration + * @param {string} [opts.conversionAccuracy='casual'] - the conversion system to use + * @return {Duration} + */ + diffNow(unit = "milliseconds", opts = {}) { + return this.diff(_DateTime.now(), unit, opts); + } + /** + * Return an Interval spanning between this DateTime and another DateTime + * @param {DateTime} otherDateTime - the other end point of the Interval + * @return {Interval|DateTime} + */ + until(otherDateTime) { + return this.isValid ? Interval.fromDateTimes(this, otherDateTime) : this; + } + /** + * Return whether this DateTime is in the same unit of time as another DateTime. + * Higher-order units must also be identical for this function to return `true`. + * Note that time zones are **ignored** in this comparison, which compares the **local** calendar time. Use {@link DateTime#setZone} to convert one of the dates if needed. + * @param {DateTime} otherDateTime - the other DateTime + * @param {string} unit - the unit of time to check sameness on + * @param {Object} opts - options + * @param {boolean} [opts.useLocaleWeeks=false] - If true, use weeks based on the locale, i.e. use the locale-dependent start of the week; only the locale of this DateTime is used + * @example DateTime.now().hasSame(otherDT, 'day'); //~> true if otherDT is in the same current calendar day + * @return {boolean} + */ + hasSame(otherDateTime, unit, opts) { + if (!this.isValid) return false; + const inputMs = otherDateTime.valueOf(); + const adjustedToZone = this.setZone(otherDateTime.zone, { + keepLocalTime: true + }); + return adjustedToZone.startOf(unit, opts) <= inputMs && inputMs <= adjustedToZone.endOf(unit, opts); + } + /** + * Equality check + * Two DateTimes are equal if and only if they represent the same millisecond, have the same zone and location, and are both valid. + * To compare just the millisecond values, use `+dt1 === +dt2`. + * @param {DateTime} other - the other DateTime + * @return {boolean} + */ + equals(other) { + return this.isValid && other.isValid && this.valueOf() === other.valueOf() && this.zone.equals(other.zone) && this.loc.equals(other.loc); + } + /** + * Returns a string representation of a this time relative to now, such as "in two days". Can only internationalize if your + * platform supports Intl.RelativeTimeFormat. Rounds towards zero by default. + * @param {Object} options - options that affect the output + * @param {DateTime} [options.base=DateTime.now()] - the DateTime to use as the basis to which this time is compared. Defaults to now. + * @param {string} [options.style="long"] - the style of units, must be "long", "short", or "narrow" + * @param {string|string[]} options.unit - use a specific unit or array of units; if omitted, or an array, the method will pick the best unit. Use an array or one of "years", "quarters", "months", "weeks", "days", "hours", "minutes", or "seconds" + * @param {boolean} [options.round=true] - whether to round the numbers in the output. + * @param {string} [options.rounding="trunc"] - rounding method to use when rounding the numbers in the output. Can be "trunc" (toward zero), "expand" (away from zero), "round", "floor", or "ceil". + * @param {number} [options.padding=0] - padding in milliseconds. This allows you to round up the result if it fits inside the threshold. Don't use in combination with {round: false} because the decimal output will include the padding. + * @param {string} options.locale - override the locale of this DateTime + * @param {string} options.numberingSystem - override the numberingSystem of this DateTime. The Intl system may choose not to honor this + * @example DateTime.now().plus({ days: 1 }).toRelative() //=> "in 1 day" + * @example DateTime.now().setLocale("es").toRelative({ days: 1 }) //=> "dentro de 1 día" + * @example DateTime.now().plus({ days: 1 }).toRelative({ locale: "fr" }) //=> "dans 23 heures" + * @example DateTime.now().minus({ days: 2 }).toRelative() //=> "2 days ago" + * @example DateTime.now().minus({ days: 2 }).toRelative({ unit: "hours" }) //=> "48 hours ago" + * @example DateTime.now().minus({ hours: 36 }).toRelative({ round: false }) //=> "1.5 days ago" + */ + toRelative(options = {}) { + if (!this.isValid) return null; + const base = options.base || _DateTime.fromObject({}, { + zone: this.zone + }), padding = options.padding ? this < base ? -options.padding : options.padding : 0; + let units = ["years", "months", "days", "hours", "minutes", "seconds"]; + let unit = options.unit; + if (Array.isArray(options.unit)) { + units = options.unit; + unit = void 0; + } + return diffRelative(base, this.plus(padding), { + ...options, + numeric: "always", + units, + unit + }); + } + /** + * Returns a string representation of this date relative to today, such as "yesterday" or "next month". + * Only internationalizes on platforms that supports Intl.RelativeTimeFormat. + * @param {Object} options - options that affect the output + * @param {DateTime} [options.base=DateTime.now()] - the DateTime to use as the basis to which this time is compared. Defaults to now. + * @param {string} options.locale - override the locale of this DateTime + * @param {string} options.unit - use a specific unit; if omitted, the method will pick the unit. Use one of "years", "quarters", "months", "weeks", or "days" + * @param {string} options.numberingSystem - override the numberingSystem of this DateTime. The Intl system may choose not to honor this + * @example DateTime.now().plus({ days: 1 }).toRelativeCalendar() //=> "tomorrow" + * @example DateTime.now().setLocale("es").plus({ days: 1 }).toRelative() //=> ""mañana" + * @example DateTime.now().plus({ days: 1 }).toRelativeCalendar({ locale: "fr" }) //=> "demain" + * @example DateTime.now().minus({ days: 2 }).toRelativeCalendar() //=> "2 days ago" + */ + toRelativeCalendar(options = {}) { + if (!this.isValid) return null; + return diffRelative(options.base || _DateTime.fromObject({}, { + zone: this.zone + }), this, { + ...options, + numeric: "auto", + units: ["years", "months", "days"], + calendary: true + }); + } + /** + * Return the min of several date times + * @param {...DateTime} dateTimes - the DateTimes from which to choose the minimum + * @return {DateTime} the min DateTime, or undefined if called with no argument + */ + static min(...dateTimes) { + if (!dateTimes.every(_DateTime.isDateTime)) { + throw new InvalidArgumentError("min requires all arguments be DateTimes"); + } + return bestBy(dateTimes, (i) => i.valueOf(), Math.min); + } + /** + * Return the max of several date times + * @param {...DateTime} dateTimes - the DateTimes from which to choose the maximum + * @return {DateTime} the max DateTime, or undefined if called with no argument + */ + static max(...dateTimes) { + if (!dateTimes.every(_DateTime.isDateTime)) { + throw new InvalidArgumentError("max requires all arguments be DateTimes"); + } + return bestBy(dateTimes, (i) => i.valueOf(), Math.max); + } + // MISC + /** + * Explain how a string would be parsed by fromFormat() + * @param {string} text - the string to parse + * @param {string} fmt - the format the string is expected to be in (see description) + * @param {Object} options - options taken by fromFormat() + * @return {Object} + */ + static fromFormatExplain(text, fmt, options = {}) { + const { + locale = null, + numberingSystem = null + } = options, localeToUse = Locale.fromOpts({ + locale, + numberingSystem, + defaultToEN: true + }); + return explainFromTokens(localeToUse, text, fmt); + } + /** + * @deprecated use fromFormatExplain instead + */ + static fromStringExplain(text, fmt, options = {}) { + return _DateTime.fromFormatExplain(text, fmt, options); + } + /** + * Build a parser for `fmt` using the given locale. This parser can be passed + * to {@link DateTime.fromFormatParser} to a parse a date in this format. This + * can be used to optimize cases where many dates need to be parsed in a + * specific format. + * + * @param {String} fmt - the format the string is expected to be in (see + * description) + * @param {Object} options - options used to set locale and numberingSystem + * for parser + * @returns {TokenParser} - opaque object to be used + */ + static buildFormatParser(fmt, options = {}) { + const { + locale = null, + numberingSystem = null + } = options, localeToUse = Locale.fromOpts({ + locale, + numberingSystem, + defaultToEN: true + }); + return new TokenParser(localeToUse, fmt); + } + /** + * Create a DateTime from an input string and format parser. + * + * The format parser must have been created with the same locale as this call. + * + * @param {String} text - the string to parse + * @param {TokenParser} formatParser - parser from {@link DateTime.buildFormatParser} + * @param {Object} opts - options taken by fromFormat() + * @returns {DateTime} + */ + static fromFormatParser(text, formatParser, opts = {}) { + if (isUndefined(text) || isUndefined(formatParser)) { + throw new InvalidArgumentError("fromFormatParser requires an input string and a format parser"); + } + const { + locale = null, + numberingSystem = null + } = opts, localeToUse = Locale.fromOpts({ + locale, + numberingSystem, + defaultToEN: true + }); + if (!localeToUse.equals(formatParser.locale)) { + throw new InvalidArgumentError(`fromFormatParser called with a locale of ${localeToUse}, but the format parser was created for ${formatParser.locale}`); + } + const { + result, + zone, + specificOffset, + invalidReason + } = formatParser.explainFromTokens(text); + if (invalidReason) { + return _DateTime.invalid(invalidReason); + } else { + return parseDataToDateTime(result, zone, opts, `format ${formatParser.format}`, text, specificOffset); + } + } + // FORMAT PRESETS + /** + * {@link DateTime#toLocaleString} format like 10/14/1983 + * @type {Object} + */ + static get DATE_SHORT() { + return DATE_SHORT; + } + /** + * {@link DateTime#toLocaleString} format like 'Oct 14, 1983' + * @type {Object} + */ + static get DATE_MED() { + return DATE_MED; + } + /** + * {@link DateTime#toLocaleString} format like 'Fri, Oct 14, 1983' + * @type {Object} + */ + static get DATE_MED_WITH_WEEKDAY() { + return DATE_MED_WITH_WEEKDAY; + } + /** + * {@link DateTime#toLocaleString} format like 'October 14, 1983' + * @type {Object} + */ + static get DATE_FULL() { + return DATE_FULL; + } + /** + * {@link DateTime#toLocaleString} format like 'Tuesday, October 14, 1983' + * @type {Object} + */ + static get DATE_HUGE() { + return DATE_HUGE; + } + /** + * {@link DateTime#toLocaleString} format like '09:30 AM'. Only 12-hour if the locale is. + * @type {Object} + */ + static get TIME_SIMPLE() { + return TIME_SIMPLE; + } + /** + * {@link DateTime#toLocaleString} format like '09:30:23 AM'. Only 12-hour if the locale is. + * @type {Object} + */ + static get TIME_WITH_SECONDS() { + return TIME_WITH_SECONDS; + } + /** + * {@link DateTime#toLocaleString} format like '09:30:23 AM EDT'. Only 12-hour if the locale is. + * @type {Object} + */ + static get TIME_WITH_SHORT_OFFSET() { + return TIME_WITH_SHORT_OFFSET; + } + /** + * {@link DateTime#toLocaleString} format like '09:30:23 AM Eastern Daylight Time'. Only 12-hour if the locale is. + * @type {Object} + */ + static get TIME_WITH_LONG_OFFSET() { + return TIME_WITH_LONG_OFFSET; + } + /** + * {@link DateTime#toLocaleString} format like '09:30', always 24-hour. + * @type {Object} + */ + static get TIME_24_SIMPLE() { + return TIME_24_SIMPLE; + } + /** + * {@link DateTime#toLocaleString} format like '09:30:23', always 24-hour. + * @type {Object} + */ + static get TIME_24_WITH_SECONDS() { + return TIME_24_WITH_SECONDS; + } + /** + * {@link DateTime#toLocaleString} format like '09:30:23 EDT', always 24-hour. + * @type {Object} + */ + static get TIME_24_WITH_SHORT_OFFSET() { + return TIME_24_WITH_SHORT_OFFSET; + } + /** + * {@link DateTime#toLocaleString} format like '09:30:23 Eastern Daylight Time', always 24-hour. + * @type {Object} + */ + static get TIME_24_WITH_LONG_OFFSET() { + return TIME_24_WITH_LONG_OFFSET; + } + /** + * {@link DateTime#toLocaleString} format like '10/14/1983, 9:30 AM'. Only 12-hour if the locale is. + * @type {Object} + */ + static get DATETIME_SHORT() { + return DATETIME_SHORT; + } + /** + * {@link DateTime#toLocaleString} format like '10/14/1983, 9:30:33 AM'. Only 12-hour if the locale is. + * @type {Object} + */ + static get DATETIME_SHORT_WITH_SECONDS() { + return DATETIME_SHORT_WITH_SECONDS; + } + /** + * {@link DateTime#toLocaleString} format like 'Oct 14, 1983, 9:30 AM'. Only 12-hour if the locale is. + * @type {Object} + */ + static get DATETIME_MED() { + return DATETIME_MED; + } + /** + * {@link DateTime#toLocaleString} format like 'Oct 14, 1983, 9:30:33 AM'. Only 12-hour if the locale is. + * @type {Object} + */ + static get DATETIME_MED_WITH_SECONDS() { + return DATETIME_MED_WITH_SECONDS; + } + /** + * {@link DateTime#toLocaleString} format like 'Fri, 14 Oct 1983, 9:30 AM'. Only 12-hour if the locale is. + * @type {Object} + */ + static get DATETIME_MED_WITH_WEEKDAY() { + return DATETIME_MED_WITH_WEEKDAY; + } + /** + * {@link DateTime#toLocaleString} format like 'October 14, 1983, 9:30 AM EDT'. Only 12-hour if the locale is. + * @type {Object} + */ + static get DATETIME_FULL() { + return DATETIME_FULL; + } + /** + * {@link DateTime#toLocaleString} format like 'October 14, 1983, 9:30:33 AM EDT'. Only 12-hour if the locale is. + * @type {Object} + */ + static get DATETIME_FULL_WITH_SECONDS() { + return DATETIME_FULL_WITH_SECONDS; + } + /** + * {@link DateTime#toLocaleString} format like 'Friday, October 14, 1983, 9:30 AM Eastern Daylight Time'. Only 12-hour if the locale is. + * @type {Object} + */ + static get DATETIME_HUGE() { + return DATETIME_HUGE; + } + /** + * {@link DateTime#toLocaleString} format like 'Friday, October 14, 1983, 9:30:33 AM Eastern Daylight Time'. Only 12-hour if the locale is. + * @type {Object} + */ + static get DATETIME_HUGE_WITH_SECONDS() { + return DATETIME_HUGE_WITH_SECONDS; + } + }; + function friendlyDateTime(dateTimeish) { + if (DateTime.isDateTime(dateTimeish)) { + return dateTimeish; + } else if (dateTimeish && dateTimeish.valueOf && isNumber(dateTimeish.valueOf())) { + return DateTime.fromJSDate(dateTimeish); + } else if (dateTimeish && typeof dateTimeish === "object") { + return DateTime.fromObject(dateTimeish); + } else { + throw new InvalidArgumentError(`Unknown datetime argument: ${dateTimeish}, of type ${typeof dateTimeish}`); + } + } + var VERSION = "3.7.2"; + exports.DateTime = DateTime; + exports.Duration = Duration; + exports.FixedOffsetZone = FixedOffsetZone; + exports.IANAZone = IANAZone; + exports.Info = Info; + exports.Interval = Interval; + exports.InvalidZone = InvalidZone; + exports.Settings = Settings; + exports.SystemZone = SystemZone; + exports.VERSION = VERSION; + exports.Zone = Zone; + } +}); + +// node_modules/cron-parser/dist/CronDate.js +var require_CronDate = __commonJS({ + "node_modules/cron-parser/dist/CronDate.js"(exports) { + "use strict"; + Object.defineProperty(exports, "__esModule", { value: true }); + exports.CronDate = exports.DAYS_IN_MONTH = exports.DateMathOp = exports.TimeUnit = void 0; + var luxon_1 = require_luxon(); + var TimeUnit; + (function(TimeUnit2) { + TimeUnit2["Second"] = "Second"; + TimeUnit2["Minute"] = "Minute"; + TimeUnit2["Hour"] = "Hour"; + TimeUnit2["Day"] = "Day"; + TimeUnit2["Month"] = "Month"; + TimeUnit2["Year"] = "Year"; + })(TimeUnit || (exports.TimeUnit = TimeUnit = {})); + var DateMathOp; + (function(DateMathOp2) { + DateMathOp2["Add"] = "Add"; + DateMathOp2["Subtract"] = "Subtract"; + })(DateMathOp || (exports.DateMathOp = DateMathOp = {})); + exports.DAYS_IN_MONTH = Object.freeze([31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]); + var _date, _dstStart, _dstEnd, _verbMap, _CronDate_static, isLeapYear_fn, _CronDate_instances, getUTC_fn; + var _CronDate = class _CronDate { + /** + * Constructs a new CronDate instance. + * @param {CronDate | Date | number | string} [timestamp] - The timestamp to initialize the CronDate with. + * @param {string} [tz] - The timezone to use for the CronDate. + */ + constructor(timestamp, tz) { + __privateAdd(this, _CronDate_instances); + __privateAdd(this, _date); + __privateAdd(this, _dstStart, null); + __privateAdd(this, _dstEnd, null); + /** + * Maps the verb to the appropriate method + */ + __privateAdd(this, _verbMap, { + add: { + [TimeUnit.Year]: this.addYear.bind(this), + [TimeUnit.Month]: this.addMonth.bind(this), + [TimeUnit.Day]: this.addDay.bind(this), + [TimeUnit.Hour]: this.addHour.bind(this), + [TimeUnit.Minute]: this.addMinute.bind(this), + [TimeUnit.Second]: this.addSecond.bind(this) + }, + subtract: { + [TimeUnit.Year]: this.subtractYear.bind(this), + [TimeUnit.Month]: this.subtractMonth.bind(this), + [TimeUnit.Day]: this.subtractDay.bind(this), + [TimeUnit.Hour]: this.subtractHour.bind(this), + [TimeUnit.Minute]: this.subtractMinute.bind(this), + [TimeUnit.Second]: this.subtractSecond.bind(this) + } + }); + const dateOpts = { zone: tz }; + if (!timestamp) { + __privateSet(this, _date, luxon_1.DateTime.local()); + } else if (timestamp instanceof _CronDate) { + __privateSet(this, _date, __privateGet(timestamp, _date)); + __privateSet(this, _dstStart, __privateGet(timestamp, _dstStart)); + __privateSet(this, _dstEnd, __privateGet(timestamp, _dstEnd)); + } else if (timestamp instanceof Date) { + __privateSet(this, _date, luxon_1.DateTime.fromJSDate(timestamp, dateOpts)); + } else if (typeof timestamp === "number") { + __privateSet(this, _date, luxon_1.DateTime.fromMillis(timestamp, dateOpts)); + } else { + __privateSet(this, _date, luxon_1.DateTime.fromISO(timestamp, dateOpts)); + __privateGet(this, _date).isValid || __privateSet(this, _date, luxon_1.DateTime.fromRFC2822(timestamp, dateOpts)); + __privateGet(this, _date).isValid || __privateSet(this, _date, luxon_1.DateTime.fromSQL(timestamp, dateOpts)); + __privateGet(this, _date).isValid || __privateSet(this, _date, luxon_1.DateTime.fromFormat(timestamp, "EEE, d MMM yyyy HH:mm:ss", dateOpts)); + } + if (!__privateGet(this, _date).isValid) { + throw new Error(`CronDate: unhandled timestamp: ${timestamp}`); + } + if (tz && tz !== __privateGet(this, _date).zoneName) { + __privateSet(this, _date, __privateGet(this, _date).setZone(tz)); + } + } + /** + * Returns daylight savings start time. + * @returns {number | null} + */ + get dstStart() { + return __privateGet(this, _dstStart); + } + /** + * Sets daylight savings start time. + * @param {number | null} value + */ + set dstStart(value) { + __privateSet(this, _dstStart, value); + } + /** + * Returns daylight savings end time. + * @returns {number | null} + */ + get dstEnd() { + return __privateGet(this, _dstEnd); + } + /** + * Sets daylight savings end time. + * @param {number | null} value + */ + set dstEnd(value) { + __privateSet(this, _dstEnd, value); + } + /** + * Adds one year to the current CronDate. + */ + addYear() { + __privateSet(this, _date, __privateGet(this, _date).plus({ years: 1 })); + } + /** + * Adds one month to the current CronDate. + */ + addMonth() { + __privateSet(this, _date, __privateGet(this, _date).plus({ months: 1 }).startOf("month")); + } + /** + * Adds one day to the current CronDate. + */ + addDay() { + __privateSet(this, _date, __privateGet(this, _date).plus({ days: 1 }).startOf("day")); + } + /** + * Adds one hour to the current CronDate. + */ + addHour() { + __privateSet(this, _date, __privateGet(this, _date).plus({ hours: 1 }).startOf("hour")); + } + /** + * Adds one minute to the current CronDate. + */ + addMinute() { + __privateSet(this, _date, __privateGet(this, _date).plus({ minutes: 1 }).startOf("minute")); + } + /** + * Adds one second to the current CronDate. + */ + addSecond() { + __privateSet(this, _date, __privateGet(this, _date).plus({ seconds: 1 })); + } + /** + * Subtracts one year from the current CronDate. + */ + subtractYear() { + __privateSet(this, _date, __privateGet(this, _date).minus({ years: 1 })); + } + /** + * Subtracts one month from the current CronDate. + * If the month is 1, it will subtract one year instead. + */ + subtractMonth() { + __privateSet(this, _date, __privateGet(this, _date).minus({ months: 1 }).endOf("month").startOf("second")); + } + /** + * Subtracts one day from the current CronDate. + * If the day is 1, it will subtract one month instead. + */ + subtractDay() { + __privateSet(this, _date, __privateGet(this, _date).minus({ days: 1 }).endOf("day").startOf("second")); + } + /** + * Subtracts one hour from the current CronDate. + * If the hour is 0, it will subtract one day instead. + */ + subtractHour() { + __privateSet(this, _date, __privateGet(this, _date).minus({ hours: 1 }).endOf("hour").startOf("second")); + } + /** + * Subtracts one minute from the current CronDate. + * If the minute is 0, it will subtract one hour instead. + */ + subtractMinute() { + __privateSet(this, _date, __privateGet(this, _date).minus({ minutes: 1 }).endOf("minute").startOf("second")); + } + /** + * Subtracts one second from the current CronDate. + * If the second is 0, it will subtract one minute instead. + */ + subtractSecond() { + __privateSet(this, _date, __privateGet(this, _date).minus({ seconds: 1 })); + } + /** + * Adds a unit of time to the current CronDate. + * @param {TimeUnit} unit + */ + addUnit(unit) { + __privateGet(this, _verbMap).add[unit](); + } + /** + * Subtracts a unit of time from the current CronDate. + * @param {TimeUnit} unit + */ + subtractUnit(unit) { + __privateGet(this, _verbMap).subtract[unit](); + } + /** + * Handles a math operation. + * @param {DateMathOp} verb - {'add' | 'subtract'} + * @param {TimeUnit} unit - {'year' | 'month' | 'day' | 'hour' | 'minute' | 'second'} + */ + invokeDateOperation(verb, unit) { + if (verb === DateMathOp.Add) { + this.addUnit(unit); + return; + } + if (verb === DateMathOp.Subtract) { + this.subtractUnit(unit); + return; + } + throw new Error(`Invalid verb: ${verb}`); + } + /** + * Returns the day. + * @returns {number} + */ + getDate() { + return __privateGet(this, _date).day; + } + /** + * Returns the year. + * @returns {number} + */ + getFullYear() { + return __privateGet(this, _date).year; + } + /** + * Returns the day of the week. + * @returns {number} + */ + getDay() { + const weekday = __privateGet(this, _date).weekday; + return weekday === 7 ? 0 : weekday; + } + /** + * Returns the month. + * @returns {number} + */ + getMonth() { + return __privateGet(this, _date).month - 1; + } + /** + * Returns the hour. + * @returns {number} + */ + getHours() { + return __privateGet(this, _date).hour; + } + /** + * Returns the minutes. + * @returns {number} + */ + getMinutes() { + return __privateGet(this, _date).minute; + } + /** + * Returns the seconds. + * @returns {number} + */ + getSeconds() { + return __privateGet(this, _date).second; + } + /** + * Returns the milliseconds. + * @returns {number} + */ + getMilliseconds() { + return __privateGet(this, _date).millisecond; + } + /** + * Returns the timezone offset from UTC in minutes (e.g. UTC+2 => 120). + * Useful for detecting DST transition days. + * + * @returns {number} UTC offset in minutes + */ + getUTCOffset() { + return __privateGet(this, _date).offset; + } + /** + * Sets the time to the start of the day (00:00:00.000) in the current timezone. + */ + setStartOfDay() { + __privateSet(this, _date, __privateGet(this, _date).startOf("day")); + } + /** + * Sets the time to the end of the day (23:59:59.999) in the current timezone. + */ + setEndOfDay() { + __privateSet(this, _date, __privateGet(this, _date).endOf("day")); + } + /** + * Returns the time. + * @returns {number} + */ + getTime() { + return __privateGet(this, _date).valueOf(); + } + /** + * Returns the UTC day. + * @returns {number} + */ + getUTCDate() { + return __privateMethod(this, _CronDate_instances, getUTC_fn).call(this).day; + } + /** + * Returns the UTC year. + * @returns {number} + */ + getUTCFullYear() { + return __privateMethod(this, _CronDate_instances, getUTC_fn).call(this).year; + } + /** + * Returns the UTC day of the week. + * @returns {number} + */ + getUTCDay() { + const weekday = __privateMethod(this, _CronDate_instances, getUTC_fn).call(this).weekday; + return weekday === 7 ? 0 : weekday; + } + /** + * Returns the UTC month. + * @returns {number} + */ + getUTCMonth() { + return __privateMethod(this, _CronDate_instances, getUTC_fn).call(this).month - 1; + } + /** + * Returns the UTC hour. + * @returns {number} + */ + getUTCHours() { + return __privateMethod(this, _CronDate_instances, getUTC_fn).call(this).hour; + } + /** + * Returns the UTC minutes. + * @returns {number} + */ + getUTCMinutes() { + return __privateMethod(this, _CronDate_instances, getUTC_fn).call(this).minute; + } + /** + * Returns the UTC seconds. + * @returns {number} + */ + getUTCSeconds() { + return __privateMethod(this, _CronDate_instances, getUTC_fn).call(this).second; + } + /** + * Returns the UTC milliseconds. + * @returns {string | null} + */ + toISOString() { + return __privateGet(this, _date).toUTC().toISO(); + } + /** + * Returns the date as a JSON string. + * @returns {string | null} + */ + toJSON() { + return __privateGet(this, _date).toJSON(); + } + /** + * Sets the day. + * @param d + */ + setDate(d) { + __privateSet(this, _date, __privateGet(this, _date).set({ day: d })); + } + /** + * Sets the year. + * @param y + */ + setFullYear(y) { + __privateSet(this, _date, __privateGet(this, _date).set({ year: y })); + } + /** + * Sets the day of the week. + * @param d + */ + setDay(d) { + __privateSet(this, _date, __privateGet(this, _date).set({ weekday: d })); + } + /** + * Sets the month. + * @param m + */ + setMonth(m) { + __privateSet(this, _date, __privateGet(this, _date).set({ month: m + 1 })); + } + /** + * Sets the hour. + * @param h + */ + setHours(h2) { + __privateSet(this, _date, __privateGet(this, _date).set({ hour: h2 })); + } + /** + * Sets the minutes. + * @param m + */ + setMinutes(m) { + __privateSet(this, _date, __privateGet(this, _date).set({ minute: m })); + } + /** + * Sets the seconds. + * @param s + */ + setSeconds(s15) { + __privateSet(this, _date, __privateGet(this, _date).set({ second: s15 })); + } + /** + * Sets the milliseconds. + * @param s + */ + setMilliseconds(s15) { + __privateSet(this, _date, __privateGet(this, _date).set({ millisecond: s15 })); + } + /** + * Returns the date as a string. + * @returns {string} + */ + toString() { + return this.toDate().toString(); + } + /** + * Returns the date as a Date object. + * @returns {Date} + */ + toDate() { + return __privateGet(this, _date).toJSDate(); + } + /** + * Returns true if the day is the last day of the month. + * @returns {boolean} + */ + isLastDayOfMonth() { + var _a5; + const { day, month } = __privateGet(this, _date); + if (month === 2) { + const isLeap = __privateMethod(_a5 = _CronDate, _CronDate_static, isLeapYear_fn).call(_a5, __privateGet(this, _date).year); + return day === exports.DAYS_IN_MONTH[month - 1] - (isLeap ? 0 : 1); + } + return day === exports.DAYS_IN_MONTH[month - 1]; + } + /** + * Returns true if the day is the last weekday of the month. + * @returns {boolean} + */ + isLastWeekdayOfMonth() { + var _a5; + const { day, month } = __privateGet(this, _date); + let lastDay; + if (month === 2) { + lastDay = exports.DAYS_IN_MONTH[month - 1] - (__privateMethod(_a5 = _CronDate, _CronDate_static, isLeapYear_fn).call(_a5, __privateGet(this, _date).year) ? 0 : 1); + } else { + lastDay = exports.DAYS_IN_MONTH[month - 1]; + } + return day > lastDay - 7; + } + /** + * Primarily for internal use. + * @param {DateMathOp} op - The operation to perform. + * @param {TimeUnit} unit - The unit of time to use. + * @param {number} [hoursLength] - The length of the hours. Required when unit is not month or day. + */ + applyDateOperation(op, unit, hoursLength) { + if (unit === TimeUnit.Month || unit === TimeUnit.Day) { + this.invokeDateOperation(op, unit); + return; + } + const previousHour = this.getHours(); + this.invokeDateOperation(op, unit); + const currentHour = this.getHours(); + const diff = currentHour - previousHour; + if (diff === 2) { + if (hoursLength !== 24) { + this.dstStart = currentHour; + } + } else if (diff === 0 && this.getMinutes() === 0 && this.getSeconds() === 0) { + if (hoursLength !== 24) { + this.dstEnd = currentHour; + } + } + } + }; + _date = new WeakMap(); + _dstStart = new WeakMap(); + _dstEnd = new WeakMap(); + _verbMap = new WeakMap(); + _CronDate_static = new WeakSet(); + isLeapYear_fn = function(year) { + return year % 4 === 0 && year % 100 !== 0 || year % 400 === 0; + }; + _CronDate_instances = new WeakSet(); + /** + * Returns the UTC date. + * @private + * @returns {DateTime} + */ + getUTC_fn = function() { + return __privateGet(this, _date).toUTC(); + }; + __privateAdd(_CronDate, _CronDate_static); + var CronDate = _CronDate; + exports.CronDate = CronDate; + exports.default = CronDate; + } +}); + +// node_modules/cron-parser/dist/fields/CronMonth.js +var require_CronMonth = __commonJS({ + "node_modules/cron-parser/dist/fields/CronMonth.js"(exports) { + "use strict"; + Object.defineProperty(exports, "__esModule", { value: true }); + exports.CronMonth = void 0; + var CronDate_1 = require_CronDate(); + var CronField_1 = require_CronField(); + var MIN_MONTH = 1; + var MAX_MONTH = 12; + var MONTH_CHARS = Object.freeze([]); + var CronMonth = class extends CronField_1.CronField { + static get min() { + return MIN_MONTH; + } + static get max() { + return MAX_MONTH; + } + static get chars() { + return MONTH_CHARS; + } + static get daysInMonth() { + return CronDate_1.DAYS_IN_MONTH; + } + /** + * CronDayOfMonth constructor. Initializes the "day of the month" field with the provided values. + * @param {MonthRange[]} values - Values for the "day of the month" field + * @param {CronFieldOptions} [options] - Options provided by the parser + */ + constructor(values, options) { + super(values, options); + this.validate(); + } + /** + * Returns an array of allowed values for the "day of the month" field. + * @returns {MonthRange[]} + */ + get values() { + return super.values; + } + }; + exports.CronMonth = CronMonth; + } +}); + +// node_modules/cron-parser/dist/fields/CronSecond.js +var require_CronSecond = __commonJS({ + "node_modules/cron-parser/dist/fields/CronSecond.js"(exports) { + "use strict"; + Object.defineProperty(exports, "__esModule", { value: true }); + exports.CronSecond = void 0; + var CronField_1 = require_CronField(); + var MIN_SECOND = 0; + var MAX_SECOND = 59; + var SECOND_CHARS = Object.freeze([]); + var CronSecond = class extends CronField_1.CronField { + static get min() { + return MIN_SECOND; + } + static get max() { + return MAX_SECOND; + } + static get chars() { + return SECOND_CHARS; + } + /** + * CronSecond constructor. Initializes the "second" field with the provided values. + * @param {SixtyRange[]} values - Values for the "second" field + * @param {CronFieldOptions} [options] - Options provided by the parser + */ + constructor(values, options) { + super(values, options); + this.validate(); + } + /** + * Returns an array of allowed values for the "second" field. + * @returns {SixtyRange[]} + */ + get values() { + return super.values; + } + }; + exports.CronSecond = CronSecond; + } +}); + +// node_modules/cron-parser/dist/fields/index.js +var require_fields = __commonJS({ + "node_modules/cron-parser/dist/fields/index.js"(exports) { + "use strict"; + var __createBinding = exports && exports.__createBinding || (Object.create ? (function(o2, m, k, k2) { + if (k2 === void 0) k2 = k; + var desc = Object.getOwnPropertyDescriptor(m, k); + if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { + desc = { enumerable: true, get: function() { + return m[k]; + } }; + } + Object.defineProperty(o2, k2, desc); + }) : (function(o2, m, k, k2) { + if (k2 === void 0) k2 = k; + o2[k2] = m[k]; + })); + var __exportStar = exports && exports.__exportStar || function(m, exports2) { + for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports2, p)) __createBinding(exports2, m, p); + }; + Object.defineProperty(exports, "__esModule", { value: true }); + __exportStar(require_types(), exports); + __exportStar(require_CronDayOfMonth(), exports); + __exportStar(require_CronDayOfWeek(), exports); + __exportStar(require_CronField(), exports); + __exportStar(require_CronHour(), exports); + __exportStar(require_CronMinute(), exports); + __exportStar(require_CronMonth(), exports); + __exportStar(require_CronSecond(), exports); + } +}); + +// node_modules/cron-parser/dist/CronFieldCollection.js +var require_CronFieldCollection = __commonJS({ + "node_modules/cron-parser/dist/CronFieldCollection.js"(exports) { + "use strict"; + Object.defineProperty(exports, "__esModule", { value: true }); + exports.CronFieldCollection = void 0; + var fields_1 = require_fields(); + var _second, _minute, _hour, _dayOfMonth, _month, _dayOfWeek, _CronFieldCollection_static, handleSingleRange_fn, handleMultipleRanges_fn; + var _CronFieldCollection = class _CronFieldCollection { + /** + * CronFieldCollection constructor. Initializes the cron fields with the provided values. + * @param {CronFields} param0 - The cron fields values + * @throws {Error} if validation fails + * @example + * const cronFields = new CronFieldCollection({ + * second: new CronSecond([0]), + * minute: new CronMinute([0, 30]), + * hour: new CronHour([9]), + * dayOfMonth: new CronDayOfMonth([15]), + * month: new CronMonth([1]), + * dayOfWeek: new CronDayOfTheWeek([1, 2, 3, 4, 5]), + * }) + * + * console.log(cronFields.second.values); // [0] + * console.log(cronFields.minute.values); // [0, 30] + * console.log(cronFields.hour.values); // [9] + * console.log(cronFields.dayOfMonth.values); // [15] + * console.log(cronFields.month.values); // [1] + * console.log(cronFields.dayOfWeek.values); // [1, 2, 3, 4, 5] + */ + constructor({ second, minute, hour, dayOfMonth, month, dayOfWeek }) { + __privateAdd(this, _second); + __privateAdd(this, _minute); + __privateAdd(this, _hour); + __privateAdd(this, _dayOfMonth); + __privateAdd(this, _month); + __privateAdd(this, _dayOfWeek); + if (!second) { + throw new Error("Validation error, Field second is missing"); + } + if (!minute) { + throw new Error("Validation error, Field minute is missing"); + } + if (!hour) { + throw new Error("Validation error, Field hour is missing"); + } + if (!dayOfMonth) { + throw new Error("Validation error, Field dayOfMonth is missing"); + } + if (!month) { + throw new Error("Validation error, Field month is missing"); + } + if (!dayOfWeek) { + throw new Error("Validation error, Field dayOfWeek is missing"); + } + if (month.values.length === 1 && !dayOfMonth.hasLastChar) { + if (!(parseInt(dayOfMonth.values[0], 10) <= fields_1.CronMonth.daysInMonth[month.values[0] - 1])) { + throw new Error("Invalid explicit day of month definition"); + } + } + __privateSet(this, _second, second); + __privateSet(this, _minute, minute); + __privateSet(this, _hour, hour); + __privateSet(this, _month, month); + __privateSet(this, _dayOfWeek, dayOfWeek); + __privateSet(this, _dayOfMonth, dayOfMonth); + } + /** + * Creates a new CronFieldCollection instance by partially overriding fields from an existing one. + * @param {CronFieldCollection} base - The base CronFieldCollection to copy fields from + * @param {CronFieldOverride} fields - The fields to override, can be CronField instances or raw values + * @returns {CronFieldCollection} A new CronFieldCollection instance + * @example + * const base = new CronFieldCollection({ + * second: new CronSecond([0]), + * minute: new CronMinute([0]), + * hour: new CronHour([12]), + * dayOfMonth: new CronDayOfMonth([1]), + * month: new CronMonth([1]), + * dayOfWeek: new CronDayOfWeek([1]) + * }); + * + * // Using CronField instances + * const modified1 = CronFieldCollection.from(base, { + * hour: new CronHour([15]), + * minute: new CronMinute([30]) + * }); + * + * // Using raw values + * const modified2 = CronFieldCollection.from(base, { + * hour: [15], // Will create new CronHour + * minute: [30] // Will create new CronMinute + * }); + */ + static from(base, fields) { + return new _CronFieldCollection({ + second: this.resolveField(fields_1.CronSecond, base.second, fields.second), + minute: this.resolveField(fields_1.CronMinute, base.minute, fields.minute), + hour: this.resolveField(fields_1.CronHour, base.hour, fields.hour), + dayOfMonth: this.resolveField(fields_1.CronDayOfMonth, base.dayOfMonth, fields.dayOfMonth), + month: this.resolveField(fields_1.CronMonth, base.month, fields.month), + dayOfWeek: this.resolveField(fields_1.CronDayOfWeek, base.dayOfWeek, fields.dayOfWeek) + }); + } + /** + * Resolves a field value, either using the provided CronField instance or creating a new one from raw values. + * @param constructor - The constructor for creating new field instances + * @param baseField - The base field to use if no override is provided + * @param fieldValue - The override value, either a CronField instance or raw values + * @returns The resolved CronField instance + * @private + */ + static resolveField(constructor, baseField, fieldValue) { + if (!fieldValue) { + return baseField; + } + if (fieldValue instanceof fields_1.CronField) { + return fieldValue; + } + return new constructor(fieldValue); + } + /** + * Returns the second field. + * @returns {CronSecond} + */ + get second() { + return __privateGet(this, _second); + } + /** + * Returns the minute field. + * @returns {CronMinute} + */ + get minute() { + return __privateGet(this, _minute); + } + /** + * Returns the hour field. + * @returns {CronHour} + */ + get hour() { + return __privateGet(this, _hour); + } + /** + * Returns the day of the month field. + * @returns {CronDayOfMonth} + */ + get dayOfMonth() { + return __privateGet(this, _dayOfMonth); + } + /** + * Returns the month field. + * @returns {CronMonth} + */ + get month() { + return __privateGet(this, _month); + } + /** + * Returns the day of the week field. + * @returns {CronDayOfWeek} + */ + get dayOfWeek() { + return __privateGet(this, _dayOfWeek); + } + /** + * Returns a string representation of the cron fields. + * @param {(number | CronChars)[]} input - The cron fields values + * @static + * @returns {FieldRange[]} - The compacted cron fields + */ + static compactField(input) { + if (input.length === 0) { + return []; + } + const output = []; + let current = void 0; + input.forEach((item, i, arr) => { + var _a5, _b; + if (current === void 0) { + current = { start: item, count: 1 }; + return; + } + const prevItem = arr[i - 1] || current.start; + const nextItem = arr[i + 1]; + if (item === "L" || item === "W") { + output.push(current); + output.push({ start: item, count: 1 }); + current = void 0; + return; + } + if (current.step === void 0 && nextItem !== void 0) { + const step = item - prevItem; + const nextStep = nextItem - item; + if (step <= nextStep) { + current = { ...current, count: 2, end: item, step }; + return; + } + current.step = 1; + } + if (item - ((_a5 = current.end) != null ? _a5 : 0) === current.step) { + current.count++; + current.end = item; + } else { + if (current.count === 1) { + output.push({ start: current.start, count: 1 }); + } else if (current.count === 2) { + output.push({ start: current.start, count: 1 }); + output.push({ + start: (_b = current.end) != null ? _b : ( + /* istanbul ignore next - see above */ + prevItem + ), + count: 1 + }); + } else { + output.push(current); + } + current = { start: item, count: 1 }; + } + }); + if (current) { + output.push(current); + } + return output; + } + /** + * Returns a string representation of the cron fields. + * @param {CronField} field - The cron field to stringify + * @static + * @returns {string} - The stringified cron field + */ + stringifyField(field) { + var _a5; + let max = field.max; + let values = field.values; + if (field instanceof fields_1.CronDayOfWeek) { + max = 6; + const dayOfWeek = __privateGet(this, _dayOfWeek).values; + values = dayOfWeek[dayOfWeek.length - 1] === 7 ? dayOfWeek.slice(0, -1) : dayOfWeek; + } + if (field instanceof fields_1.CronDayOfMonth) { + max = __privateGet(this, _month).values.length === 1 ? fields_1.CronMonth.daysInMonth[__privateGet(this, _month).values[0] - 1] : field.max; + } + const ranges = _CronFieldCollection.compactField(values); + if (ranges.length === 1) { + const singleRangeResult = __privateMethod(_a5 = _CronFieldCollection, _CronFieldCollection_static, handleSingleRange_fn).call(_a5, field, ranges[0], max); + if (singleRangeResult) { + return singleRangeResult; + } + } + return ranges.map((range) => { + var _a6; + const value = range.count === 1 ? range.start.toString() : __privateMethod(_a6 = _CronFieldCollection, _CronFieldCollection_static, handleMultipleRanges_fn).call(_a6, range, max); + if (field instanceof fields_1.CronDayOfWeek && field.nthDay > 0) { + return `${value}#${field.nthDay}`; + } + return value; + }).join(","); + } + /** + * Returns a string representation of the cron field values. + * @param {boolean} includeSeconds - Whether to include seconds in the output + * @returns {string} The formatted cron string + */ + stringify(includeSeconds = false) { + const arr = []; + if (includeSeconds) { + arr.push(this.stringifyField(__privateGet(this, _second))); + } + arr.push( + this.stringifyField(__privateGet(this, _minute)), + // minute + this.stringifyField(__privateGet(this, _hour)), + // hour + this.stringifyField(__privateGet(this, _dayOfMonth)), + // dayOfMonth + this.stringifyField(__privateGet(this, _month)), + // month + this.stringifyField(__privateGet(this, _dayOfWeek)) + ); + return arr.join(" "); + } + /** + * Returns a serialized representation of the cron fields values. + * @returns {SerializedCronFields} An object containing the cron field values + */ + serialize() { + return { + second: __privateGet(this, _second).serialize(), + minute: __privateGet(this, _minute).serialize(), + hour: __privateGet(this, _hour).serialize(), + dayOfMonth: __privateGet(this, _dayOfMonth).serialize(), + month: __privateGet(this, _month).serialize(), + dayOfWeek: __privateGet(this, _dayOfWeek).serialize() + }; + } + }; + _second = new WeakMap(); + _minute = new WeakMap(); + _hour = new WeakMap(); + _dayOfMonth = new WeakMap(); + _month = new WeakMap(); + _dayOfWeek = new WeakMap(); + _CronFieldCollection_static = new WeakSet(); + handleSingleRange_fn = function(field, range, max) { + const step = range.step; + if (!step) { + return null; + } + if (step === 1 && range.start === field.min && range.end && range.end >= max) { + return field.hasQuestionMarkChar ? "?" : "*"; + } + if (step !== 1 && range.start === field.min && range.end && range.end >= max - step + 1) { + return `*/${step}`; + } + return null; + }; + handleMultipleRanges_fn = function(range, max) { + const step = range.step; + if (step === 1) { + return `${range.start}-${range.end}`; + } + const multiplier = range.start === 0 ? range.count - 1 : range.count; + if (!step) { + throw new Error("Unexpected range step"); + } + if (!range.end) { + throw new Error("Unexpected range end"); + } + if (step * multiplier > range.end) { + const mapFn = (_2, index) => { + if (typeof range.start !== "number") { + throw new Error("Unexpected range start"); + } + return index % step === 0 ? range.start + index : null; + }; + if (typeof range.start !== "number") { + throw new Error("Unexpected range start"); + } + const seed = { length: range.end - range.start + 1 }; + return Array.from(seed, mapFn).filter((value) => value !== null).join(","); + } + return range.end === max - step + 1 ? `${range.start}/${step}` : `${range.start}-${range.end}/${step}`; + }; + __privateAdd(_CronFieldCollection, _CronFieldCollection_static); + var CronFieldCollection = _CronFieldCollection; + exports.CronFieldCollection = CronFieldCollection; + } +}); + +// node_modules/cron-parser/dist/CronExpression.js +var require_CronExpression = __commonJS({ + "node_modules/cron-parser/dist/CronExpression.js"(exports) { + "use strict"; + Object.defineProperty(exports, "__esModule", { value: true }); + exports.CronExpression = exports.LOOPS_LIMIT_EXCEEDED_ERROR_MESSAGE = exports.TIME_SPAN_OUT_OF_BOUNDS_ERROR_MESSAGE = void 0; + var CronDate_1 = require_CronDate(); + exports.TIME_SPAN_OUT_OF_BOUNDS_ERROR_MESSAGE = "Out of the time span range"; + exports.LOOPS_LIMIT_EXCEEDED_ERROR_MESSAGE = "Invalid expression, loop limit exceeded"; + var LOOP_LIMIT = 1e4; + var _options, _tz, _currentDate, _startDate, _endDate, _fields, _dstTransitionDayKey, _isDstTransitionDay, _CronExpression_static, matchSchedule_fn, _CronExpression_instances, getMinOrMax_fn, checkDstTransition_fn, moveToNextSecond_fn, moveToNextMinute_fn, isLastWeekdayOfMonthMatch_fn, matchDayOfMonth_fn, matchHour_fn, validateTimeSpan_fn, findSchedule_fn; + var _CronExpression = class _CronExpression { + /** + * Creates a new CronExpression instance. + * + * @param {CronFieldCollection} fields - Cron fields. + * @param {CronExpressionOptions} options - Parser options. + */ + constructor(fields, options) { + __privateAdd(this, _CronExpression_instances); + __privateAdd(this, _options); + __privateAdd(this, _tz); + __privateAdd(this, _currentDate); + __privateAdd(this, _startDate); + __privateAdd(this, _endDate); + __privateAdd(this, _fields); + __privateAdd(this, _dstTransitionDayKey, null); + __privateAdd(this, _isDstTransitionDay, false); + var _a5; + __privateSet(this, _options, options); + __privateSet(this, _tz, options.tz); + __privateSet(this, _startDate, options.startDate ? new CronDate_1.CronDate(options.startDate, __privateGet(this, _tz)) : null); + __privateSet(this, _endDate, options.endDate ? new CronDate_1.CronDate(options.endDate, __privateGet(this, _tz)) : null); + let currentDateValue = (_a5 = options.currentDate) != null ? _a5 : options.startDate; + if (currentDateValue) { + const tempCurrentDate = new CronDate_1.CronDate(currentDateValue, __privateGet(this, _tz)); + if (__privateGet(this, _startDate) && tempCurrentDate.getTime() < __privateGet(this, _startDate).getTime()) { + currentDateValue = __privateGet(this, _startDate); + } else if (__privateGet(this, _endDate) && tempCurrentDate.getTime() > __privateGet(this, _endDate).getTime()) { + currentDateValue = __privateGet(this, _endDate); + } + } + __privateSet(this, _currentDate, new CronDate_1.CronDate(currentDateValue, __privateGet(this, _tz))); + __privateSet(this, _fields, fields); + } + /** + * Getter for the cron fields. + * + * @returns {CronFieldCollection} Cron fields. + */ + get fields() { + return __privateGet(this, _fields); + } + /** + * Converts cron fields back to a CronExpression instance. + * + * @public + * @param {Record} fields - The input cron fields object. + * @param {CronExpressionOptions} [options] - Optional parsing options. + * @returns {CronExpression} - A new CronExpression instance. + */ + static fieldsToExpression(fields, options) { + return new _CronExpression(fields, options || {}); + } + /** + * Find the next scheduled date based on the cron expression. + * @returns {CronDate} - The next scheduled date or an ES6 compatible iterator object. + * @memberof CronExpression + * @public + */ + next() { + return __privateMethod(this, _CronExpression_instances, findSchedule_fn).call(this); + } + /** + * Find the previous scheduled date based on the cron expression. + * @returns {CronDate} - The previous scheduled date or an ES6 compatible iterator object. + * @memberof CronExpression + * @public + */ + prev() { + return __privateMethod(this, _CronExpression_instances, findSchedule_fn).call(this, true); + } + /** + * Check if there is a next scheduled date based on the current date and cron expression. + * @returns {boolean} - Returns true if there is a next scheduled date, false otherwise. + * @memberof CronExpression + * @public + */ + hasNext() { + const current = __privateGet(this, _currentDate); + try { + __privateMethod(this, _CronExpression_instances, findSchedule_fn).call(this); + return true; + } catch (e) { + return false; + } finally { + __privateSet(this, _currentDate, current); + } + } + /** + * Check if there is a previous scheduled date based on the current date and cron expression. + * @returns {boolean} - Returns true if there is a previous scheduled date, false otherwise. + * @memberof CronExpression + * @public + */ + hasPrev() { + const current = __privateGet(this, _currentDate); + try { + __privateMethod(this, _CronExpression_instances, findSchedule_fn).call(this, true); + return true; + } catch (e) { + return false; + } finally { + __privateSet(this, _currentDate, current); + } + } + /** + * Iterate over a specified number of steps and optionally execute a callback function for each step. + * @param {number} steps - The number of steps to iterate. Positive value iterates forward, negative value iterates backward. + * @returns {CronDate[]} - An array of iterator fields or CronDate objects. + * @memberof CronExpression + * @public + */ + take(limit) { + const items = []; + if (limit >= 0) { + for (let i = 0; i < limit; i++) { + try { + items.push(this.next()); + } catch (e) { + return items; + } + } + } else { + for (let i = 0; i > limit; i--) { + try { + items.push(this.prev()); + } catch (e) { + return items; + } + } + } + return items; + } + /** + * Reset the iterators current date to a new date or the initial date. + * @param {Date | CronDate} [newDate] - Optional new date to reset to. If not provided, it will reset to the initial date. + * @memberof CronExpression + * @public + */ + reset(newDate) { + __privateSet(this, _currentDate, new CronDate_1.CronDate(newDate || __privateGet(this, _options).currentDate)); + } + /** + * Generate a string representation of the cron expression. + * @param {boolean} [includeSeconds=false] - Whether to include the seconds field in the string representation. + * @returns {string} - The string representation of the cron expression. + * @memberof CronExpression + * @public + */ + stringify(includeSeconds = false) { + return __privateGet(this, _fields).stringify(includeSeconds); + } + /** + * Check if the cron expression includes the given date + * @param {Date|CronDate} date + * @returns {boolean} + */ + includesDate(date) { + const { second, minute, hour, month } = __privateGet(this, _fields); + const dt2 = new CronDate_1.CronDate(date, __privateGet(this, _tz)); + if (!second.values.includes(dt2.getSeconds()) || !minute.values.includes(dt2.getMinutes()) || !hour.values.includes(dt2.getHours()) || !month.values.includes(dt2.getMonth() + 1)) { + return false; + } + if (!__privateMethod(this, _CronExpression_instances, matchDayOfMonth_fn).call(this, dt2)) { + return false; + } + if (__privateGet(this, _fields).dayOfWeek.nthDay > 0) { + const weekInMonth = Math.ceil(dt2.getDate() / 7); + if (weekInMonth !== __privateGet(this, _fields).dayOfWeek.nthDay) { + return false; + } + } + return true; + } + /** + * Returns the string representation of the cron expression. + * @returns {CronDate} - The next schedule date. + */ + toString() { + return __privateGet(this, _options).expression || this.stringify(true); + } + /** + * Returns an iterator for iterating through future CronDate instances + * + * @name Symbol.iterator + * @memberof CronExpression + * @returns {Iterator} An iterator object for CronExpression that returns CronDate values. + */ + [Symbol.iterator]() { + return { + next: () => { + const schedule = __privateMethod(this, _CronExpression_instances, findSchedule_fn).call(this); + return { value: schedule, done: !this.hasNext() }; + } + }; + } + }; + _options = new WeakMap(); + _tz = new WeakMap(); + _currentDate = new WeakMap(); + _startDate = new WeakMap(); + _endDate = new WeakMap(); + _fields = new WeakMap(); + _dstTransitionDayKey = new WeakMap(); + _isDstTransitionDay = new WeakMap(); + _CronExpression_static = new WeakSet(); + matchSchedule_fn = function(value, sequence) { + return sequence.some((element) => element === value); + }; + _CronExpression_instances = new WeakSet(); + /** + * Returns the minimum or maximum value from the given array of numbers. + * + * @param {number[]} values - An array of numbers. + * @param {boolean} reverse - If true, returns the maximum value; otherwise, returns the minimum value. + * @returns {number} - The minimum or maximum value. + */ + getMinOrMax_fn = function(values, reverse) { + return values[reverse ? values.length - 1 : 0]; + }; + /** + * Checks whether the given date falls on a DST transition day in its timezone. + * + * This is used to disable certain “direct set” fast paths on DST days, because setting the hour + * directly may land on a non-existent or repeated local time. We cache the result per calendar day + * to keep iteration overhead low. + * + * @param {CronDate} currentDate - Date to check (in the cron timezone) + * @returns {boolean} True when the day has a DST transition + * @private + */ + checkDstTransition_fn = function(currentDate) { + const key = `${currentDate.getFullYear()}-${currentDate.getMonth() + 1}-${currentDate.getDate()}`; + if (__privateGet(this, _dstTransitionDayKey) === key) { + return __privateGet(this, _isDstTransitionDay); + } + const startOfDay = new CronDate_1.CronDate(currentDate); + startOfDay.setStartOfDay(); + const endOfDay = new CronDate_1.CronDate(currentDate); + endOfDay.setEndOfDay(); + __privateSet(this, _dstTransitionDayKey, key); + __privateSet(this, _isDstTransitionDay, startOfDay.getUTCOffset() !== endOfDay.getUTCOffset()); + return __privateGet(this, _isDstTransitionDay); + }; + /** + * Moves the date to the next/previous allowed second value. If there is no remaining allowed second + * within the current minute, rolls to the next/previous minute and resets seconds to the min/max allowed. + * + * @param {CronDate} currentDate - Mutable date being iterated + * @param {DateMathOp} dateMathVerb - Add/Subtract depending on direction + * @param {boolean} reverse - When true, iterating backwards + * @private + */ + moveToNextSecond_fn = function(currentDate, dateMathVerb, reverse) { + const seconds = __privateGet(this, _fields).second.values; + const currentSecond = currentDate.getSeconds(); + const nextSecond = __privateGet(this, _fields).second.findNearestValue(currentSecond, reverse); + if (nextSecond !== null) { + currentDate.setSeconds(nextSecond); + return; + } + currentDate.applyDateOperation(dateMathVerb, CronDate_1.TimeUnit.Minute, __privateGet(this, _fields).hour.values.length); + currentDate.setSeconds(__privateMethod(this, _CronExpression_instances, getMinOrMax_fn).call(this, seconds, reverse)); + }; + /** + * Moves the date to the next/previous allowed minute value and resets seconds to the min/max allowed. + * If there is no remaining allowed minute within the current hour, rolls to the next/previous hour and + * resets minutes/seconds to their extrema. + * + * @param {CronDate} currentDate - Mutable date being iterated + * @param {DateMathOp} dateMathVerb - Add/Subtract depending on direction + * @param {boolean} reverse - When true, iterating backwards + * @private + */ + moveToNextMinute_fn = function(currentDate, dateMathVerb, reverse) { + const minutes = __privateGet(this, _fields).minute.values; + const seconds = __privateGet(this, _fields).second.values; + const currentMinute = currentDate.getMinutes(); + const nextMinute = __privateGet(this, _fields).minute.findNearestValue(currentMinute, reverse); + if (nextMinute !== null) { + currentDate.setMinutes(nextMinute); + currentDate.setSeconds(__privateMethod(this, _CronExpression_instances, getMinOrMax_fn).call(this, seconds, reverse)); + return; + } + currentDate.applyDateOperation(dateMathVerb, CronDate_1.TimeUnit.Hour, __privateGet(this, _fields).hour.values.length); + currentDate.setMinutes(__privateMethod(this, _CronExpression_instances, getMinOrMax_fn).call(this, minutes, reverse)); + currentDate.setSeconds(__privateMethod(this, _CronExpression_instances, getMinOrMax_fn).call(this, seconds, reverse)); + }; + isLastWeekdayOfMonthMatch_fn = function(expressions, currentDate) { + const isLastWeekdayOfMonth = currentDate.isLastWeekdayOfMonth(); + return expressions.some((expression) => { + const weekday = parseInt(expression.toString().charAt(0), 10) % 7; + if (Number.isNaN(weekday)) { + throw new Error(`Invalid last weekday of the month expression: ${expression}`); + } + return currentDate.getDay() === weekday && isLastWeekdayOfMonth; + }); + }; + /** + * Determines if the given date matches the cron expression's day of month and day of week fields. + * + * The function checks the following rules: + * Rule 1: If both "day of month" and "day of week" are restricted (not wildcard), then one or both must match the current day. + * Rule 2: If "day of month" is restricted and "day of week" is not restricted, then "day of month" must match the current day. + * Rule 3: If "day of month" is a wildcard, "day of week" is not a wildcard, and "day of week" matches the current day, then the match is accepted. + * If none of the rules match, the match is rejected. + * + * @param {CronDate} currentDate - The current date to be evaluated against the cron expression. + * @returns {boolean} Returns true if the current date matches the cron expression's day of month and day of week fields, otherwise false. + * @memberof CronExpression + * @private + */ + matchDayOfMonth_fn = function(currentDate) { + var _a5, _b, _c2; + const isDayOfMonthWildcardMatch = __privateGet(this, _fields).dayOfMonth.isWildcard; + const isRestrictedDayOfMonth = !isDayOfMonthWildcardMatch; + const isDayOfWeekWildcardMatch = __privateGet(this, _fields).dayOfWeek.isWildcard; + const isRestrictedDayOfWeek = !isDayOfWeekWildcardMatch; + const matchedDOM = __privateMethod(_a5 = _CronExpression, _CronExpression_static, matchSchedule_fn).call(_a5, currentDate.getDate(), __privateGet(this, _fields).dayOfMonth.values) || __privateGet(this, _fields).dayOfMonth.hasLastChar && currentDate.isLastDayOfMonth(); + const matchedDOW = __privateMethod(_b = _CronExpression, _CronExpression_static, matchSchedule_fn).call(_b, currentDate.getDay(), __privateGet(this, _fields).dayOfWeek.values) || __privateGet(this, _fields).dayOfWeek.hasLastChar && __privateMethod(_c2 = _CronExpression, _CronExpression_static, isLastWeekdayOfMonthMatch_fn).call(_c2, __privateGet(this, _fields).dayOfWeek.values, currentDate); + if (isRestrictedDayOfMonth && isRestrictedDayOfWeek && (matchedDOM || matchedDOW)) { + return true; + } + if (matchedDOM && !isRestrictedDayOfWeek) { + return true; + } + if (isDayOfMonthWildcardMatch && !isDayOfWeekWildcardMatch && matchedDOW) { + return true; + } + return false; + }; + /** + * Determines if the current hour matches the cron expression. + * + * @param {CronDate} currentDate - The current date object. + * @param {DateMathOp} dateMathVerb - The date math operation enumeration value. + * @param {boolean} reverse - A flag indicating whether the matching should be done in reverse order. + * @returns {boolean} - True if the current hour matches the cron expression; otherwise, false. + */ + matchHour_fn = function(currentDate, dateMathVerb, reverse) { + var _a5, _b; + const hourValues = __privateGet(this, _fields).hour.values; + const hours = hourValues; + const currentHour = currentDate.getHours(); + const isMatch = __privateMethod(_a5 = _CronExpression, _CronExpression_static, matchSchedule_fn).call(_a5, currentHour, hourValues); + const isDstStart = currentDate.dstStart === currentHour; + const isDstEnd = currentDate.dstEnd === currentHour; + if (isDstStart) { + if (__privateMethod(_b = _CronExpression, _CronExpression_static, matchSchedule_fn).call(_b, currentHour - 1, hourValues)) { + return true; + } + currentDate.invokeDateOperation(dateMathVerb, CronDate_1.TimeUnit.Hour); + return false; + } + if (isDstEnd && !reverse) { + currentDate.dstEnd = null; + currentDate.applyDateOperation(CronDate_1.DateMathOp.Add, CronDate_1.TimeUnit.Hour, hours.length); + return false; + } + if (isMatch) { + return true; + } + currentDate.dstStart = null; + const nextHour = __privateGet(this, _fields).hour.findNearestValue(currentHour, reverse); + if (nextHour === null) { + currentDate.applyDateOperation(dateMathVerb, CronDate_1.TimeUnit.Day, hours.length); + return false; + } + if (__privateMethod(this, _CronExpression_instances, checkDstTransition_fn).call(this, currentDate)) { + const steps = reverse ? currentHour - nextHour : nextHour - currentHour; + for (let i = 0; i < steps; i++) { + currentDate.applyDateOperation(dateMathVerb, CronDate_1.TimeUnit.Hour, hours.length); + } + } else { + currentDate.setHours(nextHour); + } + currentDate.setMinutes(__privateMethod(this, _CronExpression_instances, getMinOrMax_fn).call(this, __privateGet(this, _fields).minute.values, reverse)); + currentDate.setSeconds(__privateMethod(this, _CronExpression_instances, getMinOrMax_fn).call(this, __privateGet(this, _fields).second.values, reverse)); + return false; + }; + /** + * Validates the current date against the start and end dates of the cron expression. + * If the current date is outside the specified time span, an error is thrown. + * + * @param currentDate {CronDate} - The current date to validate. + * @throws {Error} If the current date is outside the specified time span. + * @private + */ + validateTimeSpan_fn = function(currentDate) { + if (!__privateGet(this, _startDate) && !__privateGet(this, _endDate)) { + return; + } + const currentTime = currentDate.getTime(); + if (__privateGet(this, _startDate) && currentTime < __privateGet(this, _startDate).getTime()) { + throw new Error(exports.TIME_SPAN_OUT_OF_BOUNDS_ERROR_MESSAGE); + } + if (__privateGet(this, _endDate) && currentTime > __privateGet(this, _endDate).getTime()) { + throw new Error(exports.TIME_SPAN_OUT_OF_BOUNDS_ERROR_MESSAGE); + } + }; + /** + * Finds the next or previous schedule based on the cron expression. + * + * @param {boolean} [reverse=false] - If true, finds the previous schedule; otherwise, finds the next schedule. + * @returns {CronDate} - The next or previous schedule date. + * @private + */ + findSchedule_fn = function(reverse = false) { + var _a5, _b, _c2; + const dateMathVerb = reverse ? CronDate_1.DateMathOp.Subtract : CronDate_1.DateMathOp.Add; + const currentDate = new CronDate_1.CronDate(__privateGet(this, _currentDate)); + const startTimestamp = currentDate.getTime(); + let stepCount = 0; + while (++stepCount < LOOP_LIMIT) { + __privateMethod(this, _CronExpression_instances, validateTimeSpan_fn).call(this, currentDate); + if (!__privateMethod(this, _CronExpression_instances, matchDayOfMonth_fn).call(this, currentDate)) { + currentDate.applyDateOperation(dateMathVerb, CronDate_1.TimeUnit.Day, __privateGet(this, _fields).hour.values.length); + continue; + } + if (!(__privateGet(this, _fields).dayOfWeek.nthDay <= 0 || Math.ceil(currentDate.getDate() / 7) === __privateGet(this, _fields).dayOfWeek.nthDay)) { + currentDate.applyDateOperation(dateMathVerb, CronDate_1.TimeUnit.Day, __privateGet(this, _fields).hour.values.length); + continue; + } + if (!__privateMethod(_a5 = _CronExpression, _CronExpression_static, matchSchedule_fn).call(_a5, currentDate.getMonth() + 1, __privateGet(this, _fields).month.values)) { + currentDate.applyDateOperation(dateMathVerb, CronDate_1.TimeUnit.Month, __privateGet(this, _fields).hour.values.length); + continue; + } + if (!__privateMethod(this, _CronExpression_instances, matchHour_fn).call(this, currentDate, dateMathVerb, reverse)) { + continue; + } + if (!__privateMethod(_b = _CronExpression, _CronExpression_static, matchSchedule_fn).call(_b, currentDate.getMinutes(), __privateGet(this, _fields).minute.values)) { + __privateMethod(this, _CronExpression_instances, moveToNextMinute_fn).call(this, currentDate, dateMathVerb, reverse); + continue; + } + if (!__privateMethod(_c2 = _CronExpression, _CronExpression_static, matchSchedule_fn).call(_c2, currentDate.getSeconds(), __privateGet(this, _fields).second.values)) { + __privateMethod(this, _CronExpression_instances, moveToNextSecond_fn).call(this, currentDate, dateMathVerb, reverse); + continue; + } + if (startTimestamp === currentDate.getTime()) { + if (dateMathVerb === "Add" || currentDate.getMilliseconds() === 0) { + currentDate.applyDateOperation(dateMathVerb, CronDate_1.TimeUnit.Second, __privateGet(this, _fields).hour.values.length); + } + continue; + } + break; + } + if (stepCount > LOOP_LIMIT) { + throw new Error(exports.LOOPS_LIMIT_EXCEEDED_ERROR_MESSAGE); + } + if (currentDate.getMilliseconds() !== 0) { + currentDate.setMilliseconds(0); + } + __privateSet(this, _currentDate, currentDate); + return currentDate; + }; + __privateAdd(_CronExpression, _CronExpression_static); + var CronExpression = _CronExpression; + exports.CronExpression = CronExpression; + exports.default = CronExpression; + } +}); + +// node_modules/cron-parser/dist/utils/random.js +var require_random = __commonJS({ + "node_modules/cron-parser/dist/utils/random.js"(exports) { + "use strict"; + Object.defineProperty(exports, "__esModule", { value: true }); + exports.seededRandom = seededRandom; + function xfnv1a(str) { + let h2 = 2166136261 >>> 0; + for (let i = 0; i < str.length; i++) { + h2 ^= str.charCodeAt(i); + h2 = Math.imul(h2, 16777619); + } + return () => h2 >>> 0; + } + function mulberry32(seed) { + return () => { + let t = seed += 1831565813; + t = Math.imul(t ^ t >>> 15, t | 1); + t ^= t + Math.imul(t ^ t >>> 7, t | 61); + return ((t ^ t >>> 14) >>> 0) / 4294967296; + }; + } + function seededRandom(str) { + const seed = str ? xfnv1a(str)() : Math.floor(Math.random() * 1e10); + return mulberry32(seed); + } + } +}); + +// node_modules/cron-parser/dist/CronExpressionParser.js +var require_CronExpressionParser = __commonJS({ + "node_modules/cron-parser/dist/CronExpressionParser.js"(exports) { + "use strict"; + Object.defineProperty(exports, "__esModule", { value: true }); + exports.CronExpressionParser = exports.DayOfWeek = exports.Months = exports.CronUnit = exports.PredefinedExpressions = void 0; + var CronFieldCollection_1 = require_CronFieldCollection(); + var CronExpression_1 = require_CronExpression(); + var random_1 = require_random(); + var fields_1 = require_fields(); + var PredefinedExpressions; + (function(PredefinedExpressions2) { + PredefinedExpressions2["@yearly"] = "0 0 0 1 1 *"; + PredefinedExpressions2["@annually"] = "0 0 0 1 1 *"; + PredefinedExpressions2["@monthly"] = "0 0 0 1 * *"; + PredefinedExpressions2["@weekly"] = "0 0 0 * * 0"; + PredefinedExpressions2["@daily"] = "0 0 0 * * *"; + PredefinedExpressions2["@hourly"] = "0 0 * * * *"; + PredefinedExpressions2["@minutely"] = "0 * * * * *"; + PredefinedExpressions2["@secondly"] = "* * * * * *"; + PredefinedExpressions2["@weekdays"] = "0 0 0 * * 1-5"; + PredefinedExpressions2["@weekends"] = "0 0 0 * * 0,6"; + })(PredefinedExpressions || (exports.PredefinedExpressions = PredefinedExpressions = {})); + var CronUnit; + (function(CronUnit2) { + CronUnit2["Second"] = "Second"; + CronUnit2["Minute"] = "Minute"; + CronUnit2["Hour"] = "Hour"; + CronUnit2["DayOfMonth"] = "DayOfMonth"; + CronUnit2["Month"] = "Month"; + CronUnit2["DayOfWeek"] = "DayOfWeek"; + })(CronUnit || (exports.CronUnit = CronUnit = {})); + var Months; + (function(Months2) { + Months2[Months2["jan"] = 1] = "jan"; + Months2[Months2["feb"] = 2] = "feb"; + Months2[Months2["mar"] = 3] = "mar"; + Months2[Months2["apr"] = 4] = "apr"; + Months2[Months2["may"] = 5] = "may"; + Months2[Months2["jun"] = 6] = "jun"; + Months2[Months2["jul"] = 7] = "jul"; + Months2[Months2["aug"] = 8] = "aug"; + Months2[Months2["sep"] = 9] = "sep"; + Months2[Months2["oct"] = 10] = "oct"; + Months2[Months2["nov"] = 11] = "nov"; + Months2[Months2["dec"] = 12] = "dec"; + })(Months || (exports.Months = Months = {})); + var DayOfWeek; + (function(DayOfWeek2) { + DayOfWeek2[DayOfWeek2["sun"] = 0] = "sun"; + DayOfWeek2[DayOfWeek2["mon"] = 1] = "mon"; + DayOfWeek2[DayOfWeek2["tue"] = 2] = "tue"; + DayOfWeek2[DayOfWeek2["wed"] = 3] = "wed"; + DayOfWeek2[DayOfWeek2["thu"] = 4] = "thu"; + DayOfWeek2[DayOfWeek2["fri"] = 5] = "fri"; + DayOfWeek2[DayOfWeek2["sat"] = 6] = "sat"; + })(DayOfWeek || (exports.DayOfWeek = DayOfWeek = {})); + var _CronExpressionParser_static, getRawFields_fn, parseField_fn, parseWildcard_fn, parseHashed_fn, parseSequence_fn, parseRepeat_fn, validateRange_fn, validateRepeatInterval_fn, createRange_fn, parseRange_fn, parseNthDay_fn, isValidConstraintChar_fn; + var _CronExpressionParser = class _CronExpressionParser { + /** + * Parses a cron expression and returns a CronExpression object. + * @param {string} expression - The cron expression to parse. + * @param {CronExpressionOptions} [options={}] - The options to use when parsing the expression. + * @param {boolean} [options.strict=false] - If true, will throw an error if the expression contains both dayOfMonth and dayOfWeek. + * @param {CronDate} [options.currentDate=new CronDate(undefined, 'UTC')] - The date to use when calculating the next/previous occurrence. + * + * @returns {CronExpression} A CronExpression object. + */ + static parse(expression, options = {}) { + var _a5, _b, _c2, _d, _e3, _f, _g, _h; + const { strict = false, hashSeed } = options; + const rand = (0, random_1.seededRandom)(hashSeed); + expression = PredefinedExpressions[expression] || expression; + const rawFields = __privateMethod(_a5 = _CronExpressionParser, _CronExpressionParser_static, getRawFields_fn).call(_a5, expression, strict); + if (!(rawFields.dayOfMonth === "*" || rawFields.dayOfWeek === "*" || !strict)) { + throw new Error("Cannot use both dayOfMonth and dayOfWeek together in strict mode!"); + } + const second = __privateMethod(_b = _CronExpressionParser, _CronExpressionParser_static, parseField_fn).call(_b, CronUnit.Second, rawFields.second, fields_1.CronSecond.constraints, rand); + const minute = __privateMethod(_c2 = _CronExpressionParser, _CronExpressionParser_static, parseField_fn).call(_c2, CronUnit.Minute, rawFields.minute, fields_1.CronMinute.constraints, rand); + const hour = __privateMethod(_d = _CronExpressionParser, _CronExpressionParser_static, parseField_fn).call(_d, CronUnit.Hour, rawFields.hour, fields_1.CronHour.constraints, rand); + const month = __privateMethod(_e3 = _CronExpressionParser, _CronExpressionParser_static, parseField_fn).call(_e3, CronUnit.Month, rawFields.month, fields_1.CronMonth.constraints, rand); + const dayOfMonth = __privateMethod(_f = _CronExpressionParser, _CronExpressionParser_static, parseField_fn).call(_f, CronUnit.DayOfMonth, rawFields.dayOfMonth, fields_1.CronDayOfMonth.constraints, rand); + const { dayOfWeek: _dayOfWeek, nthDayOfWeek } = __privateMethod(_g = _CronExpressionParser, _CronExpressionParser_static, parseNthDay_fn).call(_g, rawFields.dayOfWeek); + const dayOfWeek = __privateMethod(_h = _CronExpressionParser, _CronExpressionParser_static, parseField_fn).call(_h, CronUnit.DayOfWeek, _dayOfWeek, fields_1.CronDayOfWeek.constraints, rand); + const fields = new CronFieldCollection_1.CronFieldCollection({ + second: new fields_1.CronSecond(second, { rawValue: rawFields.second }), + minute: new fields_1.CronMinute(minute, { rawValue: rawFields.minute }), + hour: new fields_1.CronHour(hour, { rawValue: rawFields.hour }), + dayOfMonth: new fields_1.CronDayOfMonth(dayOfMonth, { rawValue: rawFields.dayOfMonth }), + month: new fields_1.CronMonth(month, { rawValue: rawFields.month }), + dayOfWeek: new fields_1.CronDayOfWeek(dayOfWeek, { rawValue: rawFields.dayOfWeek, nthDayOfWeek }) + }); + return new CronExpression_1.CronExpression(fields, { ...options, expression }); + } + }; + _CronExpressionParser_static = new WeakSet(); + getRawFields_fn = function(expression, strict) { + if (strict && !expression.length) { + throw new Error("Invalid cron expression"); + } + expression = expression || "0 * * * * *"; + const atoms = expression.trim().split(/\s+/); + if (strict && atoms.length < 6) { + throw new Error("Invalid cron expression, expected 6 fields"); + } + if (atoms.length > 6) { + throw new Error("Invalid cron expression, too many fields"); + } + const defaults = ["*", "*", "*", "*", "*", "0"]; + if (atoms.length < defaults.length) { + atoms.unshift(...defaults.slice(atoms.length)); + } + const [second, minute, hour, dayOfMonth, month, dayOfWeek] = atoms; + return { second, minute, hour, dayOfMonth, month, dayOfWeek }; + }; + parseField_fn = function(field, value, constraints, rand) { + if (field === CronUnit.Month || field === CronUnit.DayOfWeek) { + value = value.replace(/[a-z]{3}/gi, (match) => { + match = match.toLowerCase(); + const replacer = Months[match] || DayOfWeek[match]; + if (replacer === void 0) { + throw new Error(`Validation error, cannot resolve alias "${match}"`); + } + return replacer.toString(); + }); + } + if (!constraints.validChars.test(value)) { + throw new Error(`Invalid characters, got value: ${value}`); + } + value = __privateMethod(this, _CronExpressionParser_static, parseWildcard_fn).call(this, value, constraints); + value = __privateMethod(this, _CronExpressionParser_static, parseHashed_fn).call(this, value, constraints, rand); + return __privateMethod(this, _CronExpressionParser_static, parseSequence_fn).call(this, field, value, constraints); + }; + parseWildcard_fn = function(value, constraints) { + return value.replace(/[*?]/g, constraints.min + "-" + constraints.max); + }; + parseHashed_fn = function(value, constraints, rand) { + const randomValue = rand(); + return value.replace(/H(?:\((\d+)-(\d+)\))?(?:\/(\d+))?/g, (_2, min, max, step) => { + if (min && max && step) { + const minNum = parseInt(min, 10); + const maxNum = parseInt(max, 10); + const stepNum = parseInt(step, 10); + if (minNum > maxNum) { + throw new Error(`Invalid range: ${minNum}-${maxNum}, min > max`); + } + if (stepNum <= 0) { + throw new Error(`Invalid step: ${stepNum}, must be positive`); + } + const minStart = Math.max(minNum, constraints.min); + const offset = Math.floor(randomValue * stepNum); + const values = []; + for (let i = Math.floor(minStart / stepNum) * stepNum + offset; i <= maxNum; i += stepNum) { + if (i >= minStart) { + values.push(i); + } + } + return values.join(","); + } else if (min && max) { + const minNum = parseInt(min, 10); + const maxNum = parseInt(max, 10); + if (minNum > maxNum) { + throw new Error(`Invalid range: ${minNum}-${maxNum}, min > max`); + } + return String(Math.floor(randomValue * (maxNum - minNum + 1)) + minNum); + } else if (step) { + const stepNum = parseInt(step, 10); + if (stepNum <= 0) { + throw new Error(`Invalid step: ${stepNum}, must be positive`); + } + const offset = Math.floor(randomValue * stepNum); + const values = []; + for (let i = Math.floor(constraints.min / stepNum) * stepNum + offset; i <= constraints.max; i += stepNum) { + if (i >= constraints.min) { + values.push(i); + } + } + return values.join(","); + } else { + return String(Math.floor(randomValue * (constraints.max - constraints.min + 1) + constraints.min)); + } + }); + }; + parseSequence_fn = function(field, val, constraints) { + const stack = []; + function handleResult(result, constraints2) { + var _a5; + if (Array.isArray(result)) { + stack.push(...result); + } else { + if (__privateMethod(_a5 = _CronExpressionParser, _CronExpressionParser_static, isValidConstraintChar_fn).call(_a5, constraints2, result)) { + stack.push(result); + } else { + const v2 = parseInt(result.toString(), 10); + const isValid = v2 >= constraints2.min && v2 <= constraints2.max; + if (!isValid) { + throw new Error(`Constraint error, got value ${result} expected range ${constraints2.min}-${constraints2.max}`); + } + stack.push(field === CronUnit.DayOfWeek ? v2 % 7 : result); + } + } + } + const atoms = val.split(","); + atoms.forEach((atom) => { + var _a5; + if (!(atom.length > 0)) { + throw new Error("Invalid list value format"); + } + handleResult(__privateMethod(_a5 = _CronExpressionParser, _CronExpressionParser_static, parseRepeat_fn).call(_a5, field, atom, constraints), constraints); + }); + return stack; + }; + parseRepeat_fn = function(field, val, constraints) { + var _a5, _b; + const atoms = val.split("/"); + if (atoms.length > 2) { + throw new Error(`Invalid repeat: ${val}`); + } + if (atoms.length === 2) { + if (!isNaN(parseInt(atoms[0], 10))) { + atoms[0] = `${atoms[0]}-${constraints.max}`; + } + return __privateMethod(_a5 = _CronExpressionParser, _CronExpressionParser_static, parseRange_fn).call(_a5, field, atoms[0], parseInt(atoms[1], 10), constraints); + } + return __privateMethod(_b = _CronExpressionParser, _CronExpressionParser_static, parseRange_fn).call(_b, field, val, 1, constraints); + }; + validateRange_fn = function(min, max, constraints) { + const isValid = !isNaN(min) && !isNaN(max) && min >= constraints.min && max <= constraints.max; + if (!isValid) { + throw new Error(`Constraint error, got range ${min}-${max} expected range ${constraints.min}-${constraints.max}`); + } + if (min > max) { + throw new Error(`Invalid range: ${min}-${max}, min(${min}) > max(${max})`); + } + }; + validateRepeatInterval_fn = function(repeatInterval) { + if (!(!isNaN(repeatInterval) && repeatInterval > 0)) { + throw new Error(`Constraint error, cannot repeat at every ${repeatInterval} time.`); + } + }; + createRange_fn = function(field, min, max, repeatInterval) { + const stack = []; + if (field === CronUnit.DayOfWeek && max % 7 === 0) { + stack.push(0); + } + for (let index = min; index <= max; index += repeatInterval) { + if (stack.indexOf(index) === -1) { + stack.push(index); + } + } + return stack; + }; + parseRange_fn = function(field, val, repeatInterval, constraints) { + const atoms = val.split("-"); + if (atoms.length <= 1) { + return isNaN(+val) ? val : +val; + } + const [min, max] = atoms.map((num) => parseInt(num, 10)); + __privateMethod(this, _CronExpressionParser_static, validateRange_fn).call(this, min, max, constraints); + __privateMethod(this, _CronExpressionParser_static, validateRepeatInterval_fn).call(this, repeatInterval); + return __privateMethod(this, _CronExpressionParser_static, createRange_fn).call(this, field, min, max, repeatInterval); + }; + parseNthDay_fn = function(val) { + const atoms = val.split("#"); + if (atoms.length <= 1) { + return { dayOfWeek: atoms[0] }; + } + const nthValue = +atoms[atoms.length - 1]; + const matches = val.match(/([,-/])/); + if (matches !== null) { + throw new Error(`Constraint error, invalid dayOfWeek \`#\` and \`${matches == null ? void 0 : matches[0]}\` special characters are incompatible`); + } + if (!(atoms.length <= 2 && !isNaN(nthValue) && nthValue >= 1 && nthValue <= 5)) { + throw new Error("Constraint error, invalid dayOfWeek occurrence number (#)"); + } + return { dayOfWeek: atoms[0], nthDayOfWeek: nthValue }; + }; + isValidConstraintChar_fn = function(constraints, value) { + return constraints.chars.some((char) => value.toString().includes(char)); + }; + __privateAdd(_CronExpressionParser, _CronExpressionParser_static); + var CronExpressionParser2 = _CronExpressionParser; + exports.CronExpressionParser = CronExpressionParser2; + } +}); + +// node_modules/cron-parser/dist/CronFileParser.js +var require_CronFileParser = __commonJS({ + "node_modules/cron-parser/dist/CronFileParser.js"(exports) { + "use strict"; + var __createBinding = exports && exports.__createBinding || (Object.create ? (function(o2, m, k, k2) { + if (k2 === void 0) k2 = k; + var desc = Object.getOwnPropertyDescriptor(m, k); + if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { + desc = { enumerable: true, get: function() { + return m[k]; + } }; + } + Object.defineProperty(o2, k2, desc); + }) : (function(o2, m, k, k2) { + if (k2 === void 0) k2 = k; + o2[k2] = m[k]; + })); + var __setModuleDefault = exports && exports.__setModuleDefault || (Object.create ? (function(o2, v2) { + Object.defineProperty(o2, "default", { enumerable: true, value: v2 }); + }) : function(o2, v2) { + o2["default"] = v2; + }); + var __importStar = exports && exports.__importStar || /* @__PURE__ */ (function() { + var ownKeys = function(o2) { + ownKeys = Object.getOwnPropertyNames || function(o3) { + var ar2 = []; + for (var k in o3) if (Object.prototype.hasOwnProperty.call(o3, k)) ar2[ar2.length] = k; + return ar2; + }; + return ownKeys(o2); + }; + return function(mod) { + if (mod && mod.__esModule) return mod; + var result = {}; + if (mod != null) { + for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); + } + __setModuleDefault(result, mod); + return result; + }; + })(); + Object.defineProperty(exports, "__esModule", { value: true }); + exports.CronFileParser = void 0; + var CronExpressionParser_1 = require_CronExpressionParser(); + var _CronFileParser_static, parseContent_fn, parseEntry_fn; + var _CronFileParser = class _CronFileParser { + /** + * Parse a crontab file asynchronously + * @param filePath Path to crontab file + * @returns Promise resolving to parse results + * @throws If file cannot be read + */ + static async parseFile(filePath) { + var _a5; + const { readFile } = await Promise.resolve().then(() => __importStar(require("fs/promises"))); + const data = await readFile(filePath, "utf8"); + return __privateMethod(_a5 = _CronFileParser, _CronFileParser_static, parseContent_fn).call(_a5, data); + } + /** + * Parse a crontab file synchronously + * @param filePath Path to crontab file + * @returns Parse results + * @throws If file cannot be read + */ + static parseFileSync(filePath) { + var _a5; + const { readFileSync: readFileSync2 } = require("fs"); + const data = readFileSync2(filePath, "utf8"); + return __privateMethod(_a5 = _CronFileParser, _CronFileParser_static, parseContent_fn).call(_a5, data); + } + }; + _CronFileParser_static = new WeakSet(); + parseContent_fn = function(data) { + var _a5; + const blocks = data.split("\n"); + const result = { + variables: {}, + expressions: [], + errors: {} + }; + for (const block of blocks) { + const entry = block.trim(); + if (entry.length === 0 || entry.startsWith("#")) { + continue; + } + const variableMatch = entry.match(/^(.*)=(.*)$/); + if (variableMatch) { + const [, key, value] = variableMatch; + result.variables[key] = value.replace(/["']/g, ""); + continue; + } + try { + const parsedEntry = __privateMethod(_a5 = _CronFileParser, _CronFileParser_static, parseEntry_fn).call(_a5, entry); + result.expressions.push(parsedEntry.interval); + } catch (err) { + result.errors[entry] = err; + } + } + return result; + }; + parseEntry_fn = function(entry) { + const atoms = entry.split(" "); + return { + interval: CronExpressionParser_1.CronExpressionParser.parse(atoms.slice(0, 5).join(" ")), + command: atoms.slice(5, atoms.length) + }; + }; + __privateAdd(_CronFileParser, _CronFileParser_static); + var CronFileParser = _CronFileParser; + exports.CronFileParser = CronFileParser; + } +}); + +// node_modules/cron-parser/dist/index.js +var require_dist = __commonJS({ + "node_modules/cron-parser/dist/index.js"(exports) { + "use strict"; + var __createBinding = exports && exports.__createBinding || (Object.create ? (function(o2, m, k, k2) { + if (k2 === void 0) k2 = k; + var desc = Object.getOwnPropertyDescriptor(m, k); + if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { + desc = { enumerable: true, get: function() { + return m[k]; + } }; + } + Object.defineProperty(o2, k2, desc); + }) : (function(o2, m, k, k2) { + if (k2 === void 0) k2 = k; + o2[k2] = m[k]; + })); + var __exportStar = exports && exports.__exportStar || function(m, exports2) { + for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports2, p)) __createBinding(exports2, m, p); + }; + Object.defineProperty(exports, "__esModule", { value: true }); + exports.CronFileParser = exports.CronExpressionParser = exports.CronExpression = exports.CronFieldCollection = exports.CronDate = void 0; + var CronExpressionParser_1 = require_CronExpressionParser(); + var CronDate_1 = require_CronDate(); + Object.defineProperty(exports, "CronDate", { enumerable: true, get: function() { + return CronDate_1.CronDate; + } }); + var CronFieldCollection_1 = require_CronFieldCollection(); + Object.defineProperty(exports, "CronFieldCollection", { enumerable: true, get: function() { + return CronFieldCollection_1.CronFieldCollection; + } }); + var CronExpression_1 = require_CronExpression(); + Object.defineProperty(exports, "CronExpression", { enumerable: true, get: function() { + return CronExpression_1.CronExpression; + } }); + var CronExpressionParser_2 = require_CronExpressionParser(); + Object.defineProperty(exports, "CronExpressionParser", { enumerable: true, get: function() { + return CronExpressionParser_2.CronExpressionParser; + } }); + var CronFileParser_1 = require_CronFileParser(); + Object.defineProperty(exports, "CronFileParser", { enumerable: true, get: function() { + return CronFileParser_1.CronFileParser; + } }); + __exportStar(require_fields(), exports); + exports.default = CronExpressionParser_1.CronExpressionParser; + } +}); // main.ts var main_exports = {}; @@ -37,7 +9148,7 @@ __export(main_exports, { default: () => ClaudeCliPlugin }); module.exports = __toCommonJS(main_exports); -var import_obsidian = require("obsidian"); +var import_obsidian2 = require("obsidian"); var import_child_process = require("child_process"); var fs2 = __toESM(require("fs")); var os2 = __toESM(require("os")); @@ -9439,6 +18550,411 @@ function detectNodeExecutable(configuredValue, platform, env, existsSync2, pathA return "node"; } +// automation.ts +var import_cron_parser = __toESM(require_dist()); +var FRONTMATTER_RE = /^---\s*\r?\n([\s\S]*?)\r?\n---\s*(?:\r?\n)?([\s\S]*)$/; +function splitFrontmatter(content) { + var _a5; + const match = FRONTMATTER_RE.exec(content); + if (!match) { + return { yaml: null, body: content }; + } + return { yaml: match[1], body: (_a5 = match[2]) != null ? _a5 : "" }; +} +function basenameWithoutExt(filePath) { + const lastSlash = filePath.lastIndexOf("/"); + const file = lastSlash >= 0 ? filePath.slice(lastSlash + 1) : filePath; + const dot = file.lastIndexOf("."); + return dot > 0 ? file.slice(0, dot) : file; +} +function parseAutomationFile(content, filePath, parseYaml2) { + const fallbackName = basenameWithoutExt(filePath); + const { yaml, body } = splitFrontmatter(content); + if (yaml === null) { + return { + ok: false, + error: { + path: filePath, + name: fallbackName, + reason: "Missing frontmatter block (expected `---` fenced YAML at top of file)." + } + }; + } + let parsed; + try { + parsed = parseYaml2(yaml); + } catch (err) { + return { + ok: false, + error: { + path: filePath, + name: fallbackName, + reason: `YAML parse error: ${err.message}` + } + }; + } + if (!parsed || typeof parsed !== "object") { + return { + ok: false, + error: { + path: filePath, + name: fallbackName, + reason: "Frontmatter must be a YAML mapping." + } + }; + } + const fm = parsed; + const hasInterval = fm.interval !== void 0 && fm.interval !== null; + const hasCron = typeof fm.cron === "string" && fm.cron.trim().length > 0; + if (hasInterval && hasCron) { + return { + ok: false, + error: { + path: filePath, + name: typeof fm.name === "string" && fm.name.trim() ? fm.name.trim() : fallbackName, + reason: "`interval` and `cron` are mutually exclusive \u2014 set only one." + } + }; + } + if (!hasInterval && !hasCron) { + return { + ok: false, + error: { + path: filePath, + name: typeof fm.name === "string" && fm.name.trim() ? fm.name.trim() : fallbackName, + reason: "Missing schedule: set either `interval` (minutes) or `cron`." + } + }; + } + let interval = null; + if (hasInterval) { + const raw = fm.interval; + if (typeof raw !== "number" || !Number.isFinite(raw) || !Number.isInteger(raw) || raw < 1) { + return { + ok: false, + error: { + path: filePath, + name: typeof fm.name === "string" && fm.name.trim() ? fm.name.trim() : fallbackName, + reason: "`interval` must be an integer >= 1 (minutes)." + } + }; + } + interval = raw; + } + let cron = null; + if (hasCron) { + const expr = fm.cron.trim(); + try { + import_cron_parser.CronExpressionParser.parse(expr); + } catch (err) { + return { + ok: false, + error: { + path: filePath, + name: typeof fm.name === "string" && fm.name.trim() ? fm.name.trim() : fallbackName, + reason: `Invalid cron expression: ${err.message}` + } + }; + } + cron = expr; + } + const trimmedBody = body.trim(); + if (!trimmedBody) { + return { + ok: false, + error: { + path: filePath, + name: typeof fm.name === "string" && fm.name.trim() ? fm.name.trim() : fallbackName, + reason: "Prompt body is empty (write the prompt below the frontmatter)." + } + }; + } + return { + ok: true, + entry: { + path: filePath, + name: typeof fm.name === "string" && fm.name.trim() ? fm.name.trim() : fallbackName, + enabled: typeof fm.enabled === "boolean" ? fm.enabled : true, + interval, + cron, + runtime: typeof fm.runtime === "string" && fm.runtime.trim() ? fm.runtime.trim() : null, + appendNewline: typeof fm.appendNewline === "boolean" ? fm.appendNewline : true, + body: trimmedBody + } + }; +} +function computeNextRun(entry, lastRun, now) { + if (entry.interval !== null) { + if (lastRun === null) { + return now; + } + return lastRun + entry.interval * 6e4; + } + if (entry.cron !== null) { + try { + const reference = lastRun != null ? lastRun : now - 1; + const it = import_cron_parser.CronExpressionParser.parse(entry.cron, { + currentDate: new Date(reference) + }); + return it.next().getTime(); + } catch (e) { + return null; + } + } + return null; +} +function describeSchedule(entry) { + if (entry.interval !== null) { + return entry.interval === 1 ? "every 1 min" : `every ${entry.interval} min`; + } + if (entry.cron !== null) { + return `cron: ${entry.cron}`; + } + return "no schedule"; +} +function pushHistory(history, record, limit) { + const next = [record, ...history]; + if (next.length > limit) { + next.length = limit; + } + return next; +} +function buildPromptPreview(body, maxLen = 120) { + const oneLine = body.replace(/\s+/g, " ").trim(); + if (oneLine.length <= maxLen) { + return oneLine; + } + return `${oneLine.slice(0, maxLen - 1)}\u2026`; +} + +// automations-modal.ts +var import_obsidian = require("obsidian"); +var AutomationsModal = class extends import_obsidian.Modal { + constructor(app, plugin) { + super(app); + this.currentTab = "automations"; + this.tabsHostEl = null; + this.bodyEl = null; + this.unsubscribe = null; + this.plugin = plugin; + } + onOpen() { + this.modalEl.addClass("any-ai-cli-automations-modal"); + this.titleEl.setText("Automations"); + this.contentEl.empty(); + this.tabsHostEl = this.contentEl.createDiv({ cls: "any-ai-cli-am-tabs" }); + this.bodyEl = this.contentEl.createDiv({ cls: "any-ai-cli-am-body" }); + this.renderTabs(); + this.renderBody(); + this.unsubscribe = this.plugin.onAutomationsChanged(() => { + this.renderBody(); + }); + } + onClose() { + var _a5; + (_a5 = this.unsubscribe) == null ? void 0 : _a5.call(this); + this.unsubscribe = null; + this.contentEl.empty(); + } + renderTabs() { + if (!this.tabsHostEl) return; + this.tabsHostEl.empty(); + const tabs = [ + { id: "automations", label: "Automations" }, + { id: "history", label: "History" } + ]; + for (const tab of tabs) { + const btn = this.tabsHostEl.createEl("button", { + text: tab.label, + cls: `any-ai-cli-am-tab${this.currentTab === tab.id ? " is-active" : ""}` + }); + btn.addEventListener("click", () => { + if (this.currentTab !== tab.id) { + this.currentTab = tab.id; + this.renderTabs(); + this.renderBody(); + } + }); + } + } + renderBody() { + if (!this.bodyEl) return; + this.bodyEl.empty(); + if (this.currentTab === "automations") { + this.renderAutomationsTab(this.bodyEl); + } else { + this.renderHistoryTab(this.bodyEl); + } + } + renderAutomationsTab(host) { + var _a5; + const folder = this.plugin.settings.automationsFolder.trim(); + if (!folder) { + host.createEl("p", { + text: "No automations folder configured. Set one in plugin settings to get started.", + cls: "any-ai-cli-am-empty" + }); + return; + } + const entries = this.plugin.getAutomations(); + const errors = this.plugin.getAutomationErrors(); + const enabledCount = entries.filter((e) => e.enabled).length; + const disabledCount = entries.length - enabledCount; + const info = host.createEl("p", { cls: "any-ai-cli-am-info" }); + info.setText( + `${entries.length} automation${entries.length === 1 ? "" : "s"} (${enabledCount} enabled, ${disabledCount} disabled) \u2014 folder: ${folder}` + ); + if (errors.length > 0) { + const errBox = host.createDiv({ cls: "any-ai-cli-am-errors" }); + errBox.createEl("strong", { text: `${errors.length} file${errors.length === 1 ? "" : "s"} could not be parsed:` }); + const list = errBox.createEl("ul"); + for (const err of errors) { + const item = list.createEl("li"); + item.createSpan({ text: err.path, cls: "any-ai-cli-am-error-path" }); + item.createSpan({ text: ` \u2014 ${err.reason}` }); + } + } + if (entries.length === 0) { + host.createEl("p", { + text: "No automations found in the configured folder.", + cls: "any-ai-cli-am-empty" + }); + return; + } + const runnable = this.isAnyViewRunning(); + const table = host.createEl("table", { cls: "any-ai-cli-am-table" }); + const thead = table.createEl("thead").createEl("tr"); + for (const label of ["Name", "Schedule", "Last run", "Next run", "Status", "Actions"]) { + thead.createEl("th", { text: label }); + } + const tbody = table.createEl("tbody"); + const now = Date.now(); + for (const entry of entries) { + const tr2 = tbody.createEl("tr"); + if (!entry.enabled) tr2.addClass("is-disabled"); + tr2.createEl("td", { text: entry.name }); + tr2.createEl("td", { text: describeSchedule(entry) }); + const lastRun = (_a5 = this.plugin.settings.automationsLastRun[entry.path]) != null ? _a5 : null; + tr2.createEl("td", { text: lastRun ? formatRelative(lastRun, now) : "never" }); + const nextRun = entry.enabled ? computeNextRun(entry, lastRun, now) : null; + tr2.createEl("td", { + text: nextRun === null ? "\u2014" : nextRun <= now ? "due now" : formatRelative(nextRun, now) + }); + const statusCell = tr2.createEl("td"); + const badge = statusCell.createSpan({ + text: entry.enabled ? "enabled" : "disabled", + cls: `any-ai-cli-am-badge ${entry.enabled ? "is-enabled" : "is-disabled"}` + }); + if (entry.runtime) { + badge.setAttribute("title", `Requires runtime "${entry.runtime}"`); + } + const actionsCell = tr2.createEl("td", { cls: "any-ai-cli-am-actions" }); + const runBtn = actionsCell.createEl("button", { text: "Run now" }); + runBtn.disabled = !runnable; + if (!runnable) { + runBtn.setAttribute("title", "Start a CLI in the sidebar panel first."); + } + runBtn.addEventListener("click", () => { + void this.plugin.triggerAutomation(entry, "manual"); + }); + const openBtn = actionsCell.createEl("button", { text: "Open" }); + (0, import_obsidian.setIcon)(openBtn.createSpan(), "external-link"); + openBtn.addEventListener("click", () => { + void this.app.workspace.openLinkText(entry.path, "", true); + this.close(); + }); + } + } + renderHistoryTab(host) { + const history = this.plugin.settings.automationsHistory; + const toolbar = host.createDiv({ cls: "any-ai-cli-am-history-toolbar" }); + toolbar.createEl("p", { + text: `${history.length} entr${history.length === 1 ? "y" : "ies"} (most recent first, capped at ${this.plugin.settings.automationsHistoryLimit}).`, + cls: "any-ai-cli-am-info" + }); + const actions = toolbar.createDiv({ cls: "any-ai-cli-am-history-actions" }); + const clearBtn = actions.createEl("button", { text: "Clear history" }); + clearBtn.disabled = history.length === 0; + clearBtn.addEventListener("click", () => { + this.plugin.clearAutomationHistory(); + new import_obsidian.Notice("Automation history cleared.", 2500); + }); + const exportBtn = actions.createEl("button", { text: "Export as markdown" }); + exportBtn.disabled = history.length === 0; + exportBtn.addEventListener("click", () => { + void this.exportHistory(history); + }); + if (history.length === 0) { + host.createEl("p", { text: "No runs recorded yet.", cls: "any-ai-cli-am-empty" }); + return; + } + const list = host.createEl("ul", { cls: "any-ai-cli-am-history-list" }); + for (const record of history) { + const li2 = list.createEl("li", { cls: "any-ai-cli-am-history-item" }); + li2.createSpan({ text: formatDateTime(record.ts), cls: "any-ai-cli-am-history-ts" }); + li2.createSpan({ text: record.name, cls: "any-ai-cli-am-history-name" }); + li2.createSpan({ + text: record.status, + cls: `any-ai-cli-am-badge is-${record.status}` + }); + li2.createSpan({ text: record.source, cls: "any-ai-cli-am-history-source" }); + const detail = record.reason || record.promptPreview || ""; + if (detail) { + li2.createSpan({ text: detail, cls: "any-ai-cli-am-history-detail" }); + } + } + } + isAnyViewRunning() { + const leaves = this.app.workspace.getLeavesOfType("claude-cli-view"); + return leaves.some((leaf) => { + const view = leaf.view; + return typeof (view == null ? void 0 : view.isProcessRunning) === "function" && view.isProcessRunning(); + }); + } + async exportHistory(history) { + const lines = []; + lines.push(`# Automations history \u2014 ${(/* @__PURE__ */ new Date()).toISOString()}`, ""); + lines.push("| Time | Name | Status | Source | Detail | Path |"); + lines.push("| --- | --- | --- | --- | --- | --- |"); + for (const r of history) { + const detail = (r.reason || r.promptPreview || "").replace(/\|/g, "\\|"); + lines.push( + `| ${formatDateTime(r.ts)} | ${r.name.replace(/\|/g, "\\|")} | ${r.status} | ${r.source} | ${detail} | ${r.path} |` + ); + } + const fileName = `automations-history-${(/* @__PURE__ */ new Date()).toISOString().slice(0, 10)}.md`; + try { + const file = await this.app.vault.create(fileName, lines.join("\n")); + await this.app.workspace.openLinkText(file.path, "", true); + this.close(); + } catch (err) { + new import_obsidian.Notice(`Export failed: ${err.message}`, 5e3); + } + } +}; +function pad(n) { + return n < 10 ? `0${n}` : `${n}`; +} +function formatDateTime(ts2) { + const d = new Date(ts2); + return `${d.getFullYear()}-${pad(d.getMonth() + 1)}-${pad(d.getDate())} ${pad(d.getHours())}:${pad(d.getMinutes())}:${pad(d.getSeconds())}`; +} +function formatRelative(ts2, now) { + const diff = ts2 - now; + const absMs = Math.abs(diff); + const sec = Math.round(absMs / 1e3); + const min = Math.round(sec / 60); + const hr3 = Math.round(min / 60); + const day = Math.round(hr3 / 24); + const arrow = diff < 0 ? "ago" : "in"; + let value; + if (sec < 60) value = `${sec}s`; + else if (min < 60) value = `${min}m`; + else if (hr3 < 24) value = `${hr3}h`; + else value = `${day}d`; + return diff < 0 ? `${value} ${arrow}` : `${arrow} ${value}`; +} + // main.ts var VIEW_TYPE_CLAUDE = "claude-cli-view"; var CODEX_DEFAULT_COMMAND = "codex --no-alt-screen -c check_for_update_on_startup=false -c hide_full_access_warning=true -c hide_world_writable_warning=true -c hide_rate_limit_model_nudge=true"; @@ -9451,12 +18967,17 @@ var DEFAULT_SETTINGS = { selectedRuntimeId: "claude", autoRestartOnRuntimeSwitch: true, autoStart: true, - nodeExecutable: "auto" + nodeExecutable: "auto", + automationsFolder: "", + automationsLastRun: {}, + automationsHistory: [], + automationsHistoryLimit: 200 }; +var AUTOMATION_TICK_MS = 3e4; function cloneDefaultRuntimes() { return DEFAULT_RUNTIMES.map((runtime) => ({ ...runtime })); } -var ClaudeCliView = class extends import_obsidian.ItemView { +var ClaudeCliView = class extends import_obsidian2.ItemView { constructor(leaf, plugin) { super(leaf); this.terminal = null; @@ -9486,12 +19007,12 @@ var ClaudeCliView = class extends import_obsidian.ItemView { const primaryRowEl = toolbarEl.createDiv({ cls: "claude-cli-toolbar-row" }); const runtimePickerEl = primaryRowEl.createDiv({ cls: "claude-cli-runtime-picker" }); const runtimeIconEl = runtimePickerEl.createSpan({ cls: "claude-cli-runtime-picker-icon" }); - (0, import_obsidian.setIcon)(runtimeIconEl, "terminal"); + (0, import_obsidian2.setIcon)(runtimeIconEl, "terminal"); this.runtimeSelect = runtimePickerEl.createEl("select", { cls: "claude-cli-runtime-select" }); this.runtimeSelect.setAttribute("aria-label", "Select runtime"); this.refreshRuntimeSelect(); const runtimeChevronEl = runtimePickerEl.createSpan({ cls: "claude-cli-runtime-picker-chevron" }); - (0, import_obsidian.setIcon)(runtimeChevronEl, "chevron-down"); + (0, import_obsidian2.setIcon)(runtimeChevronEl, "chevron-down"); const startBtn = primaryRowEl.createEl("button", { text: "Start" }); const stopBtn = primaryRowEl.createEl("button", { text: "Stop" }); const restartBtn = primaryRowEl.createEl("button", { text: "Restart" }); @@ -9505,10 +19026,13 @@ var ClaudeCliView = class extends import_obsidian.ItemView { const secondaryRowEl = toolbarEl.createDiv({ cls: "claude-cli-toolbar-row" }); const mentionBtn = secondaryRowEl.createEl("button", { text: "@active file" }); const folderMentionBtn = secondaryRowEl.createEl("button", { text: "@active folder" }); + const automationsBtn = secondaryRowEl.createEl("button", { text: "Automations" }); this.setButtonIcon(mentionBtn, "file-plus", "@active file"); this.setButtonIcon(folderMentionBtn, "folder-plus", "@active folder"); + this.setButtonIcon(automationsBtn, "calendar-clock", "Automations"); mentionBtn.addClass("claude-cli-btn-info"); folderMentionBtn.addClass("claude-cli-btn-info"); + automationsBtn.addClass("claude-cli-btn-info"); this.statusEl = this.contentEl.createDiv({ cls: "claude-cli-status" }); startBtn.addEventListener("click", () => this.startClaudeProcess()); stopBtn.addEventListener("click", () => this.stopClaudeProcess()); @@ -9519,6 +19043,9 @@ var ClaudeCliView = class extends import_obsidian.ItemView { }); mentionBtn.addEventListener("click", () => this.insertActiveFileMention()); folderMentionBtn.addEventListener("click", () => this.insertActiveFolderMention()); + automationsBtn.addEventListener("click", () => { + new AutomationsModal(this.app, this.plugin).open(); + }); this.runtimeSelect.addEventListener("change", () => { if (this.runtimeSelect) { this.setRuntime(this.runtimeSelect.value); @@ -9565,6 +19092,25 @@ var ClaudeCliView = class extends import_obsidian.ItemView { } return Promise.resolve(); } + isProcessRunning() { + return this.processHandle !== null && this.runningRuntimeId !== null; + } + getRunningRuntimeId() { + return this.runningRuntimeId; + } + matchesRuntime(target) { + if (!this.runningRuntimeId) return false; + if (this.runningRuntimeId === target) return true; + const runtime = this.plugin.settings.runtimes.find((r) => r.id === this.runningRuntimeId); + return runtime ? runtime.name.trim().toLowerCase() === target.trim().toLowerCase() : false; + } + sendAutomationPrompt(text) { + if (!this.processHandle) { + throw new Error("CLI process is not running"); + } + this.processHandle.write(text); + this.writeSystemLine(`[Automation prompt injected]`); + } refreshRuntimeSelect() { if (!this.runtimeSelect) { return; @@ -9607,7 +19153,7 @@ var ClaudeCliView = class extends import_obsidian.ItemView { const message = "No runtime configured. Add one in plugin settings."; this.writeSystemLine(`[${message}]`); this.setStatus(message); - new import_obsidian.Notice(message, 6e3); + new import_obsidian2.Notice(message, 6e3); return; } const targetLabel = targetRuntime.name || "(Unnamed runtime)"; @@ -9627,7 +19173,7 @@ var ClaudeCliView = class extends import_obsidian.ItemView { const message = `Runtime "${targetLabel}" has an empty command. Set one in plugin settings.`; this.writeSystemLine(`[${message}]`); this.setStatus(message); - new import_obsidian.Notice(message, 6e3); + new import_obsidian2.Notice(message, 6e3); return; } const codexLike = isCodexLikeCommand(command); @@ -9642,14 +19188,14 @@ var ClaudeCliView = class extends import_obsidian.ItemView { const message = `Unable to resolve current vault path. ${targetLabel} was not started.`; this.writeSystemLine(`[${message}]`); this.setStatus(message); - new import_obsidian.Notice(message, 6e3); + new import_obsidian2.Notice(message, 6e3); return; } if (!fs2.existsSync(vaultPath)) { const message = `Vault path does not exist: ${vaultPath}`; this.writeSystemLine(`[${message}]`); this.setStatus(message); - new import_obsidian.Notice(message, 6e3); + new import_obsidian2.Notice(message, 6e3); return; } const shellEnv = getShellEnv(); @@ -9674,7 +19220,7 @@ var ClaudeCliView = class extends import_obsidian.ItemView { const message = `Failed to start process: ${error.message}`; this.writeSystemLine(`[${message}]`); this.setStatus(message); - new import_obsidian.Notice(message, 7e3); + new import_obsidian2.Notice(message, 7e3); this.processHandle = null; this.runningRuntimeId = null; return; @@ -9713,7 +19259,7 @@ var ClaudeCliView = class extends import_obsidian.ItemView { const message = `Failed to stop process: ${error.message}`; this.writeSystemLine(`[${message}]`); this.setStatus(message); - new import_obsidian.Notice(message, 6e3); + new import_obsidian2.Notice(message, 6e3); } } setStatus(message) { @@ -9796,14 +19342,14 @@ var ClaudeCliView = class extends import_obsidian.ItemView { const message = "No active file detected."; this.writeSystemLine(`[${message}]`); this.setStatus(message); - new import_obsidian.Notice(message, 4e3); + new import_obsidian2.Notice(message, 4e3); return; } if (!this.processHandle) { const message = `${this.getRuntimeLabel()} process is not running. Start it before inserting a file mention.`; this.writeSystemLine(`[${message}]`); this.setStatus(message); - new import_obsidian.Notice(message, 5e3); + new import_obsidian2.Notice(message, 5e3); return; } const mention = formatActiveFileMention(activeFile.path); @@ -9818,14 +19364,14 @@ var ClaudeCliView = class extends import_obsidian.ItemView { const message = "No active file detected."; this.writeSystemLine(`[${message}]`); this.setStatus(message); - new import_obsidian.Notice(message, 4e3); + new import_obsidian2.Notice(message, 4e3); return; } if (!this.processHandle) { const message = `${this.getRuntimeLabel()} process is not running. Start it before inserting a folder mention.`; this.writeSystemLine(`[${message}]`); this.setStatus(message); - new import_obsidian.Notice(message, 5e3); + new import_obsidian2.Notice(message, 5e3); return; } const mention = formatActiveFolderMention(activeFile.path); @@ -9844,11 +19390,17 @@ var ClaudeCliView = class extends import_obsidian.ItemView { buttonEl.empty(); buttonEl.addClass("claude-cli-btn"); const iconEl = buttonEl.createSpan({ cls: "claude-cli-btn-icon" }); - (0, import_obsidian.setIcon)(iconEl, iconName); + (0, import_obsidian2.setIcon)(iconEl, iconName); buttonEl.createSpan({ text: label }); } }; -var ClaudeCliPlugin = class extends import_obsidian.Plugin { +var ClaudeCliPlugin = class extends import_obsidian2.Plugin { + constructor() { + super(...arguments); + this.automationEntries = /* @__PURE__ */ new Map(); + this.automationErrors = /* @__PURE__ */ new Map(); + this.automationListeners = /* @__PURE__ */ new Set(); + } async onload() { await this.loadSettings(); this.registerView(VIEW_TYPE_CLAUDE, (leaf) => new ClaudeCliView(leaf, this)); @@ -9863,9 +19415,188 @@ var ClaudeCliPlugin = class extends import_obsidian.Plugin { } }); this.addSettingTab(new ClaudeCliSettingTab(this.app, this)); + this.app.workspace.onLayoutReady(() => { + this.loadAutomations(); + this.runAutomationTick(); + }); + const refreshOnVaultChange = (file) => { + const folder = this.settings.automationsFolder; + if (!folder || !file) return; + if (file.path === folder || file.path.startsWith(`${folder}/`)) { + this.loadAutomations(); + } + }; + this.registerEvent(this.app.vault.on("create", refreshOnVaultChange)); + this.registerEvent(this.app.vault.on("modify", refreshOnVaultChange)); + this.registerEvent(this.app.vault.on("delete", refreshOnVaultChange)); + this.registerEvent( + this.app.vault.on("rename", (file, oldPath) => { + refreshOnVaultChange(file); + const folder = this.settings.automationsFolder; + if (folder && (oldPath === folder || oldPath.startsWith(`${folder}/`))) { + this.loadAutomations(); + } + }) + ); + this.registerInterval(activeWindow.setInterval(() => this.runAutomationTick(), AUTOMATION_TICK_MS)); } onunload() { } + getAutomations() { + return Array.from(this.automationEntries.values()).sort( + (a, b2) => a.name.localeCompare(b2.name) + ); + } + getAutomationErrors() { + return Array.from(this.automationErrors.values()); + } + onAutomationsChanged(listener) { + this.automationListeners.add(listener); + return () => this.automationListeners.delete(listener); + } + notifyAutomationsChanged() { + this.automationListeners.forEach((listener) => { + try { + listener(); + } catch (e) { + } + }); + } + loadAutomations() { + this.automationEntries.clear(); + this.automationErrors.clear(); + const folderPath = this.settings.automationsFolder.trim(); + if (!folderPath) { + this.notifyAutomationsChanged(); + return; + } + const folder = this.app.vault.getFolderByPath(folderPath); + if (!folder) { + this.notifyAutomationsChanged(); + return; + } + const files = []; + const walk = (f) => { + for (const child of f.children) { + if (child instanceof import_obsidian2.TFile && child.extension === "md") { + files.push(child); + } else if (child instanceof import_obsidian2.TFolder) { + walk(child); + } + } + }; + walk(folder); + void Promise.all( + files.map(async (file) => { + try { + const content = await this.app.vault.cachedRead(file); + const result = parseAutomationFile(content, file.path, (yaml) => (0, import_obsidian2.parseYaml)(yaml)); + if (result.ok) { + this.automationEntries.set(file.path, result.entry); + } else { + this.automationErrors.set(file.path, result.error); + } + } catch (err) { + this.automationErrors.set(file.path, { + path: file.path, + name: file.basename, + reason: `Read error: ${err.message}` + }); + } + }) + ).then(() => { + this.notifyAutomationsChanged(); + }); + } + runAutomationTick() { + var _a5; + if (this.automationEntries.size === 0) return; + const now = Date.now(); + for (const entry of this.automationEntries.values()) { + if (!entry.enabled) continue; + const last = (_a5 = this.settings.automationsLastRun[entry.path]) != null ? _a5 : null; + const next = computeNextRun(entry, last, now); + if (next !== null && next <= now) { + void this.triggerAutomation(entry, "scheduler"); + } + } + } + async triggerAutomation(entry, source) { + const now = Date.now(); + const baseRecord = { + ts: now, + path: entry.path, + name: entry.name, + source + }; + const view = this.findRunnableView(); + if (!view) { + const reason = "No CLI is running"; + this.recordHistory({ ...baseRecord, status: "skipped", reason }); + if (source === "manual") { + new import_obsidian2.Notice(`Automation "${entry.name}" skipped \u2014 ${reason.toLowerCase()}.`, 5e3); + } + return; + } + if (entry.runtime) { + const runtimeMatches = view.matchesRuntime(entry.runtime); + if (!runtimeMatches) { + const reason = `Runtime mismatch (expected "${entry.runtime}")`; + this.recordHistory({ ...baseRecord, status: "skipped", reason }); + if (source === "manual") { + new import_obsidian2.Notice(`Automation "${entry.name}" skipped \u2014 ${reason.toLowerCase()}.`, 5e3); + } + return; + } + } + try { + const text = entry.body + (entry.appendNewline ? "\n" : ""); + view.sendAutomationPrompt(text); + const runtimeId = view.getRunningRuntimeId(); + this.recordHistory({ + ...baseRecord, + status: "ran", + runtimeId, + promptPreview: buildPromptPreview(entry.body) + }); + this.settings.automationsLastRun[entry.path] = now; + await this.saveSettings(); + if (source === "manual") { + new import_obsidian2.Notice(`Automation "${entry.name}" sent.`, 3e3); + } + } catch (err) { + this.recordHistory({ + ...baseRecord, + status: "error", + reason: err.message + }); + new import_obsidian2.Notice(`Automation "${entry.name}" failed: ${err.message}`, 6e3); + } + } + findRunnableView() { + const leaves = this.app.workspace.getLeavesOfType(VIEW_TYPE_CLAUDE); + for (const leaf of leaves) { + const view = leaf.view; + if (view instanceof ClaudeCliView && view.isProcessRunning()) { + return view; + } + } + return null; + } + recordHistory(record) { + this.settings.automationsHistory = pushHistory( + this.settings.automationsHistory, + record, + this.settings.automationsHistoryLimit + ); + void this.saveSettings(); + this.notifyAutomationsChanged(); + } + clearAutomationHistory() { + this.settings.automationsHistory = []; + void this.saveSettings(); + this.notifyAutomationsChanged(); + } async activateView() { var _a5; const { workspace } = this.app; @@ -9891,7 +19622,11 @@ var ClaudeCliPlugin = class extends import_obsidian.Plugin { selectedRuntimeId, autoRestartOnRuntimeSwitch: typeof raw.autoRestartOnRuntimeSwitch === "boolean" ? raw.autoRestartOnRuntimeSwitch : DEFAULT_SETTINGS.autoRestartOnRuntimeSwitch, autoStart: typeof raw.autoStart === "boolean" ? raw.autoStart : DEFAULT_SETTINGS.autoStart, - nodeExecutable: typeof raw.nodeExecutable === "string" && raw.nodeExecutable.trim() ? raw.nodeExecutable : DEFAULT_SETTINGS.nodeExecutable + nodeExecutable: typeof raw.nodeExecutable === "string" && raw.nodeExecutable.trim() ? raw.nodeExecutable : DEFAULT_SETTINGS.nodeExecutable, + automationsFolder: typeof raw.automationsFolder === "string" ? raw.automationsFolder : DEFAULT_SETTINGS.automationsFolder, + automationsLastRun: sanitizeLastRun(raw.automationsLastRun), + automationsHistory: sanitizeHistory(raw.automationsHistory), + automationsHistoryLimit: typeof raw.automationsHistoryLimit === "number" && Number.isInteger(raw.automationsHistoryLimit) && raw.automationsHistoryLimit > 0 ? raw.automationsHistoryLimit : DEFAULT_SETTINGS.automationsHistoryLimit }; } async saveSettings() { @@ -9906,7 +19641,7 @@ var ClaudeCliPlugin = class extends import_obsidian.Plugin { }); } }; -var ClaudeCliSettingTab = class extends import_obsidian.PluginSettingTab { +var ClaudeCliSettingTab = class extends import_obsidian2.PluginSettingTab { constructor(app, plugin) { super(app, plugin); this.plugin = plugin; @@ -9914,7 +19649,7 @@ var ClaudeCliSettingTab = class extends import_obsidian.PluginSettingTab { display() { const { containerEl } = this; containerEl.empty(); - new import_obsidian.Setting(containerEl).setName("Default runtime").setDesc("Runtime selected by default when opening the panel (and used by auto-start).").addDropdown((dropdown) => { + new import_obsidian2.Setting(containerEl).setName("Default runtime").setDesc("Runtime selected by default when opening the panel (and used by auto-start).").addDropdown((dropdown) => { const runtimes = this.plugin.settings.runtimes; if (runtimes.length === 0) { dropdown.addOption("", "No runtime configured"); @@ -9937,19 +19672,19 @@ var ClaudeCliSettingTab = class extends import_obsidian.PluginSettingTab { this.plugin.notifyRuntimesChanged(); }); }); - new import_obsidian.Setting(containerEl).setName("Auto-start").setDesc("Automatically start the selected default runtime when the panel opens.").addToggle( + new import_obsidian2.Setting(containerEl).setName("Auto-start").setDesc("Automatically start the selected default runtime when the panel opens.").addToggle( (toggle) => toggle.setValue(this.plugin.settings.autoStart).onChange(async (value) => { this.plugin.settings.autoStart = value; await this.plugin.saveSettings(); }) ); - new import_obsidian.Setting(containerEl).setName("Auto-restart on runtime switch").setDesc("Automatically restart the running process when changing the runtime from the sidebar dropdown.").addToggle( + new import_obsidian2.Setting(containerEl).setName("Auto-restart on runtime switch").setDesc("Automatically restart the running process when changing the runtime from the sidebar dropdown.").addToggle( (toggle) => toggle.setValue(this.plugin.settings.autoRestartOnRuntimeSwitch).onChange(async (value) => { this.plugin.settings.autoRestartOnRuntimeSwitch = value; await this.plugin.saveSettings(); }) ); - new import_obsidian.Setting(containerEl).setName("Runtimes").setHeading(); + new import_obsidian2.Setting(containerEl).setName("Runtimes").setHeading(); containerEl.createEl("p", { text: "Configure the runtimes that show up in the sidebar dropdown. Each entry needs a display name and a launch command. Add as many as you want.", cls: "setting-item-description" @@ -9962,7 +19697,7 @@ var ClaudeCliSettingTab = class extends import_obsidian.PluginSettingTab { }); } this.plugin.settings.runtimes.forEach((runtime, index) => { - const setting = new import_obsidian.Setting(runtimesListEl).setClass("claude-cli-runtime-item"); + const setting = new import_obsidian2.Setting(runtimesListEl).setClass("claude-cli-runtime-item"); setting.infoEl.detach(); setting.addText( (text) => text.setPlaceholder("Name").setValue(runtime.name).onChange(async (value) => { @@ -9979,7 +19714,7 @@ var ClaudeCliSettingTab = class extends import_obsidian.PluginSettingTab { }).addExtraButton( (btn) => btn.setIcon("trash-2").setTooltip("Remove runtime").onClick(async () => { if (this.plugin.settings.runtimes.length <= 1) { - new import_obsidian.Notice("Keep at least one runtime configured.", 4e3); + new import_obsidian2.Notice("Keep at least one runtime configured.", 4e3); return; } const removed = this.plugin.settings.runtimes.splice(index, 1)[0]; @@ -9992,7 +19727,7 @@ var ClaudeCliSettingTab = class extends import_obsidian.PluginSettingTab { }) ); }); - new import_obsidian.Setting(containerEl).addButton( + new import_obsidian2.Setting(containerEl).addButton( (btn) => btn.setButtonText("Add runtime").setIcon("plus").onClick(async () => { this.plugin.settings.runtimes.push({ id: defaultGenerateRuntimeId(), @@ -10004,8 +19739,26 @@ var ClaudeCliSettingTab = class extends import_obsidian.PluginSettingTab { this.display(); }) ); - new import_obsidian.Setting(containerEl).setName("Advanced").setHeading(); - new import_obsidian.Setting(containerEl).setName("Node executable").setDesc("Path to the node binary used by the proxy. Leave as 'auto' for automatic detection.").addText( + new import_obsidian2.Setting(containerEl).setName("Automations").setHeading(); + containerEl.createEl("p", { + text: "Folder containing prompt automations. Each Markdown file is one automation (frontmatter sets the schedule; body is the prompt sent to the running CLI). See readme for the file format.", + cls: "setting-item-description" + }); + new import_obsidian2.Setting(containerEl).setName("Automations folder").setDesc("Vault-relative path. Leave empty to disable automations.").addText( + (text) => text.setPlaceholder("Automations").setValue(this.plugin.settings.automationsFolder).onChange(async (value) => { + this.plugin.settings.automationsFolder = value.trim(); + await this.plugin.saveSettings(); + this.plugin.loadAutomations(); + }) + ); + new import_obsidian2.Setting(containerEl).setName("Reload automations").setDesc("Force a re-scan of the automations folder (otherwise scans happen on vault changes).").addButton( + (btn) => btn.setButtonText("Reload now").setIcon("refresh-cw").onClick(() => { + this.plugin.loadAutomations(); + new import_obsidian2.Notice("Automations reloaded.", 2500); + }) + ); + new import_obsidian2.Setting(containerEl).setName("Advanced").setHeading(); + new import_obsidian2.Setting(containerEl).setName("Node executable").setDesc("Path to the node binary used by the proxy. Leave as 'auto' for automatic detection.").addText( (text) => text.setPlaceholder("Auto").setValue(this.plugin.settings.nodeExecutable).onChange(async (value) => { this.plugin.settings.nodeExecutable = value.trim() || "auto"; await this.plugin.saveSettings(); @@ -10018,7 +19771,7 @@ function resolvePluginDir2(pluginDir, vaultBasePath, path2) { } function getVaultBasePath(app) { const adapter = app.vault.adapter; - if (adapter instanceof import_obsidian.FileSystemAdapter) { + if (adapter instanceof import_obsidian2.FileSystemAdapter) { return adapter.getBasePath(); } return void 0; @@ -10235,6 +19988,42 @@ function spawnPtyProxy(params) { stdio: ["pipe", "pipe", "pipe", "ipc"] }); } +function sanitizeLastRun(raw) { + if (!raw || typeof raw !== "object") { + return {}; + } + const out = {}; + for (const [key, value] of Object.entries(raw)) { + if (typeof key === "string" && key && typeof value === "number" && Number.isFinite(value)) { + out[key] = value; + } + } + return out; +} +function sanitizeHistory(raw) { + if (!Array.isArray(raw)) { + return []; + } + const out = []; + for (const entry of raw) { + if (!entry || typeof entry !== "object") continue; + const r = entry; + if (typeof r.ts !== "number" || typeof r.path !== "string" || typeof r.name !== "string" || r.source !== "scheduler" && r.source !== "manual" || r.status !== "ran" && r.status !== "skipped" && r.status !== "error") { + continue; + } + out.push({ + ts: r.ts, + path: r.path, + name: r.name, + source: r.source, + status: r.status, + reason: typeof r.reason === "string" ? r.reason : void 0, + runtimeId: typeof r.runtimeId === "string" ? r.runtimeId : null, + promptPreview: typeof r.promptPreview === "string" ? r.promptPreview : void 0 + }); + } + return out; +} function makeProxyAdapter(handle) { var _a5, _b; const dataCallbacks = []; diff --git a/main.ts b/main.ts index 3259d3b..f6bfe6e 100644 --- a/main.ts +++ b/main.ts @@ -1,4 +1,4 @@ -import { App, FileSystemAdapter, ItemView, Notice, Plugin, PluginSettingTab, Setting, WorkspaceLeaf, setIcon } from "obsidian"; +import { App, FileSystemAdapter, ItemView, Notice, Plugin, PluginSettingTab, Setting, TFile, TFolder, WorkspaceLeaf, parseYaml, setIcon } from "obsidian"; import { spawn, type ChildProcess } from "child_process"; import * as fs from "fs"; import * as os from "os"; @@ -16,6 +16,16 @@ import { resolvePluginDir as resolvePluginDirWithVault, type CliRuntimeConfig } from "./runtime-utils"; +import { + buildPromptPreview, + computeNextRun, + parseAutomationFile, + pushHistory, + type AutomationParseError, + type AutomationRunRecord, + type ParsedAutomation +} from "./automation"; +import { AutomationsModal } from "./automations-modal"; // Injected at build time by `esbuild.config.mjs` (see `define`). These hold the // full source of `pty-proxy.js` and `pty-bridge.py` so the plugin can recreate @@ -40,6 +50,10 @@ interface ClaudeCliPluginSettings { autoRestartOnRuntimeSwitch: boolean; autoStart: boolean; nodeExecutable: string; + automationsFolder: string; + automationsLastRun: Record; + automationsHistory: AutomationRunRecord[]; + automationsHistoryLimit: number; } const DEFAULT_SETTINGS: ClaudeCliPluginSettings = { @@ -47,9 +61,15 @@ const DEFAULT_SETTINGS: ClaudeCliPluginSettings = { selectedRuntimeId: "claude", autoRestartOnRuntimeSwitch: true, autoStart: true, - nodeExecutable: "auto" + nodeExecutable: "auto", + automationsFolder: "", + automationsLastRun: {}, + automationsHistory: [], + automationsHistoryLimit: 200 }; +const AUTOMATION_TICK_MS = 30_000; + function cloneDefaultRuntimes(): CliRuntimeConfig[] { return DEFAULT_RUNTIMES.map((runtime) => ({ ...runtime })); } @@ -120,10 +140,13 @@ class ClaudeCliView extends ItemView { const secondaryRowEl = toolbarEl.createDiv({ cls: "claude-cli-toolbar-row" }); const mentionBtn = secondaryRowEl.createEl("button", { text: "@active file" }); const folderMentionBtn = secondaryRowEl.createEl("button", { text: "@active folder" }); + const automationsBtn = secondaryRowEl.createEl("button", { text: "Automations" }); this.setButtonIcon(mentionBtn, "file-plus", "@active file"); this.setButtonIcon(folderMentionBtn, "folder-plus", "@active folder"); + this.setButtonIcon(automationsBtn, "calendar-clock", "Automations"); mentionBtn.addClass("claude-cli-btn-info"); folderMentionBtn.addClass("claude-cli-btn-info"); + automationsBtn.addClass("claude-cli-btn-info"); this.statusEl = this.contentEl.createDiv({ cls: "claude-cli-status" }); @@ -133,6 +156,9 @@ class ClaudeCliView extends ItemView { clearBtn.addEventListener("click", () => this.terminal?.clear()); mentionBtn.addEventListener("click", () => this.insertActiveFileMention()); folderMentionBtn.addEventListener("click", () => this.insertActiveFolderMention()); + automationsBtn.addEventListener("click", () => { + new AutomationsModal(this.app, this.plugin).open(); + }); this.runtimeSelect.addEventListener("change", () => { if (this.runtimeSelect) { this.setRuntime(this.runtimeSelect.value); @@ -184,6 +210,29 @@ class ClaudeCliView extends ItemView { return Promise.resolve(); } + isProcessRunning(): boolean { + return this.processHandle !== null && this.runningRuntimeId !== null; + } + + getRunningRuntimeId(): string | null { + return this.runningRuntimeId; + } + + matchesRuntime(target: string): boolean { + if (!this.runningRuntimeId) return false; + if (this.runningRuntimeId === target) return true; + const runtime = this.plugin.settings.runtimes.find((r) => r.id === this.runningRuntimeId); + return runtime ? runtime.name.trim().toLowerCase() === target.trim().toLowerCase() : false; + } + + sendAutomationPrompt(text: string): void { + if (!this.processHandle) { + throw new Error("CLI process is not running"); + } + this.processHandle.write(text); + this.writeSystemLine(`[Automation prompt injected]`); + } + refreshRuntimeSelect(): void { if (!this.runtimeSelect) { return; @@ -501,6 +550,9 @@ class ClaudeCliView extends ItemView { export default class ClaudeCliPlugin extends Plugin { settings!: ClaudeCliPluginSettings; + private automationEntries: Map = new Map(); + private automationErrors: Map = new Map(); + private automationListeners: Set<() => void> = new Set(); async onload(): Promise { await this.loadSettings(); @@ -520,6 +572,34 @@ export default class ClaudeCliPlugin extends Plugin { }); this.addSettingTab(new ClaudeCliSettingTab(this.app, this)); + + this.app.workspace.onLayoutReady(() => { + this.loadAutomations(); + this.runAutomationTick(); + }); + + const refreshOnVaultChange = (file: { path: string } | null) => { + const folder = this.settings.automationsFolder; + if (!folder || !file) return; + if (file.path === folder || file.path.startsWith(`${folder}/`)) { + this.loadAutomations(); + } + }; + + this.registerEvent(this.app.vault.on("create", refreshOnVaultChange)); + this.registerEvent(this.app.vault.on("modify", refreshOnVaultChange)); + this.registerEvent(this.app.vault.on("delete", refreshOnVaultChange)); + this.registerEvent( + this.app.vault.on("rename", (file, oldPath) => { + refreshOnVaultChange(file); + const folder = this.settings.automationsFolder; + if (folder && (oldPath === folder || oldPath.startsWith(`${folder}/`))) { + this.loadAutomations(); + } + }) + ); + + this.registerInterval(activeWindow.setInterval(() => this.runAutomationTick(), AUTOMATION_TICK_MS)); } onunload(): void { @@ -527,6 +607,178 @@ export default class ClaudeCliPlugin extends Plugin { // Obsidian preserves leaf state across reloads/updates. } + getAutomations(): ParsedAutomation[] { + return Array.from(this.automationEntries.values()).sort((a, b) => + a.name.localeCompare(b.name) + ); + } + + getAutomationErrors(): AutomationParseError[] { + return Array.from(this.automationErrors.values()); + } + + onAutomationsChanged(listener: () => void): () => void { + this.automationListeners.add(listener); + return () => this.automationListeners.delete(listener); + } + + private notifyAutomationsChanged(): void { + this.automationListeners.forEach((listener) => { + try { + listener(); + } catch { + /* ignore listener errors */ + } + }); + } + + loadAutomations(): void { + this.automationEntries.clear(); + this.automationErrors.clear(); + const folderPath = this.settings.automationsFolder.trim(); + if (!folderPath) { + this.notifyAutomationsChanged(); + return; + } + const folder = this.app.vault.getFolderByPath(folderPath); + if (!folder) { + this.notifyAutomationsChanged(); + return; + } + const files: TFile[] = []; + const walk = (f: TFolder) => { + for (const child of f.children) { + if (child instanceof TFile && child.extension === "md") { + files.push(child); + } else if (child instanceof TFolder) { + walk(child); + } + } + }; + walk(folder); + + void Promise.all( + files.map(async (file) => { + try { + const content = await this.app.vault.cachedRead(file); + const result = parseAutomationFile(content, file.path, (yaml) => parseYaml(yaml)); + if (result.ok) { + this.automationEntries.set(file.path, result.entry); + } else { + this.automationErrors.set(file.path, result.error); + } + } catch (err) { + this.automationErrors.set(file.path, { + path: file.path, + name: file.basename, + reason: `Read error: ${(err as Error).message}` + }); + } + }) + ).then(() => { + this.notifyAutomationsChanged(); + }); + } + + runAutomationTick(): void { + if (this.automationEntries.size === 0) return; + const now = Date.now(); + for (const entry of this.automationEntries.values()) { + if (!entry.enabled) continue; + const last = this.settings.automationsLastRun[entry.path] ?? null; + const next = computeNextRun(entry, last, now); + if (next !== null && next <= now) { + void this.triggerAutomation(entry, "scheduler"); + } + } + } + + async triggerAutomation( + entry: ParsedAutomation, + source: "scheduler" | "manual" + ): Promise { + const now = Date.now(); + const baseRecord = { + ts: now, + path: entry.path, + name: entry.name, + source + } as const; + + const view = this.findRunnableView(); + if (!view) { + const reason = "No CLI is running"; + this.recordHistory({ ...baseRecord, status: "skipped", reason }); + if (source === "manual") { + new Notice(`Automation "${entry.name}" skipped — ${reason.toLowerCase()}.`, 5000); + } + return; + } + + if (entry.runtime) { + const runtimeMatches = view.matchesRuntime(entry.runtime); + if (!runtimeMatches) { + const reason = `Runtime mismatch (expected "${entry.runtime}")`; + this.recordHistory({ ...baseRecord, status: "skipped", reason }); + if (source === "manual") { + new Notice(`Automation "${entry.name}" skipped — ${reason.toLowerCase()}.`, 5000); + } + return; + } + } + + try { + const text = entry.body + (entry.appendNewline ? "\n" : ""); + view.sendAutomationPrompt(text); + const runtimeId = view.getRunningRuntimeId(); + this.recordHistory({ + ...baseRecord, + status: "ran", + runtimeId, + promptPreview: buildPromptPreview(entry.body) + }); + this.settings.automationsLastRun[entry.path] = now; + await this.saveSettings(); + if (source === "manual") { + new Notice(`Automation "${entry.name}" sent.`, 3000); + } + } catch (err) { + this.recordHistory({ + ...baseRecord, + status: "error", + reason: (err as Error).message + }); + new Notice(`Automation "${entry.name}" failed: ${(err as Error).message}`, 6000); + } + } + + private findRunnableView(): ClaudeCliView | null { + const leaves = this.app.workspace.getLeavesOfType(VIEW_TYPE_CLAUDE); + for (const leaf of leaves) { + const view = leaf.view; + if (view instanceof ClaudeCliView && view.isProcessRunning()) { + return view; + } + } + return null; + } + + recordHistory(record: AutomationRunRecord): void { + this.settings.automationsHistory = pushHistory( + this.settings.automationsHistory, + record, + this.settings.automationsHistoryLimit + ); + void this.saveSettings(); + this.notifyAutomationsChanged(); + } + + clearAutomationHistory(): void { + this.settings.automationsHistory = []; + void this.saveSettings(); + this.notifyAutomationsChanged(); + } + async activateView(): Promise { const { workspace } = this.app; let leaf: WorkspaceLeaf | null = workspace.getLeavesOfType(VIEW_TYPE_CLAUDE)[0] ?? null; @@ -560,7 +812,17 @@ export default class ClaudeCliPlugin extends Plugin { nodeExecutable: typeof raw.nodeExecutable === "string" && raw.nodeExecutable.trim() ? raw.nodeExecutable - : DEFAULT_SETTINGS.nodeExecutable + : DEFAULT_SETTINGS.nodeExecutable, + automationsFolder: + typeof raw.automationsFolder === "string" ? raw.automationsFolder : DEFAULT_SETTINGS.automationsFolder, + automationsLastRun: sanitizeLastRun(raw.automationsLastRun), + automationsHistory: sanitizeHistory(raw.automationsHistory), + automationsHistoryLimit: + typeof raw.automationsHistoryLimit === "number" && + Number.isInteger(raw.automationsHistoryLimit) && + raw.automationsHistoryLimit > 0 + ? raw.automationsHistoryLimit + : DEFAULT_SETTINGS.automationsHistoryLimit }; } @@ -716,6 +978,39 @@ class ClaudeCliSettingTab extends PluginSettingTab { }) ); + new Setting(containerEl).setName("Automations").setHeading(); + containerEl.createEl("p", { + text: "Folder containing prompt automations. Each Markdown file is one automation (frontmatter sets the schedule; body is the prompt sent to the running CLI). See readme for the file format.", + cls: "setting-item-description" + }); + + new Setting(containerEl) + .setName("Automations folder") + .setDesc("Vault-relative path. Leave empty to disable automations.") + .addText((text) => + text + .setPlaceholder("Automations") + .setValue(this.plugin.settings.automationsFolder) + .onChange(async (value) => { + this.plugin.settings.automationsFolder = value.trim(); + await this.plugin.saveSettings(); + this.plugin.loadAutomations(); + }) + ); + + new Setting(containerEl) + .setName("Reload automations") + .setDesc("Force a re-scan of the automations folder (otherwise scans happen on vault changes).") + .addButton((btn) => + btn + .setButtonText("Reload now") + .setIcon("refresh-cw") + .onClick(() => { + this.plugin.loadAutomations(); + new Notice("Automations reloaded.", 2500); + }) + ); + new Setting(containerEl).setName("Advanced").setHeading(); new Setting(containerEl) @@ -884,6 +1179,50 @@ function spawnPtyProxy(params: { }); } +function sanitizeLastRun(raw: unknown): Record { + if (!raw || typeof raw !== "object") { + return {}; + } + const out: Record = {}; + for (const [key, value] of Object.entries(raw as Record)) { + if (typeof key === "string" && key && typeof value === "number" && Number.isFinite(value)) { + out[key] = value; + } + } + return out; +} + +function sanitizeHistory(raw: unknown): AutomationRunRecord[] { + if (!Array.isArray(raw)) { + return []; + } + const out: AutomationRunRecord[] = []; + for (const entry of raw) { + if (!entry || typeof entry !== "object") continue; + const r = entry as Partial; + if ( + typeof r.ts !== "number" || + typeof r.path !== "string" || + typeof r.name !== "string" || + (r.source !== "scheduler" && r.source !== "manual") || + (r.status !== "ran" && r.status !== "skipped" && r.status !== "error") + ) { + continue; + } + out.push({ + ts: r.ts, + path: r.path, + name: r.name, + source: r.source, + status: r.status, + reason: typeof r.reason === "string" ? r.reason : undefined, + runtimeId: typeof r.runtimeId === "string" ? r.runtimeId : null, + promptPreview: typeof r.promptPreview === "string" ? r.promptPreview : undefined + }); + } + return out; +} + function makeProxyAdapter(handle: ChildProcess): ProcessAdapter { const dataCallbacks: Array<(data: string) => void> = []; const exitCallbacks: Array<(exitCode: number, signal: string) => void> = []; diff --git a/package-lock.json b/package-lock.json index f202c1f..7126239 100644 --- a/package-lock.json +++ b/package-lock.json @@ -11,6 +11,7 @@ "dependencies": { "@xterm/addon-fit": "^0.11.0", "@xterm/xterm": "^6.0.0", + "cron-parser": "^5.5.0", "node-pty": "^1.1.0" }, "devDependencies": { @@ -23,7 +24,8 @@ "obsidian": "latest", "tslib": "^2.8.1", "typescript": "^5.8.3", - "vitest": "^4.0.18" + "vitest": "^4.0.18", + "yaml": "^2.9.0" } }, "node_modules/@codemirror/state": { @@ -2125,6 +2127,18 @@ "license": "MIT", "peer": true }, + "node_modules/cron-parser": { + "version": "5.5.0", + "resolved": "https://registry.npmjs.org/cron-parser/-/cron-parser-5.5.0.tgz", + "integrity": "sha512-oML4lKUXxizYswqmxuOCpgFS8BNUJpIu6k/2HVHyaL8Ynnf3wdf9tkns0yRdJLSIjkJ+b0DXHMZEHGpMwjnPww==", + "license": "MIT", + "dependencies": { + "luxon": "^3.7.1" + }, + "engines": { + "node": ">=18" + } + }, "node_modules/cross-spawn": { "version": "7.0.6", "resolved": "https://registry.npmjs.org/cross-spawn/-/cross-spawn-7.0.6.tgz", @@ -4632,6 +4646,15 @@ "loose-envify": "cli.js" } }, + "node_modules/luxon": { + "version": "3.7.2", + "resolved": "https://registry.npmjs.org/luxon/-/luxon-3.7.2.tgz", + "integrity": "sha512-vtEhXh/gNjI9Yg1u4jX/0YVPMvxzHuGgCm6tC5kZyb08yjGWGnqAjGJvcXbqQR2P3MyMEFnRbpcdFS6PBcLqew==", + "license": "MIT", + "engines": { + "node": ">=12" + } + }, "node_modules/magic-string": { "version": "0.30.21", "resolved": "https://registry.npmjs.org/magic-string/-/magic-string-0.30.21.tgz", @@ -6944,9 +6967,9 @@ } }, "node_modules/yaml": { - "version": "2.8.3", - "resolved": "https://registry.npmjs.org/yaml/-/yaml-2.8.3.tgz", - "integrity": "sha512-AvbaCLOO2Otw/lW5bmh9d/WEdcDFdQp2Z2ZUH3pX9U2ihyUY0nvLv7J6TrWowklRGPYbB/IuIMfYgxaCPg5Bpg==", + "version": "2.9.0", + "resolved": "https://registry.npmjs.org/yaml/-/yaml-2.9.0.tgz", + "integrity": "sha512-2AvhNX3mb8zd6Zy7INTtSpl1F15HW6Wnqj0srWlkKLcpYl/gMIMJiyuGq2KeI2YFxUPjdlB+3Lc10seMLtL4cA==", "dev": true, "license": "ISC", "bin": { diff --git a/package.json b/package.json index 2588240..bb8840c 100644 --- a/package.json +++ b/package.json @@ -28,11 +28,13 @@ "obsidian": "latest", "tslib": "^2.8.1", "typescript": "^5.8.3", - "vitest": "^4.0.18" + "vitest": "^4.0.18", + "yaml": "^2.9.0" }, "dependencies": { "@xterm/addon-fit": "^0.11.0", "@xterm/xterm": "^6.0.0", + "cron-parser": "^5.5.0", "node-pty": "^1.1.0" } } diff --git a/styles.css b/styles.css index 3b71c58..4386139 100644 --- a/styles.css +++ b/styles.css @@ -350,3 +350,224 @@ .claude-cli-terminal .xterm-cursor-layer { position: relative; } + +.any-ai-cli-automations-modal { + max-width: 760px; + width: min(760px, 90vw); +} + +.any-ai-cli-am-tabs { + display: flex; + gap: 4px; + border-bottom: 1px solid var(--background-modifier-border); + margin-bottom: 12px; +} + +.any-ai-cli-am-tab { + background: transparent; + border: none; + border-bottom: 2px solid transparent; + color: var(--text-muted); + padding: 6px 12px; + font-size: 13px; + font-weight: 500; + cursor: pointer; +} + +.any-ai-cli-am-tab:hover { + color: var(--text-normal); +} + +.any-ai-cli-am-tab.is-active { + color: var(--text-normal); + border-bottom-color: var(--interactive-accent); +} + +.any-ai-cli-am-body { + display: flex; + flex-direction: column; + gap: 10px; +} + +.any-ai-cli-am-info { + color: var(--text-muted); + font-size: 12px; + margin: 0; +} + +.any-ai-cli-am-empty { + color: var(--text-muted); + font-style: italic; + margin: 12px 0; +} + +.any-ai-cli-am-errors { + border: 1px solid var(--background-modifier-border); + border-left: 3px solid var(--color-orange, #d97706); + background: var(--background-secondary); + padding: 8px 10px; + border-radius: 4px; + font-size: 12px; +} + +.any-ai-cli-am-errors ul { + margin: 6px 0 0 0; + padding-left: 18px; +} + +.any-ai-cli-am-error-path { + font-family: var(--font-monospace); + color: var(--text-normal); +} + +.any-ai-cli-am-table { + width: 100%; + border-collapse: collapse; + font-size: 12px; +} + +.any-ai-cli-am-table th, +.any-ai-cli-am-table td { + text-align: left; + padding: 6px 8px; + border-bottom: 1px solid var(--background-modifier-border); + vertical-align: middle; +} + +.any-ai-cli-am-table th { + font-weight: 600; + color: var(--text-muted); + font-size: 11px; + text-transform: uppercase; + letter-spacing: 0.04em; +} + +.any-ai-cli-am-table tr.is-disabled { + opacity: 0.55; +} + +.any-ai-cli-am-actions { + display: flex; + gap: 6px; + justify-content: flex-end; +} + +.any-ai-cli-am-actions button { + padding: 3px 8px; + font-size: 11px; + border-radius: 4px; + border: 1px solid var(--background-modifier-border); + background: var(--interactive-normal); + color: var(--text-normal); + cursor: pointer; +} + +.any-ai-cli-am-actions button:hover:not(:disabled) { + background: var(--interactive-hover); +} + +.any-ai-cli-am-actions button:disabled { + opacity: 0.5; + cursor: not-allowed; +} + +.any-ai-cli-am-badge { + display: inline-block; + padding: 2px 6px; + border-radius: 999px; + font-size: 11px; + font-weight: 500; +} + +.any-ai-cli-am-badge.is-enabled, +.any-ai-cli-am-badge.is-ran { + background: rgba(34, 197, 94, 0.18); + color: var(--color-green, #16a34a); +} + +.any-ai-cli-am-badge.is-disabled, +.any-ai-cli-am-badge.is-skipped { + background: rgba(234, 179, 8, 0.18); + color: var(--color-yellow, #ca8a04); +} + +.any-ai-cli-am-badge.is-error { + background: rgba(239, 68, 68, 0.18); + color: var(--color-red, #dc2626); +} + +.any-ai-cli-am-history-toolbar { + display: flex; + justify-content: space-between; + align-items: center; + gap: 12px; +} + +.any-ai-cli-am-history-actions { + display: flex; + gap: 6px; +} + +.any-ai-cli-am-history-actions button { + padding: 4px 10px; + font-size: 11px; + border-radius: 4px; + border: 1px solid var(--background-modifier-border); + background: var(--interactive-normal); + color: var(--text-normal); + cursor: pointer; +} + +.any-ai-cli-am-history-actions button:disabled { + opacity: 0.5; + cursor: not-allowed; +} + +.any-ai-cli-am-history-list { + list-style: none; + padding: 0; + margin: 0; + max-height: 50vh; + overflow-y: auto; + border: 1px solid var(--background-modifier-border); + border-radius: 4px; +} + +.any-ai-cli-am-history-item { + display: grid; + grid-template-columns: 140px 1fr auto auto; + gap: 8px; + align-items: center; + padding: 6px 10px; + border-bottom: 1px solid var(--background-modifier-border); + font-size: 12px; +} + +.any-ai-cli-am-history-item:last-child { + border-bottom: none; +} + +.any-ai-cli-am-history-ts { + font-family: var(--font-monospace); + color: var(--text-muted); + font-size: 11px; +} + +.any-ai-cli-am-history-name { + color: var(--text-normal); + font-weight: 500; +} + +.any-ai-cli-am-history-source { + font-size: 11px; + color: var(--text-muted); + text-transform: uppercase; + letter-spacing: 0.04em; +} + +.any-ai-cli-am-history-detail { + grid-column: 2 / -1; + color: var(--text-muted); + font-size: 11px; + font-style: italic; +} diff --git a/tests/automation.test.ts b/tests/automation.test.ts new file mode 100644 index 0000000..615530f --- /dev/null +++ b/tests/automation.test.ts @@ -0,0 +1,276 @@ +import { describe, expect, it } from "vitest"; +import { parse as parseYaml } from "yaml"; +import { + basenameWithoutExt, + buildPromptPreview, + computeNextRun, + describeSchedule, + isDue, + parseAutomationFile, + pushHistory, + splitFrontmatter, + type AutomationRunRecord, + type ParsedAutomation +} from "../automation"; + +const yamlParser = (text: string): unknown => parseYaml(text) as unknown; + +function makeFile(frontmatter: Record, body: string): string { + const fm = Object.entries(frontmatter) + .map(([k, v]) => `${k}: ${typeof v === "string" ? JSON.stringify(v) : String(v)}`) + .join("\n"); + return `---\n${fm}\n---\n${body}`; +} + +describe("splitFrontmatter", () => { + it("splits valid frontmatter from body", () => { + const r = splitFrontmatter("---\nfoo: 1\n---\nhello"); + expect(r.yaml).toBe("foo: 1"); + expect(r.body).toBe("hello"); + }); + + it("returns null yaml when no frontmatter", () => { + const r = splitFrontmatter("just a body"); + expect(r.yaml).toBeNull(); + expect(r.body).toBe("just a body"); + }); + + it("handles CRLF line endings", () => { + const r = splitFrontmatter("---\r\nfoo: 1\r\n---\r\nhello"); + expect(r.yaml).toBe("foo: 1"); + expect(r.body).toBe("hello"); + }); +}); + +describe("basenameWithoutExt", () => { + it("strips extension and folders", () => { + expect(basenameWithoutExt("Automations/daily.md")).toBe("daily"); + expect(basenameWithoutExt("no-folder.md")).toBe("no-folder"); + expect(basenameWithoutExt("noext")).toBe("noext"); + }); +}); + +describe("parseAutomationFile", () => { + it("parses an interval automation with defaults", () => { + const file = makeFile({ interval: 60 }, "Summarize my day."); + const r = parseAutomationFile(file, "Automations/summary.md", yamlParser); + expect(r.ok).toBe(true); + if (!r.ok) return; + expect(r.entry.name).toBe("summary"); + expect(r.entry.enabled).toBe(true); + expect(r.entry.interval).toBe(60); + expect(r.entry.cron).toBeNull(); + expect(r.entry.runtime).toBeNull(); + expect(r.entry.appendNewline).toBe(true); + expect(r.entry.body).toBe("Summarize my day."); + }); + + it("parses a cron automation with explicit name + runtime + disabled", () => { + const file = makeFile( + { name: "Daily standup", enabled: false, cron: "0 9 * * 1-5", runtime: "claude", appendNewline: false }, + "Give me the standup brief." + ); + const r = parseAutomationFile(file, "Automations/standup.md", yamlParser); + expect(r.ok).toBe(true); + if (!r.ok) return; + expect(r.entry.name).toBe("Daily standup"); + expect(r.entry.enabled).toBe(false); + expect(r.entry.interval).toBeNull(); + expect(r.entry.cron).toBe("0 9 * * 1-5"); + expect(r.entry.runtime).toBe("claude"); + expect(r.entry.appendNewline).toBe(false); + }); + + it("rejects when both interval and cron are set", () => { + const file = makeFile({ interval: 5, cron: "*/5 * * * *" }, "body"); + const r = parseAutomationFile(file, "x.md", yamlParser); + expect(r.ok).toBe(false); + if (r.ok) return; + expect(r.error.reason).toMatch(/mutually exclusive/); + }); + + it("rejects when neither interval nor cron is set", () => { + const file = makeFile({ name: "no sched" }, "body"); + const r = parseAutomationFile(file, "x.md", yamlParser); + expect(r.ok).toBe(false); + if (r.ok) return; + expect(r.error.reason).toMatch(/Missing schedule/); + }); + + it("rejects non-integer interval", () => { + const file = makeFile({ interval: 1.5 }, "body"); + const r = parseAutomationFile(file, "x.md", yamlParser); + expect(r.ok).toBe(false); + if (r.ok) return; + expect(r.error.reason).toMatch(/integer/); + }); + + it("rejects interval < 1", () => { + const file = makeFile({ interval: 0 }, "body"); + const r = parseAutomationFile(file, "x.md", yamlParser); + expect(r.ok).toBe(false); + }); + + it("rejects invalid cron", () => { + const file = makeFile({ cron: "not a cron" }, "body"); + const r = parseAutomationFile(file, "x.md", yamlParser); + expect(r.ok).toBe(false); + if (r.ok) return; + expect(r.error.reason).toMatch(/cron/i); + }); + + it("rejects empty body", () => { + const file = makeFile({ interval: 60 }, " "); + const r = parseAutomationFile(file, "x.md", yamlParser); + expect(r.ok).toBe(false); + if (r.ok) return; + expect(r.error.reason).toMatch(/empty/); + }); + + it("rejects missing frontmatter", () => { + const r = parseAutomationFile("no fences here", "x.md", yamlParser); + expect(r.ok).toBe(false); + if (r.ok) return; + expect(r.error.reason).toMatch(/Missing frontmatter/); + }); + + it("rejects frontmatter parse errors via the provided yaml parser", () => { + const r = parseAutomationFile( + "---\n: : :\n---\nbody", + "x.md", + () => { + throw new Error("boom"); + } + ); + expect(r.ok).toBe(false); + if (r.ok) return; + expect(r.error.reason).toMatch(/boom/); + }); +}); + +function intervalEntry(min: number, enabled = true): ParsedAutomation { + return { + path: "a.md", + name: "a", + enabled, + interval: min, + cron: null, + runtime: null, + appendNewline: true, + body: "x" + }; +} + +function cronEntry(expr: string, enabled = true): ParsedAutomation { + return { + path: "a.md", + name: "a", + enabled, + interval: null, + cron: expr, + runtime: null, + appendNewline: true, + body: "x" + }; +} + +describe("computeNextRun", () => { + it("interval: returns now when never run", () => { + const entry = intervalEntry(30); + const now = 1_700_000_000_000; + expect(computeNextRun(entry, null, now)).toBe(now); + }); + + it("interval: adds minutes to lastRun", () => { + const entry = intervalEntry(15); + const last = 1_700_000_000_000; + expect(computeNextRun(entry, last, last + 1_000)).toBe(last + 15 * 60_000); + }); + + it("cron: returns next occurrence after lastRun", () => { + const entry = cronEntry("*/5 * * * *"); + const last = new Date("2026-05-19T22:00:00Z").getTime(); + const now = last + 60_000; + const next = computeNextRun(entry, last, now); + expect(next).toBe(new Date("2026-05-19T22:05:00Z").getTime()); + }); + + it("cron: when never run, schedules from just-before-now", () => { + const entry = cronEntry("*/5 * * * *"); + const now = new Date("2026-05-19T22:03:00Z").getTime(); + const next = computeNextRun(entry, null, now); + expect(next).toBe(new Date("2026-05-19T22:05:00Z").getTime()); + }); +}); + +describe("isDue", () => { + it("disabled entries are never due", () => { + const entry = intervalEntry(1, false); + expect(isDue(entry, null, Date.now())).toBe(false); + }); + + it("interval entry is due once interval has passed", () => { + const entry = intervalEntry(5); + const last = 1_700_000_000_000; + expect(isDue(entry, last, last + 4 * 60_000)).toBe(false); + expect(isDue(entry, last, last + 5 * 60_000)).toBe(true); + expect(isDue(entry, last, last + 6 * 60_000)).toBe(true); + }); + + it("interval entry is due immediately when never run", () => { + const entry = intervalEntry(60); + expect(isDue(entry, null, Date.now())).toBe(true); + }); +}); + +describe("describeSchedule", () => { + it("formats interval (plural and singular)", () => { + expect(describeSchedule(intervalEntry(30))).toBe("every 30 min"); + expect(describeSchedule(intervalEntry(1))).toBe("every 1 min"); + }); + + it("formats cron", () => { + expect(describeSchedule(cronEntry("0 9 * * 1-5"))).toBe("cron: 0 9 * * 1-5"); + }); +}); + +describe("pushHistory", () => { + const makeRec = (ts: number): AutomationRunRecord => ({ + ts, + path: "a.md", + name: "a", + source: "manual", + status: "ran" + }); + + it("prepends new record", () => { + const next = pushHistory([makeRec(1)], makeRec(2), 10); + expect(next.map((r) => r.ts)).toEqual([2, 1]); + }); + + it("truncates to the limit, dropping oldest", () => { + const history: AutomationRunRecord[] = [3, 2, 1].map((t) => makeRec(t)); + const next = pushHistory(history, makeRec(4), 3); + expect(next.map((r) => r.ts)).toEqual([4, 3, 2]); + expect(next.length).toBe(3); + }); + + it("does not mutate the input array", () => { + const original = [makeRec(1)]; + pushHistory(original, makeRec(2), 10); + expect(original.map((r) => r.ts)).toEqual([1]); + }); +}); + +describe("buildPromptPreview", () => { + it("collapses whitespace and trims", () => { + expect(buildPromptPreview(" a b\nc ")).toBe("a b c"); + }); + + it("truncates with ellipsis", () => { + const long = "a".repeat(200); + const preview = buildPromptPreview(long, 50); + expect(preview.length).toBe(50); + expect(preview.endsWith("…")).toBe(true); + }); +});