67 lines
2.3 KiB
TypeScript
67 lines
2.3 KiB
TypeScript
export function formatResetCountdown(resetAtSeconds: number): string {
|
|
const nowSeconds = Math.floor(Date.now() / 1000);
|
|
const diffSeconds = resetAtSeconds - nowSeconds;
|
|
|
|
if (diffSeconds <= 0) return "resetting now";
|
|
if (diffSeconds < 60) return "resets in <1m";
|
|
|
|
const totalHours = Math.floor(diffSeconds / 3600);
|
|
const minutes = Math.floor((diffSeconds % 3600) / 60);
|
|
|
|
if (totalHours >= 24) {
|
|
const days = Math.floor(totalHours / 24);
|
|
const hours = totalHours % 24;
|
|
if (hours > 0) return `resets in ${days}d ${hours}h`;
|
|
return `resets in ${days}d`;
|
|
}
|
|
if (totalHours > 0 && minutes > 0) return `resets in ${totalHours}h ${minutes}m`;
|
|
if (totalHours > 0) return `resets in ${totalHours}h`;
|
|
return `resets in ${minutes}m`;
|
|
}
|
|
|
|
export function parseTimeInput(
|
|
value: string
|
|
): { hour: number; minute: number } | null {
|
|
const match = value.trim().match(/^(\d{1,2}):(\d{2})$/);
|
|
if (!match) return null;
|
|
|
|
const hour = Number.parseInt(match[1], 10);
|
|
const minute = Number.parseInt(match[2], 10);
|
|
if (hour > 23 || minute > 59) return null;
|
|
|
|
return { hour, minute };
|
|
}
|
|
|
|
export function formatTime(hour: number, minute: number): string {
|
|
return `${String(hour).padStart(2, "0")}:${String(minute).padStart(2, "0")}`;
|
|
}
|
|
|
|
export function formatLastUpdated(timestamp: number, now = Date.now()): string {
|
|
const elapsedSeconds = Math.max(0, Math.floor((now - timestamp) / 1000));
|
|
if (elapsedSeconds < 60) return "Updated just now";
|
|
|
|
const minutes = Math.floor(elapsedSeconds / 60);
|
|
if (minutes < 60) return `Updated ${minutes} min ago`;
|
|
|
|
const hours = Math.floor(minutes / 60);
|
|
if (hours < 24) return `Updated ${hours} hr${hours === 1 ? "" : "s"} ago`;
|
|
|
|
const days = Math.floor(hours / 24);
|
|
return `Updated ${days} day${days === 1 ? "" : "s"} ago`;
|
|
}
|
|
|
|
export function formatResetCountdownISO(isoString: string): string {
|
|
const resetAtSeconds = Math.floor(new Date(isoString).getTime() / 1000);
|
|
return formatResetCountdown(resetAtSeconds);
|
|
}
|
|
|
|
export function windowLabel(limitWindowSeconds: number): string {
|
|
const hours = limitWindowSeconds / 3600;
|
|
if (hours < 24) return `${Math.round(hours)}h`;
|
|
const days = hours / 24;
|
|
const wholeDays = Math.floor(days);
|
|
const remainderHours = Math.round(hours - wholeDays * 24);
|
|
if (remainderHours === 0) return `${wholeDays}d`;
|
|
return `${wholeDays}d ${remainderHours}h`;
|
|
}
|