2012-07-31
JSON, .NET and NSDate Redux
Here’s a version of my JSON date to NSDate conversion function updated to convert correctly to the local timezone.
/**
* Converts a .NET JSON date in the format "/Date(x)/" to an NSDate object.
* @param string The NSString to convert.
* @return the NSDate object.
*/
(NSDate*)jsonStringToNSDate(NSString* string) {
if (string == nil) return nil;
// If we encounter .NET's minimum date value we treat it as nil.
if ([string isEqualToString:@"/Date(-59011459200000)/"]) return nil;
// Extract the numeric part of the date. Dates should be in the format
// "/Date(x)/", where x is a number. This format is supplied automatically
// by JSON serialisers in .NET.
NSRange range = NSMakeRange(6, [string length] - 8);
NSString* substring = [string substringWithRange:range];
NSNumberFormatter* formatter = [[NSNumberFormatter alloc] init];
NSNumber* milliseconds = [formatter numberFromString:substring];
[formatter release];
// NSTimeInterval is specified in seconds. The value we get back from the
// web service is specified in milliseconds. Both values are since 1st Jan
// 1970 (epoch).
NSTimeInterval seconds = [milliseconds longLongValue] / 1000.0;
seconds += [[NSTimeZone localTimeZone] secondsFromGMT];
return [NSDate dateWithTimeIntervalSince1970:seconds];
}
Edit: Fixed to return milliseconds as the fractional part of the “seconds” value.