diff --git a/src/helpers/units.ts b/src/helpers/units.ts index 30f13d6..bbaa82e 100644 --- a/src/helpers/units.ts +++ b/src/helpers/units.ts @@ -1,3 +1,4 @@ +import * as chrono from "chrono-node"; import dayjs from "dayjs"; import type { Nullable } from "../types/empty"; @@ -99,3 +100,17 @@ export function centsToDollars(cents: Cents): number { export function dollarsToCents(dollars: number): Cents { return Math.ceil(dollars * 100); } + +export function parseStartDate(startDate: string): Date | "NOW" | null { + const nowRe = /\b(?:"|')?[nN][oO][wW](?:"|')?\b/; + if (nowRe.test(startDate)) { + return "NOW"; + } + + const chronoDate = chrono.parseDate(startDate); + if (!chronoDate) { + return null; + } + + return chronoDate; +} diff --git a/src/lib/buy.ts b/src/lib/buy.ts index 3bed040..2f2c174 100644 --- a/src/lib/buy.ts +++ b/src/lib/buy.ts @@ -1,6 +1,5 @@ import { confirm } from "@inquirer/prompts"; import c from "chalk"; -import * as chrono from "chrono-node"; import type { Command } from "commander"; import dayjs from "dayjs"; import duration from "dayjs/plugin/duration"; @@ -21,6 +20,7 @@ import { type Cents, centsToDollarsFormatted, computeApproximateDurationSeconds, + parseStartDate, priceWholeToCents, roundEndDate, roundStartDate, @@ -117,13 +117,12 @@ async function buyOrderAction(options: SfBuyOptions) { switch (options.start) { case null: case undefined: - case "NOW": startDate = "NOW"; break; default: { - const parsed = chrono.parseDate(options.start); + const parsed = parseStartDate(options.start); if (!parsed) { - return logAndQuit("Invalid start date"); + return logAndQuit(`Invalid start date: ${options.start}`); } startDate = parsed; }