export function getTZParts(date: Date, timeZone: string) {
  const fmt = new Intl.DateTimeFormat("en-CA", {
    timeZone,
    year: "numeric",
    month: "2-digit",
    day: "2-digit",
    hour: "2-digit",
    minute: "2-digit",
    second: "2-digit",
    hour12: false,
  })
  const parts = Object.fromEntries(fmt.formatToParts(date).map((p) => [p.type, p.value])) as Record<string, string>
  return {
    year: Number(parts.year),
    month: Number(parts.month),
    day: Number(parts.day),
    hour: Number(parts.hour),
    minute: Number(parts.minute),
    second: Number(parts.second),
  }
}

export function dateKeyTZ(date: Date, timeZone: string) {
  const p = getTZParts(date, timeZone)
  const y = String(p.year).padStart(4, "0")
  const m = String(p.month).padStart(2, "0")
  const d = String(p.day).padStart(2, "0")
  return `${y}-${m}-${d}`
}

export function isAtHourTZ(date: Date, timeZone: string, hour: number) {
  const p = getTZParts(date, timeZone)
  return p.hour === hour
}

