2013-04-01 3 views
0

사용자 위치에 따라 현재 날짜를 생성하는 함수가 있습니다. 이것은 영국에서 일광 절약 시간이 변경된 후에도 정확한 날짜와 시간을 제공합니다.NSDateformatter 일광 절약 시간 이후 잘못된 시간

그러나 내가로 변환 할 때

이 Coredata에 저장

예 : 날짜 그러나 2013년 1월 3일 12:00 ... 나를 위해에 여분의 시간을 추가 할 것으로 보인다 날짜와 문자열을 만들려고 할 때 문자열은 1/13/2013 13:00으로 표시됩니다. 내가 로케일과 타임 존으로 변화 한 것은 변화를 가져 오는 것 같지 않습니다.

어떻게 해결할 수 있습니까? 그것은 "지역 날짜"함수 내가 올바른 시간대와 날짜 시간을 생성하지 않은 것을 지적 가치가있을 수 있습니다.

코드 :

+(NSDate*)localDate{ 

NSDate* date = [NSDate date]; 

NSTimeZone* sourceTimeZone = [NSTimeZone timeZoneWithAbbreviation:@"GMT"]; 
NSTimeZone* destinationTimeZone = [NSTimeZone localTimeZone]; 

NSInteger sourceGMTOffset = [sourceTimeZone secondsFromGMTForDate:date]; 
NSInteger destinationGMTOffset = [destinationTimeZone secondsFromGMTForDate:date]; 
NSTimeInterval interval = destinationGMTOffset - sourceGMTOffset; 
return [NSDate dateWithTimeInterval:interval sinceDate:date]; 

}

에게 포맷 코드 :

-(NSString*)dateWithTime{ 
NSDateFormatter*formatter = [[NSDateFormatter alloc]init]; 

[formatter setDateFormat:@"dd"]; 
NSInteger day = [[formatter stringFromDate:self]integerValue]; 
NSInteger fullday = day; 
NSString* strDay = @""; 
if(day>29){ 
    day = day - 30; 
} 
else if(day>19){ 
    day = day - 20; 
} 

else if(day>9){ 
    day = day - 10; 
} 

if(fullday ==11){ 
    strDay = @"th"; 
} 
else if(fullday ==12){ 
    strDay = @"th"; 
} 
else if(fullday ==13){ 
    strDay = @"th"; 
} 
else if(day == 1){ 
    strDay = @"st"; 
} 
else if(day ==2){ 
    strDay = @"nd"; 
} 
else if(day ==3){ 
    strDay = @"rd"; 
} 
else{ 
    strDay = @"th"; 
} 

[formatter setDateFormat:@"MMMM"]; 
NSString* month = [formatter stringFromDate:self]; 

[formatter setDateFormat:@"HH:mm"]; 
NSString* time = [formatter stringFromDate:self]; 

return [NSString stringWithFormat:@"%u%@ %@ %@ - %@",fullday,strDay,month,[self yearString],time]; 

} 

편집 : 심지어 NSDateComponants 잘못된 시간을 반환 ..

NSCalendar *gregorian = [NSCalendar currentCalendar]; 
NSDateComponents *dateComponents = [gregorian components:(NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit) fromDate:self]; 
+0

심지어 1 년 미만? –

+0

DateFormatter는 @ "dd"에 의해 이것을 찾기위한 것이 아니며 NSDateComponent를 가지고 있습니다 [comp date]'는 현재 날짜를 알려줍니다. –

+0

방금 ​​업데이트되었습니다.구성자는 나에게 같은 문제를 준다. 형식 작성자가 추가 시간을 추가하는 것과 같습니다 (시계가 변경된 이후에만 해당됩니다) – Dan

답변

2

있는 NSDate 모르는 시간대에 대해 nes 그것은 변환되었을 때만 시간대를 계산하므로 localDate 메소드는 의도하지 않은 방식으로 물건을 푸는 것일 수 있습니다. 혼자 [NSDate date]에 의해 반환 된 값을

[formatter setTimeZone:localTimeZone]; 

또는

[gregorian setTimeZone:localTimeZone]; 

떠나 : 당신은 아마 그래서, 로컬 영역에 포맷터 또는 일정의 시간대를 설정해야 할 일은.

+0

이것은 차이를 만드는 것 같지 않습니다. 문제가 내 로컬 날짜 만들기로 인해 발생하는지 궁금하다. – Dan

+0

그래, 나는 포맷터의 시간대를 기대하고 달력은 실제로 기본적으로 localTimeZone이다. –

0

dateformatter의 로케일을 설정하십시오.

NSLocale *enUSPOSIXLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"]; 
[dateFormatter setLocale:enUSPOSIXLocale]; 
0

초기 날짜/시간이 설정된 후에 필요에 따라 표준 시간대 (DST 포함)가 변경된 경우 알림을받을 수 있습니다. 그렇다면 applicationSignificantTimeChange:은 (는) 귀하의 친구가됩니다.

3

콘솔에 NSDate를 인쇄하면 항상 UTC로 날짜가 인쇄됩니다.

[NSDate date]을 데이터베이스에 저장하기 만하면됩니다. 사용자가 시간대를 변경하지 않은 경우 데이터베이스에서 날짜를 읽고 형식을 지정하면 올바른 결과가 반환됩니다.

코어 데이터의 NSDate 객체는 UTC 시간대가있는 UNIX 타임 스탬프로 저장됩니다. 날짜 객체를 읽고 형식을 지정할 때 사용자의 현재 시간대가 적용됩니다.

핵심 데이터로 날짜를 저장하고 다른 표준 시간대에 종속되는 경우 날짜를 올바르게 형식 지정하는 유일한 방법은 해당 표준 시간대 (문자열의 시간대 이름)를 저장하는 것입니다. 날짜 개체를 읽고 서식을 지정할 때 dateFormatter에 저장된 표준 시간대를 적용해야합니다. 그런 다음 올바른 결과를 얻습니다.