LostKobrakai

LostKobrakai

Tzinfo - Parsing the time zone information format (zonefiles)

Over the holidays I worked on a library to parse zonefiles, which are present on most UNIX systems, as a potential source of timezone information. This might be useful if your elixir system does need to share timezone information with other systems, which also rely on the OS provided zonefiles, where tzdata doesn’t fit as it’s downloading/using its own database from the IANA.

The package also includes a parser for POSIX time zone strings, which is what zonefiles hold in their footer part of version 2/3.

It does not yet include code for searching the data though, which also means there’s not yet an implementation for Calendar.TimeZoneDatabase like there is for tzdata.

https://github.com/LostKobrakai/tzinfo

# https://tools.ietf.org/html/rfc8536#appendix-B.2
> Tzinfo.Parser.parse(binary_contents)
{:ok, 
%{
  bin: "",
  version: 2,
  data: %{
    designations: %{
      0 => "LMT",
      4 => "HST",
      8 => "HDT",
      12 => "HWT",
      16 => "HPT"
    },
    leap_seconds: [],
    std_wall_indicators: [0, 0, 0, 0, 1, 0],
    transition_types: [1, 2, 1, 3, 4, 1, 5],
    transitions: [
      -2_334_101_314,
      -1_157_283_000,
      -1_155_436_200,
      -880_198_200,
      -769_395_600,
      -765_376_200,
      -712_150_200
    ],
    types: [
      %{designation_index: 0, dst: false, offset: -37886},
      %{designation_index: 4, dst: false, offset: -37800},
      %{designation_index: 8, dst: true, offset: -34200},
      %{designation_index: 12, dst: true, offset: -34200},
      %{designation_index: 16, dst: true, offset: -34200},
      %{designation_index: 4, dst: false, offset: -36000}
    ],
    ut_local_indicators: [0, 0, 0, 0, 1, 0]
  },
  footer: "HST10"
}}

Source file: RFC 8536 - The Time Zone Information Format (TZif)

iex(1)> Tzinfo.Posix.parse("PST8PDT,M3.2.0/2:00:00,M11.1.0/2:00:00")
{:ok,
 %{
   std: %{abbr: "PST", std_offset: 0, utc_offset: 28800},
   dst: %{
     abbr: "PDT",
     std_offset: 3600,
     utc_offset: 28800,
     start: %{
       midnight_offset: 7200,
       type: :month_based,
       value: %{day: 0, month: 3, week: 2}
     },
     end: %{
       midnight_offset: 7200,
       type: :month_based,
       value: %{day: 0, month: 11, week: 1}
     }
   }
 }}

First Post! Switch mode

wojtekmach

wojtekmach

Hex Core Team

Nice project! For anyone curious in trying it out with zone info files shipping with macOS you can do the following:

iex> Tzinfo.Parser.parse(File.read!("/usr/share/zoneinfo/America/New_York"))
{:ok,
 %{
   bin: "",
   counters: %{char: 20, isstd: 5, isut: 5, leap: 0, time: 236, type: 5},
   data: %{
     designations: %{
       0 => "LMT",
       4 => "EDT",
       8 => "EST",
       12 => "EWT",
       16 => "EPT"
     },
     ...
 }}
— All posts loaded —

Where Next?

Trending in Announcing Top

bluzky
You may know https://ui.shadcn.com/, a UI component library for React. I really love it’s design style and components. I’ve built some co...
387 14960 120
New
JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
shahryarjb
The Chelekom project is a library of Phoenix and LiveView components generated via Mix tasks to fit developer needs seamlessly. One of i...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New
ausimian
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
wintermeyer
There are three potential reasons for members of this forum to have a look at https://vutuv.de You are tired or annoyed of LinkedIn. Yo...
New

Other Trending Topics Top

type1fool
I just stumbled on a newly redesigned elixir-lang.org. :tada: It looks like @Software_Mansion did the work, and I think it is generally a...
New
akoutmos
@hugobarauna and I (Alex Koutmos) have been hard at work on writing a book on Nerves that takes you from simply blinking LEDs to building...
New
juhalehtonen
There has been a thread to discuss the Stack Overflow Developer Survey on this forum every year since 2018, so here’s yet another one for...
New
bjorng
We want to introduce a new native datatype to Erlang: native records. Although replacing all tuple records with native records is not our...
New
spammy
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
alexslade
Fly’s CEO posted this recently - Turn And Face The Strange · The Fly Blog It says that Fly is going all-in on sprites, which is a worry ...
New

We're in Beta

About us Mission Statement