2013-03-19 3 views
3

낯선 질문이 발생했습니다. NSDateFormatter는 iOS 6에서 NSDate 인스턴스에 1988-04-10을 구문 분석 할 수 없습니다. 그러나 iOS 4 및 iOS 5에서 작동합니다. 버그입니까?NSDateFormatter는 '1988-04-10'을 iOS 6의 NSDate 인스턴스로 구문 분석 할 수 없습니다. 그게 버그 야?

다음은 코드입니다. 그것은 매우 간단합니다. 다음은

NSArray *values = @[@"1988-04-09", @"1988-04-10", @"1988-04-11"]; 
NSDateFormatter* df = [[NSDateFormatter alloc] init]; 
NSLocale *posixLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"]; 
[df setLocale:posixLocale]; 
[df setDateFormat:@"yyyy'-'MM'-'dd"]; 
NSLog(@"timeZone:%@, locale:%@", df.timeZone, df.locale.localeIdentifier); 
NSDate* date = nil; 
for (NSString *value in values) { 
    date = [df dateFromString:value]; 
    NSLog(@"The date is %@", date); 
} 
[posixLocale release]; 
[df release]; 

그것은 1988-04-10위한 널 iOS의 제

2013-03-19 09:42:40.824 SampleProject[1455:11303] timeZone:Asia/Shanghai (GMT+08:00) offset 28800, locale:en_US_POSIX 
2013-03-19 09:42:40.827 SampleProject[1455:11303] The date is 1988-04-08 16:00:00 +0000 
2013-03-19 09:42:40.827 SampleProject[1455:11303] The date is (null) 
2013-03-19 09:42:40.828 SampleProject[1455:11303] The date is 1988-04-10 15:00:00 +0000 

의 결과 아이폰 OS 4 및 iOS 5. 다음

2013-03-19 09:55:32.249 SampleProject[1844:f803] timeZone:Asia/Shanghai (CST (China)) offset 28800, locale:en_US_POSIX 
2013-03-19 09:55:32.250 SampleProject[1844:f803] The date is 1988-04-08 16:00:00 +0000 
2013-03-19 09:55:32.250 SampleProject[1844:f803] The date is 1988-04-09 16:00:00 +0000 
2013-03-19 09:55:32.251 SampleProject[1844:f803] The date is 1988-04-10 15:00:00 +0000 

의 결과이다. Is is a bug. 아무도 그것을 설명 할 수 있습니까?

답변

9

일요일, 4 월 10 일, 00:00은 Daylight Saving Time in Shanghai that year의 스위치입니다.

위대한 WWDC 2011 비디오에 따르면 : Performing Calendar Calculations 이것은 정의되지 않은 동작으로 이어질 수 있습니다. 비디오에서 리오 데 자네이로가 동일한 이유 때문에 예제로 선택되었습니다.

한 가지 해결 방법은 시간을 3pm으로 저장하는 것입니다.
또 다른 NSDateComponents에서 계산을 수행합니다.
두 해결 방법은 모두 비디오에서 설명합니다.

+1

실제로, http://www.timeanddate.com/worldclock/timezone.html?n=237&syear=1980 – Joni

+1

그건 당신이 알고있는 미친 짓이야 !! +1 – borrrden

+1

쿨, 그 이유입니다. 고맙습니다. –