(view source code of gmt.vbs as plain text)
Option Explicit
Dim dtmDate, dtmGMT
Dim colItems, objItem, objWMIService
Dim strArg, strDate
dtmDate = Now
If WScript.Arguments.Named.Count > 0 Then Syntax
If WScript.Arguments.Unnamed.Count > 0 Then
For Each strArg In WScript.Arguments.Unnamed
strDate = Trim( strDate & " " & strArg )
Next
On Error Resume Next
If IsDate( strDate ) Then
dtmDate = CDate( strDate )
Else
Syntax
End If
On Error Goto 0
End If
Set objWMIService = GetObject( "winmgmts://./root/cimv2" )
Set colItems = objWMIService.ExecQuery( "SELECT * FROM Win32_TimeZone", , 48 )
For Each objItem in colItems
dtmGMT = DateAdd( "n", - objItem.Bias, dtmDate )
If isDST( dtmDate ) Then
dtmGMT = DateAdd( "n", objItem.DaylightBias, dtmGMT )
End If
Next
Set colItems = Nothing
Set objWMIService = Nothing
WScript.Echo dtmGMT & " GMT"
Function isDST( myDate )
' Returns TRUE if the specified date is in daylight Saving Time, or FALSE if not.
' The function ignores the 'ambiguous hour' right after the Standard Time transition.
Dim myDay, myHour, myMinute, myMonth, myYear
Dim intDaylight, intLocDate, intStandard
Dim colItems, objItem, objWMIService
' Parse the specified date/time
myDate = CDate( myDate )
myDay = DatePart( "d", myDate )
myMonth = DatePart( "m", myDate )
myYear = DatePart( "yyyy", myDate )
myHour = DatePart( "h", myDate )
myMinute = DatePart( "n", myDate )
Set objWMIService = GetObject( "winmgmts://./root/cimv2" )
Set colItems = objWMIService.ExecQuery( "SELECT * FROM Win32_TimeZone", , 48 )
For Each objItem in colItems
With objItem
' Convert the Daylight Start date/time, specified date/time and Standard Start date/time to long integers (YYYYMMDDHHmm)
intDaylight = .DaylightMinute + 100 * .DaylightHour + 10000 * LastDoW( .DaylightDayOfWeek, .DaylightMonth, myYear, .DaylightDay ) + 1000000 * .DaylightMonth + 100000000 * myYear
intLocDate = myMinute + 100 * myHour + 10000 * myDay + 1000000 * myMonth + 100000000 * myYear
intStandard = .StandardMinute + 100 * .StandardHour + 10000 * LastDoW( .StandardDayOfWeek, .StandardMonth, myYear, .StandardDay ) + 1000000 * .StandardMonth + 100000000 * myYear
End With
Next
Set colItems = Nothing
Set objWMIService = Nothing
' Now that we have the long integers for the date/times, the actual comparison is quite straightforward
If intLocDate < intDaylight Then
isDST = False
ElseIf intLocDate >= intStandard Then
isDST = False
Else
isDST = True
End If
End Function
Function LastDoW( myDoW, myMonth, myYear, myWoM )
' Returns the day of the month for the specified week day, month, year and week of month
' e.g. LastDoW( 0, 3, 2011, 5 ) will return the last (5) Sunday (0) of March (3) 2011, which is 27
Dim i, j
j = 0
LastDoW = 0
For i = 1 To 31
If myWoM > j Then
If IsDate( myYear & "-" & myMonth & "-" & i ) Then
If DatePart( "w", CDate( myYear & "-" & myMonth & "-" & i ), vbSunday ) = myDoW + 1 Then
j = j + 1
LastDoW = i
End If
End If
End If
Next
End Function
Sub Syntax
Dim strMsg
strMsg = vbCrLf _
& "GMT.vbs, Version 1.10" _
& vbCrLf _
& "Return the GMT time and date for the current or specified local time and date" _
& vbCrLf & vbCrLf _
& "Usage: GMT.VBS [ date time ]" _
& vbCrLf & vbCrLf _
& "Notes:" & vbTab & "If no date/time is specified, the current date/time is assumed." _
& vbCrLf _
& " " & vbTab & "DST is checked according to the timezone rules for the current" _
& vbCrLf _
& " " & vbTab & "year, as found in the registry, but the 'ambiguous hour' right" _
& vbCrLf _
& " " & vbTab & "after the transition to Standard Time is ignored." _
& vbCrLf & vbCrLf _
& "Written by Rob van der Woude" _
& vbCrLf _
& "http://www.robvanderwoude.com"
WScript.Echo strMsg
WScript.Quit 1
End Sub
page last modified: 2024-04-16; loaded in 0.0094 seconds