[PATCH 2/2] Set system date and time with our own function

Vratislav Podzimek vpodzime at redhat.com
Tue Jul 30 09:44:11 UTC 2013


Now that we have a function to set system time, we could wrap it with one more
and use it instead of running 'date -s' which could be problematic and prints
out the time and date to tty1.

Also add logging to the set_system_time function, so that we can see why the
timestamps suddenly changed in the logs.

Signed-off-by: Vratislav Podzimek <vpodzime at redhat.com>
---
 pyanaconda/isys/__init__.py                | 33 ++++++++++++++++++++++++++++--
 pyanaconda/ntp.py                          |  1 -
 pyanaconda/ui/gui/spokes/datetime_spoke.py |  5 ++---
 3 files changed, 33 insertions(+), 6 deletions(-)

diff --git a/pyanaconda/isys/__init__.py b/pyanaconda/isys/__init__.py
index a320bf9..51976db 100755
--- a/pyanaconda/isys/__init__.py
+++ b/pyanaconda/isys/__init__.py
@@ -1,7 +1,7 @@
 #
 # isys.py - installer utility functions and glue for C module
 #
-# Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007  Red Hat, Inc.
+# Copyright (C) 2001-2013  Red Hat, Inc.
 # All rights reserved.
 #
 # This program is free software; you can redistribute it and/or modify
@@ -41,6 +41,8 @@ import blivet.arch
 import re
 import struct
 import dbus
+import time
+import datetime
 
 import logging
 log = logging.getLogger("anaconda")
@@ -120,7 +122,34 @@ def set_system_time(secs):
 
     """
 
-    return  _isys.set_system_time(secs)
+    _isys.set_system_time(secs)
+    log.info("System time set to %s", time.ctime(secs))
+
+def set_system_date_time(year=None, month=None, day=None, hour=None, minute=None,
+                         second=None, utc=False):
+    """
+    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
+    instead (i.e. the value is not changed by this function).
+
+    :type year, month, ..., second: int
+    :param utc: wheter the other parameters specify UTC or local time
+    :type utc: bool
+
+    """
+
+    # get the right values
+    utc = 0 if utc else 1
+    year = year or datetime.datetime.now().year
+    month = month or datetime.datetime.now().month
+    day = day or datetime.datetime.now().day
+    hour = hour or datetime.datetime.now().hour
+    minute = minute or datetime.datetime.now().minute
+    second = second or datetime.datetime.now().second
+
+    # struct fields -> year, month, day, hour, minute, second, week_day, year_day, utc
+    time_struct = time.struct_time((year, month, day, hour, minute, second, 0, 0, utc))
+    set_system_time(int(time.mktime(time_struct)))
 
 auditDaemon = _isys.auditdaemon
 
diff --git a/pyanaconda/ntp.py b/pyanaconda/ntp.py
index ca0bbc8..10b64dc 100644
--- a/pyanaconda/ntp.py
+++ b/pyanaconda/ntp.py
@@ -207,4 +207,3 @@ def one_time_sync_async(server, callback=None):
 
     threadMgr.add(AnacondaThread(name=thread_name, target=one_time_sync,
                                  args=(server, callback)))
-
diff --git a/pyanaconda/ui/gui/spokes/datetime_spoke.py b/pyanaconda/ui/gui/spokes/datetime_spoke.py
index 979ad61..73b2e28 100644
--- a/pyanaconda/ui/gui/spokes/datetime_spoke.py
+++ b/pyanaconda/ui/gui/spokes/datetime_spoke.py
@@ -36,6 +36,7 @@ from pyanaconda.i18n import _, N_
 from pyanaconda import timezone
 from pyanaconda.timezone import NTP_SERVICE
 from pyanaconda import iutil
+from pyanaconda import isys
 from pyanaconda import network
 from pyanaconda import nm
 from pyanaconda import ntp
@@ -619,9 +620,7 @@ class DatetimeSpoke(FirstbootSpokeMixIn, NormalSpoke):
         #day may be None if there is no such in the selected year and month
         if day:
             day = int(day)
-            seconds = datetime.datetime.now().second
-            os.system("date -s '%0.2d/%0.2d/%0.4d %0.2d:%0.2d:%0.2d'" %
-                                (month, day, year, hours, minutes, seconds))
+            isys.set_system_date_time(year, month, day, hours, minutes)
 
         #start the timer only when the spoke is shown
         if self._update_datetime_timer_id is not None:
-- 
1.7.11.7



More information about the anaconda-patches mailing list