Reputation: 3818
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)
Upvotes: 2
Views: 5015
Reputation: 1121962
You are setting the system time from the localtime
timestamp. The latter is adjusted for the local timezone, while SetSystemTime
requires you to use the UTC timezone.
Use time.gmtime()
instead:
tt = time.gmttime()
win32api.SetSystemTime(year, month, 0, day,
tt.tm_hour, tt.tt_min, tt.tt_sec, 0)
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:
import datetime
tt = datetime.datetime.utcnow().time()
win32api.SetSystemTime(year, month, 0, day,
tt.hour, tt.minute, tt.second, tt.microsecond//1000)
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:
win32_systemtime_weekday = (python_weekday + 2) % 7)
Upvotes: 8