datetime: Use timedatectl to get list of time zones

Reviewed-by: Sunil Mohan Adapa <sunil@medhas.org>
This commit is contained in:
Johannes Keyser 2017-04-27 12:48:46 +02:00 committed by Sunil Mohan Adapa
parent 1f5f1e0460
commit 68a0ed48fa
No known key found for this signature in database
GPG Key ID: 43EA1CFF0AA7C5F2

View File

@ -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()