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 <sunil@medhas.org>
Reviewed-by: James Valleroy <jvalleroy@mailbox.org>
This commit is contained in:
Sunil Mohan Adapa 2023-02-28 11:22:57 +05:30 committed by James Valleroy
parent bf2e53f323
commit 0dc8bbc865
No known key found for this signature in database
GPG Key ID: 77C0C75E7B650808

View File

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