mirror of
https://github.com/metabolist/metatext.git
synced 2024-12-01 12:30:59 +00:00
81 lines
2.9 KiB
Swift
81 lines
2.9 KiB
Swift
// Copyright © 2020 Metabolist. All rights reserved.
|
|
|
|
import Foundation
|
|
|
|
extension Date {
|
|
var timeAgo: String? {
|
|
let calendar = Calendar.autoupdatingCurrent
|
|
let now = Date()
|
|
|
|
if
|
|
let oneMinuteAgo = calendar.date(byAdding: DateComponents(minute: -1), to: now),
|
|
oneMinuteAgo < self {
|
|
Self.abbreviatedDateComponentsFormatter.allowedUnits = [.second]
|
|
} else if
|
|
let oneHourAgo = calendar.date(byAdding: DateComponents(hour: -1), to: now),
|
|
oneHourAgo < self {
|
|
Self.abbreviatedDateComponentsFormatter.allowedUnits = [.minute]
|
|
} else if
|
|
let oneDayAgo = calendar.date(byAdding: DateComponents(day: -1), to: now),
|
|
oneDayAgo < self {
|
|
Self.abbreviatedDateComponentsFormatter.allowedUnits = [.hour]
|
|
} else if
|
|
let oneWeekAgo = calendar.date(byAdding: DateComponents(weekOfMonth: -1), to: now),
|
|
oneWeekAgo < self {
|
|
Self.abbreviatedDateComponentsFormatter.allowedUnits = [.day]
|
|
} else {
|
|
return Date.shortDateStyleRelativeDateFormatter.string(from: self)
|
|
}
|
|
|
|
return Self.abbreviatedDateComponentsFormatter.string(from: self, to: now)
|
|
}
|
|
|
|
var fullUnitTimeUntil: String? {
|
|
let calendar = Calendar.autoupdatingCurrent
|
|
let now = Date()
|
|
|
|
if
|
|
let oneDayFromNow = calendar.date(byAdding: DateComponents(day: 1), to: now),
|
|
self > oneDayFromNow {
|
|
Self.fullDateComponentsFormatter.allowedUnits = [.day]
|
|
} else if
|
|
let oneHourFromNow = calendar.date(byAdding: DateComponents(hour: 1), to: now),
|
|
self > oneHourFromNow {
|
|
Self.fullDateComponentsFormatter.allowedUnits = [.hour]
|
|
} else if
|
|
let oneMinuteFromNow = calendar.date(byAdding: DateComponents(minute: 1), to: now),
|
|
self > oneMinuteFromNow {
|
|
Self.fullDateComponentsFormatter.allowedUnits = [.minute]
|
|
} else {
|
|
Self.fullDateComponentsFormatter.allowedUnits = [.second]
|
|
}
|
|
|
|
return Self.fullDateComponentsFormatter.string(from: now, to: self)
|
|
}
|
|
}
|
|
|
|
private extension Date {
|
|
private static let abbreviatedDateComponentsFormatter: DateComponentsFormatter = {
|
|
let dateComponentsFormatter = DateComponentsFormatter()
|
|
|
|
dateComponentsFormatter.unitsStyle = .abbreviated
|
|
|
|
return dateComponentsFormatter
|
|
}()
|
|
|
|
private static let fullDateComponentsFormatter: DateComponentsFormatter = {
|
|
let dateComponentsFormatter = DateComponentsFormatter()
|
|
|
|
dateComponentsFormatter.unitsStyle = .full
|
|
|
|
return dateComponentsFormatter
|
|
}()
|
|
|
|
private static let shortDateStyleRelativeDateFormatter: DateFormatter = {
|
|
let dateFormatter = DateFormatter()
|
|
|
|
dateFormatter.dateStyle = .short
|
|
|
|
return dateFormatter
|
|
}()
|
|
}
|