使用 unix 时间戳显示正确的日出/日落时间 [英] Showing correct sunrise/sunset times using unix timestamp
问题描述
我从 DarkSkyApi 获得了日出和日出的 UNIX 时间戳所选位置的日落时间,我想将其转换为 DateTime 格式并将其显示给用户.我希望时间值是本地的.
I'm getting a UNIX timestamp from DarkSkyApi for the sunrise & sunset times for the selected location and i want to convert it to a DateTime format and display it to the user. I want the time values to be local.
示例:用户在意大利并选择东京,JP"作为获取天气信息的所需位置.日出&日落时间值应该被格式化&显示为当地时间.所以对于东京来说,日出应该在凌晨 4 点 34 分左右18:36 PM 日落.
Example case : The user is in Italy and selects "Tokyo, JP" as the desired location to fetch weather info for. The sunrise & sunset time values should be formatted & shown as local times. So for Tokyo, sunrise should be something around 4:34 AM & 18:36 PM for sunset.
就我现在所拥有的而言,我得到了错误的值,例如日出和 12:172:29 日落.关于我在这里做错了什么的任何想法?
With what i have right now, i'm getting wrong values such as 12:17 for sunrise & 2:29 for sunset. Any ideas on what i'm doing wrong here?
附言tmz
var 是所选位置的时区,因此在这种情况下它将是亚洲/东京".这是我现在为日落时间所做的事情(日出时间相同):
P.S. The tmz
var is the timezone of the selected location , so in this case it would be "Asia/Tokyo". Here's what i'm doing right now for the sunset time (same for the sunrise time):
private fun setViewHolderWeekDaySunsetTime(holder: ViewHolder, sunsetTime: Long, tmz: String) {
val dt = Instant.ofEpochSecond(sunsetTime).atZone(
ZoneId.of(tmz)
)
val formatted = dt.format(DateTimeFormatter.ofPattern("HH:mm"))
holder.weekDaySunsetTime.text = formatted
}
推荐答案
使用这个函数可以找到你想要的每个地方的时区及其纬度和经度:
Use this function to find timezone of every place you want with its latitude and longitude:
fun timezonecalculator(latitude:Double,longitude:Double):Double {
val resultTimeZone = TimezoneMapper.latLngToTimezoneString(latitude, longitude)
var tz= TimeZone.getTimeZone(resultTimeZone).getDisplayName(
TimeZone.getTimeZone(resultTimeZone).inDaylightTime(Date()), TimeZone.SHORT)
if ((tz=="GMT")||(tz=="UTC")||(tz=="WET"))
tz+="+00:00"
if ((tz=="CST")||(tz=="MDT"))
tz+="-06:00"
if (tz=="AST")
tz+="-04:00"
if ((tz=="EST")||(tz=="CDT"))
tz+="-05:00"
if (tz=="MST")
tz+="+05:00"
if ((tz=="CET")||(tz=="BST"))
tz+="+01:00"
if (tz=="EET")
tz+="+02:00"
if (tz=="CEST")
{tz+="+02:00"
tz=tz.drop(1)}
if (tz=="PDT")
tz+="-07:00"
if (tz=="EDT")
tz+="-04:00"
if (tz=="EEST")
{tz+="+03:00"
tz=tz.drop(1)}
if (tz=="WEST")
{tz+="+01:00"
tz=tz.drop(1)}
var sign=tz[3]
tz=tz.drop(4)
val minute=tz.drop(3)
val hour=tz.dropLast(3)
val min=Integer.parseInt(minute)
val hou=Integer.parseInt(hour)
var timezone=hou+(min.toDouble()/60)
if (sign=='-')
timezone*=-1
return timezone
}
而 TimeZoneMapper 类是 https://raw.githubusercontent.com/drtimcooper/LatLongToTimezone/master/src/main/java/com/skedgo/converter/TimezoneMapper.java
And TimeZoneMapper class is https://raw.githubusercontent.com/drtimcooper/LatLongToTimezone/master/src/main/java/com/skedgo/converter/TimezoneMapper.java
请参阅此处了解如何使用此类:https://github.com/drtimcooper/LatLongToTimezone一个>
See here to know how to use this class: https://github.com/drtimcooper/LatLongToTimezone
你这样调用它:timezonecalculator(latitude, longitude)
示例:timezonecalculator(36.2, 59.6)
输出:4.5
这篇关于使用 unix 时间戳显示正确的日出/日落时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!