简体   繁体   中英

how to change system timezone using python in windows?

How to change system timezone using python's win32api? I've tried using SetTimeZoneInformation.

win32api.SetTimeZoneInformation(year,
                            month,
                            dayofweek,
                            hour,
                            minute,
                            second,
                            milliseconds)

this gives me an error in milliseconds parameter.

TypeError: Objects of type 'int' can not be converted to Unicode.

What is the parameter for SetTimeZoneInformation? Documentation says that it needs privilege for SE_TIME_ZONE_NAME. How to set that in python? Using WMI?

thanks,

Based on Tim Golden's win32api docs , the method takes a tuple of the following form:

[0] int : Bias

[1] string : StandardName

[2] SYSTEMTIME tuple : StandardDate

[3] int : StandardBias

[4] string : DaylightName

[5] SYSTEMTIME tuple : DaylightDate

[6] int : DaylightBias

More to the point, try win32api.GetTimeZoneInformation ( docs ) to see what the tuple ought to look like so that win32api.SetTimeZoneInformation won't complain.

Edit: Getting the necessary privilege

You need the SE_TIME_ZONE_NAME privilege (see here ). There's a handy implementation of changing privileges, AdjustPrivilege over here .

Putting it all together:

import ntsecuritycon, win32security, win32api

def AdjustPrivilege( priv ):
    flags = ntsecuritycon.TOKEN_ADJUST_PRIVILEGES | ntsecuritycon.TOKEN_QUERY
    htoken =  win32security.OpenProcessToken(win32api.GetCurrentProcess(), flags)
    id = win32security.LookupPrivilegeValue(None, priv)
    newPrivileges = [(id, ntsecuritycon.SE_PRIVILEGE_ENABLED)]
    win32security.AdjustTokenPrivileges(htoken, 0, newPrivileges)

# Enable the privilege
AdjustPrivilege(win32security.SE_TIME_ZONE_NAME)

# Set the timezone
win32api.SetTimeZoneInformation((-600,u'Eastern Standard Time',(2000,4,1,3,0,0,0,0),0,u'Eastern Daylight Time',(2000,10,1,2,0,0,0,0),-60))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM