From a75d0b35b0a27380edd6ad9bf6114ccef3521331 Mon Sep 17 00:00:00 2001 From: Uku Taht Date: Fri, 24 Mar 2023 15:41:01 +0200 Subject: [PATCH] Fix Timex.total_offset blowing up during clock changes (#2788) * Fix Timex.total_offset blowing up during clock changes * Format large numbers with underscore in tests Co-authored-by: Adam --------- Co-authored-by: Adam --- lib/plausible/site.ex | 14 +++++++++ .../templates/stats/stats.html.eex | 2 +- test/plausible/site/schema_test.exs | 31 ++++++++++++++++++- 3 files changed, 45 insertions(+), 2 deletions(-) diff --git a/lib/plausible/site.ex b/lib/plausible/site.ex index 20540aa0e..aac4c9d5e 100644 --- a/lib/plausible/site.ex +++ b/lib/plausible/site.ex @@ -75,6 +75,20 @@ defmodule Plausible.Site do ) end + def tz_offset(site, utc_now \\ DateTime.utc_now()) do + case DateTime.shift_zone(utc_now, site.timezone) do + {:ok, datetime} -> + datetime.utc_offset + datetime.std_offset + + res -> + Sentry.capture_message("Unable to determine timezone offset for", + extra: %{site: site, result: res} + ) + + 0 + end + end + def make_public(site) do change(site, public: true) end diff --git a/lib/plausible_web/templates/stats/stats.html.eex b/lib/plausible_web/templates/stats/stats.html.eex index 1fe48fa19..98e76cf3d 100644 --- a/lib/plausible_web/templates/stats/stats.html.eex +++ b/lib/plausible_web/templates/stats/stats.html.eex @@ -16,7 +16,7 @@ id="stats-react-container" style="overflow-anchor: none;" data-domain="<%= @site.domain %>" - data-offset="<%= Timex.Timezone.total_offset(Timex.Timezone.get(@site.timezone)) %>" + data-offset="<%= Plausible.Site.tz_offset(@site) %>" data-has-goals="<%= @has_goals %>" data-logged-in="<%= !!@conn.assigns[:current_user] %>" data-stats-begin="<%= @stats_start_date %>" diff --git a/test/plausible/site/schema_test.exs b/test/plausible/site/schema_test.exs index f6ee40704..91a19761f 100644 --- a/test/plausible/site/schema_test.exs +++ b/test/plausible/site/schema_test.exs @@ -1,5 +1,34 @@ defmodule Plausible.SiteTest do - use ExUnit.Case + use Plausible.DataCase + alias Plausible.Site doctest Plausible.Site + + describe "tz_offset/2" do + test "returns offset from utc in seconds" do + site = build(:site, timezone: "US/Eastern") + + assert Site.tz_offset(site, ~U[2023-01-01 00:00:00Z]) == -18_000 + end + + test "returns correct offset from utc during summer time" do + site = build(:site, timezone: "US/Eastern") + + assert Site.tz_offset(site, ~U[2023-07-01 00:00:00Z]) == -14_400 + end + + test "returns correct offset when changing from winter to summer time" do + site = build(:site, timezone: "US/Eastern") + + assert Site.tz_offset(site, ~U[2023-03-12 06:59:59Z]) == -18_000 + assert Site.tz_offset(site, ~U[2023-03-12 07:00:00Z]) == -14_400 + end + + test "returns correct offset when changing from summer to winter time" do + site = build(:site, timezone: "US/Eastern") + + assert Site.tz_offset(site, ~U[2023-11-05 05:59:59Z]) == -14_400 + assert Site.tz_offset(site, ~U[2023-11-05 06:00:00Z]) == -18_000 + end + end end