This commit is contained in:
2025-07-19 16:42:47 -04:00
parent 6e46775f58
commit e3c3f2c6f0
38 changed files with 556 additions and 367 deletions

View File

@ -0,0 +1,50 @@
//
// Date+humanTimeInterval.swift
// Workouts
//
// Created by rzen on 7/19/25 at 1:06PM.
//
// 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")"
}
}
}