[英]how to change system timezone using python in windows?
如何使用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文檔 ,該方法采用以下形式的元組:
[0] int:偏差
[1]字符串:StandardName
[2] SYSTEMTIME元組:StandardDate
[3] int:StandardBias
[4]字符串:DaylightName
[5] SYSTEMTIME元組:DaylightDate
[6] int:DaylightBias
更重要的是,嘗試使用win32api.GetTimeZoneInformation
( docs )來查看元組的外觀,以便win32api.SetTimeZoneInformation
不會抱怨。
編輯:獲得必要的特權
您需要SE_TIME_ZONE_NAME
特權(請參閱此處 )。 有一個方便的實現改變權限, AdjustPrivilege
在這里 。
放在一起:
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.