如何通过 phone、Outlook 电子邮件或城市获取用户的本地时间(时区)?

How get user's local time (time zones) by their phone, outlook email or city?

我正在开发 rails 项目,其中包含搜索用户。 每个用户有:

我正在寻找获取用户本地时间或时区的最快速、最简单的方法。

我的想法:

  1. 通过 google api 获取城市坐标,之后 - 通过坐标获取时区

  2. 使用其他特定休息获取时区API

但是这个想法的实现不是很好的原因,导致长时间request/responce延迟...所以它不适合我。

问题:

谢谢!

我的算法:

  1. 创建附加模型 TimezoneDictionary,例如
  2. 首先在 TimezoneDictionary 模型中检查时区。
  3. 如果在 TimezoneDictionary 中找不到时区值 - 向 Google API 请求时区并将其保存到 TimezoneDictionary:

    a) 通过 Google Geolocation API

    请求坐标

    b) 通过 Google TimeZone API

  4. 请求时区

并且当我们有时区值("Europe/Kyiv" 例如)时,我们可以使用 ActiveSupport 获取本地时间:

local_time = Time.now.in_time_zone "Europe/Kyiv"

P.S。我为这个问题创建了一个 gem which_time(例如通过城市名称获得 timzone/time),所以如果你想要 - 使用它 :)。我将非常感谢拉取请求。

您可以尝试通过我们的新 API

来解决这个问题
curl -s 'https://api.teleport.org/api/cities/?search=Tallinn,%20Estonia&embed=city:search-results/city:item/city:timezone/tz:offsets-now' | jq '._embedded."city:search-results"[0]._embedded."city:item"._embedded."city:timezone"'

这将 运行 在 Teleport API 上搜索城市匹配 'Tallinn, Estonia' 并获取结果的时区信息。

我用./jq解析了它,结果是这样的:

{
  "_embedded": {
    "tz:offsets-now": {
      "_links": {
        "self": {
          "href": "https://api.teleport.org/api/timezones/iana:Europe%2FTallinn/offsets/?date=2015-09-07T11%3A41%3A00Z"
        }
      },
      "base_offset_min": 120,
      "dst_offset_min": 60,
      "end_time": "2015-10-25T01:00:00Z",
      "short_name": "EEST",
      "total_offset_min": 180,
      "transition_time": "2015-03-29T01:00:00Z"
    }
  },
  "_links": {
    "self": {
      "href": "https://api.teleport.org/api/timezones/iana:Europe%2FTallinn/"
    },
    "tz:offsets": {
      "href": "https://api.teleport.org/api/timezones/iana:Europe%2FTallinn/offsets/{?date}",
      "templated": true
    },
    "tz:offsets-now": {
      "href": "https://api.teleport.org/api/timezones/iana:Europe%2FTallinn/offsets/?date=2015-09-07T11%3A41%3A00Z"
    }
  },
  "iana_name": "Europe/Tallinn"
}