summaryrefslogtreecommitdiff
path: root/lib/swp/utils.ts
diff options
context:
space:
mode:
Diffstat (limited to 'lib/swp/utils.ts')
-rw-r--r--lib/swp/utils.ts68
1 files changed, 68 insertions, 0 deletions
diff --git a/lib/swp/utils.ts b/lib/swp/utils.ts
new file mode 100644
index 00000000..1856628c
--- /dev/null
+++ b/lib/swp/utils.ts
@@ -0,0 +1,68 @@
+/**
+ * SWP 관련 유틸리티 함수
+ */
+
+/**
+ * SWP API 날짜 포맷팅 (간단 버전)
+ * 오전/오후 → AM/PM 변환 및 (KST) 추가
+ *
+ * @param dateStr "2017-02-20 오전 8:52:00" 형식
+ * @returns "2017-02-20 AM 8:52:00 (KST)"
+ */
+export function formatSwpDate(dateStr: string): string {
+ if (!dateStr || dateStr === "1900-01-01 오전 12:00:00") return "-";
+ return dateStr.replace("오후", "PM").replace("오전", "AM") + " (KST)";
+}
+
+/**
+ * SWP API 날짜 포맷팅 (간결 버전)
+ * 오전/오후를 24시간 형식으로 변환하고 초 제거
+ *
+ * @param dateStr "2017-02-20 오전 8:52:00" 형식
+ * @returns "2017-02-20 08:52"
+ */
+export function formatSwpDateShort(dateStr: string): string {
+ if (!dateStr || dateStr === "1900-01-01 오전 12:00:00") return "-";
+
+ try {
+ // 날짜와 시간 분리
+ const parts = dateStr.split(" ");
+ if (parts.length < 3) return formatSwpDate(dateStr);
+
+ const datePart = parts[0]; // "2017-02-20"
+ const ampm = parts[1]; // "오전" or "오후"
+ const timePart = parts[2]; // "8:52:00"
+
+ // 시간 파싱
+ const [hours, minutes] = timePart.split(":");
+ let hour = parseInt(hours, 10);
+
+ // 오후인 경우 12시간 더하기 (12시는 제외)
+ if (ampm === "오후" && hour !== 12) {
+ hour += 12;
+ } else if (ampm === "오전" && hour === 12) {
+ hour = 0;
+ }
+
+ return `${datePart} ${hour.toString().padStart(2, "0")}:${minutes}`;
+ } catch {
+ return formatSwpDate(dateStr);
+ }
+}
+
+/**
+ * 파일 크기 포맷팅
+ *
+ * @param sizeStr 바이트 단위 파일 크기 문자열
+ * @returns "1.23 MB" 또는 "456.78 KB" 형식
+ */
+export function formatFileSize(sizeStr: string): string {
+ const bytes = parseInt(sizeStr, 10);
+ if (isNaN(bytes)) return sizeStr;
+
+ const kb = bytes / 1024;
+ const mb = kb / 1024;
+
+ return mb >= 1 ? `${mb.toFixed(2)} MB` : `${kb.toFixed(2)} KB`;
+}
+