diff --git a/lib/plausible/stats/query.ex b/lib/plausible/stats/query.ex index f5bd669a48a3..c4e18a01ec4c 100644 --- a/lib/plausible/stats/query.ex +++ b/lib/plausible/stats/query.ex @@ -57,15 +57,19 @@ defmodule Plausible.Stats.Query do Date.range( date_range.first, - earliest(date_range.last, today) + clamp(today, date_range) ) else date_range end end - defp earliest(a, b) do - if Date.compare(a, b) in [:eq, :lt], do: a, else: b + defp clamp(date, date_range) do + cond do + date in date_range -> date + Date.before?(date, date_range.first) -> date_range.first + Date.after?(date, date_range.last) -> date_range.last + end end def set(query, keywords) do diff --git a/test/plausible/stats/query_test.exs b/test/plausible/stats/query_test.exs index fae5524c35d3..ce5af98cd8e6 100644 --- a/test/plausible/stats/query_test.exs +++ b/test/plausible/stats/query_test.exs @@ -266,6 +266,13 @@ defmodule Plausible.Stats.QueryTest do ~U[2024-05-07 07:00:00Z], trim_trailing: true ) == Date.range(~D[2024-05-05], ~D[2024-05-06]) + + assert date_range( + {~U[2024-05-05 12:00:00Z], ~U[2024-05-08 11:59:59Z]}, + "Etc/GMT+12", + ~U[2024-05-03 07:00:00Z], + trim_trailing: true + ) == Date.range(~D[2024-05-05], ~D[2024-05-05]) end end