Microsoft Identity Integration Server 2003 Developer Reference

Example: Converting Local Time to UTC Time

WMI functions that use time information, such as the ClearPasswordHistory or ClearRuns require that you use UTC time.

The following VBScript converts local time to UTC time.

Option Explicit

On Error Resume Next

Set objArgs = WScript.Arguments
Dim convertYear,convertMonth,convertDay,convertHour,convertMin,convertSec,convertMil,AMPM,ToUTC,TimeConst

'Fill In Default settings
ToUTC		 = True
TimeConst	 = "UTC"
AMPM			= "am"
convertMil	= "000"

LoadArgs 
ValidateInput
convertHour = FormatHour(convertHour)

Wscript.Echo ConvertToUTC(convertYear & convertMonth & convertDay & convertHour & convertMin & convertSec) & " " & TimeConst

Function ConvertToUTC(strTime)
   On Error Resume Next
   'Parses a date time string passing in the following format
   '20040806065423 - Year Month Day Hour Minutes Seconds

   Dim strDateTime, UTCDate, Computer
   Dim YerArg : YerArg = left(strTime,4   )
   Dim MonArg : MonArg = mid (strTime,5,2 )
   Dim DayArg : DayArg = mid (strTime,7,2 )
   Dim HrsArg : HrsArg = mid (strTime,9,2 ) 
   Dim MinArg : MinArg = mid (strTime,11,2)
   Dim SecArg : SecArg = mid (strTime,13,2)

   'Use Win32_ComputerSystem CurrentTimeZone property, because it automatically adjusts the
   'Time Zone bias for daylight saving time Win32_Time Zone Bias property does not.

   'Get the machines current time zone offset
   For Each LocalTimeZone in GetObject("winmgmts:").InstancesOf("Win32_ComputerSystem")
		TimeZoneOffset = LocalTimeZone.CurrentTimeZone
   Next

   'Wscript.Echo "The current time difference is " & TimeZoneOffset & " minutes (" & TimeZoneOffset/60 & " hrs)" 

   strDateTime = MonArg & "-" & DayArg & "-" & YerArg & " " & HrsArg & ":" & MinArg & ":" & SecArg

   if TimeZoneOffset < 0 Then
		if ToUTC then
				UTCDate = DateAdd("n",  ABS(TimeZoneOffset), strDateTime)
		else
				UTCDate = DateAdd("n", -ABS(TimeZoneOffset), strDateTime)
		end if
   else
		if ToUTC then
				UTCDate = DateAdd("n", -ABS(TimeZoneOffset), strDateTime)
		else
				UTCDate = DateAdd("n", ABS(TimeZoneOffset), strDateTime)
		end if
   end if

   If Err.Number <> 0 Then 
		Wscript.Echo "ConvertToUTC::Invalid Argument"
		Usage
   End If

   ConvertToUTC = Trim(Year(UTCDate) & "-" & FormatArgs(Month(UTCDate)) & "-" & FormatArgs(Day(UTCDate)) & " " & FormatArgs(Hour(UTCDate)) & ":" & FormatArgs(Minute(UTCDate)) & ":" & FormatArgs(Second(UTCDate))) & "." & convertMil

End Function

Sub Usage
   Wscript.Echo vbCrLf & "cscript ConvertUTC.vbs [Year Month Day] [Hour Min Sec [MilSec]] [AM/PM] [U/L]"
   Wscript.Echo vbCrLf & "Year	Year"
   Wscript.Echo "Month	 Month"
   Wscript.Echo "Day	 Day"
   Wscript.Echo "Hour	Hour"
   Wscript.Echo "Min	 Minutes"
   Wscript.Echo "Sec	 Seconds"
   Wscript.Echo "MilSec	Milliseconds [000 default]"
   Wscript.Echo "AM/PM	 For non 24 hour time specify AM or PM [AM default]"
   Wscript.Echo "U/L	 Specify the direction for the conversion:"
   Wscript.Echo "		 U = Local time to UTC time [default]"
   Wscript.Echo "		 L = UTC time to local time" & vbcrlf
   Wscript.Echo vbCrLf & "Usage Examples:"
   Wscript.Echo "1) cscript ConvertUTC.vbs U or cscript ConvertUTC.vbs"
   Wscript.Echo "2) cscript ConvertUTC.vbs L"
   Wscript.Echo "3) cscript ConvertUTC.vbs 2004 12 31 5 55 55 p"
   Wscript.Echo "3) cscript ConvertUTC.vbs 2004 12 31 17 55 55 123 u"
   Wscript.Echo "4) cscript ConvertUTC.vbs 17 55 55 l"
   Wscript.Echo "5) cscript ConvertUTC.vbs 17 55 55 u"
   Wscript.Echo "6) cscript ConvertUTC.vbs 5 55 55 123 p u"
   Wscript.Echo vbCrLf & "Results:" & vbcrlf & "1) Converts Local now to UTC now  [2004-08-11 23:22:19.000 UTC  ]"
   Wscript.Echo "2) Converts UTC now to Local now  [2004-08-11 09:22:19.000 Local]"
   Wscript.Echo "3) 2005-01-01 00:55:55.000 UTC"
   Wscript.Echo "4) 2004-08-11 10:55:55.000 Local"
   Wscript.Echo "5) 2004-08-12 00:55:55.000 UTC"
   Wscript.Echo "6) 2004-08-12 00:55:55.123 UTC"
   Wscript.Quit(1)
