mirror of
https://github.com/freedombox/FreedomBox.git
synced 2026-08-26 12:46:08 +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 import forms
|
||||||
from django.utils.translation import ugettext_lazy as _
|
from django.utils.translation import ugettext_lazy as _
|
||||||
import glob
|
import logging
|
||||||
import re
|
import subprocess
|
||||||
|
|
||||||
from plinth.forms import ServiceForm
|
from plinth.forms import ServiceForm
|
||||||
|
|
||||||
|
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
class DateTimeForm(ServiceForm):
|
class DateTimeForm(ServiceForm):
|
||||||
"""Date/time configuration form."""
|
"""Date/time configuration form."""
|
||||||
time_zone = forms.ChoiceField(
|
time_zone = forms.ChoiceField(
|
||||||
label=_('Time Zone'),
|
label=_('Time Zone'),
|
||||||
help_text=_('Set your time zone to get accurate timestamps. \
|
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):
|
def __init__(self, *args, **kwargs):
|
||||||
"""Initialize the date/time form."""
|
"""Initialize the date/time form."""
|
||||||
@ -48,23 +51,14 @@ This will set the systemwide time zone.'))
|
|||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def get_time_zones():
|
def get_time_zones():
|
||||||
"""Return list of available time zones"""
|
"""Return the list time zones."""
|
||||||
time_zones = []
|
command = ['timedatectl', 'list-timezones']
|
||||||
for line in open('/usr/share/zoneinfo/zone.tab'):
|
try:
|
||||||
if re.match(r'^(#|\s*$)', line):
|
process = subprocess.run(command, stdout=subprocess.PIPE,
|
||||||
continue
|
check=True)
|
||||||
|
except subprocess.CalledProcessError as exception:
|
||||||
|
logger.exception('Error getting time zones: %s', exception)
|
||||||
|
return []
|
||||||
|
|
||||||
try:
|
output = process.stdout.decode()
|
||||||
time_zones.append(line.split()[2])
|
return output.splitlines()
|
||||||
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
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user