在 Objective C 中使用开放天气 api 时,如何在他们的时区获取远程位置的日出/日落时间,而不是设备时区?
How to get the sunrise/ sunset time for remote location in their timezone, not device timezone when using open weather api, in Objective C?
我得到的是 sunrise/sunset 时间,但它在我的设备使用的时区内。例如,我在纽约,当我查找 EST 中的城市时,它 returns 是正确时区的 sunrise/sunset 时间。但是,如果我查看迪拜,时间是关闭的。返回的迪拜日落时间是 10:57PM。这是纽约 (EST) 太阳落在迪拜的时间。
这是我的 OpenWeatherAPI 响应-
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 中查询的远程时区的 sunrise/sunset 时间?
我想出来了,你就是这样做的-
您使用 API 响应中返回的 timezone
字段并将其设置为偏移量。
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];
我得到的是 sunrise/sunset 时间,但它在我的设备使用的时区内。例如,我在纽约,当我查找 EST 中的城市时,它 returns 是正确时区的 sunrise/sunset 时间。但是,如果我查看迪拜,时间是关闭的。返回的迪拜日落时间是 10:57PM。这是纽约 (EST) 太阳落在迪拜的时间。
这是我的 OpenWeatherAPI 响应-
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 中查询的远程时区的 sunrise/sunset 时间?
我想出来了,你就是这样做的-
您使用 API 响应中返回的 timezone
字段并将其设置为偏移量。
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];