53 lines
1.5 KiB
Swift
53 lines
1.5 KiB
Swift
//
|
|
// Formatters.swift
|
|
// Haru_Danim
|
|
//
|
|
|
|
import Foundation
|
|
|
|
enum Format {
|
|
/// 1:23:45 또는 23:45 형식 (진행 중 타이머용)
|
|
static func timer(_ interval: TimeInterval) -> String {
|
|
let total = Int(interval.rounded(.down))
|
|
let h = total / 3600
|
|
let m = (total % 3600) / 60
|
|
let s = total % 60
|
|
if h > 0 { return String(format: "%d:%02d:%02d", h, m, s) }
|
|
return String(format: "%d:%02d", m, s)
|
|
}
|
|
|
|
/// "2시간 30분" / "45분" / "30초" 형식
|
|
static func durationShort(_ interval: TimeInterval) -> String {
|
|
let total = Int(interval.rounded())
|
|
let h = total / 3600
|
|
let m = (total % 3600) / 60
|
|
if h > 0 {
|
|
return m > 0 ? "\(h)시간 \(m)분" : "\(h)시간"
|
|
}
|
|
if m > 0 { return "\(m)분" }
|
|
return "\(total)초"
|
|
}
|
|
|
|
static func weekdayShort(_ weekday: Int) -> String {
|
|
let names = ["일", "월", "화", "수", "목", "금", "토"]
|
|
let index = (weekday - 1) % 7
|
|
return names[max(0, index)]
|
|
}
|
|
|
|
static func shortDate(_ date: Date) -> String {
|
|
date.formatted(.dateTime.month(.defaultDigits).day())
|
|
}
|
|
|
|
static func fullDate(_ date: Date) -> String {
|
|
date.formatted(.dateTime.year().month().day().weekday(.short))
|
|
}
|
|
|
|
static func time(_ date: Date) -> String {
|
|
date.formatted(.dateTime.hour().minute())
|
|
}
|
|
|
|
static func percent(_ ratio: Double) -> String {
|
|
"\(Int((ratio * 100).rounded()))%"
|
|
}
|
|
}
|