首页 > 解决方案 > 如何在 Flutter 中使用 OpenWeather API 的时区属性获取任何城市/国家的当前时间?

问题描述

我通过使用 OpenWeather 的 API 得到了这个 JSON 响应:

{
"coord": {
"lon": 139.69,
"lat": 35.69
},
"weather": [
{
"id": 803,
"main": "Clouds",
"description": "broken clouds",
"icon": "04d"
}
],
"base": "stations",
"main": {
"temp": 306.63,
"feels_like": 311.16,
"temp_min": 304.82,
"temp_max": 308.15,
"pressure": 1011,
"humidity": 63
},
"visibility": 10000,
"wind": {
"speed": 3.1,
"deg": 140
},
"clouds": {
"all": 75
},
"dt": 1598419499,
"sys": {
"type": 1,
"id": 8077,
"country": "JP",
"sunrise": 1598386129,
"sunset": 1598433465
},
"timezone": 32400,
"id": 1850144,
"name": "Tokyo",
"cod": 200
}

我希望通过使用timezone属性或其他属性从该城市获取当前时间。可能吗?如何?

标签: androidapiflutterdatetimeopenweathermap

解决方案


使用timezone从 API 获得的获取城市当前时间的方法如下:

DateTime.now().add(Duration(seconds: timezone - DateTime.now().timeZoneOffset.inSeconds))

你会DateTime从这里得到一个对象。


推荐阅读