mirror of
https://github.com/plausible/analytics.git
synced 2024-12-22 17:11:36 +03:00
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:
parent
6637751a5e
commit
a75d0b35b0
@ -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
|
||||
|
@ -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 %>"
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user