Files
workouts/Workouts/Utils/Date+humanTimeInterval.swift
2025-07-25 17:42:25 -04:00

51 lines
1.4 KiB
Swift
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//
// 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")"
}
}
}