forked from isair/JSONHelper
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDate.swift
More file actions
25 lines (20 loc) · 746 Bytes
/
Date.swift
File metadata and controls
25 lines (20 loc) · 746 Bytes
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
//
// Copyright © 2016 Baris Sencan. All rights reserved.
//
import Foundation
extension Date: Convertible {
fileprivate static let sharedFormatter = DateFormatter()
public static func convert<T>(fromValue value: T?) throws -> Date? {
guard let value = value else { return nil }
if let unixTimestamp = value as? Int {
return self.init(timeIntervalSince1970: TimeInterval(unixTimestamp))
} else if let dateString = value as? String {
if let convertedDate = JSONHelper.dateFormatter.date(from: dateString) {
return self.init(timeIntervalSince1970: convertedDate.timeIntervalSince1970)
} else {
throw ConversionError.invalidValue
}
}
throw ConversionError.unsupportedType
}
}