使用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.
示例案例:用户位于意大利,并选择"Tokyo,JP"作为获取天气信息的所需位置.日出与日落日落时间值应设置为&显示为当地时间.因此,对于东京来说,日出时间应该是凌晨4:34左右.日落时间下午18:36.
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:17日落2: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?
P.S. tmz
var是所选位置的时区,因此在这种情况下,它将是"Asia/Tokyo".这是我现在在日落时间(日出时间相同)所做的事情:
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)
You invoke it this way: timezonecalculator(latitude, longitude)
示例:timezonecalculator(36.2, 59.6)
输出: 4.5
这篇关于使用Unix时间戳显示正确的日出/日落时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!