mirror of
https://github.com/Dimillian/IceCubesApp.git
synced 2025-01-08 15:25:24 +00:00
Fix relative date
This commit is contained in:
parent
d5177f9b2d
commit
22b4044dfd
1 changed files with 9 additions and 11 deletions
|
@ -1,13 +1,19 @@
|
||||||
import Foundation
|
import Foundation
|
||||||
|
|
||||||
fileprivate enum CodingKeys: CodingKey {
|
fileprivate enum CodingKeys: CodingKey {
|
||||||
case asDate, relativeFormatted, shortDateFormatted
|
case asDate
|
||||||
}
|
}
|
||||||
|
|
||||||
public struct ServerDate: Codable, Hashable, Equatable {
|
public struct ServerDate: Codable, Hashable, Equatable {
|
||||||
public let asDate: Date
|
public let asDate: Date
|
||||||
public let relativeFormatted: String
|
|
||||||
public let shortDateFormatted: String
|
public var relativeFormatted: String {
|
||||||
|
Self.createdAtRelativeFormatter.localizedString(for: asDate, relativeTo: Date())
|
||||||
|
}
|
||||||
|
|
||||||
|
public var shortDateFormatted: String {
|
||||||
|
Self.createdAtShortDateFormatted.string(from: asDate)
|
||||||
|
}
|
||||||
|
|
||||||
private static let calendar = Calendar(identifier: .gregorian)
|
private static let calendar = Calendar(identifier: .gregorian)
|
||||||
|
|
||||||
|
@ -34,8 +40,6 @@ public struct ServerDate: Codable, Hashable, Equatable {
|
||||||
|
|
||||||
public init() {
|
public init() {
|
||||||
asDate = Date()-100
|
asDate = Date()-100
|
||||||
relativeFormatted = Self.createdAtRelativeFormatter.localizedString(for: asDate, relativeTo: Date())
|
|
||||||
shortDateFormatted = Self.createdAtShortDateFormatted.string(from: asDate)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public init(from decoder: Decoder) throws {
|
public init(from decoder: Decoder) throws {
|
||||||
|
@ -44,21 +48,15 @@ public struct ServerDate: Codable, Hashable, Equatable {
|
||||||
let container = try decoder.singleValueContainer()
|
let container = try decoder.singleValueContainer()
|
||||||
let stringDate = try container.decode(String.self)
|
let stringDate = try container.decode(String.self)
|
||||||
asDate = Self.createdAtDateFormatter.date(from: stringDate)!
|
asDate = Self.createdAtDateFormatter.date(from: stringDate)!
|
||||||
relativeFormatted = Self.createdAtRelativeFormatter.localizedString(for: asDate, relativeTo: Date())
|
|
||||||
shortDateFormatted = Self.createdAtShortDateFormatted.string(from: asDate)
|
|
||||||
} catch {
|
} catch {
|
||||||
// Decode from cache
|
// Decode from cache
|
||||||
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)
|
||||||
relativeFormatted = try container.decode(String.self, forKey: .relativeFormatted)
|
|
||||||
shortDateFormatted = try container.decode(String.self, forKey: .shortDateFormatted)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public func encode(to encoder: Encoder) throws {
|
public func encode(to encoder: Encoder) throws {
|
||||||
var container = encoder.container(keyedBy: CodingKeys.self)
|
var container = encoder.container(keyedBy: CodingKeys.self)
|
||||||
try container.encode(asDate, forKey: .asDate)
|
try container.encode(asDate, forKey: .asDate)
|
||||||
try container.encode(relativeFormatted, forKey: .relativeFormatted)
|
|
||||||
try container.encode(shortDateFormatted, forKey: .shortDateFormatted)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue