All Projects → mathieuprog → tz

mathieuprog / tz

Licence: Apache-2.0 license
Time zone support for Elixir

Programming Languages

elixir
2628 projects

Projects that are alternatives of or similar to tz

tz
🌐 A time zone helper
Stars: ✭ 528 (+623.29%)
Mutual labels:  timezone, tzdata
Friend-Time
Discord bot - Friend Time helps your server coordinate times and events by converting times mentioned in chat between time zones!
Stars: ✭ 62 (-15.07%)
Mutual labels:  timezone, time-zone
Soulvercore
A powerful Swift framework for evaluating mathematical expressions
Stars: ✭ 245 (+235.62%)
Mutual labels:  timezone
IP2Location-C-Library
IP2Location C library enables the user to find the country, region, city, coordinates, zip code, time zone, ISP, domain name, connection type, area code, weather station code, weather station name, mobile, usage types, etc that any IP address or hostname originates from.
Stars: ✭ 37 (-49.32%)
Mutual labels:  timezone
timeshape
Java library to find timezone based on geo coordinates
Stars: ✭ 116 (+58.9%)
Mutual labels:  timezone
Timezone-List
A small package use to create a timezone list box in Laravel
Stars: ✭ 51 (-30.14%)
Mutual labels:  timezone
Vytal
Browser extension to spoof timezone, geolocation, locale and user agent.
Stars: ✭ 1,449 (+1884.93%)
Mutual labels:  timezone
Timezone
Arduino library to facilitate time zone conversions and automatic daylight saving (summer) time adjustments.
Stars: ✭ 205 (+180.82%)
Mutual labels:  timezone
hs-hourglass
efficient and simpler time API for haskell
Stars: ✭ 43 (-41.1%)
Mutual labels:  timezone
time machine
A date and time API for Dart
Stars: ✭ 120 (+64.38%)
Mutual labels:  timezone
typebeat
Parsing of the Content-Type header in pure OCaml
Stars: ✭ 20 (-72.6%)
Mutual labels:  iana
temps-lite
A smart, good-looking little app which tries to speak your language the way you are used to.
Stars: ✭ 40 (-45.21%)
Mutual labels:  timezone
ISO8601
🎗 Super lightweight ISO8601 Date Formatter in Swift
Stars: ✭ 20 (-72.6%)
Mutual labels:  timezone
countdown
jQuery CountDown Clock - Simple countdown plugin for product special offers
Stars: ✭ 33 (-54.79%)
Mutual labels:  timezone
Prayer Times Android Azan
Prayer + Time + Android + Kotlin + Azan + Library + timezone + islamic + salah + Library aiming to calculate prayer time with one line code , if you implement prayer time application , there is no need to do this headache again .
Stars: ✭ 251 (+243.84%)
Mutual labels:  timezone
liwasc
List, wake and scan nodes in a network.
Stars: ✭ 129 (+76.71%)
Mutual labels:  iana
Timezonefinder
fast python package for finding the timezone of any point on earth (coordinates) offline
Stars: ✭ 242 (+231.51%)
Mutual labels:  timezone
TimesDates.jl
Nanosecond resolution for Time and Date, TimeZones
Stars: ✭ 28 (-61.64%)
Mutual labels:  timezone
compact-timezone-list
Simple array of timezones with their long name, tz code, and UTC offset.
Stars: ✭ 33 (-54.79%)
Mutual labels:  timezone
llttz
The easy way to get java TimeZone from latitude/longitude
Stars: ✭ 27 (-63.01%)
Mutual labels:  timezone

Tz

Time zone support for Elixir.

The Elixir standard library does not ship with a time zone database. As a result, the functions in the DateTime module can, by default, only operate on datetimes in the UTC time zone. Alternatively (and deliberately), the standard library relies on third-party libraries, such as tz, to bring in time zone support and deal with datetimes in other time zones than UTC.

The tz library relies on the time zone database maintained by IANA. As of version 0.21.1, tz uses version tzdata2022a of the IANA time zone database.

Features

Battle-tested

The tz library is tested against nearly 10 million past dates, which includes most of all possible imaginable edge cases.

Pre-compiled time zone data

Time zone periods are deducted from the IANA time zone data. A period is a period of time where a certain offset is observed. Example: in Belgium, from 31 March 2019 until 27 October 2019, clock went forward by 1 hour; this means that during this period, Belgium observed a total offset of 2 hours from UTC time.

