[PATCH 19/22] Do not set $TZ
David Shea
dshea at redhat.com
Wed Jun 3 16:03:43 UTC 2015
For datetime.now(), use explicit tzinfo objects to set the time zone.
Avoid uses of mktime by calculating datetime.timedelta objects from a
given datetime to timestamp 0. Instead of logging a time with ctime,
print out the UTC version of a timestamp and slap "UTC" on the end.
(cherry picked from commit f5e6f66d23d2f9fd88bef0e06c245d58b29d768b)
Related: rhbz#1188287
---
pyanaconda/isys/__init__.py | 17 +++++++++++------
pyanaconda/timezone.py | 9 +++++++++
pyanaconda/ui/gui/spokes/datetime_spoke.py | 10 +++++-----
3 files changed, 25 insertions(+), 11 deletions(-)
diff --git a/pyanaconda/isys/__init__.py b/pyanaconda/isys/__init__.py
index e6e3d02..5cb0a72 100644
--- a/pyanaconda/isys/__init__.py
+++ b/pyanaconda/isys/__init__.py
@@ -41,6 +41,7 @@ import struct
import dbus
import time
import datetime
+import pytz
import logging
log = logging.getLogger("anaconda")
@@ -117,10 +118,10 @@ def set_system_time(secs):
"""
_isys.set_system_time(secs)
- log.info("System time set to %s", time.ctime(secs))
+ log.info("System time set to %s UTC", time.asctime(time.gmtime(secs)))
def set_system_date_time(year=None, month=None, day=None, hour=None, minute=None,
- second=None):
+ second=None, tz=None):
"""
Set system date and time given by the parameters as numbers. If some
parameter is missing or None, the current system date/time field is used
@@ -131,7 +132,7 @@ def set_system_date_time(year=None, month=None, day=None, hour=None, minute=None
"""
# get the right values
- now = datetime.datetime.now()
+ now = datetime.datetime.now(tz)
year = year if year is not None else now.year
month = month if month is not None else now.month
day = day if day is not None else now.day
@@ -139,9 +140,13 @@ def set_system_date_time(year=None, month=None, day=None, hour=None, minute=None
minute = minute if minute is not None else now.minute
second = second if second is not None else now.second
- # struct fields -> year, month, day, hour, minute, second, week_day, year_day, dst
- time_struct = time.struct_time((year, month, day, hour, minute, second, 0, 0, -1))
- set_system_time(int(time.mktime(time_struct)))
+ set_date = datetime.datetime(year, month, day, hour, minute, second, tzinfo=tz)
+
+ # Calculate the number of seconds between this time and timestamp 0
+ epoch = datetime.datetime.fromtimestamp(0, pytz.UTC)
+ timestamp = (set_date - epoch).total_seconds()
+
+ set_system_time(timestamp)
def total_memory():
"""Returns total system memory in kB (given to us by /proc/meminfo)"""
diff --git a/pyanaconda/timezone.py b/pyanaconda/timezone.py
index 9cc2c53..956c33e 100644
--- a/pyanaconda/timezone.py
+++ b/pyanaconda/timezone.py
@@ -215,3 +215,12 @@ def is_valid_timezone(timezone):
return timezone in pytz.common_timezones + etc_zones
+def get_timezone(timezone):
+ """
+ Return a tzinfo object for a given timezone name.
+
+ :param str timezone: the timezone name
+ :rtype: datetime.tzinfo
+ """
+
+ return pytz.timezone(timezone)
diff --git a/pyanaconda/ui/gui/spokes/datetime_spoke.py b/pyanaconda/ui/gui/spokes/datetime_spoke.py
index e892a5f..c7b866b 100644
--- a/pyanaconda/ui/gui/spokes/datetime_spoke.py
+++ b/pyanaconda/ui/gui/spokes/datetime_spoke.py
@@ -34,7 +34,7 @@ from pyanaconda.ui.gui.helpers import GUIDialogInputCheckHandler
from pyanaconda.ui.helpers import InputCheck
from pyanaconda.i18n import _, CN_
-from pyanaconda.timezone import NTP_SERVICE, get_all_regions_and_timezones, is_valid_timezone
+from pyanaconda.timezone import NTP_SERVICE, get_all_regions_and_timezones, get_timezone, is_valid_timezone
from pyanaconda.localization import get_xlated_timezone, resolve_date_format
from pyanaconda import iutil
from pyanaconda import isys
@@ -46,7 +46,6 @@ from pyanaconda import constants
from pyanaconda.threads import threadMgr, AnacondaThread
import datetime
-import os
import re
import threading
import locale as locale_mod
@@ -404,6 +403,7 @@ class DatetimeSpoke(FirstbootSpokeMixIn, NormalSpoke):
self._update_datetime_timer_id = None
self._start_updating_timer_id = None
+ self._tz = None
def initialize(self):
NormalSpoke.initialize(self)
@@ -712,7 +712,7 @@ class DatetimeSpoke(FirstbootSpokeMixIn, NormalSpoke):
return (hours + correction) % 24
def _update_datetime(self):
- now = datetime.datetime.now()
+ now = datetime.datetime.now(self._tz)
if self._radioButton24h.get_active():
self._hoursLabel.set_text("%0.2d" % now.hour)
else:
@@ -758,7 +758,7 @@ class DatetimeSpoke(FirstbootSpokeMixIn, NormalSpoke):
day = self._get_combo_selection(self._dayCombo)[0]
#day may be None if there is no such in the selected year and month
if day:
- isys.set_system_date_time(year, month, day, hours, minutes)
+ isys.set_system_date_time(year, month, day, hours, minutes, tz=self._tz)
#start the timer only when the spoke is shown
if self._update_datetime_timer_id is not None:
@@ -1012,7 +1012,7 @@ class DatetimeSpoke(FirstbootSpokeMixIn, NormalSpoke):
timezone = location.get_property('zone')
if self._set_timezone(timezone):
# timezone successfully set
- os.environ["TZ"] = timezone
+ self._tz = get_timezone(timezone)
self._update_datetime()
def on_timeformat_changed(self, button24h, *args):
--
2.1.0
More information about the anaconda-patches
mailing list