Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
21 lines
744 B
Swift
Executable File
21 lines
744 B
Swift
Executable File
// TimeAgo — the short "how long ago" label comments and notifications share.
|
|
|
|
import Foundation
|
|
|
|
/// "3w" / "5h" / "now" — the Dart _relativeTime buckets; nil hides the label
|
|
/// entirely (unparseable or absent date), like the Dart '' check.
|
|
func shortTimeAgo(_ date: Date?) -> String? {
|
|
guard let date else { return nil }
|
|
let seconds = max(0, Int(Date().timeIntervalSince(date)))
|
|
let days = seconds / 86_400
|
|
if days >= 365 { return "\(days / 365)y" }
|
|
if days >= 30 { return "\(days / 30)mo" }
|
|
if days >= 7 { return "\(days / 7)w" }
|
|
if days >= 1 { return "\(days)d" }
|
|
let hours = seconds / 3600
|
|
if hours >= 1 { return "\(hours)h" }
|
|
let minutes = seconds / 60
|
|
if minutes >= 1 { return "\(minutes)m" }
|
|
return "now"
|
|
}
|