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 <hq@mtod.org>

---------

Co-authored-by: Adam <hq@mtod.org>
This commit is contained in:
Uku Taht 2023-03-24 15:41:01 +02:00 committed by GitHub
parent 6637751a5e
commit a75d0b35b0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 45 additions and 2 deletions

View File

@ -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

View File

@ -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 %>"

View File

@ -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