Google地图时区API可以UTC获取当地时间 [英] Google Maps Time Zone API get local time in UTC

查看:1163
本文介绍了Google地图时区API可以UTC获取当地时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Google文档说:




给定位置的本地时间是时间戳参数和dstOffset和rawOffset字段的总和


timestamp + dstOffset + rawOffset = UTC的本地时间(以秒为单位)



问题



我试图让当地时间在正确的时区。
我以为新的Date(timestamp + dstOffset + rawOffset * 1000)将以UTC返回本地时间,而是发现实际的数值确实在本地时间,在错误的时区(UTC)中。



示例:



timestamp: 1456349190



可读时间戳: 新日期(1456349190 * 1000).toGMTString()(Wed,24 Feb 2016 21:26 :30 GMT)



https://maps.googleapis.com/maps/api/timezone/json?location=39.2079206,-84.5274616& ; timestamp = 1456349190& key = $ {googleApiKey}



返回:




  • dstOffset :0

  • rawOffset :-18000

  • timeZoneId :America / New_York



数据:



以秒为单位 = 1456349190 + 0 +(-18000)= 1456331190



以毫秒为单位的总和 = 1456331190 * 1000 = 1456331190000



假定本地日期 = 新日期(1456331190000).toGMTString()(Wed,24 Feb 2016 16:26:30 GMT)



问题



t 可读时间戳假定的本地日期是相同的,因为它们都是UTC?
似乎假定的本地日期应该是Wed,24 Feb 2016 16:26:30 EST



这是否正确?



如果是这样,我似乎只需要从假定的本地日期中提取需要的值(本地小时)并应用从google api timeZoneId (America / New_York)返回的正确时区,因为16:26:30是我需要的正确的本地时间。



< h1>有用的提示

这些是帮助我更好地了解时间戳的一些指导原则:





Google API告诉您,rawOffset的位置为-18000
< a href =https://i.stack.imgur.com/8Q4B9.png =nofollow noreferrer>



当您使用时间戳计算日期时,应以UTC为单位。
因为Date类将时间戳视为UTC。



在您的解释中,值1456331190000是本地时间戳,但是 Date类将该值视为UTC。



这是您的错误。








这是代码(在Node.js中)




Google Docs says:

The local time of a given location is the sum of the timestamp parameter, and the dstOffset and rawOffset fields from the result.

timestamp + dstOffset + rawOffset = local time in seconds in UTC

Problem

I am trying to get the local time in the correct time zone. I thought that new Date(timestamp + dstOffset + rawOffset *1000) would return the local time in UTC, but instead I find that the actual numerical values are indeed in local time, in the wrong time zone (UTC).

Example:

timestamp: 1456349190

readable timestamp: new Date(1456349190*1000).toGMTString() (Wed, 24 Feb 2016 21:26:30 GMT)

https://maps.googleapis.com/maps/api/timezone/json?location=39.2079206,-84.5274616&timestamp=1456349190&key=${googleApiKey}

Returns:

  • dstOffset: 0
  • rawOffset: -18000
  • timeZoneId: America/New_York

Data:

Sum in seconds = 1456349190 + 0 + (-18000) = 1456331190

Sum in milliseconds = 1456331190*1000 = 1456331190000

Supposed Local Date = new Date(1456331190000).toGMTString() (Wed, 24 Feb 2016 16:26:30 GMT)

Question

Shouldn't readable timestamp and Supposed Local Date be the same since both are in UTC? Seems like Supposed Local Date should really be Wed, 24 Feb 2016 16:26:30 EST

Is this correct?

If so it seems like I just need to extract the values I need (local hour) from Supposed Local Date and apply the correct time zone returned from google api timeZoneId (America/New_York) because 16:26:30 is the correct local time I need.

Helpful Tips

These are some guidelines that helped me understand timestamp better:

"The unix timestamp isn't affected by a timezone setting. Setting the timezone only affects the interpretation of the timestamp value."

解决方案

Timestamp value indicates the seconds since 1/1/1970 UTC. Not local timestamp.

The Google API tells you the rawOffset of location is "-18000"

When you calculate the date with timestamp, you should align the unit in UTC. Because Date class treats the timestamp as UTC.

In your explanation, the value 1456331190000 is a local timestamp, but the Date class treats the value as UTC.

This is your mistake point.


Here is the code (in Node.js)

这篇关于Google地图时区API可以UTC获取当地时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