Skip to content

Commit

Permalink
check
Browse files Browse the repository at this point in the history
  • Loading branch information
Sladuca committed Sep 10, 2024
1 parent 60af5e9 commit dbb3cf0
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 34 deletions.
53 changes: 23 additions & 30 deletions src/helpers/units.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import type { Nullable } from "../types/empty";

export type Epoch = number;

const MILLS_PER_EPOCH = 1000 * 60;
const MILLS_PER_EPOCH = 1000 * 60; // 1 minute
const EPOCHS_PER_HOUR = (3600 * 1000) / MILLS_PER_EPOCH;

export function currentEpoch(): Epoch {
return Math.floor(Date.now() / MILLS_PER_EPOCH);
Expand All @@ -14,6 +15,27 @@ export function epochToDate(epoch: Epoch): Date {
return new Date(epoch * MILLS_PER_EPOCH);
}

export function roundStartDate(startDate: Date): Date {
const now = currentEpoch();
const startEpoch = dateToEpoch(startDate);
if (startEpoch <= now + 1) {
return epochToDate(now + 1);
} else {
return epochToDate(roundEpochUpToHour(startEpoch));
}
}

export function roundEndDate(endDate: Date): Date {
return epochToDate(roundEpochUpToHour(dateToEpoch(endDate)));
}

function dateToEpoch(date: Date): number {
return Math.ceil(date.getTime() / MILLS_PER_EPOCH);
}
function roundEpochUpToHour(epoch: number): number {
return Math.ceil(epoch / EPOCHS_PER_HOUR) * EPOCHS_PER_HOUR;
}

// -- currency

export type Cents = number;
Expand Down Expand Up @@ -59,32 +81,3 @@ export function priceWholeToCenticents(
export function centicentsToDollarsFormatted(centicents: Centicents): string {
return `$${(centicents / 10_000).toFixed(2)}`;
}

const MILLS_PER_EPOCH = 1000 * 60; // 1 minute
const EPOCHS_PER_HOUR = (3600 * 1000) / MILLS_PER_EPOCH;
function currentEpoch(): number {
return Math.floor(Date.now() / MILLS_PER_EPOCH);
}
function dateToEpoch(date: Date): number {
return Math.ceil(date.getTime() / MILLS_PER_EPOCH);
}
function epochToDate(epoch: number): Date {
return new Date(epoch * MILLS_PER_EPOCH);
}
function roundEpochUpToHour(epoch: number): number {
return Math.ceil(epoch / EPOCHS_PER_HOUR) * EPOCHS_PER_HOUR;
}

export function roundStartDate(startDate: Date): Date {
const now = currentEpoch();
const startEpoch = dateToEpoch(startDate);
if (startEpoch <= now + 1) {
return epochToDate(now + 1);
} else {
return epochToDate(roundEpochUpToHour(startEpoch));
}
}

export function roundEndDate(endDate: Date): Date {
return epochToDate(roundEpochUpToHour(dateToEpoch(endDate)));
}
8 changes: 4 additions & 4 deletions src/lib/dev.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { confirm } from "@inquirer/prompts";
import chalk from "chalk";
import type { Command } from "commander";
import dayjs from "dayjs";
import utc from "dayjs/plugin/utc";
import {
deleteConfig,
getConfigPath,
Expand All @@ -11,11 +14,8 @@ import {
logLoginMessageAndQuit,
logSessionTokenExpiredAndQuit,
} from "../helpers/errors";
import { getApiUrl } from "../helpers/urls";
import { currentEpoch, epochToDate } from "../helpers/units";
import dayjs from "dayjs";
import utc from "dayjs/plugin/utc";
import chalk from "chalk";
import { getApiUrl } from "../helpers/urls";

dayjs.extend(utc);

Expand Down

0 comments on commit dbb3cf0

Please sign in to comment.