Skip to content

Instantly share code, notes, and snippets.

@wanbok
Created May 25, 2017 13:18
Show Gist options
  • Save wanbok/e8d0de67bef9610b3fbad5efc8ae3c43 to your computer and use it in GitHub Desktop.
Save wanbok/e8d0de67bef9610b3fbad5efc8ae3c43 to your computer and use it in GitHub Desktop.
Handle 10microseconds with DateFormatter
//
// 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