Add proper fallback for when a TZ isn't registered
Signed-off-by: Thomas Citharel <tcit@tcit.fr>
This commit is contained in:
parent
f5791d1413
commit
14545fd983
|
@ -6,6 +6,8 @@ defmodule Mobilizon.Service.DateTime do
|
||||||
|
|
||||||
@typep to_string_format :: :short | :medium | :long | :full
|
@typep to_string_format :: :short | :medium | :long | :full
|
||||||
|
|
||||||
|
@utc_timezone "Etc/UTC"
|
||||||
|
|
||||||
@spec datetime_to_string(DateTime.t(), String.t(), to_string_format()) :: String.t()
|
@spec datetime_to_string(DateTime.t(), String.t(), to_string_format()) :: String.t()
|
||||||
def datetime_to_string(%DateTime{} = datetime, locale \\ "en", format \\ :medium) do
|
def datetime_to_string(%DateTime{} = datetime, locale \\ "en", format \\ :medium) do
|
||||||
Mobilizon.Cldr.DateTime.to_string!(datetime,
|
Mobilizon.Cldr.DateTime.to_string!(datetime,
|
||||||
|
@ -75,7 +77,7 @@ defmodule Mobilizon.Service.DateTime do
|
||||||
def calculate_next_day_notification(%Date{} = day, options \\ []) do
|
def calculate_next_day_notification(%Date{} = day, options \\ []) do
|
||||||
compare_to = Keyword.get(options, :compare_to, DateTime.utc_now())
|
compare_to = Keyword.get(options, :compare_to, DateTime.utc_now())
|
||||||
notification_time = Keyword.get(options, :notification_time, ~T[18:00:00])
|
notification_time = Keyword.get(options, :notification_time, ~T[18:00:00])
|
||||||
timezone = Keyword.get(options, :timezone, "Etc/UTC") || "Etc/UTC"
|
timezone = options |> Keyword.get(:timezone, @utc_timezone) |> fallback_tz()
|
||||||
|
|
||||||
send_at = DateTime.new!(day, notification_time, timezone)
|
send_at = DateTime.new!(day, notification_time, timezone)
|
||||||
|
|
||||||
|
@ -145,7 +147,7 @@ defmodule Mobilizon.Service.DateTime do
|
||||||
@spec appropriate_first_day_of_week(DateTime.t(), keyword) :: DateTime.t() | nil
|
@spec appropriate_first_day_of_week(DateTime.t(), keyword) :: DateTime.t() | nil
|
||||||
defp appropriate_first_day_of_week(%DateTime{} = datetime, options) do
|
defp appropriate_first_day_of_week(%DateTime{} = datetime, options) do
|
||||||
locale = Keyword.get(options, :locale, "en")
|
locale = Keyword.get(options, :locale, "en")
|
||||||
timezone = Keyword.get(options, :timezone, "Etc/UTC")
|
timezone = options |> Keyword.get(:timezone, @utc_timezone) |> fallback_tz()
|
||||||
|
|
||||||
local_datetime = datetime_tz_convert(datetime, timezone)
|
local_datetime = datetime_tz_convert(datetime, timezone)
|
||||||
|
|
||||||
|
@ -170,7 +172,7 @@ defmodule Mobilizon.Service.DateTime do
|
||||||
options
|
options
|
||||||
) do
|
) do
|
||||||
notification_time = Keyword.get(options, :notification_time, ~T[08:00:00])
|
notification_time = Keyword.get(options, :notification_time, ~T[08:00:00])
|
||||||
timezone = Keyword.get(options, :timezone, "Etc/UTC") || "Etc/UTC"
|
timezone = options |> Keyword.get(:timezone, @utc_timezone) |> fallback_tz()
|
||||||
DateTime.new!(date, notification_time, timezone)
|
DateTime.new!(date, notification_time, timezone)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -182,7 +184,7 @@ defmodule Mobilizon.Service.DateTime do
|
||||||
compare_to_day = Keyword.get(options, :compare_to_day, Date.utc_today())
|
compare_to_day = Keyword.get(options, :compare_to_day, Date.utc_today())
|
||||||
compare_to = Keyword.get(options, :compare_to_datetime, DateTime.utc_now())
|
compare_to = Keyword.get(options, :compare_to_datetime, DateTime.utc_now())
|
||||||
start_time = Keyword.get(options, :start_time, @start_time)
|
start_time = Keyword.get(options, :start_time, @start_time)
|
||||||
timezone = Keyword.get(options, :timezone, "Etc/UTC") || "Etc/UTC"
|
timezone = options |> Keyword.get(:timezone, @utc_timezone) |> fallback_tz()
|
||||||
end_time = Keyword.get(options, :end_time, @end_time)
|
end_time = Keyword.get(options, :end_time, @end_time)
|
||||||
|
|
||||||
DateTime.compare(compare_to, DateTime.new!(compare_to_day, start_time, timezone)) in [
|
DateTime.compare(compare_to, DateTime.new!(compare_to_day, start_time, timezone)) in [
|
||||||
|
@ -213,4 +215,13 @@ defmodule Mobilizon.Service.DateTime do
|
||||||
def is_same_day?(%DateTime{} = one, %DateTime{} = two) do
|
def is_same_day?(%DateTime{} = one, %DateTime{} = two) do
|
||||||
DateTime.to_date(one) == DateTime.to_date(two)
|
DateTime.to_date(one) == DateTime.to_date(two)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@spec fallback_tz(String.t()) :: String.t()
|
||||||
|
defp fallback_tz(timezone) do
|
||||||
|
if Tzdata.zone_exists?(timezone) do
|
||||||
|
timezone
|
||||||
|
else
|
||||||
|
@utc_timezone
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue