Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for isdst.bat

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

  1. @ECHO OFF
  2. IF NOT "%OS%"=="Windows_NT" GOTO Syntax
  3.  
  4. SETLOCAL ENABLEDELAYEDEXPANSION
  5.  
  6. IF NOT "%~3"=="" CALL :Syntax Too many command line arguments
  7.  
  8. :: Get all timezone settings and store them in variables
  9. :: (innermost loop chomps variables, i.e. removes CR/LF from values)
  10. FOR /F "tokens=1* delims==" %%B IN ('WMIC Path Win32_TimeZone Get /Format:List ^| FIND "="') DO (
  11. 	FOR %%D IN (%%C) DO (
  12. 		SET %%B=%%D
  13. 	)
  14. )
  15.  
  16. :: Local time may be specified on the command line in YYYYMMDD HHmm format,
  17. :: if not retrieve all date and time values and store them in variables
  18. IF "%~1"=="" (
  19. 	FOR /F "tokens=1* delims==" %%B IN ('WMIC Path Win32_LocalTime Get /Format:List ^| FIND "="') DO (
  20. 		FOR %%D IN (%%C) DO (
  21. 			SET %%B=%%D
  22. 		)
  23. 	)
  24. ) ELSE (
  25. 	REM Check for YYYYMMDD format
  26. 	ECHO.%~1| FINDSTR /R /X /C:"20[0-9][0-9][01][0-9][0-3][0-9]" >NUL
  27. 	IF ERRORLEVEL 1 CALL :Syntax Invalid date
  28. 	SET LocalDate=%~1
  29. 	REM Check for HHmm format or 0000 if empty
  30. 	IF "%~2"=="" (
  31. 		SET LocalTime=0000
  32. 	) ELSE (
  33. 		ECHO.%~2| FINDSTR /R /X /C:"[0-2][0-9][0-5][0-9]" >NUL
  34. 		IF ERRORLEVEL 1 CALL :Syntax Invalid time
  35. 		SET LocalTime=%~2
  36. 		IF !LocalTime! GTR 2359 CALL :Syntax Invalid time
  37. 	)
  38. )
  39.  
  40. :: Convert and/or extract variables, depending on what is specified and what is missing
  41. IF "%~1"=="" (
  42. 	SET /A LocalDate = %Month%00 + %Day%
  43. 	SET LocalDate=0!LocalDate!
  44. 	SET Localdate=%Year%!Localdate:~-4!
  45. 	SET /A LocalTime = %Hour%00 + %Minute%
  46. 	SET LocalTime=0!LocalTime!
  47. 	SET LocalTime=!LocalTime:~-4!
  48. ) ELSE (
  49. 	SET Year=%LocalDate:~0,4%
  50. 	SET /A Month = 1%LocalDate:~4,2% - 100
  51. 	SET /A Day   = 1%LocalDate:~6,2% - 100
  52. 	SET /A Hour  = 1%LocalTime:~0,2% - 100
  53. 	SET /A Minute= 1%LocalTime:~2,2% - 100
  54. 	IF !Month! EQU  0 CALL :Syntax Invalid date
  55. 	IF !Month! GTR 12 CALL :Syntax Invalid date
  56. 	IF !Day!   EQU  0 CALL :Syntax Invalid date
  57. 	IF !Day!   GTR 31 CALL :Syntax Invalid date
  58. )
  59.  
  60. :: Test for leap year
  61. SET Leapyear=0
  62. SET /A "Leaptest = %Year% %% 4"
  63. IF %Leaptest% EQU 0 SET LeapYear=1
  64. SET /A "Leaptest = %Year% %% 100"
  65. IF %Leaptest% EQU 0 SET LeapYear=0
  66. SET /A "Leaptest = %Year% %% 400"
  67. IF %Leaptest% EQU 0 SET LeapYear=1
  68.  
  69. :: Array with days per month
  70. SET MaxDay.1=31
  71. SET /A Maxday.2 = 28 + %LeapYear%
  72. SET MaxDay.3=31
  73. SET MaxDay.4=30
  74. SET MaxDay.5=31
  75. SET MaxDay.6=30
  76. SET MaxDay.7=31
  77. SET MaxDay.8=31
  78. SET MaxDay.9=30
  79. SET MaxDay.10=31
  80. SET MaxDay.11=30
  81. SET MaxDay.12=31
  82.  
  83. :: Number of days for specified or current months
  84. SET MaxDays=!MaxDay.%Month%!
  85. IF %Day% GTR %MaxDays% CALL :Syntax Invalid date
  86.  
  87. :: Calculate DST start date and time
  88. CALL :LastDow %DaylightDayOfWeek% %DaylightMonth% %Year% %DaylightDay%
  89. SET /A DaylightStartDate = %DaylightMonth%00 + %LastDoW%
  90. SET DaylightStartDate=0%DaylightStartDate%
  91. SET DaylightStartDate=%Year%%DaylightStartDate:~-4%
  92. SET /A DaylightStartTime = %DaylightHour%00 + %DaylightMinute%
  93. SET DaylightStartTime=0%DaylightStartTime%
  94. SET DaylightStartTime=%DaylightStartTime:~-4%
  95.  
  96. :: Calculate Standard Time start date and time
  97. CALL :LastDow %StandardDayOfWeek% %StandardMonth% %Year% %StandardDay%
  98. SET /A StandardStartDate = %StandardMonth%00 + %LastDow%
  99. SET StandardStartDate=0%StandardStartDate%
  100. SET StandardStartDate=%Year%%StandardStartDate:~-4%
  101. SET /A StandardStartTime = %StandardHour%00 + %StandardMinute%
  102. SET StandardStartTime=0%StandardStartTime%
  103. SET StandardStartTime=%StandardStartTime:~-4%
  104.  
  105. :: For debugging only
  106. ::SET DaylightStart
  107. ::SET StandardStart
  108. ::SET LocalDate
  109. ::SET LocalTime
  110.  
  111. SET IsDST=2
  112. IF %LocalDate% GTR %DaylightStartDate% (
  113. 	IF %LocalDate% LSS %StandardStartDate% (
  114. 		SET IsDST=0
  115. 	)
  116. )
  117. IF %LocalDate% EQU %DaylightStartDate% (
  118. 	IF %LocalTime% GEQ %DaylightStartTime% (
  119. 		SET IsDST=0
  120. 	)
  121. )
  122. IF %LocalDate% EQU %StandardStartDate% (
  123. 	IF %LocalTime% LSS %StandardStartTime% (
  124. 		SET IsDST=0
  125. 	)
  126. )
  127.  
  128. IF %IsDST% EQU 2 (
  129. 	ECHO DST: No
  130. ) ELSE (
  131. 	ECHO DST: Yes
  132. )
  133.  
  134. ENDLOCAL & EXIT /B %IsDST%
  135.  
  136.  
  137. :LastDoW
  138. :: A REALLY DIRTY way of getting the DST and Standard Time start date/times:
  139. :: set the system date to every date in the month and check the DayOfWeek,
  140. :: then compare it to the TimeZone defined start date/time/dow.
  141. :: Now let's just hope no policies on your system restrict you from changing the date...
  142.  
  143. 	SETLOCAL ENABLEDELAYEDEXPANSION
  144.  
  145. 	:: Required parameters
  146. 	SET DoW=%1
  147. 	SET Month=%2
  148. 	SET Year=%3
  149. 	SET WoM=%4
  150.  
  151. 	:: First of all save the current date
  152. 	FOR %%A IN (%Date%) DO SET Today=%%A
  153.  
  154. 	:: Test the date formats M/D/Y and D-M-Y; add more yourself if required
  155. 	SET DateFormat=
  156. 	ECHO.| DATE 5/31/2010 >NUL 2>&1
  157. 	DATE /T | FINDSTR /R /C:"5.31.2010" >NUL
  158. 	IF NOT ERRORLEVEL 1 (
  159. 		SET DateFormat=M/D/Y
  160. 	) ELSE (
  161. 		ECHO.| DATE 31-5-2010 >NUL 2>&1
  162. 		DATE /T | FINDSTR /R /C:"31.0*5.2010" >NUL
  163. 		IF NOT ERRORLEVEL 1 (
  164. 			SET DateFormat=D-M-Y
  165. 		)
  166. 	)
  167. 	:: Restore the current date
  168. 	DATE %Today%
  169. 	:: Abort if the local date format was not found
  170. 	IF NOT DEFINED DateFormat CALL :Syntax Unknown date/time format
  171.  
  172. 	SET Count=0
  173. 	FOR /L %%A IN (1,1,%MaxDays%) DO (
  174. 		SET DayOfWeek=
  175. 		REM Set the date ...
  176. 		IF "%DateFormat%"=="D-M-Y" DATE %%A-%Month%-%Year%
  177. 		IF "%DateFormat%"=="M/D/Y" DATE %Month%/%%A/%Year%
  178. 		REM Get the DayOfWeek for the new date ...
  179. 		FOR /F "skip=1" %%B IN ('WMIC Path Win32_LocalTime Get DayOfWeek') DO (
  180. 			FOR %%C IN (%%B) DO (
  181. 				SET DayOfWeek=%%C
  182. 			)
  183. 		)
  184. 		REM If it wasn't found yet, check if the DayOfWeek
  185. 		REM matches the requested DoW, and if so, save it
  186. 		IF !Count! LSS %WoM% (
  187. 			IF !DayOfWeek! EQU %DoW% (
  188. 				SET /A Count += 1
  189. 				SET LastDoW=%%A
  190. 			)
  191. 		)
  192. 	)
  193. 	:: Restore the current date
  194. 	DATE %Today%
  195. 	ENDLOCAL & SET LastDoW=%LastDoW%
  196. GOTO:EOF
  197.  
  198.  
  199. :Syntax
  200. IF NOT "%*"=="" ECHO.
  201. IF NOT "%*"=="" ECHO ERROR: %*
  202. ECHO.
  203. ECHO IsDST.bat,  Version 1.03 for Windows XP professional and later
  204. ECHO Check if the current or specified date/time is in Daylight Saving Time
  205. ECHO.
  206. ECHO Usage:  ISDST  [ YYYYMMDD  [ HHmm ] ]
  207. ECHO.
  208. ECHO Notes:  Displays the result on screen, and returns 'errorlevel' 0 if the
  209. ECHO         date is in DST, or 2 if not (1 is reserved for command line errors).
  210. ECHO         If no date is specified, the current date is assumed.
  211. ECHO         If a date but no time is specified, 00:00 is assumed.
  212. ECHO         DST is checked according to the timezone rules for the current
  213. ECHO         year, as found in the registry.
  214. ECHO         The script ignores the 'ambiguous hour' right after the transition
  215. ECHO         to Standard Time.
  216. ECHO.
  217. ECHO Written by Rob van der Woude
  218. ECHO http://www.robvanderwoude.com"
  219.  
  220. :: To abort the entire script we CANNOT use EXIT's /B switch; if
  221. :: the /B switch were used, the batch file would continue after
  222. :: displaying this help text, which we certainly do not want.
  223. IF "%OS%"=="Windows_NT" ENDLOCAL
  224. IF "%OS%"=="Windows_NT" EXIT 1
  225.  

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