Google地图时区API可以UTC获取当地时间

编程入门 行业动态 更新时间:2024-10-28 17:23:07
本文介绍了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)

maps.googleapis/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是我需要的正确的本地时间。

有用的提示

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

Google API告诉您,rawOffset的位置为-18000

当您使用时间戳计算日期时,应以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)

maps.googleapis/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获取当地时间

本文发布于:2023-10-17 13:28:47,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1501067.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:当地时间   时区   地图   Google   API

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!