Python module to change system date and time
import sys
import datetime
time_tuple = ( 2012, # Year
9, # Month
6, # Day
0, # Hour
38, # Minute
0, # Second
0, # Millisecond
)
def _win_set_time(time_tuple):
import pywin32
# http://timgolden.me.uk/pywin32-docs/win32api__SetSystemTime_meth.html
# pywin32.SetSystemTime(year, month , dayOfWeek , day , hour , minute , second , millseconds )
dayOfWeek = datetime.datetime(time_tuple).isocalendar()[2]
pywin32.SetSystemTime( time_tuple[:2] + (dayOfWeek,) + time_tuple[2:])
def _linux_set_time(time_tuple):
import ctypes
import ctypes.util
import time
# /usr/include/linux/time.h:
#
# define CLOCK_REALTIME 0
CLOCK_REALTIME = 0
# /usr/include/time.h
#
# struct timespec
# {
# __time_t tv_sec; /* Seconds. */
# long int tv_nsec; /* Nanoseconds. */
# };
class timespec(ctypes.Structure):
_fields_ = [("tv_sec", ctypes.c_long),
("tv_nsec", ctypes.c_long)]
librt = ctypes.CDLL(ctypes.util.find_library("rt"))
ts = timespec()
ts.tv_sec = int( time.mktime( datetime.datetime( *time_tuple[:6]).timetuple() ) )
ts.tv_nsec = time_tuple[6] * 1000000 # Millisecond to nanosecond
# http://linux.die.net/man/3/clock_settime
librt.clock_settime(CLOCK_REALTIME, ctypes.byref(ts))
if sys.platform=='linux2':
_linux_set_time(time_tuple)
elif sys.platform=='win32':
_win_set_time(time_tuple)
I don't have a windows machine so I didn't test it on windows... But you get the idea.
Python module to set local time and date
The win32api module wraps SetSystemTime
to take the SYSTEMTIME
structure fields as 8 separate function parameters. The time should be passed as UTC. wDayOfWeek
is ignored (i.e. pass it as 0).
The win32api module wraps SetLocalTime
more conveniently and idiomatically. It takes a datetime.datetime
instance instead of requiring separate parameters. For example:
import datetime
import win32api
dt = datetime.datetime(2018, 1, 4, 14, 30)
win32api.SetLocalTime(dt)
The Token of the calling process must have SeSystemtimePrivilege, so generally the user needs to be an elevated administrator.
Easy way to get the correct time in python
Try ntplib for python. I've played around with it a bit and it seems pretty stable.
https://pypi.python.org/pypi/ntplib/
How Do You Programmatically Set the Hardware Clock on Linux?
Check out the rtc man-page for details, but if you are logged in as root, something like this:
#include <linux/rtc.h>
#include <sys/ioctl.h>
struct rtc_time {
int tm_sec;
int tm_min;
int tm_hour;
int tm_mday;
int tm_mon;
int tm_year;
int tm_wday; /* unused */
int tm_yday; /* unused */
int tm_isdst;/* unused */
};
int fd;
struct rtc_time rt;
/* set your values here */
fd = open("/dev/rtc", O_RDONLY);
ioctl(fd, RTC_SET_TIME, &rt);
close(fd);
Related Topics
How to Write a 'Try'/'Except' Block That Catches All Exceptions
How to Split a Huge CSV File Based on Content of First Column
Ignore Case in Glob() on Linux
Detect Log File Rotation (While Watching Log File for Modification)
How to Convert .Pcm Files to .Wav Files (Scripting)
What Is the Return Value of Subprocess.Call()
Typeerror: 'Str' Does Not Support the Buffer Interface
Getting a List of All Subdirectories in the Current Directory
When Using Os.Execlp, Why 'Python' Needs 'Python' as Argv[0]
Python Multiprocessing Memory Usage
Python: Interplay Between Lib/Site-Packages/Site.Py and Lib/Site.Py
Better Way to Script Usb Device Mount in Linux
How Would a Python Script Running on Linux Call a Routine in a Python Script Running Under Wine