我在将字符串转换为日期时遇到了一个奇怪的问题:
NSString *goodDateString = @"2012-06-28 16:08:56.871000";
NSString *badDateString = @"2012-06-28 16:11:17.999771";
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss.SSS"];
NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:@"UTC"];
[formatter setTimeZone:timeZone];
NSDate *goodDate = [formatter dateFromString:goodDateString];
NSDate *badDate = [formatter dateFromString:badDateString];
NSAssert(goodDate, @"date is nil"); //passes this test
NSAssert(badDate, @"date is nil"); //fails this test
由于某些原因,当我尝试将其转换为日期时,badDateString返回nil。我不知道为什么。日期字符串有问题吗?你知道吗
编辑:字符串来自Python服务器。我在这个日期使用dateString = date.strftime('%Y-%m-%d %H:%M:%S.%f')
:2012-06-28 16:11:17
,它返回2012-06-28 16:11:17.999771
,上面的日期格式化程序无法解析它。任何python人都知道如何将日期的最后一部分限制为3位小数,而不是6位小数?你知道吗
看起来可能是虫子。这与^{} 将分数秒999771舍入到下一整秒有关,并且由于某种原因导致格式失败。如果您将分数秒更改为
999499
,它将工作,但只需将其更改为9995
,它将失败。我会考虑提交一个bug report并将其添加到Open Radar。同时,只需编写代码来截断3位数后的小数秒。你知道吗相关问题 更多 >
编程相关推荐