From 7d873d431e87a5509267d3b9891bb6e1e0813e84 Mon Sep 17 00:00:00 2001 From: catborise Date: Mon, 9 Nov 2026 11:29:03 +0300 Subject: [PATCH] fix(settings): TZ sets the panel time zone in Docker Docker builds settings.py from the template, whose TIME_ZONE was fixed to UTC: TZ in docker-compose moved the container's clock and its backup time but not the panel's dates, and Django then reset the process's TZ to UTC. TIME_ZONE is now TZ, or UTC; the installer still writes the server's zone into settings.py. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Kk4h2TcUiaWmU5MUszYSea --- README.md | 1 + docker-compose.yml | 1 + webvirtcloud/settings.py.template | 5 ++++- webvirtcloud/tests/test_settings_env.py | 9 ++++++++- 4 files changed, 14 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index a038e6e1..6bde5fe2 100644 --- a/README.md +++ b/README.md @@ -344,6 +344,7 @@ If libvirt warns `Host SMBIOS information is not available`, install `dmidecode` | `WEBVIRTCLOUD_DEBUG=1` | Django debug mode: error pages show the traceback and settings; for trying things out only, never on a panel others use | | `WEBVIRTCLOUD_DB_HOST` | use PostgreSQL instead of sqlite (new installs); `WEBVIRTCLOUD_DB_PORT`/`_NAME`/`_USER`/`_PASSWORD` default to 5432/webvirtcloud/webvirtcloud/empty | | `EMAIL_HOST`, `EMAIL_PORT`, `EMAIL_HOST_USER`, `EMAIL_HOST_PASSWORD`, `EMAIL_USE_TLS` | SMTP for e-mail OTP; `EMAIL_USE_TLS` is 1 (STARTTLS, the default) or 0 | +| `TZ` | time zone of the panel's dates and times and, in Docker, of the container (its daily backup), e.g. `Europe/Istanbul`; default UTC (the installer writes the server's zone into `settings.py` instead) | | `WEBVIRTCLOUD_LOG_FILE` | log file (default `data/webvirtcloud.log`; a relative path is from the panel's directory; rotated at 10 MB × 5) | | `SECRET_KEY` | Django secret key (default: the `data/secret_key` file) | | `ADMIN_USERNAME`, `ADMIN_PASSWORD` | the first superuser, read by the first `migrate` only, see [First login](#first-login) | diff --git a/docker-compose.yml b/docker-compose.yml index 15bcfb03..d2d09edf 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -29,6 +29,7 @@ services: # Host names/IPs the panel is reached by (comma-separated; "*" accepts any). # Without it only localhost works. Set it in docker-compose.override.yml. - ALLOWED_HOSTS=localhost + # Time zone of the panel's dates and the daily backup (e.g. Europe/Istanbul) - TZ=UTC volumes: # Persistent SQLite database and runtime data diff --git a/webvirtcloud/settings.py.template b/webvirtcloud/settings.py.template index cdc59720..bb5a3c78 100644 --- a/webvirtcloud/settings.py.template +++ b/webvirtcloud/settings.py.template @@ -235,7 +235,10 @@ LANGUAGES = [ ("zh-hans", "简体中文"), ] -TIME_ZONE = "UTC" +# the installer writes the server's zone; Docker takes TZ (Django sets the process's TZ from this) +TIME_ZONE = os.getenv("TZ") or "UTC" +if TIME_ZONE.startswith(":"): # ":/etc/localtime", a libc form naming a file, is no zone name for Django + TIME_ZONE = "UTC" USE_I18N = True diff --git a/webvirtcloud/tests/test_settings_env.py b/webvirtcloud/tests/test_settings_env.py index dedf2722..b5bd051c 100644 --- a/webvirtcloud/tests/test_settings_env.py +++ b/webvirtcloud/tests/test_settings_env.py @@ -16,7 +16,7 @@ CONFIG_VARS = [ "WEBVIRTCLOUD_HTTPS", "WEBVIRTCLOUD_DEBUG", "WEBVIRTCLOUD_SERIAL_CONSOLE", "ALLOWED_HOSTS", "EMAIL_HOST", "EMAIL_PORT", "EMAIL_HOST_USER", "EMAIL_HOST_PASSWORD", "EMAIL_USE_TLS", "WEBVIRTCLOUD_DB_HOST", "WEBVIRTCLOUD_DB_PORT", "WEBVIRTCLOUD_DB_NAME", - "WEBVIRTCLOUD_DB_USER", "WEBVIRTCLOUD_DB_PASSWORD", + "WEBVIRTCLOUD_DB_USER", "WEBVIRTCLOUD_DB_PASSWORD", "TZ", ] @@ -178,3 +178,10 @@ class LogFileEnvTestCase(SimpleTestCase): self.assertEqual(self.log_file({"WEBVIRTCLOUD_LOG_FILE": ""}), str(settings.BASE_DIR / "data" / "webvirtcloud.log")) with tempfile.TemporaryDirectory() as tmp: self.assertEqual(self.log_file({"WEBVIRTCLOUD_LOG_FILE": f"{tmp}/w.log"}), f"{tmp}/w.log") + + +class TimeZoneEnvTestCase(SimpleTestCase): + def test_time_zone_from_tz_or_utc(self): + self.assertEqual(load_settings({}, ["TIME_ZONE"])["TIME_ZONE"], "UTC") + self.assertEqual(load_settings({"TZ": "Europe/Istanbul"}, ["TIME_ZONE"])["TIME_ZONE"], "Europe/Istanbul") + self.assertEqual(load_settings({"TZ": ":/etc/localtime"}, ["TIME_ZONE"])["TIME_ZONE"], "UTC")