【问题标题】:Google Maps Time Zone API get local time in UTCGoogle Maps Time Zone API 以 UTC 获取本地时间
【发布时间】:2016-02-24 21:56:58
【问题描述】:

谷歌文档说:

给定位置的本地时间是时间戳参数与结果中的 dstOffset 和 rawOffset 字段的总和。

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

问题

我正在尝试获取正确时区的当地时间。 我认为 new Date(timestamp + dstOffset + rawOffset *1000) 会以UTC返回本地时间,但我发现实际数值确实是本地时间,在错误的时区(UTC)中。

示例:

时间戳: 1456349190

可读时间戳: new Date(1456349190*1000).toGMTString()(格林威治标准时间 2016 年 2 月 24 日星期三 21:26:30)

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

返回:

  • dstOffset:0
  • rawOffset:-18000
  • timeZoneId:美国/纽约

数据:

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

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

假定的本地日期 = new Date(1456331190000).toGMTString()(格林威治标准时间 2016 年 2 月 24 日星期三 16:26:30)

问题

可读时间戳假定的本地日期不应该相同,因为两者都是 UTC 吗? Supposed Local Date 似乎真的应该是 Wed, 24 Feb 2016 16:26:30 EST

这对吗?

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

实用提示

这些是帮助我更好地理解时间戳的一些准则:

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

【问题讨论】:

  • “UTC 本地时间”是荒谬的。时间要么是本地时间,要么是 UTC。不可能两者兼而有之。如果您住在像冰岛这样的地方,那么您的本地时间恰好与 UTC 全年对齐,但从概念上讲,这仍然是 本地 时间。如果您居住在英国,那么您的当地时间在冬季与 UTC 对齐,在夏季与 UTC 偏移一小时,这也是您的当地时间。 UTC 对地球上的每个人都有相同的价值。

标签: javascript google-maps date time timezone


【解决方案1】:

时间戳值表示自 1970 年 1 月 1 日 UTC 以来的秒数。不是本地时间戳。

Google API 告诉您 location 的 rawOffset 是“-18000”

当您计算带有时间戳的日期时,您应该将单位与 UTC 对齐。 因为 Date 类将时间戳视为 UTC。

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

这是你的错误点。


这是代码(在 Node.js 中)

【讨论】:

  • 这仅在 process.env.TZ 更改为所需时区(当前设置为 UTC)时有效。还有其他方法吗?谢谢!
  • process.env.TZ 用于更改本地时区(因为我不在纽约)。你不需要它。
  • 我的 process.env.TZ 必须保持 UTC。我正在查询不同的位置,所以我希望得到不同的时区。如何在不依赖 process.env.TZ 的情况下获取本地时间戳。类似新日期(时间戳,时区)的东西?
  • 非常感谢您的帮助!
【解决方案2】:

我刚刚解决了我添加这一行的问题:document.getElementById("utc_offset").value = place.utc_offset;

var placeSearch, autocomplete;
var componentForm = {
  street_number: 'short_name',
  route: 'long_name',
  locality: 'long_name',
  administrative_area_level_1: 'short_name',
  country: 'long_name',
  postal_code: 'short_name'

};

function initAutocomplete() {
  // Create the autocomplete object, restricting the search to geographical
  // location types.
  autocomplete = new google.maps.places.Autocomplete(
      /** @type {!HTMLInputElement} */(document.getElementById('autocomplete')),
      {types: ['geocode']});

  // When the user selects an address from the dropdown, populate the address
  // fields in the form.
  autocomplete.addListener('place_changed', fillInAddress);
}

function fillInAddress() {
  // Get the place details from the autocomplete object.
  var place = autocomplete.getPlace();

  for (var component in componentForm) {
    document.getElementById(component).value = '';
    document.getElementById(component).disabled = false;
  }

  // Get each component of the address from the place details
  // and fill the corresponding field on the form.
  for (var i = 0; i < place.address_components.length; i++) {
    var addressType = place.address_components[i].types[0];
    if (componentForm[addressType]) {
      var val = place.address_components[i][componentForm[addressType]];
      document.getElementById(addressType).value = val;
    }
  }
//Get the UTC here and complete the form
  document.getElementById("utc_offset").value = place.utc_offset;
}

// Bias the autocomplete object to the user's geographical location,
// as supplied by the browser's 'navigator.geolocation' object.
function geolocate() {
  if (navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(function(position) {
      var geolocation = {
        lat: position.coords.latitude,
        lng: position.coords.longitude
      };
      var circle = new google.maps.Circle({
        center: geolocation,
        radius: position.coords.accuracy
      });
      autocomplete.setBounds(circle.getBounds());
    });
  }
}

参考:https://developers.google.com/maps/documentation/javascript/examples/places-autocomplete-addressform

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-11-25
    • 2017-06-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-12
    • 2018-07-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多