2012-12-20 43 views
4

如何確定我的時區偏移量使用VBScript如何使用VBScript確定我的時區偏移量?

Windows操作系統提供了TZ環境變量。對於東部標準時間(紐約),其值爲EST5EDT。但是,我正在查找UTC的有符號整數偏移量。 (這是東部標準時間的-5。)

回答

8

這是一個修改後的函數,它似乎考慮了夏令時。 (由this SO question啓發。)

Function GetTimeZoneOffset() 
    Const sComputer = "." 

    Dim oWmiService : Set oWmiService = _ 
     GetObject("winmgmts:{impersonationLevel=impersonate}!\\" _ 
        & sComputer & "\root\cimv2") 

    Set cItems = oWmiService.ExecQuery("SELECT * FROM Win32_ComputerSystem") 

    For Each oItem In cItems 
     GetTimeZoneOffset = oItem.CurrentTimeZone/60 
     Exit For 
    Next 
End Function 

[原創功能,不考慮夏令時。]

這裏是我的回答我的問題(original source)。

對於東部標準時間(紐約),這個VBScript函數將返回-5

Function GetTimeZoneOffset() 
    Const sComputer = "." 

    Dim oWmiService : Set oWmiService = _ 
     GetObject("winmgmts:{impersonationLevel=impersonate}!\\" _ 
        & sComputer & "\root\cimv2") 

    Dim cTimeZone : Set cTimeZone = _ 
     oWmiService.ExecQuery("Select * from Win32_TimeZone") 

    Dim oTimeZone 
    For Each oTimeZone in cTimeZone 
     GetTimeZoneOffset = oTimeZone.Bias/60 
     Exit For 
    Next 
End Function 
+0

這似乎並不佔DST? –

+0

@Alex - 請讓我知道我的修改函數現在是否在您的系統上解釋夏令時。 – DavidRR

+0

對於任何搜索的人來說,這種方法實際上允許您通過WMI準確查詢基於時間的事件查看器事件。然後,您可以進行計算以將事件的默認寫入爲UTC。 – Beems