mirror of
https://github.com/Dimillian/IceCubesApp.git
synced 2025-04-27 02:14:45 +00:00
Better date pre compute
This commit is contained in:
parent
8f0548f45d
commit
f3043b608c
1 changed files with 6 additions and 2 deletions
|
@ -6,11 +6,11 @@ private enum CodingKeys: CodingKey {
|
||||||
|
|
||||||
public struct ServerDate: Codable, Hashable, Equatable, Sendable {
|
public struct ServerDate: Codable, Hashable, Equatable, Sendable {
|
||||||
public let asDate: Date
|
public let asDate: Date
|
||||||
private let aDay: TimeInterval = 60 * 60 * 24
|
private let isOlderThanADay: Bool
|
||||||
|
|
||||||
public var relativeFormatted: String {
|
public var relativeFormatted: String {
|
||||||
let date = asDate
|
let date = asDate
|
||||||
if Date().timeIntervalSince(date) >= aDay {
|
if isOlderThanADay {
|
||||||
return DateFormatterCache.shared.createdAtRelativeFormatter.localizedString(for: date,
|
return DateFormatterCache.shared.createdAtRelativeFormatter.localizedString(for: date,
|
||||||
relativeTo: Date())
|
relativeTo: Date())
|
||||||
} else {
|
} else {
|
||||||
|
@ -27,6 +27,7 @@ public struct ServerDate: Codable, Hashable, Equatable, Sendable {
|
||||||
|
|
||||||
public init() {
|
public init() {
|
||||||
asDate = Date() - 100
|
asDate = Date() - 100
|
||||||
|
isOlderThanADay = false
|
||||||
}
|
}
|
||||||
|
|
||||||
public init(from decoder: Decoder) throws {
|
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)
|
let container = try decoder.container(keyedBy: CodingKeys.self)
|
||||||
asDate = try container.decode(Date.self, forKey: .asDate)
|
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 {
|
public func encode(to encoder: Encoder) throws {
|
||||||
|
|
Loading…
Reference in a new issue