- lib/date.js: dayjs 기반 공통 날짜 유틸리티 모듈 추가 - toKST, formatDate, formatTime, utcToKSTDateTime, nowKST - parseNitterDateTime (Nitter 날짜 파싱) - dayjs/plugin/utc, timezone, customParseFormat 사용 - youtube-bot.js: 로컬 날짜 함수 제거, lib/date.js 사용 - x-bot.js: 로컬 날짜 함수 제거, lib/date.js 사용 - 중복 코드 제거로 유지보수성 향상
85 lines
2 KiB
JavaScript
85 lines
2 KiB
JavaScript
/**
|
|
* 날짜/시간 유틸리티 (dayjs 기반)
|
|
* 백엔드 전체에서 공통으로 사용
|
|
*/
|
|
|
|
import dayjs from "dayjs";
|
|
import utc from "dayjs/plugin/utc.js";
|
|
import timezone from "dayjs/plugin/timezone.js";
|
|
import customParseFormat from "dayjs/plugin/customParseFormat.js";
|
|
|
|
// 플러그인 등록
|
|
dayjs.extend(utc);
|
|
dayjs.extend(timezone);
|
|
dayjs.extend(customParseFormat);
|
|
|
|
// 기본 시간대: KST
|
|
const KST = "Asia/Seoul";
|
|
|
|
/**
|
|
* UTC 시간을 KST로 변환
|
|
* @param {Date|string} utcDate - UTC 시간
|
|
* @returns {dayjs.Dayjs} KST 시간
|
|
*/
|
|
export function toKST(utcDate) {
|
|
return dayjs(utcDate).tz(KST);
|
|
}
|
|
|
|
/**
|
|
* 날짜를 YYYY-MM-DD 형식으로 포맷
|
|
* @param {Date|string|dayjs.Dayjs} date - 날짜
|
|
* @returns {string} YYYY-MM-DD
|
|
*/
|
|
export function formatDate(date) {
|
|
return dayjs(date).format("YYYY-MM-DD");
|
|
}
|
|
|
|
/**
|
|
* 시간을 HH:mm:ss 형식으로 포맷
|
|
* @param {Date|string|dayjs.Dayjs} date - 시간
|
|
* @returns {string} HH:mm:ss
|
|
*/
|
|
export function formatTime(date) {
|
|
return dayjs(date).format("HH:mm:ss");
|
|
}
|
|
|
|
/**
|
|
* UTC 시간을 KST로 변환 후 날짜/시간 분리
|
|
* @param {Date|string} utcDate - UTC 시간
|
|
* @returns {{date: string, time: string}} KST 날짜/시간
|
|
*/
|
|
export function utcToKSTDateTime(utcDate) {
|
|
const kst = toKST(utcDate);
|
|
return {
|
|
date: kst.format("YYYY-MM-DD"),
|
|
time: kst.format("HH:mm:ss"),
|
|
};
|
|
}
|
|
|
|
/**
|
|
* 현재 KST 시간 반환
|
|
* @returns {dayjs.Dayjs} 현재 KST 시간
|
|
*/
|
|
export function nowKST() {
|
|
return dayjs().tz(KST);
|
|
}
|
|
|
|
/**
|
|
* Nitter 날짜 문자열 파싱 (UTC 반환)
|
|
* 예: "Jan 9, 2026 · 4:00 PM UTC" → Date 객체
|
|
* @param {string} timeStr - Nitter 날짜 문자열
|
|
* @returns {dayjs.Dayjs|null} UTC 시간
|
|
*/
|
|
export function parseNitterDateTime(timeStr) {
|
|
if (!timeStr) return null;
|
|
try {
|
|
const cleaned = timeStr.replace(" · ", " ").replace(" UTC", "");
|
|
const date = dayjs.utc(cleaned, "MMM D, YYYY h:mm A");
|
|
if (!date.isValid()) return null;
|
|
return date;
|
|
} catch (e) {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
export default dayjs;
|