Files
web-camera-category/app/js/naming.js
T
2026-10-24 17:32:25 +07:00

68 lines
2.2 KiB
JavaScript

// ── ตั้งชื่อโฟลเดอร์/ไฟล์ (ปี พ.ศ. ตามเวลาไทย) ───────────────
import { SITE_LABEL, TIME_ZONE } from "./config.js";
const partsFmt = new Intl.DateTimeFormat("en-GB", {
timeZone: TIME_ZONE,
year: "numeric", month: "2-digit", day: "2-digit",
hour: "2-digit", minute: "2-digit", second: "2-digit",
hour12: false,
});
export function bkkParts(date = new Date()) {
const p = {};
for (const { type, value } of partsFmt.formatToParts(date)) p[type] = value;
const year = Number(p.year);
return {
year,
thaiYear: year + 543,
month: p.month,
day: p.day,
hour: p.hour === "24" ? "00" : p.hour,
minute: p.minute,
second: p.second,
};
}
/** "Site A 2569-10-24" */
export function defaultFolderName(date = new Date()) {
const p = bkkParts(date);
return `${SITE_LABEL} ${p.thaiYear}-${p.month}-${p.day}`;
}
/** EXIF DateTimeOriginal: "YYYY:MM:DD HH:MM:SS" (เวลาไทย) */
export function exifDateString(date = new Date()) {
const p = bkkParts(date);
return `${p.year}:${p.month}:${p.day} ${p.hour}:${p.minute}:${p.second}`;
}
/** ชื่อไฟล์: 20261024-171230 */
export function fileStamp(date = new Date()) {
const p = bkkParts(date);
return `${p.year}${p.month}${p.day}-${p.hour}${p.minute}${p.second}`;
}
/** วันที่แบบอ่านง่าย: 24/10/2569 17:12 */
export function humanDateTime(ts) {
const p = bkkParts(new Date(ts));
return `${p.day}/${p.month}/${p.thaiYear} ${p.hour}:${p.minute}`;
}
/** ตัดอักขระต้องห้ามของ Windows/Android ออก */
export function safeFileName(name, fallback = "file") {
const cleaned = String(name)
.replace(/[\\/:*?"<>|\u0000-\u001f]/g, "-")
.replace(/\s+/g, " ")
.trim()
.replace(/^\.+/, "");
return cleaned || fallback;
}
/** ให้ได้ชื่อไม่ซ้ำในรายการที่มีอยู่: base, base (2), base (3) */
export function uniqueName(base, existingNames) {
const taken = new Set(existingNames);
if (!taken.has(base)) return base;
let n = 2;
while (taken.has(`${base} (${n})`)) n++;
return `${base} (${n})`;
}