Time Zone API 提供地球表面各个位置的时间偏移数据。请求特定纬度/经度对和日期的时区信息。API 会返回该时区的名称、相对于世界协调时间 (UTC) 的时间偏移值以及夏令时偏移值。
示例请求和响应
通过 HTTPS 接口访问 Time Zone API(请求以网址字符串形式构建),使用纬度/经度坐标指定 location
,使用 timestamp
指定日期,以及使用 API 密钥。
以下查询针对美国内华达州执行时区请求。时间戳设置为 2012 年 3 月 8 日。
网址
https://maps.googleapis.com/maps/api/timezone/json ?location=39.6034810%2C-119.6822510 ×tamp=1331161200 &key=YOUR_API_KEY
cURL
curl -L -X GET 'https://maps.googleapis.com/maps/api/timezone/json?location=39.6034810%2C-119.6822510×tamp=1331161200&key=YOUR_API_KEY'
JavaScript
var axios = require('axios'); var config = { method: 'get', url: 'https://maps.googleapis.com/maps/api/timezone/json?location=39.6034810%2C-119.6822510×tamp=1331161200&key=YOUR_API_KEY', headers: { } }; axios(config) .then(function (response) { console.log(JSON.stringify(response.data)); }) .catch(function (error) { console.log(error); });
Python
import requests url = "https://maps.googleapis.com/maps/api/timezone/json?location=39.6034810%2C-119.6822510×tamp=1331161200&key=YOUR_API_KEY" payload={} headers = {} response = requests.request("GET", url, headers=headers, data=payload) print(response.text)
Java
OkHttpClient client = new OkHttpClient().newBuilder() .build(); MediaType mediaType = MediaType.parse("text/plain"); RequestBody body = RequestBody.create(mediaType, ""); Request request = new Request.Builder() .url("https://maps.googleapis.com/maps/api/timezone/json?location=39.6034810%2C-119.6822510×tamp=1331161200&key=YOUR_API_KEY") .method("GET", body) .build(); Response response = client.newCall(request).execute();
Ruby
require "uri" require "net/http" url = URI("https://maps.googleapis.com/maps/api/timezone/json?location=39.6034810%2C-119.6822510×tamp=1331161200&key=YOUR_API_KEY") https = Net::HTTP.new(url.host, url.port) https.use_ssl = true request = Net::HTTP::Get.new(url) response = https.request(request) puts response.read_body
Go
package main import ( "fmt" "net/http" "io/ioutil" ) func main() { url := "https://maps.googleapis.com/maps/api/timezone/json?location=39.6034810,-119.6822510×tamp=1331161200&key=YOUR_API_KEY" method := "GET" client := &http.Client { } req, err := http.NewRequest(method, url, nil) if err != nil { fmt.Println(err) return } res, err := client.Do(req) if err != nil { fmt.Println(err) return } defer res.Body.Close() body, err := ioutil.ReadAll(res.Body) if err != nil { fmt.Println(err) return } fmt.Println(string(body)) }
邮差
OpenAPI 规范也可用作 Postman 集合。
在 Postman 中运行要测试这一点,您可以在网络浏览器中输入网址(请务必将 YOUR_API_KEY
替换为您的实际 API 密钥)。响应中包含指定位置和日期 (timestamp)
的时区数据。
如需详细了解如何构建请求网址和可用参数以及如何了解响应,请参阅开发者指南。
下面是一个 JSON 格式的示例响应:
JSON
{ "dstOffset": 0, "rawOffset": -28800, "status": "OK", "timeZoneId": "America/Los_Angeles", "timeZoneName": "Pacific Standard Time", }
XML
<TimeZoneResponse> <status>OK</status> <raw_offset>-28800.0000000</raw_offset> <dst_offset>0.0000000</dst_offset> <time_zone_id>America/Los_Angeles</time_zone_id> <time_zone_name>Pacific Standard Time</time_zone_name> </TimeZoneResponse>
使用客户端库编码入门指南
客户端库通过提供常见任务(例如身份验证、请求限制和自动重试)的简单原生实现,使用 Google 地图网络服务 API 更轻松地进行开发。您可以在 Google 地图服务的 Java 客户端、Python 客户端、Go 客户端和 Node.js 客户端中使用 Time Zone API。
身份验证、配额、价格和政策
身份验证
如需使用 Time Zone API,您必须先启用该 API 并获取正确的身份验证凭据。如需了解详情,请参阅 Google Maps Platform 使用入门。
配额和价格
如需详细了解 Time Zone API 的配额和定价,请查看用量和结算页面。
政策
使用 Time Zone API 时,必须遵守 API 政策。
了解详情
在 Time Zone API 中,您还可以将语言参数设置为返回非英语默认语言的结果。如需查看更多示例和其他详情,请参阅 Time Zone API 开发者指南。
Time Zone API 开发者指南适用于想要在其中一个 Google Maps Platform API 提供的地图上添加时间数据的网站和移动开发者。其中介绍了如何使用 API 以及有关可用参数的参考资料。