以下代码有效,但感觉很脏。有没有更标准的方法将带有偏移的纪元日期转换为 NSDate?
- (NSDate *) dateFromJSONString: (NSString *) JSONString{
//expects JSON from .NET WCF Service in epoch ticks, ex:
//"timeScheduled":"\/Date(1348600140000+0100)\/"
NSString *date = [[JSONString stringByReplacingOccurrencesOfString:@"/Date(" withString:@""] stringByReplacingOccurrencesOfString:@")/" withString:@""];
NSString *offsetString = [date substringFromIndex:(date.length - 5)];
//convert to seconds
NSTimeInterval dateInterval = [date doubleValue] /1000;
//gets offset value in seconds - +0100 -> 100 -> 1 -> 3600
double offsetValue = ([offsetString doubleValue] / 100) * 60 * 60;
if ([[offsetString substringToIndex:1] isEqualToString:@"+"]) {
dateInterval = dateInterval + offsetValue;
}
else{
dateInterval = dateInterval - offsetValue;
}
NSDate *retVal = [[NSDate alloc]initWithTimeIntervalSince1970:dateInterval];
return retVal;
}