Consulting. Training. Development.

Don't (always) depend on third-party services

TL;DR: Don’t depend on third-party services if you can easily write your on in a couple of hours.

I hate timezones. And I believe you hate them too. But it happens that you have to work with them pretty often. And one day I had to detect timezones based on coordinates. You know, you have latitude and longitude and you need to know a timezone of this place. So what did I do? Right, I’ve googled it and found a couple of services that could do this kind of work for me like Geonames, Askgeo, Google, Yahoo Places and so on.

Geonames API is pretty nice so I’ve gave it a shot. It was working quite good but one day it became very slow. I mean VERY slow: I had to wait about 5-10 seconds to get requested timezone. Actually I don’t know why exactly it happened but it was awful. The application that used timezone feature became unusable.

So I decided to make my own service that I could run on my server and have a total control of it. Please welcome, Timezoner:

  • Rails API application
  • PostgreSQL + PostGIS
  • Shapefile with Timezones map from here
  • One SQL query.

So how it works? After you set it up just make a request to the server with lat and lng params and you get JSON with detected timezone for the given place.

We need PostGIS to make only one query to the database using ST_Contains function. You can read more about how to use ST_Contains in docs but our case can be illustrated with the following image:


where that point is a coordinates defined with passed lat and lng params and that polygon is a polygon that defines appropriate timezone (thank to for this data)

That’s all. It was pretty easy to replace not very fast (at least for that specific moment) service with our own which works way more faster.