Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 2 additions & 8 deletions src/commands/PerpsCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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: {
Expand Down
10 changes: 2 additions & 8 deletions src/commands/SpotCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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);
}
}
16 changes: 16 additions & 0 deletions src/lib/DateConverter.ts
Original file line number Diff line number Diff line change
@@ -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));
}
}