From 0dc8bbc865d3688daaa4fef6b11c47bc23636b4b Mon Sep 17 00:00:00 2001 From: Sunil Mohan Adapa Date: Tue, 28 Feb 2023 11:22:57 +0530 Subject: [PATCH] datetime: Use timedatectl to read current timezone Closes: #2326. There are plans to remove /etc/timezone from Debian (Debian bug: #822733). It has been removed temporarily and that caused failures in FreedomBox. Since we use systemd-timesyncd and timedatectl, use timedatectl to retrieve the currently set timezone value. This eliminates the need to read timezone at a lower level. Tests: - Getting and setting the timezone works. - Removing /etc/timezone and /etc/localtime then visiting the Date & Time app works. After setting the timezone, /etc/localtime file is created as symlink. Signed-off-by: Sunil Mohan Adapa Reviewed-by: James Valleroy --- plinth/modules/datetime/views.py | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/plinth/modules/datetime/views.py b/plinth/modules/datetime/views.py index 68c228e9e..c10f3b284 100644 --- a/plinth/modules/datetime/views.py +++ b/plinth/modules/datetime/views.py @@ -2,7 +2,7 @@ """FreedomBox app for configuring date and time.""" import logging -import pathlib +import subprocess from django.contrib import messages from django.utils.translation import gettext as _ @@ -24,15 +24,19 @@ class DateTimeAppView(AppView): def get_initial(self): """Return the values to fill in the form.""" status = super().get_initial() - status['time_zone'] = self.get_current_time_zone() + status['time_zone'] = self.get_current_time_zone() or 'none' return status @staticmethod def get_current_time_zone(): """Get current time zone.""" - path = pathlib.Path('/etc/timezone') - time_zone = path.read_text(encoding='utf-8').rstrip() - return time_zone or 'none' + try: + process = subprocess.run( + ['timedatectl', 'show', '--property', 'Timezone', '--value'], + stdout=subprocess.PIPE, check=True) + return process.stdout.decode().strip() + except subprocess.CalledProcessError: + return None def form_valid(self, form): """Change the timezone."""