Objective c NSDateFormatter难题!这是新的山狮虫吗?
我相信我可能在Objective c NSDateFormatter难题!这是新的山狮虫吗?,objective-c,macos,cocoa,nsdateformatter,Objective C,Macos,Cocoa,Nsdateformatter,我相信我可能在NSDateFormatter中发现了一个bug,我不确定这是否本地化为在系统首选项中指定了巴西时区 这里有一个完整的项目: 守则的相关摘录如下: NSArray *unformattedDatesArray = [[NSArray alloc] initWithObjects:@"2011-09-18", @"2011-10-16",@"2011-11-13",@"2011-12-11",@"2012-01-08",@"2012-02-05", nil]; NSStr
NSDateFormatter
中发现了一个bug,我不确定这是否本地化为在系统首选项中指定了巴西时区
这里有一个完整的项目:
守则的相关摘录如下:
NSArray *unformattedDatesArray = [[NSArray alloc] initWithObjects:@"2011-09-18",
@"2011-10-16",@"2011-11-13",@"2011-12-11",@"2012-01-08",@"2012-02-05", nil];
NSString *resultedString = @"";
int ii = 0;
NSUInteger items = [unformattedDatesArray count];
for (ii=0; ii<items; ii++) {
NSLog(@"original item # %d = %@",ii,[unformattedDatesArray objectAtIndex:ii]);
resultedString = [resultedString stringByAppendingString:@"original item # "];
resultedString = [resultedString stringByAppendingString:[NSString stringWithFormat: @"%d = ",ii]];
resultedString = [resultedString stringByAppendingString:[unformattedDatesArray objectAtIndex:ii]];
resultedString = [resultedString stringByAppendingString:[NSString stringWithFormat:@"\n"]];
}
NSDateFormatter *originalFormat = [[NSDateFormatter alloc] init];
[originalFormat setDateFormat:@"yyyy-MM-dd"]; // hh:mm:ss
NSDateFormatter *newFormat = [[NSDateFormatter alloc] init];
[newFormat setDateFormat:@"dd/MM/yyyy"]; // hh:mm:ss
NSString *formattedDate;
for (ii=0; ii<items; ii++) {
formattedDate = [newFormat stringFromDate:[originalFormat
dateFromString:[unformattedDatesArray objectAtIndex:ii]]];
NSLog(@"formatted item # %d = %@",ii,formattedDate);
resultedString = [resultedString stringByAppendingString:@"formatted item # "];
resultedString = [resultedString stringByAppendingString:[NSString stringWithFormat: @"%d = ",ii]];
resultedString = [resultedString stringByAppendingString:(formattedDate == nil ? @"ERROR" : formattedDate)];
resultedString = [resultedString stringByAppendingString:[NSString stringWithFormat:@"\n"]];
}
[resultingStringTextView insertText:resultedString];
请注意,如果转到系统首选项-->日期和时间-->时区
把你的时区改成里约热内卢,你会得到同样的结果
==========================================================尝试解决问题===================================
我正试图找到一个解决这个问题的方法,而苹果并没有修复它
我有两个问题,一个是从这个日期开始呈现PDF文件,我想我已经通过以下方式解决了这个问题:
if (formated_issue_date == NULL) {
formated_issue_date = [BHDateFormatter bhDateFormat:data_emissao];
}
使用此处理程序:
+(NSString*)bhDateFormat:(NSString *)inputedDate{
NSString *formattedDate = @"";
NSRange range82 = NSMakeRange (8, 2);
NSRange range52 = NSMakeRange (5, 2);
NSRange range04 = NSMakeRange (0, 4);
NSString *firstBlock = [inputedDate substringWithRange:range82];
NSString *secondBlock = [inputedDate substringWithRange:range52];
NSString *thirdBlock = [inputedDate substringWithRange:range04];
formattedDate = [formattedDate stringByAppendingString:firstBlock];
formattedDate = [formattedDate stringByAppendingString:@"/"];
formattedDate = [formattedDate stringByAppendingString:secondBlock];
formattedDate = [formattedDate stringByAppendingString:@"/"];
formattedDate = [formattedDate stringByAppendingString:thirdBlock];
return formattedDate;
}
第二个问题是,发票日期被加载到包含日期格式化程序的表视图中,更改该视图将导致表的排序丢失
使用与我的处理程序相同的原则,并坚持传递2011年10月16日的日期会在表视图上导致错误,例如:-[\uu NSCFString timeIntervalSinceReferenceDate]:发送到实例0x40265f3a0的无法识别的选择器
这是迄今为止我发现的最好的代码:
if (formatterDate == NULL) {
NSLog (@"outprintString '%@' = NULL", dateString);
NSString *newDate = [BHDateFormatter bhDateFormat:dateString];
if ([newDate isEqualToString:@"16/10/2011"]) {
formatterDate = [inputFormatter dateFromString:@"2011-10-17"];
NSLog(@"formatterDate = %@", formatterDate);
NSAlert *alert = [[[NSAlert alloc]init] autorelease];
[alert setMessageText:[NSString stringWithFormat:NSLocalizedString(@"Mountain Lion Bug: I have found a date \"16/10/2011\" in your invoice # %@ issued by %@, I must rename the date in your table view to \"17/10/2011\", the date on the rendered invoice file will be display correctly. I apologize for this, but it is an operating system level bug that has to be fixed by Apple.","A comment here"),invoiceNumber, emitente]];
[alert runModal];
}
}
你能评论一下吗
谢谢 我相信你是对的——这是一个bug,你应该报告它。有趣的是,如果您将时区设置为努克格陵兰岛(Nuuk Greenland),它与里约位于同一时区,则工作正常。另外,“2011-10-15”和“2011-10-17”在里约时区也能正常工作,但“2011-10-16”却不行,这真的很奇怪 这是我用来测试的简化代码:
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
NSArray *unformattedDatesArray = [[NSArray alloc] initWithObjects:@"2011-09-18",@"2011-10-16",@"2011-11-13",@"2011-12-11",@"2012-01-08",@"2012-02-05", nil];
NSDateFormatter *originalFormat = [[NSDateFormatter alloc] init];
[originalFormat setDateFormat:@"yyyy-MM-dd"]; // hh:mm:ss
NSDateFormatter *newFormat = [[NSDateFormatter alloc] init];
[newFormat setDateFormat:@"dd/MM/yyyy"]; // hh:mm:ss
for (NSString * aString in unformattedDatesArray) {
NSString *formattedDate = [newFormat stringFromDate:[originalFormat dateFromString:aString]];
NSLog(@"Original date string is: %@ formatted date as string is: %@",aString, formattedDate);
}
}
补充资料:
为了找到这个问题的根源,我用以下代码循环了2011年的所有日期:
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
NSDateFormatter *originalFormat = [[NSDateFormatter alloc] init];
[originalFormat setDateFormat:@"yyyy-MM-dd"]; // hh:mm:ss
NSDateFormatter *newFormat = [[NSDateFormatter alloc] init];
[newFormat setDateFormat:@"dd/MM/yyyy"]; // hh:mm:ss
NSDate *start = [originalFormat dateFromString:@"2011-01-01"];
NSDate *newDate;
for (int i=0; i<365; i++) {
newDate = [start dateByAddingTimeInterval:86400 * i];
NSString *formattedDate = [newFormat stringFromDate:newDate];
NSLog(@"formatted date as string is: %@",formattedDate);
}
}
-(无效)应用程序设计完成启动:(NSNotification*)通知{
NSDateFormatter*原始格式=[[NSDateFormatter alloc]init];
[原始格式setDateFormat:@“yyyy-MM-dd”];//hh:MM:ss
NSDateFormatter*newFormat=[[NSDateFormatter alloc]init];
[newFormat setDateFormat:@“dd/MM/yyyy”];//hh:MM:ss
NSDate*start=[原始格式dateFromString:@“2011-01-01”];
NSDate*newDate;
对于(int i=0;i这不是一个bug。10月16日是巴西进入南半球夏时制的那一天,你建议的时间(假设为午夜,因为你没有给出明确的时间)是在失踪的一小时内。这是一个不存在的时间。+1是的,关于美国/圣保罗
,底层的zoneinfo
文件中似乎有问题。请对此提出一个错误:。亲爱的Rick,感谢您的回复。您认为我同时可以安全地使用我的代码吗?1)编写我自己的格式化处理程序?2)如果格式化失败,请在输出字符串中插入和错误字符串?3)还有其他想法吗?这不是错误。10月16日是巴西进入南半球夏令时的日子,而你建议的时间是在缺失的一小时内。同样,2月19日是他们取消夏令时的日子,所以时间是重复的。@DaveDeLong这不是一个错误。事实上,当年巴西并非所有地区都采用夏令时,但由于部分地区采用了夏令时,系统对里约做出了合理的假设。不包括时间,你就隐含了时间。去年有一些很好的WWDC视频关于日历计算。我相信建议是不要使用午夜,使用中午,并明确地这样做。否则你会陷入这样的陷阱。是的。NSDate包括时间。如果你忽略它,那么你就隐含地使用午夜,问题是有时有两个午夜,有时没有午夜。请参阅20世纪的WWDC视频13
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
NSDateFormatter *originalFormat = [[NSDateFormatter alloc] init];
[originalFormat setDateFormat:@"yyyy-MM-dd"]; // hh:mm:ss
NSDateFormatter *newFormat = [[NSDateFormatter alloc] init];
[newFormat setDateFormat:@"dd/MM/yyyy"]; // hh:mm:ss
NSDate *start = [originalFormat dateFromString:@"2011-01-01"];
NSDate *newDate;
for (int i=0; i<365; i++) {
newDate = [start dateByAddingTimeInterval:86400 * i];
NSString *formattedDate = [newFormat stringFromDate:newDate];
NSLog(@"formatted date as string is: %@",formattedDate);
}
}