{"version":3,"file":"date-utils.mjs","names":[],"sources":["../../../../src/_generated/style-rules/date-utils.ts"],"sourcesContent":["/*\n * AUTO-GENERATED FILE — DO NOT EDIT.\n * Source of truth: packages/style-rules/src/\n * Regenerate with `pnpm generate:shared` (runs automatically at build/dev/install).\n */\n\n/*!\n *\n *  Copyright (c) \"Neo4j\"\n *  Neo4j Sweden AB [http://neo4j.com]\n *\n *  This file is part of Neo4j.\n *\n *  Neo4j is free software: you can redistribute it and/or modify\n *  it under the terms of the GNU General Public License as published by\n *  the Free Software Foundation, either version 3 of the License, or\n *  (at your option) any later version.\n *\n *  This program is distributed in the hope that it will be useful,\n *  but WITHOUT ANY WARRANTY; without even the implied warranty of\n *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n *  GNU General Public License for more details.\n *\n *  You should have received a copy of the GNU General Public License\n *  along with this program.  If not, see <http://www.gnu.org/licenses/>.\n */\n\nimport { Temporal } from 'temporal-polyfill';\n\nconst MS_PER_DAY = 86_400_000;\n\nfunction plainTimeToMs(pt: Temporal.PlainTime): number {\n  return (\n    pt.hour * 3_600_000 +\n    pt.minute * 60_000 +\n    pt.second * 1_000 +\n    pt.millisecond\n  );\n}\n\nfunction parseOffsetMs(offsetStr: string): number | null {\n  const match = offsetStr.match(/^([+-])(\\d{2}):(\\d{2})$/);\n  if (!match) {\n    return null;\n  }\n  const sign = match[1] === '+' ? 1 : -1;\n  return (\n    sign *\n    (parseInt(match[2], 10) * 3_600_000 + parseInt(match[3], 10) * 60_000)\n  );\n}\n\n/**\n * Parses an ISO 8601 time string to UTC milliseconds since midnight\n * (0–86,399,999). Returns `null` if the string cannot be parsed.\n *\n * Three formats are accepted:\n * 1. Local time (no offset): `\"14:30:00\"`, `\"14:30:00.500\"` — treated as UTC\n * 2. UTC time with Z suffix: `\"14:30:00Z\"` — treated identically to the local form\n * 3. Offset time: `\"14:30:00+02:00\"` — the offset is applied so the result\n *    is the equivalent UTC time of day. `\"12:00:00+01:00\"` resolves to\n *    39,600,000 (11:00 UTC), which is less than `\"12:00:00+00:00\"` (43,200,000).\n *\n * Values wrap around midnight: `\"00:00:00+01:00\"` → 82,800,000 (23:00 UTC).\n */\nexport function parseTimeString(input: string): number | null {\n  // Reject datetime strings and anything with an IANA timezone bracket.\n  // Temporal.PlainTime.from silently extracts the time portion from strings\n  // containing a 'T' separator. IANA brackets are meaningless for time-only\n  // values (there is no date to anchor DST to), so they are also rejected.\n  if (input.includes('T') || input.includes('[')) {\n    return null;\n  }\n  // Offset time: detect \"+HH:MM\" or \"-HH:MM\" suffix first. Temporal.PlainTime.from\n  // silently strips offsets and returns the clock-face time, so we must handle\n  // offset strings explicitly before falling through to the plain parse.\n  const offsetMatch = input.match(/([+-]\\d{2}:\\d{2})(\\[.*\\])?$/);\n  if (offsetMatch) {\n    const offsetStr = offsetMatch[1];\n    const timeStr = input.slice(0, input.indexOf(offsetStr));\n    const offsetMs = parseOffsetMs(offsetStr);\n    if (offsetMs !== null) {\n      try {\n        const localMs = plainTimeToMs(Temporal.PlainTime.from(timeStr));\n        // Subtract the UTC offset to normalise to UTC time-of-day; wrap around midnight\n        return (((localMs - offsetMs) % MS_PER_DAY) + MS_PER_DAY) % MS_PER_DAY;\n      } catch {\n        /* empty */\n      }\n    }\n    return null;\n  }\n\n  // UTC time with Z suffix (e.g. \"14:30:00Z\") — strip Z and treat as local (UTC) time.\n  // Only apply when there is no 'T' in the string; a datetime string like\n  // \"2024-06-15T14:30:00Z\" must not be accepted here.\n  const normalized =\n    input.endsWith('Z') && !input.includes('T') ? input.slice(0, -1) : input;\n\n  // Local time (no offset) — treated as UTC\n  try {\n    return plainTimeToMs(Temporal.PlainTime.from(normalized));\n  } catch {\n    /* empty */\n  }\n\n  return null;\n}\n\n/**\n * Parses an ISO 8601 or RFC 9557 date/datetime string to a Unix millisecond\n * timestamp. Returns `null` if the string cannot be parsed.\n *\n * Three formats are tried in order:\n * 1. RFC 9557 ZonedDateTime with IANA bracket notation\n *    e.g. `\"2024-06-15T14:00:00[Europe/Stockholm]\"` (offset inferred from the\n *    named timezone, including DST) or `\"2024-06-15T14:00:00+02:00[Europe/Stockholm]\"`\n *    (offset explicit; bracket must agree with the IANA zone)\n * 2. UTC or fixed-offset Instant\n *    e.g. `\"2024-01-01T00:00:00Z\"`, `\"2024-01-01T00:00:00+02:00\"`\n * 3. Plain date or datetime (no offset, `Z`, or IANA bracket), interpreted as UTC\n *    e.g. `\"2024-01-01\"`, `\"2024-01-01T14:30:00\"`, `\"2024-01-01T14:30:00.500\"`\n *\n * Strings with a time component that lack `Z`, an offset, or an IANA bracket are\n * parsed as UTC wall-clock time. (Neo4j `LocalDateTime` values should instead be\n * parsed with {@link parseLocalDateString}, in the browser's local timezone.)\n */\nexport function parseDateString(input: string): number | null {\n  // RFC 9557 / ZonedDateTime with IANA bracket notation\n  try {\n    return Temporal.ZonedDateTime.from(input).epochMilliseconds;\n  } catch {\n    /* empty */\n  }\n\n  // UTC or fixed-offset datetime (no IANA name)\n  try {\n    return Temporal.Instant.from(input).epochMilliseconds;\n  } catch {\n    /* empty */\n  }\n\n  // Plain datetime — interpret as UTC.\n  // Also covers date-only strings like \"2024-01-01\" (PlainDateTime.from parses\n  // them as midnight), so a separate PlainDate path is not needed.\n  try {\n    return Temporal.PlainDateTime.from(input).toZonedDateTime('UTC')\n      .epochMilliseconds;\n  } catch {\n    /* empty */\n  }\n\n  return null;\n}\n\n/**\n * Parses a plain (unzoned) ISO 8601 date or datetime string to a Unix\n * millisecond timestamp, interpreting the wall-clock time in the given\n * `timeZone` (defaults to the browser's local timezone via\n * `Temporal.Now.timeZoneId()`). Returns `null` if the string cannot be\n * parsed or contains any timezone qualifier.\n *\n * Only plain date/datetime strings are accepted — strings with `Z`, a\n * fixed UTC offset (`+02:00`), or an IANA bracket (`[Europe/Stockholm]`)\n * are **rejected** and return `null`. This mirrors the semantics of\n * Neo4j `LocalDateTime`, which has no associated timezone.\n *\n * Accepted formats:\n * - Plain datetime: `\"2024-01-01T14:30:00\"`, `\"2024-01-01T14:30:00.500\"`\n * - Date-only (treated as local midnight): `\"2024-01-01\"`\n *\n * The optional `timeZone` parameter overrides the browser's local timezone,\n * which is useful for deterministic testing.\n */\nexport function parseLocalDateString(\n  input: string,\n  timeZone?: string,\n): number | null {\n  // Reject strings that carry explicit timezone information. This mirrors the\n  // cascade in parseDateString but uses successful parsing as a rejection\n  // signal rather than an acceptance signal:\n  //\n  // - ZonedDateTime.from succeeds  → string has an IANA bracket → reject\n  // - Instant.from succeeds        → string has Z or a fixed offset → reject\n  // - PlainDateTime.from succeeds  → truly plain string → accept\n  //\n  // The polyfill's PlainDateTime.from() silently strips offsets/brackets\n  // instead of throwing, so these two pre-checks are necessary to enforce\n  // the plain-only constraint.\n  try {\n    Temporal.ZonedDateTime.from(input);\n    return null;\n  } catch {\n    /* not a zoned datetime — continue */\n  }\n\n  try {\n    Temporal.Instant.from(input);\n    return null;\n  } catch {\n    /* not a UTC/offset instant — continue */\n  }\n\n  const tz = timeZone ?? Temporal.Now.timeZoneId();\n  try {\n    return Temporal.PlainDateTime.from(input).toZonedDateTime(tz)\n      .epochMilliseconds;\n  } catch {\n    /* empty */\n  }\n\n  return null;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;AA6BA,MAAM,aAAa;AAEnB,SAAS,cAAc,IAAgC;CACrD,OACE,GAAG,OAAO,OACV,GAAG,SAAS,MACZ,GAAG,SAAS,MACZ,GAAG;AAEP;AAEA,SAAS,cAAc,WAAkC;CACvD,MAAM,QAAQ,UAAU,MAAM,yBAAyB;CACvD,IAAI,CAAC,OACH,OAAO;CAGT,QADa,MAAM,OAAO,MAAM,IAAI,OAGjC,SAAS,MAAM,IAAI,EAAE,IAAI,OAAY,SAAS,MAAM,IAAI,EAAE,IAAI;AAEnE;;;;;;;;;;;;;;AAeA,SAAgB,gBAAgB,OAA8B;CAK5D,IAAI,MAAM,SAAS,GAAG,KAAK,MAAM,SAAS,GAAG,GAC3C,OAAO;CAKT,MAAM,cA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