Fix timezone input (#2222)

* Update Timex version from 3.7.7 to 3.7.8

* Generate timezone list from Tzdata

This commit fixes a bug where timezone changes weren't updating the
timezone list displayed when editing or creating a site.

Timezones were being pulled from a static list. This commit changes it
to generate the list from Tzdata, that uses a timezone database with
updated information on time changes. Additionally it adds more timezones
with aliases and links to the list.

Closes #1340

* Use timezone name from browser to recommend timezone

This commit matches the timezone name instead of offset to recommend a
timezone when creating a new site. The JavaScript Intl.DateTimeFormat
API is widely supported according to the link. In any case, if the
timezone fails to match by name, it fallbacks to the offset strategy.

https://caniuse.com/mdn-javascript_builtins_intl_datetimeformat_resolvedoptions_computed_timezone

Closes #904
pull/2229/head
Vinicius Brasil 4 years ago committed by GitHub
parent 2692bf4e38
commit 178e34b9e7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -31,6 +31,8 @@ All notable changes to this project will be documented in this file.
- The time format (12 hour or 24 hour) for graph timelines is now presented based on the browser's defined language
- Choice of metric for main-graph both in UI and API (visitors, pageviews, bounce_rate, visit_duration) plausible/analytics#1364
- New width=manual mode for embedded dashboards plausible/analytics#2148
- Add more timezone options
- Add new strategy to recommend timezone when creating a new site
### Fixed
- Hash part of the URL can now be used when excluding pages with `script.exclusions.hash.js`.
@ -42,6 +44,7 @@ All notable changes to this project will be documented in this file.
- The exported `pages.csv` file now includes pageviews again [plausible/analytics#1878](https://github.com/plausible/analytics/issues/1878)
- Fix a bug where city, region and country filters were filtering stats but not the location list
- Fix a bug where regions were not being saved
- Timezone offset labels now update with time changes
### Changed
- Cache the tracking script for 24 hours

@ -1,123 +1,23 @@
defmodule Plausible.Timezones do
@moduledoc "https://stackoverflow.com/a/52265733"
def options do
Tzdata.zone_list()
|> Enum.map(&build_option/1)
|> Enum.sort_by(& &1[:offset], :desc)
end
@options [
[key: "(GMT-12:00) International Date Line West", value: "Etc/GMT+12", offset: "720"],
[key: "(GMT-11:00) Midway Island, Samoa", value: "Pacific/Midway", offset: "660"],
[key: "(GMT-10:00) Hawaii", value: "Pacific/Honolulu", offset: "600"],
[key: "(GMT-09:00) Alaska", value: "US/Alaska", offset: "540"],
[key: "(GMT-08:00) Pacific Time (US & Canada)", value: "America/Los_Angeles", offset: "480"],
[key: "(GMT-08:00) Tijuana, Baja California", value: "America/Tijuana"],
[key: "(GMT-07:00) Arizona", value: "US/Arizona"],
[key: "(GMT-07:00) Chihuahua, La Paz, Mazatlan", value: "America/Chihuahua"],
[key: "(GMT-07:00) Mountain Time (US & Canada)", value: "US/Mountain", offset: "420"],
[key: "(GMT-06:00) Central America", value: "America/Managua"],
[key: "(GMT-06:00) Central Time (US & Canada)", value: "US/Central", offset: "360"],
[key: "(GMT-06:00) Guadalajara, Mexico City, Monterrey", value: "America/Mexico_City"],
[key: "(GMT-06:00) Saskatchewan", value: "Canada/Saskatchewan"],
[key: "(GMT-05:00) Bogota, Lima, Quito, Rio Branco", value: "America/Bogota"],
[key: "(GMT-05:00) Eastern Time (US & Canada)", value: "US/Eastern", offset: "300"],
[key: "(GMT-05:00) Indiana (East)", value: "US/East-Indiana"],
[key: "(GMT-04:00) Atlantic Time (Canada)", value: "Canada/Atlantic", offset: "240"],
[key: "(GMT-04:00) Caracas, La Paz", value: "America/Caracas"],
[key: "(GMT-04:00) Manaus", value: "America/Manaus"],
[key: "(GMT-04:00) Santiago", value: "America/Santiago"],
[key: "(GMT-03:30) Newfoundland", value: "Canada/Newfoundland"],
[key: "(GMT-03:00) Brasilia", value: "America/Sao_Paulo", offset: "180"],
[key: "(GMT-03:00) Buenos Aires, Georgetown", value: "America/Argentina/Buenos_Aires"],
[key: "(GMT-03:00) Greenland", value: "America/Godthab"],
[key: "(GMT-03:00) Montevideo", value: "America/Montevideo"],
[key: "(GMT-02:00) Mid-Atlantic", value: "America/Noronha", offset: "120"],
[key: "(GMT-01:00) Cape Verde Is.", value: "Atlantic/Cape_Verde", offset: "60"],
[key: "(GMT-01:00) Azores", value: "Atlantic/Azores"],
[key: "(GMT+00:00) Casablanca, Monrovia, Reykjavik", value: "Africa/Casablanca"],
[
key: "(GMT+00:00) Greenwich Mean Time : Dublin, Edinburgh, Lisbon, London",
value: "Etc/Greenwich",
offset: "0"
],
[
key: "(GMT+01:00) Amsterdam, Berlin, Bern, Rome, Stockholm, Vienna",
value: "Europe/Amsterdam",
offset: "-60"
],
[
key: "(GMT+01:00) Belgrade, Bratislava, Budapest, Ljubljana, Prague",
value: "Europe/Belgrade"
],
[key: "(GMT+01:00) Brussels, Copenhagen, Madrid, Paris", value: "Europe/Brussels"],
[key: "(GMT+01:00) Sarajevo, Skopje, Warsaw, Zagreb", value: "Europe/Sarajevo"],
[key: "(GMT+01:00) West Central Africa", value: "Africa/Lagos"],
[key: "(GMT+02:00) Amman", value: "Asia/Amman"],
[key: "(GMT+02:00) Athens, Bucharest, Istanbul", value: "Europe/Athens"],
[key: "(GMT+02:00) Beirut", value: "Asia/Beirut"],
[key: "(GMT+02:00) Cairo", value: "Africa/Cairo"],
[key: "(GMT+02:00) Harare, Pretoria", value: "Africa/Harare"],
[
key: "(GMT+02:00) Helsinki, Kyiv, Riga, Sofia, Tallinn, Vilnius",
value: "Europe/Helsinki",
offset: "-120"
],
[key: "(GMT+02:00) Jerusalem", value: "Asia/Jerusalem"],
[key: "(GMT+02:00) Minsk", value: "Europe/Minsk"],
[key: "(GMT+02:00) Windhoek", value: "Africa/Windhoek"],
[key: "(GMT+03:00) Kuwait, Riyadh, Baghdad", value: "Asia/Kuwait"],
[
key: "(GMT+03:00) Moscow, St. Petersburg, Volgograd",
value: "Europe/Moscow",
offset: "-180"
],
[key: "(GMT+03:00) Nairobi", value: "Africa/Nairobi"],
[key: "(GMT+03:00) Tbilisi", value: "Asia/Tbilisi"],
[key: "(GMT+03:30) Tehran", value: "Asia/Tehran", offset: "-210"],
[key: "(GMT+04:00) Abu Dhabi, Muscat", value: "Asia/Muscat", offset: "-240"],
[key: "(GMT+04:00) Baku", value: "Asia/Baku"],
[key: "(GMT+04:00) Yerevan", value: "Asia/Yerevan"],
[key: "(GMT+04:30) Kabul", value: "Asia/Kabul", offset: "-270"],
[key: "(GMT+05:00) Yekaterinburg", value: "Asia/Yekaterinburg"],
[key: "(GMT+05:00) Islamabad, Karachi, Tashkent", value: "Asia/Karachi", offset: "-300"],
[
key: "(GMT+05:30) Chennai, Kolkata, Mumbai, New Delhi",
value: "Asia/Calcutta",
offset: "-330"
],
[key: "(GMT+05:30) Sri Jayawardenapura", value: "Asia/Calcutta"],
[key: "(GMT+05:45) Kathmandu", value: "Asia/Katmandu", offset: "-345"],
[key: "(GMT+06:00) Almaty, Novosibirsk", value: "Asia/Almaty", offset: "-360"],
[key: "(GMT+06:00) Astana, Dhaka", value: "Asia/Dhaka"],
[key: "(GMT+06:30) Yangon (Rangoon)", value: "Asia/Rangoon", offset: "-390"],
[key: "(GMT+07:00) Bangkok, Hanoi, Jakarta", value: "Asia/Bangkok", offset: "-420"],
[key: "(GMT+07:00) Krasnoyarsk", value: "Asia/Krasnoyarsk"],
[
key: "(GMT+08:00) Beijing, Chongqing, Hong Kong, Urumqi",
value: "Asia/Hong_Kong",
offset: "-480"
],
[key: "(GMT+08:00) Kuala Lumpur, Singapore", value: "Asia/Kuala_Lumpur"],
[key: "(GMT+08:00) Irkutsk, Ulaan Bataar", value: "Asia/Irkutsk"],
[key: "(GMT+08:00) Perth", value: "Australia/Perth"],
[key: "(GMT+08:00) Taipei", value: "Asia/Taipei"],
[key: "(GMT+09:00) Osaka, Sapporo, Tokyo", value: "Asia/Tokyo", offset: "-540"],
[key: "(GMT+09:00) Seoul", value: "Asia/Seoul"],
[key: "(GMT+09:00) Yakutsk", value: "Asia/Yakutsk"],
[key: "(GMT+09:30) Adelaide", value: "Australia/Adelaide", offset: "-570"],
[key: "(GMT+09:30) Darwin", value: "Australia/Darwin"],
[key: "(GMT+10:00) Brisbane", value: "Australia/Brisbane", offset: "-600"],
[key: "(GMT+10:00) Canberra, Melbourne, Sydney", value: "Australia/Canberra"],
[key: "(GMT+10:00) Hobart", value: "Australia/Hobart"],
[key: "(GMT+10:00) Guam, Port Moresby", value: "Pacific/Guam"],
[key: "(GMT+10:00) Vladivostok", value: "Asia/Vladivostok"],
[
key: "(GMT+11:00) Magadan, Solomon Is., New Caledonia",
value: "Asia/Magadan",
offset: "-660"
],
[key: "(GMT+12:00) Auckland, Wellington", value: "Pacific/Auckland", offset: "-720"],
[key: "(GMT+12:00) Fiji, Kamchatka, Marshall Is.", value: "Pacific/Fiji"],
[key: "(GMT+13:00) Nuku'alofa", value: "Pacific/Tongatapu", offset: "-780"]
]
defp build_option(timezone_code) do
timezone_info = Timex.Timezone.get(timezone_code)
offset_in_minutes = div(timezone_info.offset_utc, -60)
def options() do
@options
hhmm_formatted_offset =
timezone_info
|> Timex.TimezoneInfo.format_offset()
|> String.slice(0..-4)
[
key: "(GMT#{hhmm_formatted_offset}) #{timezone_code}",
value: timezone_code,
offset: offset_in_minutes
]
end
end

@ -63,8 +63,18 @@
</div>
</div>
<script>
var offset = (new Date()).getTimezoneOffset()
var option = document.querySelector('#tz-select option[offset="' + offset + '"]')
var option;
if (typeof Intl !== "undefined") {
var timezoneName = Intl.DateTimeFormat().resolvedOptions().timeZone
option = document.querySelector('#tz-select option[value="' + timezoneName + '"]')
}
if (!option) {
var offset = (new Date()).getTimezoneOffset()
option = document.querySelector('#tz-select option[offset="' + offset + '"]')
}
if (option) { option.selected = "selected"}
</script>

@ -110,7 +110,7 @@ defmodule Plausible.MixProject do
{:sentry, "~> 8.0"},
{:siphash, "~> 3.2"},
{:telemetry, "~> 1.0", override: true},
{:timex, "~> 3.6"},
{:timex, "~> 3.7"},
{:ua_inspector, "~> 3.0"}
]
end

@ -114,7 +114,7 @@
"telemetry_metrics_prometheus_core": {:hex, :telemetry_metrics_prometheus_core, "1.0.2", "c98b1c580de637bfeac00db41b9fb91fb4c3548ee3d512a8ed7299172312eaf3", [:mix], [{:telemetry, "~> 0.4 or ~> 1.0.0", [hex: :telemetry, repo: "hexpm", optional: false]}, {:telemetry_metrics, "~> 0.6", [hex: :telemetry_metrics, repo: "hexpm", optional: false]}], "hexpm", "48351a0d56f80e38c997b44232b1043e0a081670d16766eee920e6254175b730"},
"telemetry_poller": {:hex, :telemetry_poller, "1.0.0", "db91bb424e07f2bb6e73926fcafbfcbcb295f0193e0a00e825e589a0a47e8453", [:rebar3], [{:telemetry, "~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "b3a24eafd66c3f42da30fc3ca7dda1e9d546c12250a2d60d7b81d264fbec4f6e"},
"telemetry_registry": {:hex, :telemetry_registry, "0.3.0", "6768f151ea53fc0fbca70dbff5b20a8d663ee4e0c0b2ae589590e08658e76f1e", [:mix, :rebar3], [{:telemetry, "~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "492e2adbc609f3e79ece7f29fec363a97a2c484ac78a83098535d6564781e917"},
"timex": {:hex, :timex, "3.7.7", "3ed093cae596a410759104d878ad7b38e78b7c2151c6190340835515d4a46b8a", [:mix], [{:combine, "~> 0.10", [hex: :combine, repo: "hexpm", optional: false]}, {:gettext, "~> 0.10", [hex: :gettext, repo: "hexpm", optional: false]}, {:tzdata, "~> 1.0", [hex: :tzdata, repo: "hexpm", optional: false]}], "hexpm", "0ec4b09f25fe311321f9fc04144a7e3affe48eb29481d7a5583849b6c4dfa0a7"},
"timex": {:hex, :timex, "3.7.8", "0e6e8bf7c0aba95f1e13204889b2446e7a5297b1c8e408f15ab58b2c8dc85f81", [:mix], [{:combine, "~> 0.10", [hex: :combine, repo: "hexpm", optional: false]}, {:gettext, "~> 0.10", [hex: :gettext, repo: "hexpm", optional: false]}, {:tzdata, "~> 1.1", [hex: :tzdata, repo: "hexpm", optional: false]}], "hexpm", "8f3b8edc5faab5205d69e5255a1d64a83b190bab7f16baa78aefcb897cf81435"},
"tls_certificate_check": {:hex, :tls_certificate_check, "1.14.0", "6d1638d56ac68b25c987d401dffb7cd059281339aadc3f8bf27ab33ee19ddbfe", [:rebar3], [{:ssl_verify_fun, "1.1.6", [hex: :ssl_verify_fun, repo: "hexpm", optional: false]}], "hexpm", "b4452ddd3ae89cd84451afa0e218cb3ccd5178fe3c1de7fabcbddb12a137bcf4"},
"tzdata": {:hex, :tzdata, "1.1.1", "20c8043476dfda8504952d00adac41c6eda23912278add38edc140ae0c5bcc46", [:mix], [{:hackney, "~> 1.17", [hex: :hackney, repo: "hexpm", optional: false]}], "hexpm", "a69cec8352eafcd2e198dea28a34113b60fdc6cb57eb5ad65c10292a6ba89787"},
"ua_inspector": {:hex, :ua_inspector, "3.0.1", "649e68e31296a7bc39204e84b13a14c245b61df43fde0dd91ef784a98ecb04d4", [:mix], [{:hackney, "~> 1.0", [hex: :hackney, repo: "hexpm", optional: false]}, {:yamerl, "~> 0.7", [hex: :yamerl, repo: "hexpm", optional: false]}], "hexpm", "b510ce5a5fd70a37bf5eab8008a25fc57388ea240b95bb4d98fc892648f9b916"},

@ -0,0 +1,14 @@
defmodule Plausible.TimezonesTest do
use ExUnit.Case, async: true
test "options/0 returns a list of timezones" do
options = Plausible.Timezones.options()
refute Enum.empty?(options)
gmt12 = Enum.find(options, &(&1[:value] == "Etc/GMT+12"))
assert [key: "(GMT-12:00) Etc/GMT+12", value: "Etc/GMT+12", offset: 720] = gmt12
hawaii = Enum.find(options, &(&1[:value] == "US/Hawaii"))
assert [key: "(GMT-10:00) US/Hawaii", value: "US/Hawaii", offset: 600] = hawaii
end
end
Loading…
Cancel
Save