bmalets
bmalets

Reputation: 3297

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

I'm developing rails project, which contain searching for a users. Each user have:

I am searching for the most fast and simple way to get user's local time or time zone.

My ideas:

  1. get city coordinates via google api, and after - get timezone by voordinates

  2. get time zone using other specific rest API

But this realisation of this ideas is not good cause of long request/responce delays... So it is not suitable for me.

Questions:

Thanks!

Upvotes: 1

Views: 460

Answers (2)

Joonathan
Joonathan

Reputation: 144

You could try to solve this via our new 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"'

This would run a search on Teleport API for city matching 'Tallinn, Estonia' and would fetch the timezone information for the result.

I used ./jq to parse it, the result would look like:

{
  "_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"
}

Upvotes: 1

bmalets
bmalets

Reputation: 3297

My algorithm:

  1. Create additional model TimezoneDictionary e.g.
  2. Check for timezone in TimezoneDictionary model first.
  3. if timezone value is not found in TimezoneDictionary - make a request for timezone to Google API, and save it into TimezoneDictionary:

    a) request for coordinates via Google Geolocation API

    b) request for timezone via Google TimeZone API

And when we have timezone value ("Europe/Kyiv" e.g.) we can get local time with ActiveSupport:

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

P.S. I created a gem which_time for this issue (to get timzone/time by city name e.g.), so if you have the desire - use it :). I will be very thankful for pull request.

Upvotes: 0

Related Questions