lib/icalendar_timezones.ex

defmodule ICalendarTimezones do
  @moduledoc """
  Documentation for `ICalendarTimezones`.
  """
  import ICalendarTimezones.Backend, only: [timezones_data: 0]

  @timezone_data timezones_data()

  @doc """
  Returns VTIMEZONE data
  """
  @spec vtimezone(String.t()) :: String.t() | nil
  def vtimezone(tz_id) when is_binary(tz_id) do
    tz_data = vcalendar(tz_id)

    case Regex.run(~r/BEGIN:VTIMEZONE(.+?)END:VTIMEZONE/s, tz_data, capture: :first) do
      [tz_data] when is_binary(tz_data) -> tz_data
      _ -> nil
    end
  end

  @doc """
  Returns VCALENDAR objects containing VTIMEZONE
  """
  @spec vcalendar(String.t()) :: String.t() | nil
  def vcalendar(tz_id) when is_binary(tz_id) do
    Map.get(@timezone_data, tz_id)
  end

  @doc """
  Retuns the list of IANA timezones identifiers
  """
  defdelegate timezones_list, to: ICalendarTimezones.Backend
end