【发布时间】:2014-04-05 17:07:43
【问题描述】:
所以,我正在尝试为日出和日落创建一个 NSDate 对象。我根据NSDatePicker 获取日期,从地图获取坐标,从地图获取 GPS 时区。
我使用此代码获取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;
}
为什么会发生这种情况,谁能给我一个解决方案?
【问题讨论】:
标签: ios timezone nsdate nsdateformatter