Created
May 25, 2017 13:18
-
-
Save wanbok/e8d0de67bef9610b3fbad5efc8ae3c43 to your computer and use it in GitHub Desktop.
Handle 10microseconds with DateFormatter
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// Created by Wanbok Choi on 2017. 5. 4.. | |
// | |
import ObjectMapper | |
import SwiftDate | |
enum DateFormat: String { | |
case time = "hh:mm a" | |
case normal = "yyyy-MM-dd" | |
case representation = "dd/MM/yy" | |
case ISO8601 = "yyyy-MM-dd'T'HH:mm:ss.SSSSSS'Z'" | |
case chatSection = "dd MMM yyyy" | |
} | |
final class NewISO8601DateTransform: DateFormatterTransform { | |
public init() { | |
let formatter = DateFormatter() | |
formatter.locale = Locale(identifier: "en_US_POSIX") | |
formatter.timeZone = TimeZone(identifier: "GMT") | |
formatter.dateFormat = DateFormat.ISO8601.rawValue | |
super.init(dateFormatter: formatter) | |
} | |
override func transformFromJSON(_ value: Any?) -> Date? { | |
if let dateString = value as? String { | |
let date = dateFormatter.date(from: dateString) | |
guard let microSecondString = dateString.components(separatedBy: ".").last, | |
case let startIndex = microSecondString.index(microSecondString.startIndex, offsetBy: 3), | |
case let endIndex = microSecondString.index(before: microSecondString.endIndex), | |
let microSecond = Int(microSecondString.substring(with: startIndex..<endIndex)) | |
else { return date } | |
return date?.addingTimeInterval(Double(microSecond) / 1_000_000) | |
} | |
return nil | |
} | |
override func transformToJSON(_ value: Date?) -> String? { | |
if let date = value { | |
let dateString = dateFormatter.string(from: date) | |
guard let microSecondString = String(format: "%0.6f", date.timeIntervalSinceReferenceDate).components(separatedBy: ".").last, | |
case let startIndex = microSecondString.index(microSecondString.endIndex, offsetBy: -3), | |
case let microSecond = microSecondString.substring(from: startIndex) | |
else { return dateString } | |
return dateString.replacingOccurrences(of: "000Z", with: "\(microSecond)Z") | |
} | |
return nil | |
} | |
} | |
extension Date { | |
func string(format: DateFormat, isNetwork: Bool = false) -> String { | |
let region = Region(tz: isNetwork ? .gmt : .current, cal: .current, loc: .englishUnitedStatesComputer) | |
let dateString = self.string(format: .custom(format.rawValue), in: region) | |
guard case DateFormat.ISO8601 = format, | |
let microSecondString = String(format: "%0.6f", self.timeIntervalSinceReferenceDate).components(separatedBy: ".").last, | |
case let startIndex = microSecondString.index(microSecondString.endIndex, offsetBy: -3), | |
case let microSecond = microSecondString.substring(from: startIndex) | |
else { return dateString } | |
return dateString.replacingOccurrences(of: "000Z", with: "\(microSecond)Z") | |
} | |
} | |
extension String { | |
func date(format: DateFormat, isNetwork: Bool = false) -> Date? { | |
let region = Region(tz: isNetwork ? .gmt : .current, cal: .current, loc: .englishUnitedStatesComputer) | |
return self.date(format: DateFormat.custom(format.rawValue), fromRegion: region)?.absoluteDate | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment