I am trying to set system date (not time) using following code. I want to set the current time to the new date. Following is a sample code and I found the time is not correct after change.
day = 20
month = 3
year = 2010
timetuple = time.localtime()
print timetuple
print timetuple[3], timetuple[4], timetuple[5]
win32api.SetSystemTime(year, month, timetuple[6]+1,
day, timetuple[3], timetuple[4], timetuple[5], 1)
You are setting the system time from the
localtimetimestamp. The latter is adjusted for the local timezone, whileSetSystemTimerequires you to use the UTC timezone.Use
time.gmtime()instead:You then also avoid having to deal with whether or not you are in summer time (DST) now, vs. March when you would be in winter time.
Alternatively you can use a
datetime.datetime.utcnow()call and get the millisecond parameter as a bonus:Note that I left the weekday item set to 0 in both examples; it is ignored when calling
SetSystemTime. If it was not ignored, then your code example had the value wrong; the Python value ranges from 0 to 6 for Monday through to Sunday, while the Win32 API wants 1 through to 7 for Sunday through to Saturday. You’d have to add 2 and use modulo 7: