Better date pre compute

This commit is contained in:
Thomas Ricouard 2023-12-28 12:31:16 +01:00
parent 8f0548f45d
commit f3043b608c
1 changed files with 6 additions and 2 deletions

View File

@ -6,11 +6,11 @@ private enum CodingKeys: CodingKey {
public struct ServerDate: Codable, Hashable, Equatable, Sendable {
public let asDate: Date
private let aDay: TimeInterval = 60 * 60 * 24
private let isOlderThanADay: Bool
public var relativeFormatted: String {
let date = asDate
if Date().timeIntervalSince(date) >= aDay {
if isOlderThanADay {
return DateFormatterCache.shared.createdAtRelativeFormatter.localizedString(for: date,
relativeTo: Date())
} else {
@ -27,6 +27,7 @@ public struct ServerDate: Codable, Hashable, Equatable, Sendable {
public init() {
asDate = Date() - 100
isOlderThanADay = false
}
public init(from decoder: Decoder) throws {
@ -40,6 +41,9 @@ public struct ServerDate: Codable, Hashable, Equatable, Sendable {
let container = try decoder.container(keyedBy: CodingKeys.self)
asDate = try container.decode(Date.self, forKey: .asDate)
}
let aDay: TimeInterval = 60 * 60 * 24
isOlderThanADay = Date().timeIntervalSince(asDate) >= aDay
}
public func encode(to encoder: Encoder) throws {