{
  "name": "timestamp",
  "type": "registry:ui",
  "framework": "react",
  "title": "Timestamp",
  "description": "One instant, written in the reader's own locale and time zone.",
  "files": [
    {
      "path": "ui/timestamp.tsx",
      "content": "import * as React from \"react\"\n\nimport { cn } from \"@/lib/utils\"\n\n/*\n * The timestamp a message carries as `<t:1700000000:R>`: one instant, written\n * in the reader's own locale and time zone rather than the author's.\n *\n * The styles are the ones the raw syntax names, and they map onto\n * `Intl.DateTimeFormat` because that is the thing that already knows what a\n * date looks like in a hundred places. `relative` is the exception — it is\n * `Intl.RelativeTimeFormat`, and it is the only one that keeps changing after\n * it is rendered.\n */\nexport type TimestampStyle =\n  | \"shortTime\"\n  | \"longTime\"\n  | \"shortDate\"\n  | \"longDate\"\n  | \"shortDateTime\"\n  | \"longDateTime\"\n  | \"relative\"\n\nconst FORMATS = {\n  shortTime: { hour: \"numeric\", minute: \"2-digit\" },\n  longTime: { hour: \"numeric\", minute: \"2-digit\", second: \"2-digit\" },\n  shortDate: { day: \"2-digit\", month: \"2-digit\", year: \"numeric\" },\n  longDate: { day: \"numeric\", month: \"long\", year: \"numeric\" },\n  shortDateTime: {\n    day: \"numeric\",\n    month: \"long\",\n    year: \"numeric\",\n    hour: \"numeric\",\n    minute: \"2-digit\",\n  },\n  longDateTime: {\n    weekday: \"long\",\n    day: \"numeric\",\n    month: \"long\",\n    year: \"numeric\",\n    hour: \"numeric\",\n    minute: \"2-digit\",\n  },\n} as const satisfies Record<\n  Exclude<TimestampStyle, \"relative\">,\n  Intl.DateTimeFormatOptions\n>\n\ninterface RelativeScale {\n  readonly limit: number\n  readonly unit: Intl.RelativeTimeFormatUnit\n  readonly ms: number\n}\n\n/* Anything older than a year is counted in years, so this is where the\n * ladder stops and what it falls back to. */\nconst YEARS = {\n  limit: Infinity,\n  unit: \"year\",\n  ms: 31_536_000_000,\n} as const satisfies RelativeScale\n\nconst SCALES = [\n  { limit: 60, unit: \"second\", ms: 1000 },\n  { limit: 3600, unit: \"minute\", ms: 60_000 },\n  { limit: 86_400, unit: \"hour\", ms: 3_600_000 },\n  { limit: 2_592_000, unit: \"day\", ms: 86_400_000 },\n  { limit: 31_536_000, unit: \"month\", ms: 2_592_000_000 },\n  YEARS,\n] as const satisfies readonly RelativeScale[]\n\n/** `in 3 hours`, `2 days ago` — the largest unit that still says something. */\nfunction relative(date: Date, now: Date, locale: string | undefined): string {\n  const difference = date.getTime() - now.getTime()\n  const seconds = Math.abs(difference) / 1000\n  const scale: RelativeScale =\n    SCALES.find((each) => seconds < each.limit) ?? YEARS\n  const formatter = new Intl.RelativeTimeFormat(locale, { numeric: \"auto\" })\n  return formatter.format(Math.round(difference / scale.ms), scale.unit)\n}\n\nexport interface TimestampProps extends Omit<\n  React.TimeHTMLAttributes<HTMLTimeElement>,\n  \"dateTime\"\n> {\n  /** The instant. A number is read as seconds, the way the raw syntax writes it. */\n  value: Date | number\n  /** Which of the styles to render. Defaults to the one the syntax defaults to. */\n  format?: TimestampStyle\n  /** Overrides the reader's locale, which is otherwise the browser's. */\n  locale?: string\n  /**\n   * Time zone of the text, such as `\"Europe/Paris\"`. Defaults to the zone of\n   * the runtime. Set it when the server and the browser sit in other zones.\n   */\n  timeZone?: string\n  /**\n   * How often a relative timestamp redraws, in milliseconds. Zero freezes it,\n   * which is what a server render wants.\n   */\n  tick?: number\n}\n\nconst Timestamp = React.forwardRef<HTMLTimeElement, TimestampProps>(\n  (\n    {\n      className,\n      value,\n      format = \"shortDateTime\",\n      locale,\n      timeZone,\n      tick = 60_000,\n      ...props\n    },\n    ref\n  ) => {\n    /*\n     * A bare number is seconds, not milliseconds: that is what the message\n     * syntax carries, and converting here means callers never have to\n     * remember which one this component wanted.\n     */\n    const date = React.useMemo(\n      () => (value instanceof Date ? value : new Date(value * 1000)),\n      [value]\n    )\n\n    const [now, setNow] = React.useState(() => new Date())\n\n    React.useEffect(() => {\n      if (format !== \"relative\" || tick <= 0) return\n      const timer = setInterval(() => setNow(new Date()), tick)\n      return () => clearInterval(timer)\n    }, [format, tick])\n\n    const text =\n      format === \"relative\"\n        ? relative(date, now, locale)\n        : new Intl.DateTimeFormat(locale, {\n            ...FORMATS[format],\n            timeZone,\n          }).format(date)\n\n    /*\n     * The full instant lives in the tooltip and in `dateTime`, so a relative\n     * label never hides the thing it is a label for.\n     */\n    const exact = new Intl.DateTimeFormat(locale, {\n      ...FORMATS.longDateTime,\n      timeZone,\n    }).format(date)\n\n    return (\n      <time\n        ref={ref}\n        dateTime={date.toISOString()}\n        title={exact}\n        className={cn(\n          \"rounded-[var(--newt-radius-sm)] bg-newt-bg-elevated px-1 text-newt-text-primary\",\n          className\n        )}\n        {...props}\n      >\n        {text}\n      </time>\n    )\n  }\n)\nTimestamp.displayName = \"Timestamp\"\n\nexport { Timestamp }\n",
      "type": "registry:ui"
    }
  ]
}