diff options
Diffstat (limited to '')
-rw-r--r-- | lib/util.ex | 25 |
1 files changed, 25 insertions, 0 deletions
diff --git a/lib/util.ex b/lib/util.ex index d35157b..ce46174 100644 --- a/lib/util.ex +++ b/lib/util.ex @@ -1,5 +1,30 @@ defmodule Util do + def to_naive_date_time(naive = %NaiveDateTime{}), do: naive + def to_naive_date_time(datetime = %DateTime{}), do: DateTime.to_naive(datetime) + def to_naive_date_time(timestamp) when is_integer(timestamp) do + timestamp + |> to_date_time() + |> to_naive_date_time() + end + + def to_date_time(naive_or_timestamp, timezone \\ "Europe/Paris") + + def to_date_time(date = %DateTime{}, timezone) do + DateTime.shift_zone!(data, timezone, Tzdata.TimeZoneDatabase) + end + + def to_date_time(naive = %NaiveDateTime{}, timezone) do + DateTime.from_naive!(naive, timezone, Tzdata.TimeZoneDatabase) + end + + # todo: this is wrong. + def to_date_time(timestamp, timezone) when is_integer(timestamp) do + timestamp + |> DateTime.from_unix!(:millisecond) + |> DateTime.shift_zone!(timezone, Tzdata.TimeZoneDatabase) + end + def plusminus(number) when number > 0, do: "+#{number}" def plusminus(0), do: "0" def plusminus(number) when number < 0, do: "#{number}" |