Meshtastic-Apple/Meshtastic/Extensions/Date.swift

41 lines
967 B
Swift
Raw Permalink Normal View History

2023-04-25 17:56:57 -07:00
//
// Date.swift
// Meshtastic
//
// Copyright(c) Garth Vander Houwen 4/25/23.
//
import Foundation
extension Date {
var lastHeard: String {
2025-03-30 18:13:03 -07:00
if self.timeIntervalSince1970 > 0 && self < Calendar.current.date(byAdding: .year, value: 1, to: Date())! {
formatted()
} else {
2025-05-03 08:58:33 -07:00
"Unknown Age".localized
}
}
2023-04-25 17:56:57 -07:00
func formattedDate(format: String) -> String {
let dateformat = DateFormatter()
dateformat.dateFormat = format
2025-03-30 18:13:03 -07:00
if self.timeIntervalSince1970 > 0 && self < Calendar.current.date(byAdding: .year, value: 1, to: Date())! {
2023-09-04 20:17:09 -07:00
return dateformat.string(from: self)
} else {
2025-05-03 08:58:33 -07:00
return "Unknown Age".localized
2023-09-04 20:17:09 -07:00
}
2023-04-25 17:56:57 -07:00
}
2024-04-14 14:51:59 -07:00
func relativeTimeOfDay() -> String {
let hour = Calendar.current.component(.hour, from: self)
2024-04-14 14:51:59 -07:00
switch hour {
2025-05-08 08:59:24 -07:00
case 6..<12: return "Morning".localized
case 12: return "Midday".localized
case 13..<17: return "Afternoon".localized
case 17..<22: return "Evening".localized
default: return "Nighttime".localized
2024-04-14 14:51:59 -07:00
}
}
2023-04-25 17:56:57 -07:00
}