summaryrefslogtreecommitdiff
path: root/lib/dolce/utils/date-formatter.ts
blob: ebf9653a8586f7c12aea796fb86147e72ccd45f3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/**
 * DOLCE 날짜 포맷팅 유틸리티
 * 
 * SWP의 날짜 포맷팅 함수를 재사용
 * 모든 날짜는 KST (Korea Standard Time, GMT+9) 타임존
 */

import { formatSwpDate } from "@/lib/swp/utils";
import { format, parseISO, isValid } from "date-fns";

/**
 * SWP와 동일한 방식
 * Postgres Date 타입(ISO String) 처리 추가
 */
export function formatDolceDateTime(dateStr: string | null): string {
  if (!dateStr) return "-";

  // ISO string check (e.g. "2023-11-27T10:20:30.000Z" or similar)
  if (dateStr.includes("-") && (dateStr.includes("T") || dateStr.includes(":"))) {
    try {
      const date = parseISO(dateStr);
      if (isValid(date)) {
        // Requested format: YYYY-MM-DD AM/PM HH:mm:ss
        // date-fns format: yyyy-MM-dd a hh:mm:ss
        return format(date, "yyyy-MM-dd a hh:mm:ss");
      }
    } catch (e) {
      // ignore parse error, fallback to default
      console.error("Date parse error", e);
    }
  }

  return formatSwpDate(dateStr);
}

/**
 * YYYYMMDD 형식을 YYYY-MM-DD로 변환
 * 
 * @param dateStr "20170220" 형식
 * @returns "2017-02-20"
 */
export function formatDolceDateYYYYMMDD(dateStr: string | null): string | null {
  if (!dateStr || dateStr.length !== 8) return null;
  
  try {
    const year = dateStr.substring(0, 4);
    const month = dateStr.substring(4, 6);
    const day = dateStr.substring(6, 8);
    
    return `${year}-${month}-${day}`;
  } catch {
    return dateStr;
  }
}

/**
 * 통합 날짜 포맷팅
 * 
 * @param dateStr 날짜 문자열 (다양한 형식 지원)
 * @returns 포맷팅된 날짜 문자열
 */
export function formatDolceDate(dateStr: string | null): string {
  if (!dateStr) return "-";
  
  // YYYYMMDD 형식 (8자리 숫자)
  if (/^\d{8}$/.test(dateStr)) {
    return formatDolceDateYYYYMMDD(dateStr) || dateStr;
  }
  
  // 날짜+시간 형식
  return formatDolceDateTime(dateStr);
}