Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for isdst.vbs

(view source code of isdst.vbs as plain text)

  1. Option Explicit
  2.  
  3. Dim blnDST
  4. Dim dtmDate
  5. Dim strArg, strDate, strTimeZone
  6.  
  7. dtmDate = Now
  8.  
  9. If WScript.Arguments.Named.Count   > 0 Then Syntax
  10. If WScript.Arguments.Unnamed.Count > 0 Then
  11. 	For Each strArg In WScript.Arguments.Unnamed
  12. 		strDate = Trim( strDate & " " & strArg )
  13. 	Next
  14. 	On Error Resume Next
  15. 	If IsDate( strDate ) Then
  16. 		dtmDate = CDate( strDate )
  17. 	Else
  18. 		Syntax
  19. 	End If
  20. 	On Error Goto 0
  21. End If
  22.  
  23. blnDST = isDST( dtmDate )
  24. WScript.Echo "Date/time   : " & vbTab & dtmDate     & vbCrLf _
  25.            & "Timezone    : " & vbTab & strTimeZone & vbCrLf _
  26.            & "Date in DST : " & vbTab & blnDST
  27.  
  28. If Not blnDST Then WScript.Quit 2
  29.  
  30.  
  31. Function isDST( myDate )
  32. ' Returns TRUE if the specified date is in daylight Saving Time, or FALSE if not.
  33. ' The function ignores the 'ambiguous hour' right after the Standard Time transition.
  34. 	Dim myDay, myHour, myMinute, myMonth, myYear
  35. 	Dim lngDaylight, lngLocDate, lngStandard
  36. 	Dim colItems, objItem, objWMIService
  37. 	Dim strTZDST, strTZStd
  38.  
  39. 	' Parse the specified date/time
  40. 	myDate   = CDate( myDate )
  41. 	myDay    = DatePart( "d",    myDate )
  42. 	myMonth  = DatePart( "m",    myDate )
  43. 	myYear   = DatePart( "yyyy", myDate )
  44. 	myHour   = DatePart( "h",    myDate )
  45. 	myMinute = DatePart( "n",    myDate )
  46.  
  47. 	Set objWMIService = GetObject( "winmgmts://./root/cimv2" )
  48. 	Set colItems = objWMIService.ExecQuery( "SELECT * FROM Win32_TimeZone", , 48 )
  49.  
  50. 	For Each objItem in colItems
  51. 		With objItem
  52. 			' Convert the Daylight Start date/time, specified date/time and Standard Start date/time to long integers (YYYYMMDDHHmm)
  53. 			lngDaylight = .DaylightMinute + 100 * .DaylightHour + 10000 * LastDoW( .DaylightDayOfWeek, .DaylightMonth, myYear, .DaylightDay ) + 1000000 * .DaylightMonth + 100000000 * myYear
  54. 			lngLocDate  =  myMinute       + 100 *  myHour       + 10000 * myDay                                                               + 1000000 *  myMonth       + 100000000 * myYear
  55. 			lngStandard = .StandardMinute + 100 * .StandardHour + 10000 * LastDoW( .StandardDayOfWeek, .StandardMonth, myYear, .StandardDay ) + 1000000 * .StandardMonth + 100000000 * myYear
  56. 			' Store the names for DST and Standard Time
  57. 			strTZDST    = .DaylightName
  58. 			strTZStd    = .StandardName
  59. 		End With
  60. 	Next
  61.  
  62. 	Set colItems = Nothing
  63. 	Set objWMIService = Nothing
  64.  
  65. 	' Now that we have the long integers for the date/times, the actual comparison is quite straightforward
  66. 	If lngLocDate < lngDaylight Then
  67. 		isDST       = False
  68. 		strTimeZone = strTZStd
  69. 	ElseIf lngLocDate >= lngStandard Then
  70. 		isDST       = False
  71. 		strTimeZone = strTZStd
  72. 	Else
  73. 		isDST       = True
  74. 		strTimeZone = strTZDST
  75. 	End If
  76. End Function
  77.  
  78.  
  79. Function LastDoW( myDoW, myMonth, myYear, myWoM )
  80. ' Returns the day of the month for the specified week day, month, year and week of month
  81. ' e.g. LastDoW( 0, 3, 2011, 5 ) will return the last (5) Sunday (0) of March (3) 2011, which is 27
  82. 	Dim i, j
  83. 	j = 0
  84. 	LastDoW = 0
  85. 	For i = 1 To 31
  86. 		If myWoM > j Then
  87. 			If IsDate( myYear & "-" & myMonth & "-" & i ) Then
  88. 				If DatePart( "w", CDate( myYear & "-" & myMonth & "-" & i ), vbSunday ) = myDoW + 1 Then
  89. 						j = j + 1
  90. 						LastDoW = i
  91. 				End If
  92. 			End If
  93. 		End If
  94. 	Next
  95. End Function
  96.  
  97.  
  98. Sub Syntax
  99. 	Dim strMsg
  100. 	strMsg = vbCrLf _
  101. 	       & "isDST.vbs,  Version 1.10" _
  102. 	       & vbCrLf _
  103. 	       & "Check if the current or specified date/time is in Daylight Saving Time" _
  104. 	       & vbCrLf & vbCrLf _
  105. 	       & "Usage:"  & vbTab & "ISDST.VBS  [ date  time ]" _
  106. 	       & vbCrLf & vbCrLf _
  107. 	       & "Notes:" & vbTab & "Displays the result on screen, and returns 'errorlevel' 0 if the" _
  108. 	       & vbCrLf _
  109. 	       & "      " & vbTab & "date is in DST, or 2 if not (1 is reserved for command line errors)." _
  110. 	       & vbCrLf _
  111. 	       & "      " & vbTab & "If no date/time is specified, the current date/time is assumed." _
  112. 	       & vbCrLf _
  113. 	       & "      " & vbTab & "DST is checked according to the timezone rules for the current" _
  114. 	       & vbCrLf _
  115. 	       & "      " & vbTab & "year, as found in the registry." _
  116. 	       & vbCrLf _
  117. 	       & "      " & vbTab & "The script ignores the 'ambiguous hour' right after the transition" _
  118. 	       & vbCrLf _
  119. 	       & "      " & vbTab & "to Standard Time." _
  120. 	       & vbCrLf & vbCrLf _
  121. 	       & "Written by Rob van der Woude" _
  122. 	       & vbCrLf _
  123. 	       & "http://www.robvanderwoude.com"
  124. 	WScript.Echo strMsg
  125. 	WScript.Quit 1
  126. End Sub
  127.  

page last uploaded: 2016-12-15, 11:20