Google Maps Time Zone API get local time in UTC

我只是一个虾纸丫 提交于 2019-12-13 08:47:13

问题


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."


回答1:


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)




回答2:


I just solve my problem adding this line: 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());
    });
  }
}

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



来源:https://stackoverflow.com/questions/35613997/google-maps-time-zone-api-get-local-time-in-utc

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