51 lines
1.4 KiB
Swift
51 lines
1.4 KiB
Swift
//
|
||
// Date+humanTimeInterval.swift
|
||
// Workouts
|
||
//
|
||
// Created by rzen on 7/19/25 at 1:06 PM.
|
||
//
|
||
// Copyright 2025 Rouslan Zenetl. All Rights Reserved.
|
||
//
|
||
|
||
import Foundation
|
||
|
||
extension Date {
|
||
func humanTimeInterval(to referenceDate: Date = Date()) -> String {
|
||
let seconds = Int(referenceDate.timeIntervalSince(self))
|
||
let absSeconds = abs(seconds)
|
||
|
||
let minute = 60
|
||
let hour = 3600
|
||
let day = 86400
|
||
let week = 604800
|
||
let month = 2592000
|
||
let year = 31536000
|
||
|
||
switch absSeconds {
|
||
case 0..<5:
|
||
return "just now"
|
||
case 5..<minute:
|
||
return "\(absSeconds) seconds"
|
||
case minute..<hour:
|
||
let minutes = absSeconds / minute
|
||
return "\(minutes) minute\(minutes == 1 ? "" : "s")"
|
||
case hour..<day:
|
||
let hours = absSeconds / hour
|
||
return "\(hours) hour\(hours == 1 ? "" : "s")"
|
||
case day..<week:
|
||
let days = absSeconds / day
|
||
return "\(days) day\(days == 1 ? "" : "s")"
|
||
case week..<month:
|
||
let weeks = absSeconds / week
|
||
return "\(weeks) week\(weeks == 1 ? "" : "s")"
|
||
case month..<year:
|
||
let months = absSeconds / month
|
||
return "\(months) month\(months == 1 ? "" : "s")"
|
||
default:
|
||
let years = absSeconds / year
|
||
return "\(years) year\(years == 1 ? "" : "s")"
|
||
}
|
||
}
|
||
}
|
||
|