End Sub

Sub LoadArgs
   On Error Resume Next 
   if objArgs.Count > 0 Then
		If IsNumeric(objArgs(0)) Then
		 if Len(objArgs(0)) = 4 Then
				convertYear  = CheckYear (objArgs(0))
				convertMonth = FormatArgs(objArgs(1))
				convertDay   = FormatArgs(objArgs(2))
				convertHour  = FormatArgs(objArgs(3))
				convertMin   = FormatArgs(objArgs(4))
				convertSec   = FormatArgs(objArgs(5))

				If objArgs.Count >= 7 then 
				 if IsNumeric(objArgs(6)) Then
						convertMil = FormatMil (objArgs(6))
				 Else
						SetValues(objArgs(6))
				 end if
				End if
	
				If objArgs.Count >= 8 then SetValues(objArgs(7))
				If objArgs.Count = 9 then SetValues(objArgs(8))
	
				If Err.Number <> 0 Then 
				 WScript.Echo "LoadArgs::Invalid arguments"
				 Usage
				End If	
			
		 Else
				'Did not pass in Year Month Day arguments just time
				LoadDefaults
				convertHour  = FormatArgs(objArgs(0))
				convertMin   = FormatArgs(objArgs(1))
				convertSec   = FormatArgs(objArgs(2))
				If objArgs.Count >= 4 then 
				 If IsNumeric(objArgs(3)) Then
						convertMil = FormatMil (objArgs(3))
				 Else
						SetValues(objArgs(3))
				 End If
				End if
				If objArgs.Count >= 5 then SetValues(objArgs(4))
				If objArgs.Count = 6 then SetValues(objArgs(5))
		 End If
		Else
		 'First Argument IS NOT numerical value
		 LoadDefaults
		 SetValues(objArgs(0))
		End If
   Else
		'Nothing passed as args
		LoadDefaults
   End If
End Sub

Function FormatHour(ArgVal)
   Select Case AMPM
		Case "pm"
		 If convertHour < 12 Then ArgVal = convertHour + 12
		Case "am"
		 If convertHour = 12 Then ArgVal = "00"
   End Select

   FormatHour = ArgVal
End Function

Sub SetValues(Arg)
   Dim SetVal : SetVal = Flase
   
   Select Case Left(lcase(arg),1)
		Case "u"
		 TimeConst = "UTC"
		 ToUTC = True
		 SetVal = True
		Case "l"
		 TimeConst = "Local"
		 ToUTC = False
		 SetVal = True
		Case "a"
		 AMPM = "am"
		 SetVal = True
		Case "p"
		 AMPM = "pm"
		 SetVal = True
   End Select

   If IsNumeric(Left(arg,1)) or Not SetVal Then
		WScript.Echo "SetValues::Invalid arguments"
		Usage
   End If

End Sub

Sub LoadDefaults
   CurTime = Now
   convertYear  = Year(CurTime)
   convertMonth = FormatArgs(Month(CurTime))
   convertDay   = FormatArgs(Day(CurTime))
   convertHour  = FormatArgs(Hour(CurTime))
   convertMin   = FormatArgs(Minute(CurTime))
   convertSec   = FormatArgs(Second(CurTime))
   ToUTC		= True

End Sub

Sub ValidateInput
   Dim ValErr
   ValErr = 0
   If convertHour  > 23 Then ValErr = 1
   If convertDay   > 31 Then ValErr = 1
   If convertMonth > 12 Then ValErr = 1
   If convertMin   > 59 Then ValErr = 1
   If convertSec   > 59 Then ValErr = 1
   If ValErr > 0 Then
		WScript.Echo "ValidateInput::Invalid arguments"
		Usage
   End if
End Sub

Function FormatArgs(str)
   if Len(str) < 2 Then str = "0" & str
   FormatArgs = str
   If not IsNumeric(str) Then 
		WScript.Echo "FormatArgs::Invalid arguments"
		Usage
   End If
End Function

Function CheckYear(str)
	
   CheckYear = str

   If not IsNumeric(str) or Len(str) <> 4 Then 
		WScript.Echo "CheckYear::Invalid arguments"
		Usage
   End If

End Function

Function FormatMil(str)
   tStr = str
   if Len(str) < 3 Then 
		for i = Len(str) + 1 to 3
			tStr = tStr+ "0"
		Next
   end if

   if Len(tStr) > 3 then tStr = Left(tStr,3)
   FormatMil = tStr
   If not IsNumeric(tStr) Then 
		WScript.Echo "FormatMil::Invalid arguments"
		Usage
   End If
End Function

Sub ErrorHandler (ErrorMessage)
  WScript.Echo ErrorMessage
  WScript.Quit(1)
End Sub