J
J
Is there a better way to do this?
def SkewTime():
'''
Optional function. We can skew time by 1 hour if we'd like to see real sync
changes being enforced
'''
TIME_SKEW=1
logging.info('Time Skewing has been selected. Setting clock ahead 1 hour')
# Let's get our current time
t = TimeCheck()
logging.info('Current time is: %s' % time.asctime(t))
# Now create new time string in the form MMDDhhmmYYYY for the date program
hr = t.tm_hour + TIME_SKEW
date_string = time.strftime('%m%d%H%M%Y',(t.tm_year,
t.tm_mon,
t.tm_mday,
hr,
t.tm_min,
t.tm_sec,
t.tm_wday,
t.tm_yday,
t.tm_isdst))
logging.debug('New date string is: %s' % date_string)
logging.debug('Setting new system time/date')
status = SilentCall('/bin/date %s' % date_string)
logging.info('Pre-sync time is: %s' % time.asctime())
TimeCheck() as referenced above is a simple function that just returns
the time.time_struct object from time.localtime(). I pull time a few
times and it was a little cleaner to put that into a function and just
call the function whenever I needed to.
SilentCall() is a modification of subprocess.call() (which in reality
just calls Popen(*popenargs,**kwargs).wait()) but it defaults to
redirecting stdin and stdout to /dev/null to suppress shell output
from the command being called.
Anyway, what I'm wondering, is, while this works, is there a better
way to do it than using part of the originally returned time_struct
and injecting my own new hour argument (hr).
The goal of this function is to just set the system clock one hour
ahead, so when I call the Linux command 'ntpdate' I can get a real
time change when it syncs the local clock to an NTP server.
This just looks... well, big to me. I tried passing only the things I
really needed to time.strftime(), but apparently, that requires the
full 9-tuple from time_struct, not just individual parts of it.
Like I said, it works well, I just wonder if there is a cleaner way of
setting the local clock to a different time in python without having
to do all this. The reason most of that exists, is because the linux
date command expects to see the new date/time like this:
MMDDhhmmYYYY.ss.
Or am I just looking at this too hard and really did work it out nicely?
Cheers
Jeff
def SkewTime():
'''
Optional function. We can skew time by 1 hour if we'd like to see real sync
changes being enforced
'''
TIME_SKEW=1
logging.info('Time Skewing has been selected. Setting clock ahead 1 hour')
# Let's get our current time
t = TimeCheck()
logging.info('Current time is: %s' % time.asctime(t))
# Now create new time string in the form MMDDhhmmYYYY for the date program
hr = t.tm_hour + TIME_SKEW
date_string = time.strftime('%m%d%H%M%Y',(t.tm_year,
t.tm_mon,
t.tm_mday,
hr,
t.tm_min,
t.tm_sec,
t.tm_wday,
t.tm_yday,
t.tm_isdst))
logging.debug('New date string is: %s' % date_string)
logging.debug('Setting new system time/date')
status = SilentCall('/bin/date %s' % date_string)
logging.info('Pre-sync time is: %s' % time.asctime())
TimeCheck() as referenced above is a simple function that just returns
the time.time_struct object from time.localtime(). I pull time a few
times and it was a little cleaner to put that into a function and just
call the function whenever I needed to.
SilentCall() is a modification of subprocess.call() (which in reality
just calls Popen(*popenargs,**kwargs).wait()) but it defaults to
redirecting stdin and stdout to /dev/null to suppress shell output
from the command being called.
Anyway, what I'm wondering, is, while this works, is there a better
way to do it than using part of the originally returned time_struct
and injecting my own new hour argument (hr).
The goal of this function is to just set the system clock one hour
ahead, so when I call the Linux command 'ntpdate' I can get a real
time change when it syncs the local clock to an NTP server.
This just looks... well, big to me. I tried passing only the things I
really needed to time.strftime(), but apparently, that requires the
full 9-tuple from time_struct, not just individual parts of it.
Like I said, it works well, I just wonder if there is a cleaner way of
setting the local clock to a different time in python without having
to do all this. The reason most of that exists, is because the linux
date command expects to see the new date/time like this:
MMDDhhmmYYYY.ss.
Or am I just looking at this too hard and really did work it out nicely?
Cheers
Jeff