2013-11-28 66 views
1

如何使用python的win32api更改系統時區?我試過使用SetTimeZoneInformation。如何在Windows中使用python更改系統時區?

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?

感謝,

回答

1

基於添金的win32api docs,所述方法採用以下形式的元組:

[0] INT:偏置

[1]字符串:StandardName

[2] SYSTEMTIME元組:StandardDate

[3] int:StandardBias

[4]字符串:DaylightName

[5] SYSTEMTIME元組:DaylightDate

[6] INT:DaylightBias

更關鍵的是,嘗試win32api.GetTimeZoneInformationdocs)看什麼元組應當看起來像這樣win32api.SetTimeZoneInformation不會抱怨。

編輯:獲得必要的特權

您需要SE_TIME_ZONE_NAME特權(見here)。有一個方便實施更改權限,AdjustPrivilegeover 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)) 
+0

@unice讓我知道,如果它的工作對你 – pandita

+0

很抱歉這麼晚纔回復。謝謝@pandita,它的工作原理。我可以用第二個參數修改當前的日期和時間嗎?因爲當我使用SetSystemTime更改日期/時間時,它會自動添加+8小時或根據時區。 – unice

+0

@pandita如果我想將時區更改爲「歐洲/倫敦」,我該怎麼做?只需更換「東部標準時間」將不起作用。任何解釋?或者,那個論點的所有有效值是什麼? – swdev

相關問題