-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRelativeTimeText.swift
More file actions
39 lines (34 loc) · 1 KB
/
RelativeTimeText.swift
File metadata and controls
39 lines (34 loc) · 1 KB
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
//
// RelativeTimeText.swift
// DevLog
//
// Created by opfic on 3/25/26.
//
import SwiftUI
struct RelativeTimeText: View {
let date: Date
var bodyFont: Font = .caption2
var bodyColor: Color = .gray
var body: some View {
TimelineView(.periodic(from: .now, by: 1.0)) { context in
Text(relativeTimeText(from: date, now: context.date) + " 업데이트")
.font(bodyFont)
.foregroundStyle(bodyColor)
}
}
private func relativeTimeText(from date: Date, now: Date) -> String {
let seconds = Int(now.timeIntervalSince(date))
if seconds < 60 {
return "\(max(0, seconds))초 전"
} else if seconds < 3600 {
let minutes = seconds / 60
return "\(minutes)분 전"
} else if seconds < 86400 {
let hours = seconds / 3600
return "\(hours)시간 전"
} else {
let days = seconds / 86400
return "\(days)일 전"
}
}
}