Skip to content

Instantly share code, notes, and snippets.

@alicanbatur
Created April 5, 2017 11:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alicanbatur/40397fea54590b61ae5683c7c58f56be to your computer and use it in GitHub Desktop.
Save alicanbatur/40397fea54590b61ae5683c7c58f56be to your computer and use it in GitHub Desktop.
ObjectMapper date transform (formatter) for FIRServerValue.timestamp(). Firebase's timestamp.
import Foundation
import ObjectMapper
open class FirebaseDateTransform: TransformType {
public typealias Object = Date
public typealias JSON = Double
public init() {}
open func transformFromJSON(_ value: Any?) -> Date? {
if let t = value as? TimeInterval {
return Date(timeIntervalSince1970: t/1000)
}
return nil
}
open func transformToJSON(_ value: Date?) -> Double? {
if let date = value {
return Double(date.timeIntervalSince1970)
}
return nil
}
}
@phatmann
Copy link

There is an error in the transformToJSON. Should be multiplied by 1000.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment