android - 如何在 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
属性或其他属性从该城市获取当前时间。可能吗?如何?
解决方案
使用timezone
从 API 获得的获取城市当前时间的方法如下:
DateTime.now().add(Duration(seconds: timezone - DateTime.now().timeZoneOffset.inSeconds))
你会DateTime
从这里得到一个对象。
推荐阅读
- database - MongoDB内部如何实现多键索引
- vb.net - 每 100 个字符拆分字符串不起作用
- python - 在python中捕获linux终端响应
- javascript - 如何在函数中添加这个到handleChange和componentDidMount的回调?
- python - Django ORM:如何查询处于只读模式且在model.py文件中没有引用的辅助数据库
- c - 使用此内存分配器时收到分段错误
- angular - 在两个组件中显示相同的时间计数
- c++ - 使用超过 1 个源文件时对类成员函数的未定义引用
- android - 使用EventBus的android片段之间的通信
- visual-studio - Microsoft.Office.Core.CustomTaskPane 和 Microsoft.Office.Tools.CustomTaskPane.. 有什么区别?