Created
February 27, 2015 18:28
-
-
Save xinmyname/6ad1093bb6911b5af2dc to your computer and use it in GitHub Desktop.
Deserialize WCF Date
This file contains 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
import Foundation | |
func deserializeWCFDateString(json:AnyObject?) -> NSDate? { | |
if let jsonDateString = json as? String { | |
if countElements(jsonDateString) < 18 { | |
return nil | |
} | |
let start = advance(jsonDateString.startIndex, 6) | |
let end = advance(jsonDateString.startIndex, 18) | |
let text = jsonDateString.substringWithRange(start...end) | |
let msSince1970 = (text as NSString).doubleValue | |
let secSince1970:NSTimeInterval = msSince1970 / 1000 | |
return NSDate(timeIntervalSince1970: secSince1970) | |
} | |
return nil | |
} | |
deserializeWCFDateString("/Date(1424684197910)/") | |
deserializeWCFDateString("") | |
deserializeWCFDateString(nil) | |
deserializeWCFDateString(NSNull()) | |
deserializeWCFDateString("a bunch of garbage that is long enough") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment