Skip to content

Open source, privacy-first web analytics. Lightweight, cookie-free Google Analytics alternative. Self-hosted or cloud.

Fix timezone display

Uku Taht9f2cdea50381
diff --git a/lib/plausible/stats/stats.ex b/lib/plausible/stats/stats.ex
index f3a8e1efaf..6dc5060773 100644
--- a/lib/plausible/stats/stats.ex
+++ b/lib/plausible/stats/stats.ex
@@ -244,9 +244,11 @@ defmodule Plausible.Stats do
defp base_query(site, query) do
{:ok, first} = NaiveDateTime.new(query.date_range.first, ~T[00:00:00])
first_datetime = Timex.to_datetime(first, site.timezone)
+ |> Timex.Timezone.convert("UTC")
{:ok, last} = NaiveDateTime.new(query.date_range.last |> Timex.shift(days: 1), ~T[00:00:00])
last_datetime = Timex.to_datetime(last, site.timezone)
+ |> Timex.Timezone.convert("UTC")
from(e in Plausible.Event,
where: e.name == "pageview",
diff --git a/test/plausible/stats/stats_test.exs b/test/plausible/stats/stats_test.exs
index 2467771f60..306375c2c8 100644
--- a/test/plausible/stats/stats_test.exs
+++ b/test/plausible/stats/stats_test.exs
@@ -152,6 +152,19 @@ defmodule Plausible.StatsTest do
assert plot == [1] ++ zeroes ++ [1]
end
+ test "displays hourly stats in configured timezone" do
+ site = insert(:site, timezone: "CET") # UTC+1
+ insert(:pageview, hostname: site.domain, timestamp: ~N[2019-01-01 00:00:00]) # Timestamp is in UTC
+
+ query = Stats.Query.from(site.timezone, %{"period" => "day", "date" => "2019-01-01"})
+
+ {plot, _labels, _index} = Stats.calculate_plot(site, query)
+
+ zeroes = Stream.repeatedly(fn -> 0 end) |> Stream.take(22) |> Enum.into([])
+
+ assert plot == [0, 1] ++ zeroes # Expecting pageview to show at 1am CET
+ end
+
test "displays pageviews for a month" do
site = insert(:site)
insert(:pageview, hostname: site.domain, timestamp: ~N[2019-01-01 12:00:00])