简体   繁体   English

如何在Windows中使用python更改系统时区?

[英]how to change system timezone using python in windows?

How to change system timezone using python's win32api? 如何使用python的win32api更改系统时区? I've tried using SetTimeZoneInformation. 我尝试使用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? SetTimeZoneInformation的参数是什么? Documentation says that it needs privilege for SE_TIME_ZONE_NAME. 文档说它需要SE_TIME_ZONE_NAME的特权。 How to set that in python? 如何在python中设置它? Using WMI? 使用WMI吗?

thanks, 谢谢,

Based on Tim Golden's win32api docs , the method takes a tuple of the following form: 基于Tim Golden的win32api文档 ,该方法采用以下形式的元组:

[0] int : Bias [0] int:偏差

[1] string : StandardName [1]字符串:StandardName

[2] SYSTEMTIME tuple : StandardDate [2] SYSTEMTIME元组:StandardDate

[3] int : StandardBias [3] int:StandardBias

[4] string : DaylightName [4]字符串:DaylightName

[5] SYSTEMTIME tuple : DaylightDate [5] SYSTEMTIME元组:DaylightDate

[6] int : DaylightBias [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. 更重要的是,尝试使用win32api.GetTimeZoneInformationdocs )来查看元组的外观,以便win32api.SetTimeZoneInformation不会抱怨。

Edit: Getting the necessary privilege 编辑:获得必要的特权

You need the SE_TIME_ZONE_NAME privilege (see here ). 您需要SE_TIME_ZONE_NAME特权(请参阅此处 )。 There's a handy implementation of changing privileges, AdjustPrivilege over here . 有一个方便的实现改变权限, AdjustPrivilege 在这里

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))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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