repo_description: A cross-platform desktop All-in-One assistant tool for Claude Code, Codex, OpenCode, openclaw & Gemini CLI.
repo_topics: ai-tools claude-code codex desktop-app mcp minimax omo open-source openclaw openclaw-ui opencode provider-management rust skills skills-management tauri typescript wsl-support
code: import type { UsageRangePreset, UsageRangeSelection } from "@/types/usage";
  
  const DAY_SECONDS = 24 * 60 * 60;
  const DAY_MS = DAY_SECONDS * 1000;
  
  export interface ResolvedUsageRange {
    startDate: number;
    endDate: number;
  }
  
  function getStartOfLocalDayDate(nowMs: number): Date {
    const date = new Date(nowMs);
    return new Date(date.getFullYear(), date.getMonth(), date.getDate());
  }
  
  function getPresetLookbackStart(
    preset: Exclude<UsageRangePreset, "today" | "1d" | "custom">,
    nowMs: number,
  ): number {
    const dayCount = preset === "7d" ? 7 : preset === "14d" ? 14 : 30;
    return Math.floor(
      getStartOfLocalDayDate(nowMs - (dayCount - 1) * DAY_MS).getTime() / 1000,
    );
  }
  
  export function resolveUsageRange(
    selection: UsageRangeSelection,
    nowMs: number = Date.now(),
  ): ResolvedUsageRange {
    const endDate = Math.floor(nowMs / 1000);
  
    switch (selection.preset) {
      case "today":
        return {
          startDate: Math.floor(getStartOfLocalDayDate(nowMs).getTime() / 1000),
          endDate,
        };
      case "1d":
        return {
          startDate: endDate - DAY_SECONDS,
          endDate,
        };
      case "7d":
      case "14d":
      case "30d":
        return {
          startDate: getPresetLookbackStart(selection.preset, nowMs),
          endDate,
        };
      case "custom": {
        const startDate = selection.customStartDate ?? endDate - DAY_SECONDS;
        const customEndDate = selection.customEndDate ?? endDate;
        return {
          startDate,
          endDate: customEndDate,
        };
      }
    }
  }
  
  export function getUsageRangePresetLabel(
    preset: UsageRangePreset,
    t: (key: string, options?: { defaultValue?: string }) => string,
  ): string {
    switch (preset) {
      case "today":
        return t("usage.presetToday", { defaultValue: "当天" });
      case "1d":
        return t("usage.preset1d", { defaultValue: "1d" });
      case "7d":
        return t("usage.preset7d", { defaultValue: "7d" });
      case "14d":
        return t("usage.preset14d", { defaultValue: "14d" });
      case "30d":
        return t("usage.preset30d", { defaultValue: "30d" });
      case "custom":
        return t("usage.customRange", { defaultValue: "日历筛选" });
    }
  }
  