Wincc 时间转换脚本LocalTimeToUTC
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
Function LocalTimeToUTC(localTime)
' Dim localTime
Dim strComputer, objWMIService, colTimeZone, objTimeZone, colComputerSystem, objComputerSystem
Dim UTCLocalDIFF
' Set UTCLocalDIFF = ScreenItems("EAFeld1")
strComputer = "."
' localTime = Now
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colComputerSystem = objWMIService.ExecQuery("Select * from Win32_ComputerSystem")
For Each objComputerSystem In colComputerSystem
If objComputerSystem.DaylightInEffect Then
Set colTimeZone = objWMIService.ExecQuery("Select * from Win32_TimeZone")
For Each objTimeZone In colTimeZone
UTCLocalDIFF = objTimeZone.DaylightBias - objTimeZone.Bias ' in Minutes
' HMIRuntime.Trace vbCrLf & "Difference between LocalTime and UTC: " & UTCLocalDIFF & vbCrLf
Next
Else
Set colTimeZone = objWMIService.ExecQuery("Select * from Win32_TimeZone")
For Each objTimeZone In colTimeZone
UTCLocalDIFF = -1 * objTimeZone.Bias 'in Minutes
' HMIRuntime.Trace vbCrLf & "Difference between LocalTime and UTC: " & UTCLocalDIFF & vbCrLf
Next
End If
Next
'Dim UTCLocal
'Set UTCLocal = ScreenItems("EAFeld4")
Dim UTCTime
UTCTime = DateAdd("n",UTCLocalDIFF,localTime)
'UTCLocal.OutputValue = UTCTime
'HMIRuntime.Trace "UTC Zeit: " & UTCTime & vbCrLf
'Dim LocalTimeEA
'Set LocalTimeEA = ScreenItems("EAFeld5")
'LocalTimeEA.OutputValue = Now
'HMIRuntime.Trace "Locale Zeit: " & Now & vbCrLf
Dim SQLTimeFormat,SQLTimeFormatEA
SQLTimeFormat = Year(UTCTime) & "-" & Right ("00" & Month(UTCTime),2)
SQLTimeFormat = SQLTimeFormat & "-" & Right ("00" & Day(UTCTime),2)
SQLTimeFormat = SQLTimeFormat & " " & Right ("00" & Hour(UTCTime),2)
SQLTimeFormat = SQLTimeFormat & ":" & Right ("00" & Minute(UTCTime),2)
SQLTimeFormat = SQLTimeFormat & ":" & Right ("00" & Second(UTCTime),2)
'Set SQLTimeFormatEA = ScreenItems("EAFeld6")
'SQLTimeFormatEA.OutputValue = SQLTimeFormat
'HMIRuntime.Trace "UTC Zeit im SQL Format: " & SQLTimeFormat & vbCrLf
'Set UTCLocal = Nothing
Set objWMIService = Nothing
Set colComputerSystem = Nothing
Set UTCLocalDIFF = Nothing
LocalTimeToUTC=SQLTimeFormat
'HMIRuntime.Trace "UTC out SQL Format: " & LocalTimeToUTC & vbCrLf
'Set SQLTimeFormatEA = Nothing
'Set LocalTimeEA = Nothing
End Function
Function UTCToLocalTime(localTime)
' Dim localTime
Dim strComputer, objWMIService, colTimeZone, objTimeZone, colComputerSystem, objComputerSystem
Dim UTCLocalDIFF
' Set UTCLocalDIFF = ScreenItems("EAFeld1")
strComputer = "."
' localTime = Now
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colComputerSystem = objWMIService.ExecQuery("Sel
ect * from Win32_ComputerSystem")
For Each objComputerSystem In colComputerSystem
If objComputerSystem.DaylightInEffect Then
Set colTimeZone = objWMIService.ExecQuery("Select * from Win32_TimeZone")
For Each objTimeZone In colTimeZone
UTCLocalDIFF = objTimeZone.DaylightBias - objTimeZone.Bias ' in Minutes
' HMIRuntime.Trace "Difference between LocalTime and UTC: " & UTCLocalDIFF & vbCrLf
Next
Else
Set colTimeZone = objWMIService.ExecQuery("Select * from Win32_TimeZone")
For Each objTimeZone In colTimeZone
UTCLocalDIFF = -1 * objTimeZone.Bias 'in Minutes
' HMIRuntime.Trace "Difference between LocalTime and UTC: " & UTCLocalDIFF & vbCrLf
Next
End If
Next
'Dim UTCLocal
'Set UTCLocal = ScreenItems("EAFeld4")
Dim UTCTime
UTCTime = DateAdd("n",0-UTCLocalDIFF,localTime)
'UTCLocal.OutputValue = UTCTime
'HMIRuntime.Trace "UTC Zeit: " & UTCTime & vbCrLf
'Dim LocalTimeEA
'Set LocalTimeEA = ScreenItems("EAFeld5")
'LocalTimeEA.OutputValue = Now
'HMIRuntime.Trace "Locale Zeit: " & Now & vbCrLf
Dim SQLTimeFormat,SQLTimeFormatEA
SQLTimeFormat = Year(UTCTime) & "-" & Right ("00" & Month(UTCTime),2)
SQLTimeFormat = SQLTimeFormat & "-" & Right ("00" & Day(UTCTime),2)
SQLTimeFormat = SQLTimeFormat & " " & Right ("00" & Hour(UTCTime),2)
SQLTimeFormat = SQLTimeFormat & ":" & Right ("00" & Minute(UTCTime),2)
SQLTimeFormat = SQLTimeFormat & ":" & Right ("00" & Second(UTCTime),2)
'Set SQLTimeFormatEA = ScreenItems("EAFeld6")
'SQLTimeFormatEA.OutputValue = SQLTimeFormat
'HMIRuntime.Trace "UTC Zeit im SQL Format: " & SQLTimeFormat & vbCrLf
'Set UTCLocal = Nothing
Set objWMIService = Nothing
Set colComputerSystem = Nothing
Set UTCLocalDIFF = Nothing
'HMIRuntime.Trace "UTC out SQL Format: " & SQLTimeFormat & vbCrLf
UTCToLocalTime=SQLTimeFormat
'Set SQLTimeFormatEA = Nothing
'Set LocalTimeEA = Nothing
End Function