date_time.ex 6.85 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
defmodule Mobilizon.Service.DateTime do
  @moduledoc """
  Module to represent a datetime in a given locale
  """
  alias Cldr.DateTime.Relative

  def datetime_to_string(%DateTime{} = datetime, locale \\ "en", format \\ :medium) do
    Mobilizon.Cldr.DateTime.to_string!(datetime, format: format, locale: locale_or_default(locale))
  end

  def datetime_to_time_string(%DateTime{} = datetime, locale \\ "en", format \\ :short) do
    Mobilizon.Cldr.Time.to_string!(datetime, format: format, locale: locale_or_default(locale))
  end

  @spec datetime_tz_convert(DateTime.t(), String.t()) :: DateTime.t()
  def datetime_tz_convert(%DateTime{} = datetime, timezone) do
    case DateTime.shift_zone(datetime, timezone) do
Thomas Citharel's avatar
Thomas Citharel committed
18
19
      {:ok, datetime_with_tz} ->
        datetime_with_tz
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40

      _ ->
        datetime
    end
  end

  @spec datetime_relative(DateTime.t(), String.t()) :: String.t()
  def datetime_relative(%DateTime{} = datetime, locale \\ "en") do
    Relative.to_string!(datetime, Mobilizon.Cldr,
      relative_to: DateTime.utc_now(),
      locale: locale_or_default(locale)
    )
  end

  defp locale_or_default(locale) do
    if Mobilizon.Cldr.known_locale_name(locale) do
      locale
    else
      "en"
    end
  end
Thomas Citharel's avatar
Thomas Citharel committed
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64

  def is_first_day_of_week(%Date{} = date, locale \\ "en") do
    Date.day_of_week(date) == Cldr.Calendar.first_day_for_locale(locale)
  end

  @spec calculate_first_day_of_week(Date.t(), String.t()) :: Date.t()
  def calculate_first_day_of_week(%Date{} = date, locale \\ "en") do
    if is_first_day_of_week(date, locale),
      do: date,
      else: calculate_first_day_of_week(Date.add(date, -1), locale)
  end

  @doc """
  Calculate the time when a notification should be sent, based on a daily schedule

  ## Parameters
   * `compare_to` When to compare to. Defaults to the current datetime
   * `notification_time` The time when the notification is being sent. Defaults to `~T[08:00:00]`
   * `timezone` The user's timezone. Needed to convert the time in the user's local timezone. Defaults to `"Etc/UTC"`
  """
  @spec calculate_next_day_notification(Date.t(), Keyword.t()) :: DateTime.t()
  def calculate_next_day_notification(%Date{} = day, options \\ []) do
    compare_to = Keyword.get(options, :compare_to, DateTime.utc_now())
    notification_time = Keyword.get(options, :notification_time, ~T[18:00:00])
65
    timezone = Keyword.get(options, :timezone, "Etc/UTC") || "Etc/UTC"
Thomas Citharel's avatar
Thomas Citharel committed
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192

    send_at = DateTime.new!(day, notification_time, timezone)

    if DateTime.compare(send_at, compare_to) == :lt do
      day
      |> Date.add(1)
      |> DateTime.new!(notification_time, timezone)
    else
      send_at
    end
  end

  @doc """
  Calculate the time when a notification should be sent, based on a weekly schedule

  ## Parameters
   * `compare_to` When to compare to. Defaults to the current datetime
   * `notification_time` The time when the notification is being sent. Defaults to `~T[08:00:00]`
   * `timezone` The user's timezone. Needed to convert the time in the user's local timezone. Defaults to `"Etc/UTC"`
   * `locale` The user's locale. Allows to get the first day of the week to send the notification on the beginning of the week. Defaults to `"en"`.
  """
  @spec calculate_next_week_notification(DateTime.t(), Keyword.t()) :: DateTime.t() | nil
  def calculate_next_week_notification(begins_on, options \\ []) do
    # That's now, but we allow to override it for tests
    compare_to = Keyword.get(options, :compare_to, DateTime.utc_now())

    # If the event is in the future
    if DateTime.compare(begins_on, compare_to) == :gt do
      # We get the day of the scheduled notification next week
      notification_date = appropriate_first_day_of_week(begins_on, options)

      if is_nil(notification_date) do
        nil
      else
        # This is the datetime when the notification should be sent
        if DateTime.compare(notification_date, compare_to) == :gt do
          notification_date
        else
          nil
        end
      end
    else
      # In the past, don't send anything
      nil
    end
  end

  @spec next_first_day_of_week(DateTime.t(), Keyword.t()) :: Date.t() | nil
  def next_first_day_of_week(%DateTime{} = datetime, options) do
    locale = Keyword.get(options, :locale, "en")
    compare_to = Keyword.get(options, :compare_to, DateTime.utc_now())

    next_first_day_of_week =
      compare_to
      |> DateTime.to_date()
      |> calculate_first_day_of_week(locale)
      |> Timex.add(Timex.Duration.from_weeks(1))
      |> build_notification_datetime(options)

    if Date.compare(datetime, next_first_day_of_week) == :gt do
      next_first_day_of_week
    else
      nil
    end
  end

  def appropriate_first_day_of_week(%DateTime{} = datetime, options) do
    locale = Keyword.get(options, :locale, "en")
    timezone = Keyword.get(options, :timezone, "Etc/UTC")

    local_datetime = datetime_tz_convert(datetime, timezone)

    first_day = local_datetime |> DateTime.to_date() |> calculate_first_day_of_week(locale)
    first_datetime = build_notification_datetime(first_day, options)

    if DateTime.compare(local_datetime, first_datetime) == :gt do
      first_datetime
    else
      next_first_day_of_week(local_datetime, options)
    end
  end

  @spec build_notification_datetime(Date.t(), Keyword.t()) :: DateTime.t()
  def build_notification_datetime(
        %Date{} = date,
        options
      ) do
    notification_time = Keyword.get(options, :notification_time, ~T[08:00:00])
    timezone = Keyword.get(options, :timezone, "Etc/UTC")
    DateTime.new!(date, notification_time, timezone)
  end

  @start_time ~T[08:00:00]
  @end_time ~T[09:00:00]

  @spec is_between_hours(Keyword.t()) :: boolean()
  def is_between_hours(options \\ []) when is_list(options) do
    compare_to_day = Keyword.get(options, :compare_to_day, Date.utc_today())
    compare_to = Keyword.get(options, :compare_to_datetime, DateTime.utc_now())
    start_time = Keyword.get(options, :start_time, @start_time)
    timezone = Keyword.get(options, :timezone, "Etc/UTC")
    end_time = Keyword.get(options, :end_time, @end_time)

    DateTime.compare(compare_to, DateTime.new!(compare_to_day, start_time, timezone)) in [
      :gt,
      :eq
    ] &&
      DateTime.compare(
        compare_to,
        DateTime.new!(compare_to_day, end_time, timezone)
      ) == :lt
  end

  @spec is_between_hours_on_first_day(Keyword.t()) :: boolean()
  def is_between_hours_on_first_day(options) when is_list(options) do
    compare_to_day = Keyword.get(options, :compare_to_day, Date.utc_today())
    locale = Keyword.get(options, :locale, "en")

    Mobilizon.Service.DateTime.is_first_day_of_week(compare_to_day, locale) &&
      is_between_hours(options)
  end

  @spec is_delay_ok_since_last_notification_sent(DateTime.t()) :: boolean()
  def is_delay_ok_since_last_notification_sent(%DateTime{} = last_notification_sent) do
    DateTime.compare(DateTime.add(last_notification_sent, 3_600), DateTime.utc_now()) ==
      :lt
  end
193
end