如果我没有漏掉什么,您需要在远程计算机上获取本地时间和它的时区偏移量,对吗?
这里 我看到了一个基于 WMI Win32_TimeZone Bias
属性的自我回答。但是根据这个 MSDN 的例子:“将本地时间转换为 UTC 时间”,引用:
使用 Win32_ComputerSystem CurrentTimeZone 属性,因为它会自动调整夏令时的时区偏移量,而 Win32_TimeZone Bias 属性则不会。
所以,下一个示例函数获取当前日期时间,我将让您修改它以进行具体的日期时间测量。
' "." mean local computer
WScript.Echo FormatDateTime(UTCDate("."), 0)
Function UTCDate(strComputer)
Dim objWMIService, ColDate, ColCS
On Error Resume Next
Set objWMIService = _
GetObject("winmgmts:{impersonationLevel=impersonate}!\\" _
& strComputer & "\root\cimv2")
Set ColDate = objWMIService.ExecQuery("Select * From Win32_LocalTime")
UTCDate = DateSerial(100, 1, 1)
For Each objDate In ColDate
UTCDate = DateAdd("yyyy", ObjDate.Year - 100, UTCDate)
UTCDate = DateAdd("m", ObjDate.Month - 1, UTCDate)
UTCDate = DateAdd("d", ObjDate.Day - 1, UTCDate)
UTCDate = DateAdd("h", ObjDate.Hour, UTCDate)
UTCDate = DateAdd("n", ObjDate.Minute, UTCDate)
UTCDate = DateAdd("s", ObjDate.Second, UTCDate)
Next
' http://msdn.microsoft.com/en-us/library/windows/desktop/ms696015(v=vs.85).aspx
Set ColCS = objWMIService.ExecQuery("Select * From Win32_ComputerSystem")
Dim TimeZoneOffset, LocalTimeZone
For Each LocalTimeZone In ColCS
TimeZoneOffset = LocalTimeZone.CurrentTimeZone
Next
If TimeZoneOffset < 0 Then
TimeZoneOffset = Abs(TimeZoneOffset)
Else
TimeZoneOffset = -Abs(TimeZoneOffset)
End If
UTCDate = DateAdd("n", TimeZoneOffset, UTCDate)
If Err Then UTCDate = vbNull
Set objWMIService = Nothing
End Function
[编辑] 好像不能信任 MSDN(或者可能只适用于当前时间)。不过,我看到这不是你要找的答案。如果找不到更好的想法,你可以自动化这个 在线计算器。
P.S. 好的,划掉上面的代码,让我们尝试另一个“迭代”。
只是要注意,我编写了一个与我的时区(保加利亚)兼容的代码,在那里太平洋夏令时从三月的最后一个星期日开始,到十月的最后一个星期日结束。这使我可以轻松设置我的 PDT 范围。简而言之,思路是使算法适用于您的地区。其余部分很清楚,希望如此。
With New DateDrill
Debug.WriteLine .UTCDate("2008-6-28")
Debug.WriteLine .UTCDate("2014-1-21")
End With
Class DateDrill
Public Function UTCDate(ByVal dtDate)
If Not IsDate(dtDate) Then Err.Raise 5
dtDate = CDate(dtDate)
Dim ZoneBias: ZoneBias = TimeZoneBias()
If IsPDT(Now) <> IsPDT(dtDate) Then
ZoneBias = ZoneBias - 60
End If
UTCDate = DateAdd("n", ZoneBias, dtDate)
End Function
Private Function IsPDT(ByVal dtDate)
If Not IsDate(dtDate) Then Err.Raise 5
dtDate = CDate(dtDate)
Dim pdtLow, pdtUpr, nDaysBack
pdtLow = DateSerial(Year(dtDate), 3, 31)
pdtUpr = DateSerial(Year(dtDate), 10, 31)
pdtLow = DateAdd("h", 2, pdtLow)
pdtUpr = DateAdd("h", 2, pdtUpr)
nDaysBack = Weekday(pdtLow) - 1
If nDaysBack <> 0 Then
pdtLow = DateAdd("d", -nDaysBack, pdtLow)
End If
nDaysBack = Weekday(pdtUpr) - 1
If nDaysBack <> 0 Then
pdtUpr = DateAdd("d", -nDaysBack, pdtUpr)
End If
IsPDT = (dtDate >= pdtLow And dtDate <= pdtUpr)
End Function
Private Function TimeZoneBias()
Dim LTZone
With GetObject("winmgmts:" & _
"{impersonationLevel=impersonate}!\\.\root\cimv2")
For Each LTZone In .ExecQuery(_
"Select * From Win32_ComputerSystem")
TimeZoneBias = LTZone.CurrentTimeZone
Next
End With
TimeZoneBias = TimeZoneBias * -1
End Function
End Class