Skip to content

Commit 8d6d828

Browse files
authored
Reduce reliance on Timex and use native time API where feasible (#5712)
* Replace usages of `Timex.to_unix` with native API * Wrap call to `Timex.is_valid_timezone?` * Wrap calls to `Timex.today(tz)` * Replace `Timex.today()` with `Date.utc_today()` * Replace `Timex.now()` with `DateTime.utc_now()` * Replace `Timex.compare` with `Date.compare` * Wrap `Timex.diff` calls * Replace `Timex.Timezone.convert` with `DateTime.shift_zone!` * Wrap `Timex.parse!` * Replace `Timex.to_date` with native API calls * Replace `Timex.beginning|end_of...` with native API calls for Date * Wrap `Timex.beginning|end_of...` for DateTimes and Dates for years * Replace `Timex.format(!)` with native API calls * Replace `Timex.to_naive_datetime` with native API calls * Wrap time humanizing routines using Timex * Remove unnecessary `use Timex` instances * Replace `Timex.shift` with native API calls * Make `QueryParser.parse_date` handle gaps and ambiguities gracefully * Replace `Timex.now(tz)` with `DateTime.now!(tz)` * Use a more suitable Date function for comparison (h/t @aerosol)
1 parent f3950b2 commit 8d6d828

55 files changed

Lines changed: 412 additions & 284 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

lib/plausible/auth/totp.ex

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,9 @@ defmodule Plausible.Auth.TOTP do
336336
|> Repo.one()
337337

338338
if datetime do
339-
Timex.to_unix(datetime)
339+
datetime
340+
|> DateTime.from_naive!("Etc/UTC")
341+
|> DateTime.to_unix()
340342
end
341343
end
342344

lib/plausible/billing/subscriptions.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ defmodule Plausible.Billing.Subscriptions do
2727

2828
def expired?(%Subscription{next_bill_date: next_bill_date} = subscription) do
2929
deleted? = Subscription.Status.deleted?(subscription)
30-
expired? = Timex.compare(next_bill_date, Date.utc_today()) < 0
30+
expired? = Date.before?(next_bill_date, Date.utc_today())
3131

3232
deleted? && expired?
3333
end

lib/plausible/data_migration/populate_event_session_columns.ex

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -150,8 +150,6 @@ defmodule Plausible.DataMigration.PopulateEventSessionColumns do
150150
end
151151

152152
defp format_duration(seconds) do
153-
seconds
154-
|> Timex.Duration.from_seconds()
155-
|> Timex.format_duration(Timex.Format.Duration.Formatters.Humanized)
153+
Plausible.Times.humanize_seconds(seconds)
156154
end
157155
end

lib/plausible/exports.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ defmodule Plausible.Exports do
154154
["expiry-date=", expiry_date, ", rule-id=", _rule_id] =
155155
String.split(x_amz_expiration, "\"", trim: true)
156156

157-
Timex.parse!(expiry_date, "{RFC1123}")
157+
Plausible.Times.parse!(expiry_date, "{RFC1123}")
158158
end
159159

160160
%{

lib/plausible/google/api.ex

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ defmodule Plausible.Google.API do
33
API to Google services.
44
"""
55

6-
use Timex
7-
86
alias Plausible.Google.HTTP
97
alias Plausible.Google.SearchConsole
108
alias Plausible.Stats.Query

lib/plausible/google/ga4/http.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ defmodule Plausible.Google.GA4.HTTP do
269269
date =
270270
case report["rows"] do
271271
[%{"dimensionValues" => [%{"value" => date_str}]}] ->
272-
Timex.parse!(date_str, "%Y%m%d", :strftime) |> NaiveDateTime.to_date()
272+
Plausible.Times.parse!(date_str, "%Y%m%d", :strftime) |> NaiveDateTime.to_date()
273273

274274
_ ->
275275
nil

lib/plausible/imported/google_analytics4.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,7 @@ defmodule Plausible.Imported.GoogleAnalytics4 do
337337

338338
defp get_date(%{dimensions: %{"date" => date}}) do
339339
date
340-
|> Timex.parse!("%Y%m%d", :strftime)
340+
|> Plausible.Times.parse!("%Y%m%d", :strftime)
341341
|> NaiveDateTime.to_date()
342342
end
343343

lib/plausible/site.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ defmodule Plausible.Site do
222222
defp validate_timezone(changeset) do
223223
tz = get_field(changeset, :timezone)
224224

225-
if Timex.is_valid_timezone?(tz) do
225+
if Plausible.Timezones.valid?(tz) do
226226
changeset
227227
else
228228
add_error(changeset, :timezone, "is invalid")

lib/plausible/stats/filters/query_parser.ex

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -197,8 +197,12 @@ defmodule Plausible.Stats.Filters.QueryParser do
197197
end
198198

199199
defp parse_date(site, date_string, _date, _now) when is_binary(date_string) do
200-
case Date.from_iso8601(date_string) do
201-
{:ok, date} -> {:ok, date, DateTime.new!(date, ~T[00:00:00], site.timezone)}
200+
with {:ok, date} <- Date.from_iso8601(date_string),
201+
{:ok, datetime} <- DateTime.new(date, ~T[00:00:00], site.timezone) do
202+
{:ok, date, datetime}
203+
else
204+
{:gap, just_before, _just_after} -> just_before
205+
{:ambiguous, first_datetime, _second_datetime} -> first_datetime
202206
_ -> {:error, "Invalid date '#{date_string}'."}
203207
end
204208
end
@@ -231,8 +235,8 @@ defmodule Plausible.Stats.Filters.QueryParser do
231235
end
232236

233237
defp parse_time_range(site, "year", date, _now) do
234-
last = date |> Timex.end_of_year()
235-
first = last |> Timex.beginning_of_year()
238+
last = date |> Plausible.Times.end_of_year()
239+
first = last |> Plausible.Times.beginning_of_year()
236240
{:ok, DateTimeRange.new!(first, last, site.timezone)}
237241
end
238242

lib/plausible/stats/interval.ex

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ defmodule Plausible.Stats.Interval do
4242
"""
4343
def default_for_date_range(%DateTimeRange{first: first, last: last}) do
4444
cond do
45-
Timex.diff(last, first, :months) > 0 ->
45+
Plausible.Times.diff(last, first, :month) > 0 ->
4646
"month"
4747

4848
DateTime.diff(last, first, :day) > 0 ->
@@ -75,15 +75,15 @@ defmodule Plausible.Stats.Interval do
7575
table =
7676
with %Date{} = from <- Keyword.get(opts, :from),
7777
%Date{} = to <- Keyword.get(opts, :to),
78-
true <- abs(Timex.diff(from, to, :months)) > 12 do
78+
true <- abs(Plausible.Times.diff(from, to, :month)) > 12 do
7979
Map.replace(@valid_by_period, "custom", ["week", "month"])
8080
else
8181
_ ->
8282
@valid_by_period
8383
end
8484

8585
with %Date{} = stats_start <- Plausible.Sites.stats_start_date(site),
86-
true <- abs(Timex.diff(Date.utc_today(), stats_start, :months)) > 12 do
86+
true <- abs(Plausible.Times.diff(Date.utc_today(), stats_start, :month)) > 12 do
8787
Map.replace(table, "all", ["week", "month"])
8888
else
8989
_ ->

0 commit comments

Comments
 (0)