簡體   English   中英

將NSString轉換為日期錯誤

[英]Convert NSString to date error

我有一個奇怪的問題,將字符串轉換為日期:

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位?

看起來它可能是一個bug。 這與SSS將小數秒999771向下舍入到下一整秒並且由於某種原因導致格式失敗的事實有關。 如果您將小數秒更改為999499它將起作用,但只是將其更改為9995將導致它失敗。 我會考慮提交錯誤報告並將其添加到Open Radar 在平均時間內只需編寫代碼來截斷超過3位的小數秒。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM