Objective c 在目标C中,当使用开放天气api时,如何获取远程位置在其时区而不是设备时区的日出/日落时间?
我正在获取日出/日落时间,但我的设备使用的是时区。例如,我在纽约,当我查找EST中的城市时,它返回正确时区中的日出/日落时间。但如果我看看迪拜,时间就不多了。返回迪拜的日落时间是晚上10:57。这是纽约时间迪拜日落的时间 这是我对OpenWeatherAPI的回应-Objective c 在目标C中,当使用开放天气api时,如何获取远程位置在其时区而不是设备时区的日出/日落时间?,objective-c,nsdateformatter,timezone-offset,openweathermap,Objective C,Nsdateformatter,Timezone Offset,Openweathermap,我正在获取日出/日落时间,但我的设备使用的是时区。例如,我在纽约,当我查找EST中的城市时,它返回正确时区中的日出/日落时间。但如果我看看迪拜,时间就不多了。返回迪拜的日落时间是晚上10:57。这是纽约时间迪拜日落的时间 这是我对OpenWeatherAPI的回应- base = stations; clouds = { all = 20; }; cod = 200; coord = { lat = "25.27";
base = stations;
clouds = {
all = 20;
};
cod = 200;
coord = {
lat = "25.27";
lon = "55.3";
};
dt = 1565620936;
id = 292223;
main = {
humidity = 71;
pressure = 995;
temp = 36;
"temp_max" = 36;
"temp_min" = 36;
};
name = Dubai;
sys = {
country = AE;
id = 7537;
message = "0.0067";
sunrise = 1565574654;
sunset = 1565621827;
type = 1;
};
timezone = 14400;
visibility = 7000;
weather = (
{
description = "few clouds";
icon = 02d;
id = 801;
main = Clouds;
}
);
wind = {
deg = 340;
speed = "4.6";
};
如何在Objective-C中查询的远程时区中获取日出/日落时间?我算出了,这就是您的方法- 使用API响应中返回的时区字段并将其设置为偏移量
double offset = [json[@"timezone"]doubleValue]; //getting timezone offset
double sunriseTimestampval = [json[@"sys"][@"sunrise"]doubleValue];
NSTimeInterval sunriseTimestamp = (NSTimeInterval)sunriseTimestampval;
NSDate* sunriseDate = [NSDate dateWithTimeIntervalSince1970:sunriseTimestamp];
NSDateFormatter *sunriseDateFormatter = [[NSDateFormatter alloc] init];
[sunriseDateFormatter setDateFormat:@"hh:mm"];
[sunriseDateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:offset]];//->Converting to remote timezone
NSString *sunriseTime = [sunriseDateFormatter stringFromDate:sunriseDate];