Remove Timex.now (#4546)

* rm Timex.now

* fix test

---------

Co-authored-by: Adrian Gruntkowski <adrian.gruntkowski@gmail.com>
This commit is contained in:
ruslandoga 2024-09-09 16:40:15 +07:00 committed by GitHub
parent 8ee4827fea
commit d17ac82058
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
12 changed files with 20 additions and 19 deletions

View File

@ -99,7 +99,7 @@ defmodule Plausible.Auth.UserAdmin do
"ended"
%{end_date: %Date{} = end_date} ->
days_left = Timex.diff(end_date, Timex.now(), :days)
days_left = Timex.diff(end_date, DateTime.utc_now(), :days)
"#{days_left} days left"
end
end

View File

@ -98,7 +98,7 @@ defmodule Plausible.DataMigration.PopulateEventSessionColumns do
{:ok, %{rows: [[merges]]}} = run_sql("get-merges-progress")
{:ok, %{rows: disks}} = run_sql("get-disks")
IO.puts("\n\n#{Timex.now() |> DateTime.to_iso8601()}")
IO.puts("\n\n#{DateTime.utc_now() |> DateTime.to_iso8601()}")
# List partitions that need to run
IO.puts(

View File

@ -19,7 +19,7 @@ defmodule Plausible.DataMigration.VersionedSessions do
def run(opts \\ []) do
run_exchange? = Keyword.get(opts, :run_exchange?, true)
unique_suffix = Timex.now() |> Calendar.strftime(@suffix_format)
unique_suffix = DateTime.utc_now() |> Calendar.strftime(@suffix_format)
cluster? = Plausible.IngestRepo.clustered_table?("sessions_v2")

View File

@ -131,7 +131,7 @@ defmodule Plausible.Google.API do
end
defp needs_to_refresh_token?(%NaiveDateTime{} = expires_at) do
thirty_seconds_ago = DateTime.shift(Timex.now(), second: 30)
thirty_seconds_ago = DateTime.shift(DateTime.utc_now(), second: 30)
Timex.before?(expires_at, thirty_seconds_ago)
end

View File

@ -44,7 +44,7 @@ defmodule Plausible.Session.Salts do
defp generate_and_persist_new_salt() do
salt = :crypto.strong_rand_bytes(16)
Repo.insert_all("salts", [%{salt: salt, inserted_at: Timex.now()}])
Repo.insert_all("salts", [%{salt: salt, inserted_at: DateTime.utc_now()}])
salt
end

View File

@ -61,7 +61,7 @@ defmodule Plausible.Stats.Comparisons do
def compare(%Plausible.Site{} = site, %Stats.Query{} = source_query, mode, opts \\ []) do
opts =
opts
|> Keyword.put_new(:now, Timex.now(site.timezone))
|> Keyword.put_new(:now, DateTime.now!(site.timezone))
|> Keyword.put_new(:match_day_of_week?, false)
source_date_range = DateTimeRange.to_date_range(source_query.date_range)

View File

@ -239,12 +239,12 @@ defmodule Plausible.Stats.Legacy.QueryBuilder do
end
defp today(tz) do
Timex.now(tz) |> Timex.to_date()
DateTime.now!(tz) |> Timex.to_date()
end
defp parse_single_date(tz, params) do
case params["date"] do
"today" -> Timex.now(tz) |> Timex.to_date()
"today" -> DateTime.now!(tz) |> Timex.to_date()
date when is_binary(date) -> Date.from_iso8601!(date)
_ -> today(tz)
end

View File

@ -263,14 +263,14 @@ defmodule PlausibleWeb.Api.StatsController do
case query.interval do
"hour" ->
current_date =
Timex.now(site.timezone)
DateTime.now!(site.timezone)
|> Calendar.strftime("%Y-%m-%d %H:00:00")
Enum.find_index(dates, &(&1 == current_date))
"day" ->
current_date =
Timex.now(site.timezone)
DateTime.now!(site.timezone)
|> Timex.to_date()
|> Date.to_string()
@ -278,7 +278,7 @@ defmodule PlausibleWeb.Api.StatsController do
"week" ->
current_date =
Timex.now(site.timezone)
DateTime.now!(site.timezone)
|> Timex.to_date()
|> Time.date_or_weekstart(query)
|> Date.to_string()
@ -287,7 +287,7 @@ defmodule PlausibleWeb.Api.StatsController do
"month" ->
current_date =
Timex.now(site.timezone)
DateTime.now!(site.timezone)
|> Timex.to_date()
|> Timex.beginning_of_month()
|> Date.to_string()
@ -296,7 +296,7 @@ defmodule PlausibleWeb.Api.StatsController do
"minute" ->
current_date =
Timex.now(site.timezone)
DateTime.now!(site.timezone)
|> Calendar.strftime("%Y-%m-%d %H:%M:00")
Enum.find_index(dates, &(&1 == current_date))

View File

@ -49,7 +49,7 @@ defmodule Plausible.Workers.ScheduleEmailReports do
end
def monday_9am(timezone) do
Timex.now(timezone)
DateTime.now!(timezone)
|> DateTime.shift(week: 1)
|> Timex.beginning_of_week()
|> DateTime.shift(hour: 9)
@ -89,7 +89,7 @@ defmodule Plausible.Workers.ScheduleEmailReports do
end
def first_of_month_9am(timezone) do
Timex.now(timezone)
DateTime.now!(timezone)
|> DateTime.shift(month: 1)
|> Timex.beginning_of_month()
|> DateTime.shift(hour: 9)

View File

@ -58,7 +58,7 @@ defmodule Plausible.Workers.SendEmailReport do
defp put_last_month_query(%{site: site} = assigns) do
last_month =
Timex.now(site.timezone)
DateTime.now!(site.timezone)
|> DateTime.shift(month: -1)
|> Timex.beginning_of_month()
|> Date.to_iso8601()
@ -69,7 +69,7 @@ defmodule Plausible.Workers.SendEmailReport do
end
defp put_last_week_query(%{site: site} = assigns) do
today = Timex.now(site.timezone) |> DateTime.to_date()
today = DateTime.now!(site.timezone) |> DateTime.to_date()
date = Date.shift(today, week: -1) |> Timex.end_of_week() |> Date.to_iso8601()
query = Query.from(site, %{"period" => "7d", "date" => date})

View File

@ -44,7 +44,7 @@ defmodule Plausible.Workers.SendSiteSetupEmails do
for site <- Repo.all(q) do
owner = Plausible.Users.with_subscription(site.owner)
setup_completed = Plausible.Sites.has_stats?(site)
hours_passed = Timex.diff(Timex.now(), site.inserted_at, :hours)
hours_passed = Timex.diff(DateTime.utc_now(), site.inserted_at, :hours)
if !setup_completed && hours_passed > 47 do
send_setup_help_email(owner, site)

View File

@ -153,7 +153,8 @@ defmodule Plausible.Stats.QueryTest do
expected_first_datetime = DateTime.new!(~D[2020-01-01], ~T[00:00:00], site.timezone)
expected_last_datetime =
Timex.today(site.timezone)
DateTime.now!(site.timezone)
|> DateTime.to_date()
|> DateTime.new!(~T[23:59:59], site.timezone)
assert query.date_range.first == expected_first_datetime