Skip to content

Commit

Permalink
Switch to core DateTime library
Browse files Browse the repository at this point in the history
  • Loading branch information
maennchen committed Feb 26, 2020
1 parent 03e7e13 commit eebea08
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 318 deletions.
25 changes: 0 additions & 25 deletions lib/quantum/application.ex

This file was deleted.

94 changes: 57 additions & 37 deletions lib/quantum/date_library.ex
Original file line number Diff line number Diff line change
@@ -1,63 +1,83 @@
defmodule Quantum.DateLibrary do
@moduledoc """
This Behaviour offers Date Library Independant integration of helper
functions.
@moduledoc false

**This behaviour is considered internal. Breaking Changes can occur on every
release.**
require Logger

Make sure your implementation passes `Quantum.DateLibraryTest`. Otherwise
unexpected behaviour can occur.
alias Quantum.DateLibrary.{InvalidDateTimeForTimezoneError, InvalidTimezoneError}

@doc """
Convert Date to Utc
"""
@spec to_utc!(NaiveDateTime.t(), :utc | String.t()) :: NaiveDateTime.t()

@date_library Application.get_env(:quantum, :date_library, Quantum.DateLibrary.Timex)
def to_utc!(date, :utc), do: date

@doc """
Convert `NaiveDateTime` in UTC to `NaiveDateTime` in given tz.
def to_utc!(date, tz) when is_binary(tz) do
dt =
case DateTime.from_naive(date, tz) do
{:ok, dt} ->
dt

* Should raise an `InvalidTimezoneError` if the timezone is not valid.
"""
@callback utc_to_tz!(NaiveDateTime.t(), String.t()) :: NaiveDateTime.t() | no_return
{:ambiguous, _, _} ->
raise InvalidDateTimeForTimezoneError

@doc """
Convert `NaiveDateTime` in given tz to `NaiveDateTime` in UTC.
{:gap, _, _} ->
raise InvalidDateTimeForTimezoneError

* Should raise an `InvalidDateTimeForTimezoneError` if the time is not valid.
* Should raise an `InvalidTimezoneError` if the timezone is not valid.
"""
@callback tz_to_utc!(NaiveDateTime.t(), String.t()) :: NaiveDateTime.t() | no_return
{:error, :incompatible_calendars} ->
raise InvalidDateTimeForTimezoneError

@doc """
Gives back the required application dependency to start, if any is needed.
"""
@callback dependency_application :: atom | nil
{:error, :time_zone_not_found} ->
raise InvalidTimezoneError

@doc """
Convert Date to Utc
"""
@spec to_utc!(NaiveDateTime.t(), :utc | binary) :: NaiveDateTime.t() | no_return
def to_utc!(date, :utc), do: date
def to_utc!(date, tz) when is_binary(tz), do: @date_library.tz_to_utc!(date, tz)
{:error, :utc_only_time_zone_database} ->
Logger.warn("Timezone database not setup")
raise InvalidTimezoneError
end

case DateTime.shift_zone(dt, "Etc/UTC") do
{:ok, dt} ->
DateTime.to_naive(dt)

{:error, :utc_only_time_zone_database} ->
Logger.warn("Timezone database not setup")
raise InvalidTimezoneError
end
end

@doc """
Convert Date to TZ
"""
@spec to_utc!(NaiveDateTime.t(), :utc | binary) :: NaiveDateTime.t() | no_return
@spec to_tz!(NaiveDateTime.t(), :utc | String.t()) :: NaiveDateTime.t()
def to_tz!(date, :utc), do: date
def to_tz!(date, tz) when is_binary(tz), do: @date_library.utc_to_tz!(date, tz)

def to_tz!(date, tz) when is_binary(tz) do
result =
date
|> DateTime.from_naive!("Etc/UTC")
|> DateTime.shift_zone(tz)

case result do
{:ok, dt} ->
DateTime.to_naive(dt)

{:error, :time_zone_not_found} ->
raise InvalidTimezoneError

{:error, :utc_only_time_zone_database} ->
Logger.warn("Timezone database not setup")
raise InvalidTimezoneError
end
end

defmodule InvalidDateTimeForTimezoneError do
@moduledoc """
Raised when a time does not exist in a timezone. THis happens for example when chaninging from DST to normal time.
"""
@moduledoc false

defexception message: "The requested time does not exist in the given timezone."
end

defmodule InvalidTimezoneError do
@moduledoc """
Raised when a timezone does not exist.
"""
@moduledoc false

defexception message: "The requested timezone is invalid."
end
Expand Down
65 changes: 0 additions & 65 deletions lib/quantum/date_library/calendar.ex

This file was deleted.

85 changes: 0 additions & 85 deletions lib/quantum/date_library/core.ex

This file was deleted.

73 changes: 0 additions & 73 deletions lib/quantum/date_library/timex.ex

This file was deleted.

2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ defmodule Quantum.Mixfile do
end

def application do
[mod: {Quantum.Application, []}, extra_applications: [:logger]]
[extra_applications: [:logger]]
end

defp elixirc_paths(:test), do: ["lib", "test/support"]
Expand Down
Loading

0 comments on commit eebea08

Please sign in to comment.