Showing correct sunrise/sunset times using unix timestamp

房东的猫 提交于 2020-06-05 12:04:05

问题


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.

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.

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?

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
}

回答1:


it sounds like the api is returning different time zones depending on what city you request data

So taking that into consideration, when converting the timestamp to a datetime object, you need to do something like this:

import java.time.*

val dt = Instant.ofEpochSecond(/*put time value here*/)
                .atZone(/*put time zone here*/)
                .toLocalDateTime() //this will convert it to your system's date time



回答2:


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
}

And TimeZoneMapper class is https://raw.githubusercontent.com/drtimcooper/LatLongToTimezone/master/src/main/java/com/skedgo/converter/TimezoneMapper.java

See here to know how to use this class: https://github.com/drtimcooper/LatLongToTimezone

You invoke it this way: timezonecalculator(latitude, longitude)

Example: timezonecalculator(36.2, 59.6) Output: 4.5



来源:https://stackoverflow.com/questions/61663913/showing-correct-sunrise-sunset-times-using-unix-timestamp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!