Skip to content

Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.

1.2 KB
Raw Blame
// SPDX-License-Identifier: AGPL-3.0-only
// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0

import type { Locale } from "./messages";

const relativeTimeFormatters = new Map<string, Intl.RelativeTimeFormat>();

function getFormatter(
  locale: Locale,
  style: "short" | "long",
): Intl.RelativeTimeFormat {
  const key = `${locale}:${style}`;
  let formatter = relativeTimeFormatters.get(key);
  if (!formatter) {
    formatter = new Intl.RelativeTimeFormat(locale, {
      numeric: "always",
      style,
    });
    relativeTimeFormatters.set(key, formatter);
  }
  return formatter;
}

export function formatRelativeTime(
  locale: Locale,
  value: number,
  unit: Intl.RelativeTimeFormatUnit,
): string {
  // format() throws on NaN, and a throw during commit would unmount the tree.
  if (!Number.isFinite(value)) {
    return "";
  }
  const short = getFormatter(locale, "short");
  const formatted = short.format(value, unit);
  // Some CLDR short patterns drop the tense marker (Arabic "few" months); fall back to long style.
  if (value !== 0 && formatted === short.format(-value, unit)) {
    return getFormatter(locale, "long").format(value, unit);
  }
  return formatted;
}