Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for gmt.vbs

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

  1. Option Explicit
  2.  
  3. Dim dtmDate, dtmGMT
  4. Dim colItems, objItem, objWMIService
  5. Dim strArg, strDate
  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.  
  24.  
  25. Set objWMIService = GetObject( "winmgmts://./root/cimv2" )
  26. Set colItems = objWMIService.ExecQuery( "SELECT * FROM Win32_TimeZone", , 48 )
  27. For Each objItem in colItems
  28. 	dtmGMT = DateAdd( "n", - objItem.Bias, dtmDate )
  29. 	If isDST( dtmDate ) Then
  30. 		dtmGMT = DateAdd( "n", objItem.DaylightBias, dtmGMT )
  31. 	End If
  32. Next
  33. Set colItems = Nothing
  34. Set objWMIService = Nothing
  35.  
  36. WScript.Echo dtmGMT & " GMT"
  37.  
  38.  
  39. Function isDST( myDate )
  40. ' Returns TRUE if the specified date is in daylight Saving Time, or FALSE if not.
  41. ' The function ignores the 'ambiguous hour' right after the Standard Time transition.
  42. 	Dim myDay, myHour, myMinute, myMonth, myYear
  43. 	Dim intDaylight, intLocDate, intStandard
  44. 	Dim colItems, objItem, objWMIService
  45.  
  46. 	' Parse the specified date/time
  47. 	myDate   = CDate( myDate )
  48. 	myDay    = DatePart( "d",    myDate )
  49. 	myMonth  = DatePart( "m",    myDate )
  50. 	myYear   = DatePart( "yyyy", myDate )
  51. 	myHour   = DatePart( "h",    myDate )
  52. 	myMinute = DatePart( "n",    myDate )
  53.  
  54. 	Set objWMIService = GetObject( "winmgmts://./root/cimv2" )
  55. 	Set colItems = objWMIService.ExecQuery( "SELECT * FROM Win32_TimeZone", , 48 )
  56.  
  57. 	For Each objItem in colItems
  58. 		With objItem
  59. 			' Convert the Daylight Start date/time, specified date/time and Standard Start date/time to long integers (YYYYMMDDHHmm)
  60. 			intDaylight = .DaylightMinute + 100 * .DaylightHour + 10000 * LastDoW( .DaylightDayOfWeek, .DaylightMonth, myYear, .DaylightDay ) + 1000000 * .DaylightMonth + 100000000 * myYear
  61. 			intLocDate  =  myMinute       + 100 *  myHour       + 10000 * myDay                                                               + 1000000 *  myMonth       + 100000000 * myYear
  62. 			intStandard = .StandardMinute + 100 * .StandardHour + 10000 * LastDoW( .StandardDayOfWeek, .StandardMonth, myYear, .StandardDay ) + 1000000 * .StandardMonth + 100000000 * myYear
  63. 		End With
  64. 	Next
  65.  
  66. 	Set colItems = Nothing
  67. 	Set objWMIService = Nothing
  68.  
  69. 	' Now that we have the long integers for the date/times, the actual comparison is quite straightforward
  70. 	If intLocDate < intDaylight Then
  71. 		isDST = False
  72. 	ElseIf intLocDate >= intStandard Then
  73. 		isDST = False
  74. 	Else
  75. 		isDST = True
  76. 	End If
  77. End Function
  78.  
  79.  
  80. Function LastDoW( myDoW, myMonth, myYear, myWoM )
  81. ' Returns the day of the month for the specified week day, month, year and week of month
  82. ' e.g. LastDoW( 0, 3, 2011, 5 ) will return the last (5) Sunday (0) of March (3) 2011, which is 27
  83. 	Dim i, j
  84. 	j = 0
  85. 	LastDoW = 0
  86. 	For i = 1 To 31
  87. 		If myWoM > j Then
  88. 			If IsDate( myYear & "-" & myMonth & "-" & i ) Then
  89. 				If DatePart( "w", CDate( myYear & "-" & myMonth & "-" & i ), vbSunday ) = myDoW + 1 Then
  90. 						j = j + 1
  91. 						LastDoW = i
  92. 				End If
  93. 			End If
  94. 		End If
  95. 	Next
  96. End Function
  97.  
  98.  
  99. Sub Syntax
  100. 	Dim strMsg
  101. 	strMsg = vbCrLf _
  102. 	       & "GMT.vbs,  Version 1.10" _
  103. 	       & vbCrLf _
  104. 	       & "Return the GMT time and date for the current or specified local time and date" _
  105. 	       & vbCrLf & vbCrLf _
  106. 	       & "Usage:  GMT.VBS  [ date time ]" _
  107. 	       & vbCrLf & vbCrLf _
  108. 	       & "Notes:" & vbTab & "If no date/time is specified, the current date/time is assumed." _
  109. 	       & vbCrLf _
  110. 	       & "      " & vbTab & "DST is checked according to the timezone rules for the current" _
  111. 	       & vbCrLf _
  112. 	       & "      " & vbTab & "year, as found in the registry, but the 'ambiguous hour' right" _
  113. 	       & vbCrLf _
  114. 	       & "      " & vbTab & "after the transition to Standard Time is ignored." _
  115. 	       & vbCrLf & vbCrLf _
  116. 	       & "Written by Rob van der Woude" _
  117. 	       & vbCrLf _
  118. 	       & "http://www.robvanderwoude.com"
  119. 	WScript.Echo strMsg
  120. 	WScript.Quit 1
  121. End Sub
  122.  

page last modified: 2024-04-16; loaded in 0.0094 seconds