The time zone periods are computed and made available in Elixir maps during compilation time, to be consumed by the DateTime module.

Automatic time zone data updates

tz can watch for IANA time zone database updates and automatically recompile the time zone periods.

To enable automatic updates, add Tz.UpdatePeriodically as a child in your supervisor:

{Tz.UpdatePeriodically, []}

You may pass the option :interval_in_days in order to configure the frequency of the task.

{Tz.UpdatePeriodically, [interval_in_days: 5]}

If you do not wish to update automatically, but still wish to be alerted for new upcoming IANA updates, add Tz.WatchPeriodically as a child in your supervisor:

{Tz.WatchPeriodically, []}

You may pass the options:

  • :interval_in_days: frequency of the task
  • :on_update: a callback executed when an update is available

This will simply log to your server when a new time zone database is available.

To avoid the updater to run while executing tests, you may conditionally add the child worker in your supervisor. For example:

children = [
  MyApp.RepoBase,
  MyApp.Endpoint,
]
|> append_if(Application.get_env(:my_app, :env) != :test, {Tz.UpdatePeriodically, []})
defp append_if(list, condition, item) do
  if condition, do: list ++ [item], else: list
end

In config.exs, add config :my_app, env: Mix.env().

Lastly, add the http client mint and ssl certificate store castore into your mix.exs file:

defp deps do
  [
    {:castore, "~> 0.1.17"},
    {:mint, "~> 1.4"},
    {:tz, "~> 0.21.1"}
  ]
end

Custom HTTP client

You may implement the Tz.HTTP.HTTPClient behaviour in order to use another HTTP client.

Example using Finch:

defmodule MyApp.Tz.HTTPClient do
  @behaviour Tz.HTTP.HTTPClient

  alias Tz.HTTP.HTTPResponse
  alias MyApp.MyFinch

  @impl Tz.HTTP.HTTPClient
  def request(hostname, path) do
    {:ok, response} =
      Finch.build(:get, "https://" <> Path.join(hostname, path))
      |> Finch.request(MyFinch)

    %HTTPResponse{
      status_code: response.status,
      body: response.body
    }
  end
end

A Tz.HTTP.HTTPResponse struct must be returned with fields :status_code and :body.

The custom module must then be passed into the config:

config :tz, :http_client, MyApp.Tz.HTTPClient

Usage

To use the tz database, either configure it via configuration:

config :elixir, :time_zone_database, Tz.TimeZoneDatabase

or by calling Calendar.put_time_zone_database/1:

Calendar.put_time_zone_database(Tz.TimeZoneDatabase)

or by passing the module name Tz.TimeZoneDatabase directly to the functions that need a time zone database:

DateTime.now("America/Sao_Paulo", Tz.TimeZoneDatabase)

Refer to the DateTime API for more details about handling datetimes with time zones.

Performance tweaks

tz provides two environment options to tweak performance.

You can decrease compilation time, by rejecting time zone periods before a given year:

config :tz, reject_time_zone_periods_before_year: 2010

By default, no periods are rejected.

For time zones that have ongoing DST changes, period lookups for dates far in the future will result in periods being dynamically computed based on the IANA data. For example, what is the period for 20 March 2040 for New York (let's assume that the last rules for New York still mention an ongoing DST change as you read this)? We can't compile periods indefinitely in the future; by default, such periods are computed until 5 years from compilation time. Dynamic period computations is a slow operation.

You can decrease period lookup time for such periods lookups, by specifying until what year those periods have to be computed:

config :tz, build_time_zone_periods_with_ongoing_dst_changes_until_year: 20 + NaiveDateTime.utc_now().year

Note that increasing the year will also slightly increase compilation time, as it will generate more periods to compile.

Custom storage location of time zone files

By default, the files are stored in the priv directory of the tz library. You may customize the directory that will hold all of the IANA timezone data. For example, if you want to store the files in your project's priv dir instead:

config :tz, :data_dir, Path.join(Path.dirname(__DIR__), "priv")

Get the IANA time zone database version

Tz.iana_version() == "2022a"

Time zone utility functions

Tz's API is intentionally kept as minimal as possible to implement Calendar.TimeZoneDatabase's behaviour. Utility functions around time zones are provided by TzExtra.

Other IANA time zone database implementations

Installation

Add tz for Elixir as a dependency in your mix.exs file:

def deps do
  [
    {:tz, "~> 0.21.1"}
  ]
end

HexDocs

HexDocs documentation can be found at https://hexdocs.pm/tz.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].