-
Notifications
You must be signed in to change notification settings - Fork 14
/
TimestampDateTransform.swift
49 lines (41 loc) · 1.56 KB
/
TimestampDateTransform.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
//
// TimestampDateTransformer.swift
// CodableExample
//
// Created by Condy on 2024/7/4.
//
import Foundation
/// Decodes String or TimeInterval values as an Since1970 time `Date`.
open class TimestampDateTransform: TransformType {
public enum TimestampSince1970Type: TimeInterval {
/// The interval is in seconds since midnight UTC on January 1st, 1970.
case seconds = 1
/// The interval is in milliseconds since midnight UTC on January 1st, 1970.
case milliseconds = 1_000
/// The interval is in microseconds since midnight UTC on January 1st, 1970.
case microseconds = 1_000_000
/// The interval is in nanoseconds since midnight UTC on January 1st, 1970.
case nanoseconds = 1_000_000_000
}
public typealias Object = Date
public typealias JSON = TimeInterval
let type: TimestampSince1970Type
public init(type: TimestampSince1970Type = .seconds) {
self.type = type
}
open func transformFromJSON(_ value: Any) -> Date? {
if let value = value as? TimeInterval {
return Date(timeIntervalSince1970: value / type.rawValue)
}
if let value = value as? String ?? {
Hollow.transfer2String(with: value)
}() {
let timeInterval = TimeInterval(atof(value)) / type.rawValue
return Date(timeIntervalSince1970: timeInterval)
}
return nil
}
open func transformToJSON(_ value: Date) -> TimeInterval? {
return value.timeIntervalSince1970 * type.rawValue
}
}