mirror of
https://github.com/freedombox/FreedomBox.git
synced 2026-02-11 08:23:49 +00:00
datetime: Use timedatectl to get list of time zones
Reviewed-by: Sunil Mohan Adapa <sunil@medhas.org>
This commit is contained in:
parent
1f5f1e0460
commit
68a0ed48fa
@ -21,18 +21,21 @@ Forms for configuring date and time
|
||||
|
||||
from django import forms
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
import glob
|
||||
import re
|
||||
import logging
|
||||
import subprocess
|
||||
|
||||
from plinth.forms import ServiceForm
|
||||
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class DateTimeForm(ServiceForm):
|
||||
"""Date/time configuration form."""
|
||||
time_zone = forms.ChoiceField(
|
||||
label=_('Time Zone'),
|
||||
help_text=_('Set your time zone to get accurate timestamps. \
|
||||
This will set the systemwide time zone.'))
|
||||
This will set the system-wide time zone.'))
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
"""Initialize the date/time form."""
|
||||
@ -48,23 +51,14 @@ This will set the systemwide time zone.'))
|
||||
|
||||
@staticmethod
|
||||
def get_time_zones():
|
||||
"""Return list of available time zones"""
|
||||
time_zones = []
|
||||
for line in open('/usr/share/zoneinfo/zone.tab'):
|
||||
if re.match(r'^(#|\s*$)', line):
|
||||
continue
|
||||
"""Return the list time zones."""
|
||||
command = ['timedatectl', 'list-timezones']
|
||||
try:
|
||||
process = subprocess.run(command, stdout=subprocess.PIPE,
|
||||
check=True)
|
||||
except subprocess.CalledProcessError as exception:
|
||||
logger.exception('Error getting time zones: %s', exception)
|
||||
return []
|
||||
|
||||
try:
|
||||
time_zones.append(line.split()[2])
|
||||
except IndexError:
|
||||
pass
|
||||
|
||||
time_zones.sort()
|
||||
|
||||
additional_time_zones = [
|
||||
path[len('/usr/share/zoneinfo/'):]
|
||||
for path in glob.glob('/usr/share/zoneinfo/Etc/*')]
|
||||
|
||||
# Add additional time zones at the top to make them more
|
||||
# noticeable because people won't look for them
|
||||
return additional_time_zones + time_zones
|
||||
output = process.stdout.decode()
|
||||
return output.splitlines()
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user