58 lines
1.8 KiB
Swift
58 lines
1.8 KiB
Swift
//
|
|
// GetLatestFinishedLiveResponse.swift
|
|
// SodaLive
|
|
//
|
|
// Created by klaus on 7/22/25.
|
|
//
|
|
|
|
import Foundation
|
|
|
|
struct GetLatestFinishedLiveResponse: Decodable {
|
|
let memberId: Int
|
|
let nickname: String
|
|
let profileImageUrl: String
|
|
let timeAgo: String
|
|
let dateUtc: String
|
|
}
|
|
|
|
// MARK: - 상대 시간 문자열
|
|
extension GetLatestFinishedLiveResponse {
|
|
/// `dateUtc`(UTC 문자열)을 파싱해 디바이스 타임존 기준
|
|
/// 상대 시간(방금 전/분/시간/일/개월/년 전)을 반환합니다.
|
|
/// 파싱 실패 시, `date`를 그대로 반환합니다.
|
|
func relativeTimeText(now: Date = Date()) -> String {
|
|
guard let createdAt = DateParser.parse(dateUtc) else {
|
|
return timeAgo
|
|
}
|
|
|
|
let nowDate = now
|
|
let interval = max(0, nowDate.timeIntervalSince(createdAt))
|
|
|
|
// 연/월 차이는 달력 기준으로 계산(윤년/월 길이 반영)
|
|
let calendar = Calendar.current
|
|
let ym = calendar.dateComponents([.year, .month],
|
|
from: createdAt,
|
|
to: nowDate)
|
|
if let years = ym.year, years >= 1 {
|
|
return I18n.Time.yearsAgo(years)
|
|
}
|
|
if let months = ym.month, months >= 1 {
|
|
return I18n.Time.monthsAgo(months)
|
|
}
|
|
|
|
// 일/시간/분은 초 기반으로 간단 처리
|
|
if interval < 60 {
|
|
return I18n.Time.justNow
|
|
} else if interval < 3600 {
|
|
let minutes = Int(interval / 60)
|
|
return I18n.Time.minutesAgo(max(1, minutes))
|
|
} else if interval < 86_400 {
|
|
let hours = Int(interval / 3600)
|
|
return I18n.Time.hoursAgo(max(1, hours))
|
|
} else {
|
|
let days = Int(interval / 86_400)
|
|
return I18n.Time.daysAgo(max(1, days))
|
|
}
|
|
}
|
|
}
|