无效 sunrise/set 次?
Invalid sunrise/set times?
我得到了 sunrise/set 次的虚假值(例如内华达州):
let url = "http://api.openweathermap.org/data/2.5/weather?q=Nevada,us&APPID=<app_id>&units=metric"
let req = new Request(url)
let json = await req.loadJSON()
function convertTime(unixTime){
let dt = new Date(unixTime * 1000)
let h = dt.getHours()
let m = "0" + dt.getMinutes()
let t = h + ":" + m.substr(-2)
return t
}
let sRise = convertTime(json.sys.sunrise)
let sSet = convertTime(json.sys.sunset)
alert(convertTime(sRise));
// Shows: 14:32 should be 05:24
alert(convertTime(sSet));
// Shows: 05:10 should be 19:55
我错过了什么吗? (P.S。我在欧洲,但通过代理获得了相同的结果)。
天气 API (https://openweathermap.org/current) 有另一个数据元素,可帮助将 sys
对象提供的 UTC 时间转换为请求区域的本地时间。您的转换函数需要考虑偏移量。示例:
let url = "http://api.openweathermap.org/data/2.5/weather?q=Nevada,us&APPID=<app_id>&units=metric"
let req = new Request(url)
let json = await req.loadJSON()
function convertTime(unixTime, offset){
let dt = new Date((unixTime + offset) * 1000)
let h = dt.getHours()
let m = "0" + dt.getMinutes()
let t = h + ":" + m.substr(-2)
return t
}
let sRise = convertTime(json.sys.sunrise, json.timezone)
let sSet = convertTime(json.sys.sunset, json.timezone)
alert(convertTime(sRise));
// Shows: 05:24
alert(convertTime(sSet));
// Shows: 19:55
我得到了 sunrise/set 次的虚假值(例如内华达州):
let url = "http://api.openweathermap.org/data/2.5/weather?q=Nevada,us&APPID=<app_id>&units=metric"
let req = new Request(url)
let json = await req.loadJSON()
function convertTime(unixTime){
let dt = new Date(unixTime * 1000)
let h = dt.getHours()
let m = "0" + dt.getMinutes()
let t = h + ":" + m.substr(-2)
return t
}
let sRise = convertTime(json.sys.sunrise)
let sSet = convertTime(json.sys.sunset)
alert(convertTime(sRise));
// Shows: 14:32 should be 05:24
alert(convertTime(sSet));
// Shows: 05:10 should be 19:55
我错过了什么吗? (P.S。我在欧洲,但通过代理获得了相同的结果)。
天气 API (https://openweathermap.org/current) 有另一个数据元素,可帮助将 sys
对象提供的 UTC 时间转换为请求区域的本地时间。您的转换函数需要考虑偏移量。示例:
let url = "http://api.openweathermap.org/data/2.5/weather?q=Nevada,us&APPID=<app_id>&units=metric"
let req = new Request(url)
let json = await req.loadJSON()
function convertTime(unixTime, offset){
let dt = new Date((unixTime + offset) * 1000)
let h = dt.getHours()
let m = "0" + dt.getMinutes()
let t = h + ":" + m.substr(-2)
return t
}
let sRise = convertTime(json.sys.sunrise, json.timezone)
let sSet = convertTime(json.sys.sunset, json.timezone)
alert(convertTime(sRise));
// Shows: 05:24
alert(convertTime(sSet));
// Shows: 19:55