gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 18:42:24 25 4
gpt4 key购买 nike

如何使用python的win32api更改系统时区?我尝试过使用 SetTimeZoneInformation。

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

这给了我一个以毫秒为单位的参数错误。

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

SetTimeZoneInformation 的参数是什么?文档说它需要 SE_TIME_ZONE_NAME 的权限。如何在 python 中设置它?使用 WMI?

谢谢

最佳答案

基于 Tim Golden 的 win32api docs ,该方法采用以下形式的元组:

[0] int:偏差

[1] 字符串:标准名称

[2] SYSTEMTIME 元组:StandardDate

[3] int :标准偏差

[4] 字符串:日光名称

[5] SYSTEMTIME 元组:DaylightDate

[6] int :日光偏差

更重要的是,尝试 win32api.GetTimeZoneInformation ( docs ) 看看元组应该是什么样子,这样 win32api.SetTimeZoneInformation 就不会提示。

编辑:获取必要的权限

您需要 SE_TIME_ZONE_NAME 权限(请参阅 here )。有一个更改权限的便捷实现,AdjustPrivilege over here .

把它们放在一起:

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

关于python - 如何在Windows中使用python更改系统时区?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20256946/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com