Wincc 时间转换脚本LocalTimeToUTC

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

相关文档
最新文档