import { getDateTimeFormat } from "./intl-cache"; /** * เดือนแบบ `"YYYY-MM"` (รูปแบบเดียวกับ `CalendarMonthGrid month`) — ไม่มีวัน/เวลา จึงไม่มีปัญหาโซนเวลา * ยกจาก DueBell `CycleMonth` ให้แอปบัญชี/รายงานใช้ร่วม */ export type MonthKey = string; const MONTH_KEY = /^(\d{4})-(\d{2})$/; /** แยก `"YYYY-MM"` → ปี + เดือน (0–11); รูปแบบผิด = throw (ค่าจาก URL ให้เช็กด้วย `isMonthKey` ก่อน) */ export function parseMonthKey(key: MonthKey): { year: number; monthIndex: number } { const m = MONTH_KEY.exec(key); const monthIndex = m ? Number(m[2]) - 1 : Number.NaN; if (!m || monthIndex < 0 || monthIndex > 11) { throw new Error(`month ต้องเป็น "YYYY-MM" ได้ "${key}"`); } return { year: Number(m[1]), monthIndex }; } /** ปี ค.ศ. ที่รับ — 2400 ขึ้นไปคือปี พ.ศ. ที่พิมพ์มา ("2569-10" จะแสดงเป็น "ตุลาคม 3112") */ const MAX_YEAR = 2399; /** เช็กค่าจาก URL/ฟอร์มก่อน parse — ไม่รับเดือนนอก 01–12 และปี พ.ศ. (2400 ขึ้นไป) */ export function isMonthKey(value: unknown): value is MonthKey { if (typeof value !== "string") return false; const m = MONTH_KEY.exec(value); return !!m && Number(m[2]) >= 1 && Number(m[2]) <= 12 && Number(m[1]) <= MAX_YEAR; } function fromParts(year: number, monthIndex: number): MonthKey { return `${String(year).padStart(4, "0")}-${String(monthIndex + 1).padStart(2, "0")}`; } /** วันที่ 1 ของเดือน — `new Date(y, m)` แปลงปี 0–99 เป็น 1900–1999 จึงตั้งปีด้วย setFullYear */ function firstDay(year: number, monthIndex: number): Date { const d = new Date(2000, monthIndex, 1); d.setFullYear(year); return d; } /** Date → `"YYYY-MM"` ตามเวลาท้องถิ่นของเครื่อง */ export function toMonthKey(date: Date): MonthKey { return fromParts(date.getFullYear(), date.getMonth()); } /** * เดือนปัจจุบัน — ไม่ส่ง `timeZone` = โซนเวลาของเครื่อง * ส่ง `timeZone` (เช่น "Asia/Bangkok") เมื่อค่าถูกคำนวณทั้งบน server (UTC) และ browser แล้วต้องตรงกัน (กัน hydration mismatch ต้นเดือน) */ export function currentMonthKey(now: Date = new Date(), timeZone?: string): MonthKey { if (!timeZone) return toMonthKey(now); // MonthStepper เรียกทุก render — ใช้ formatter ที่เก็บไว้ const parts = getDateTimeFormat("en-CA", { timeZone, year: "numeric", month: "2-digit", }).formatToParts(now); const year = parts.find((p) => p.type === "year")?.value ?? String(now.getFullYear()); const month = parts.find((p) => p.type === "month")?.value ?? "01"; return `${year}-${month}`; } /** เลื่อนเดือน (ลบ = ย้อนหลัง) ข้ามปีให้เอง: shiftMonth("2026-01", -1) → "2025-12" */ export function shiftMonth(key: MonthKey, by: number): MonthKey { // คิดเป็นจำนวนเดือนตรง ๆ — new Date(y, …) แปลงปี 0–99 เป็น 19xx ("0099-01" -1 เดิมได้ "1998-12") const { year, monthIndex } = parseMonthKey(key); // ไม่ให้เลยช่วง "0000-01"–"9999-12" — ได้คีย์ที่ parseMonthKey ไม่รับ ("00-1-12") แล้ว render ถัดไป throw (0.98.2) const total = Math.min(Math.max(year * 12 + monthIndex + by, 0), 9999 * 12 + 11); return fromParts(Math.floor(total / 12), total % 12); } /** เทียบเดือน: < 0 = a ก่อน b (เทียบเป็นสตริงได้เพราะเติม 0 แล้ว) */ export function compareMonth(a: MonthKey, b: MonthKey): number { return a < b ? -1 : a > b ? 1 : 0; } /** * ชื่อเดือนสำหรับแสดง — `th-TH` ได้ปี พ.ศ. ตาม Intl (`"ตุลาคม 2569"`), `en-US` ได้ ค.ศ. * ต้องการ ค.ศ. ในภาษาไทย ใช้ locale `"th-TH-u-ca-gregory"` */ export function formatMonthKey( key: MonthKey, locale = "th-TH", style: "long" | "short" = "long", ): string { const { year, monthIndex } = parseMonthKey(key); return getDateTimeFormat(locale, { month: style, year: "numeric" }).format( firstDay(year, monthIndex), ); }