diff --git a/src/commands/PerpsCommand.ts b/src/commands/PerpsCommand.ts index 313872c..2e86304 100644 --- a/src/commands/PerpsCommand.ts +++ b/src/commands/PerpsCommand.ts @@ -5,6 +5,7 @@ import type { Command } from "commander"; import { PerpsClient } from "../clients/PerpsClient.ts"; import { Asset, resolveAsset } from "../lib/Asset.ts"; import { Config } from "../lib/Config.ts"; +import { DateConverter } from "../lib/DateConverter.ts"; import { NumberConverter } from "../lib/NumberConverter.ts"; import { Output } from "../lib/Output.ts"; import { Signer } from "../lib/Signer.ts"; @@ -904,14 +905,7 @@ export class PerpsCommand { } private static parseTimestamp(value: string): string { - if (/^\d+$/.test(value)) { - return value; - } - const ms = new Date(value).getTime(); - if (isNaN(ms)) { - throw new Error(`Invalid date: ${value}`); - } - return String(Math.floor(ms / 1000)); + return DateConverter.parseTimestamp(value); } private static async history(opts: { diff --git a/src/commands/SpotCommand.ts b/src/commands/SpotCommand.ts index ab28856..6255940 100644 --- a/src/commands/SpotCommand.ts +++ b/src/commands/SpotCommand.ts @@ -15,6 +15,7 @@ import { } from "../clients/UltraClient.ts"; import { Asset, resolveWalletAsset } from "../lib/Asset.ts"; import { Config } from "../lib/Config.ts"; +import { DateConverter } from "../lib/DateConverter.ts"; import { NumberConverter } from "../lib/NumberConverter.ts"; import { Output } from "../lib/Output.ts"; import { Signer } from "../lib/Signer.ts"; @@ -928,13 +929,6 @@ export class SpotCommand { } private static parseTimestamp(value: string): string { - if (/^\d+$/.test(value)) { - return new Date(Number(value) * 1000).toISOString(); - } - const ms = new Date(value).getTime(); - if (isNaN(ms)) { - throw new Error(`Invalid date: ${value}`); - } - return new Date(ms).toISOString(); + return DateConverter.parseTimestamp(value); } } diff --git a/src/lib/DateConverter.ts b/src/lib/DateConverter.ts new file mode 100644 index 0000000..a1baeaa --- /dev/null +++ b/src/lib/DateConverter.ts @@ -0,0 +1,16 @@ +export class DateConverter { + /** + * Parse a timestamp string (ISO date or UNIX timestamp) and return Unix seconds. + * Accepts both ISO 8601 date strings and numeric UNIX timestamps. + */ + public static parseTimestamp(value: string): string { + if (/^\d+$/.test(value)) { + return value; + } + const ms = new Date(value).getTime(); + if (isNaN(ms)) { + throw new Error(`Invalid date: ${value}`); + } + return String(Math.floor(ms / 1000)); + } +}