TzWorld
Resolve timezones from a location efficiently using PostGIS and Ecto (documentation).
TzWorld works by fetching timezone GeoJson data from this project, and then inserting it in a PostGIS table. Once populated, the database can be queried to retrieve a timezone name from coordinates.
Installation
- Add
tz_worldto your list of dependencies inmix.exs:
def deps do
[
{:tz_world, "~> 0.1.0"}
]
end
After adding TzWorld as a dependency, run mix deps.get to install it.
- Set your Ecto repository in your
config.ex:
config :tz_world,
repo: MyApp.Repo
- If you haven't already done so, add the
Geo.PostGISEcto extension to your project
# Create a new file anywhere in your application with the following:
Postgrex.Types.define(MyApp.PostgresTypes,
[Geo.PostGIS.Extension] ++ Ecto.Adapters.Postgres.extensions(),
json: Poison)
- Generate the migration to create the PostGIS table that will hold the GeoJson data:
$ mix tz_world.gen.migration
Then run the migration with mix ecto.migrate.
- Populate the table:
$ mix tz_world.install
This will Fetch GeoJson data from here, and insert it your database.
Usage
TzWorld.timezone_at(%Geo.Point{coordinates: {3.2, 45.32}})
"Europe/Paris"