【发布时间】:2012-12-08 11:12:47
【问题描述】:
如何使用 VBScript 确定我的时区偏移量?
Windows 操作系统提供TZ 环境变量。对于东部标准时间(纽约),其值为EST5EDT。但是,我正在寻找与 UTC 的有符号整数偏移量。 (这是东部标准时间的 -5。)
【问题讨论】:
标签: vbscript timezone wmi utc timezone-offset
如何使用 VBScript 确定我的时区偏移量?
Windows 操作系统提供TZ 环境变量。对于东部标准时间(纽约),其值为EST5EDT。但是,我正在寻找与 UTC 的有符号整数偏移量。 (这是东部标准时间的 -5。)
【问题讨论】:
标签: vbscript timezone wmi utc timezone-offset
这是一个修改后的函数,似乎考虑了夏令时。 (灵感来自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
【讨论】: