(view source code of isdst.vbs as plain text)
Option Explicit
Dim blnDST
Dim dtmDate
Dim strArg, strDate, strTimeZone
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
blnDST = isDST( dtmDate )
WScript.Echo "Date/time : " & vbTab & dtmDate & vbCrLf _
& "Timezone : " & vbTab & strTimeZone & vbCrLf _
& "Date in DST : " & vbTab & blnDST
If Not blnDST Then WScript.Quit 2
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 lngDaylight, lngLocDate, lngStandard
Dim colItems, objItem, objWMIService
Dim strTZDST, strTZStd
' 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)
lngDaylight = .DaylightMinute + 100 * .DaylightHour + 10000 * LastDoW( .DaylightDayOfWeek, .DaylightMonth, myYear, .DaylightDay ) + 1000000 * .DaylightMonth + 100000000 * myYear
lngLocDate = myMinute + 100 * myHour + 10000 * myDay + 1000000 * myMonth + 100000000 * myYear
lngStandard = .StandardMinute + 100 * .StandardHour + 10000 * LastDoW( .StandardDayOfWeek, .StandardMonth, myYear, .StandardDay ) + 1000000 * .StandardMonth + 100000000 * myYear
' Store the names for DST and Standard Time
strTZDST = .DaylightName
strTZStd = .StandardName
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 lngLocDate < lngDaylight Then
isDST = False
strTimeZone = strTZStd
ElseIf lngLocDate >= lngStandard Then
isDST = False
strTimeZone = strTZStd
Else
isDST = True
strTimeZone = strTZDST
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 _
& "isDST.vbs, Version 1.10" _
& vbCrLf _
& "Check if the current or specified date/time is in Daylight Saving Time" _
& vbCrLf & vbCrLf _
& "Usage:" & vbTab & "ISDST.VBS [ date time ]" _
& vbCrLf & vbCrLf _
& "Notes:" & vbTab & "Displays the result on screen, and returns 'errorlevel' 0 if the" _
& vbCrLf _
& " " & vbTab & "date is in DST, or 2 if not (1 is reserved for command line errors)." _
& vbCrLf _
& " " & 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." _
& vbCrLf _
& " " & vbTab & "The script ignores the 'ambiguous hour' right after the transition" _
& vbCrLf _
& " " & vbTab & "to Standard Time." _
& 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.0133 seconds