2014-03-04 2 views
-1

그래서 일출과 일몰에 NSDate 개체를 만들려고합니다. 나는 NSDatePicker에 기초한 날짜를 얻었고, 나는지도로부터 좌표를 얻었고, 나는지도에서 GPS로부터 시간대를 얻는다.NSDate 및 NSDateformatter 잘못된 출력

은 내가 NSDate 객체를 얻기 위해이 코드를 사용 : https://github.com/MosheBerman/KosherCocoa-legacy 이 하나의 좌표 얻을 : https://github.com/digdog/MapKitDragAndDrop 그리고 좌표에 따라 시간대를 얻을이 하나 https://github.com/Alterplay/APTimeZones합니다.

지금 내 실제 위치는 로스 앤젤레스에 있으며, 일출과 일몰은 테스트를 위해 덴마크에서 돌아 왔습니다.

-(NSString *)sunriseDate{ 
    //Create the GeoLocation based on 'latitude' and 'longitude' (getting the from MapKitDragAndDrop) and 'location.timeZone' (getting that from APTimeZones). 
    GeoLocation *position = [[GeoLocation alloc] initWithName:@"position" andLatitude:latitude andLongitude:longitude andTimeZone:location.timeZone]; 

    AstronomicalCalendar *astronomicalCalender = [[AstronomicalCalendar alloc] initWithLocation:position]; 

    //daysBetween is the value from NSDatePicker 
    astronomicalCalender.workingDate = [NSDate dateWithTimeIntervalSinceNow:kSecondsInADay*[self daysBetween]]; 

    NSDate *sunriseDate = [astronomicalCalender sunrise]; 
    NSLog(@"Sunrise time: %@", sunriseDate); 
    //This spits out: Sunrise time: 2014-03-05 06:09:53 AM +0000 which is the right time. 
    NSDateFormatter *sunriseTime = [[NSDateFormatter alloc] init]; 
    [sunriseTime setDateFormat:@"HH:mm:ss"]; 

    NSString *sunriseString = [sunriseTime stringFromDate:sunriseDate]; 
    NSLog(@"Sunrisestring: %@", sunriseString); 
    //This spits out: 10:09:53 PM. 
    return sunriseString; 

} 

왜 이런 일이 일어날까요?

답변

1

[sunriseTime setDateFormat:@"HH:mm:ss"];의.

github https://github.com/Alterplay/APTimeZones에있는 라이브러리가있어서 좌표를 기준으로 시간대를 결정하는 데 도움이되었습니다.

그때 나는이 시간대에 맞는 시간을 넣어

[sunriseTime setTimeZone:location.timeZone]; 

을 사용했다.

희망이 있으면 누구에게나 도움이됩니다.

0

입력 형식을 올바르게 일치시켜야합니다.

시간에만 관심이있을 수 있지만 NSDateFormatter는 신경 쓰지 않습니다. NSDate는 결코 한 번도 아닙니다. 그것은 중요한 시점이며 날짜도 포함되어 있습니다. 날짜 및 시간 섹션이 없으면 작동하지 않습니다.

또한 스택 오버플로에 대한 가장 많이 묻는 질문 중 하나입니다. 다른 모든 NSDate에서 NSString (또는 그 반대) 질문은 대답 할 것입니다.

날짜 형식이어야합니다 ...

@"YYYY-MM-dd hh:mm:ss a EEEE" 

저는 믿습니다. 어쨌든 그걸 좋아하지.

+0

나는 아무것도 찾지 못했고 나는 찾고 있었다. 나는이 대답에서 아무것도 얻을 수 없었다. 어떻게 표시할지 그리고/또는 iPhone이있는 시간대를 제외하고. 만약 당신이 내 자신의 검색 이후로 이야기하고있는 스레드의 방향으로 나를 가리킬 수 있다면. 짧은되었습니다. (EEE는 하루 동안, zzzz는 시간대를 기준으로 함) – 4FunAndProfit

0

이 뱉어 내기 : 10:09:53 PM. 이는 그리니치 표준시와 8 시간 차이가있는 귀하의 시간대에 맞는 현지 시간입니다. 일출 시간 : 2014-03-05 06:09:53 AM +0000. 그게 다야. 독일 사람이 일어 났을 때 저녁이 있습니다.

+0

정확하게 존경받는 시간대를 위해 시간을 낼 필요가 있습니다. – 4FunAndProfit

0

Fogmeister가 말했듯이 NSDateFormatter를 만들 때 시간대를 포함시켜야합니다. Apple's Docs, Data Formatting Guide에서 전리품을 가지고 :

고정 형식 이 날짜 포맷에 대한 사용자 정의 고정 된 형식을 지정하려면 setDateFormat를 사용 :. 형식 문자열은 유니 코드 기술 표준 # 35의 형식 패턴을 사용합니다.

유니 코드 공식 사이트 :

http://www.unicode.org/reports/tr35/tr35-31/tr35-dates.html#Date_Format_Patterns

0

당신은이를 사용하려고 할 수 있습니다 : [sunriseTime setDateFormat:@"yyyy-MM-dd hh:mm:ss a EEEE"];

대신 같은 일에 비틀 수있는 사람에게

+0

나는 이것을 시도했다. 이것은 시간대 ekstra 만 반환합니다. 좌표에 대한 올바른 시간과 일치하도록 출력을 변경하지 않습니다. ''hh : mm : ss ''부분은 똑같은 것을 인쇄합니다. – 4FunAndProfit