(view source code of isdst.bat as plain text)
@ECHO OFF
IF NOT "%OS%"=="Windows_NT" GOTO Syntax
SETLOCAL ENABLEDELAYEDEXPANSION
IF NOT "%~3"=="" CALL :Syntax Too many command line arguments
:: Get all timezone settings and store them in variables
:: (innermost loop chomps variables, i.e. removes CR/LF from values)
FOR /F "tokens=1* delims==" %%B IN ('WMIC Path Win32_TimeZone Get /Format:List ^| FIND "="') DO (
FOR %%D IN (%%C) DO (
SET %%B=%%D
)
)
:: Local time may be specified on the command line in YYYYMMDD HHmm format,
:: if not retrieve all date and time values and store them in variables
IF "%~1"=="" (
FOR /F "tokens=1* delims==" %%B IN ('WMIC Path Win32_LocalTime Get /Format:List ^| FIND "="') DO (
FOR %%D IN (%%C) DO (
SET %%B=%%D
)
)
) ELSE (
REM Check for YYYYMMDD format
ECHO.%~1| FINDSTR /R /X /C:"20[0-9][0-9][01][0-9][0-3][0-9]" >NUL
IF ERRORLEVEL 1 CALL :Syntax Invalid date
SET LocalDate=%~1
REM Check for HHmm format or 0000 if empty
IF "%~2"=="" (
SET LocalTime=0000
) ELSE (
ECHO.%~2| FINDSTR /R /X /C:"[0-2][0-9][0-5][0-9]" >NUL
IF ERRORLEVEL 1 CALL :Syntax Invalid time
SET LocalTime=%~2
IF !LocalTime! GTR 2359 CALL :Syntax Invalid time
)
)
:: Convert and/or extract variables, depending on what is specified and what is missing
IF "%~1"=="" (
SET /A LocalDate = %Month%00 + %Day%
SET LocalDate=0!LocalDate!
SET Localdate=%Year%!Localdate:~-4!
SET /A LocalTime = %Hour%00 + %Minute%
SET LocalTime=0!LocalTime!
SET LocalTime=!LocalTime:~-4!
) ELSE (
SET Year=%LocalDate:~0,4%
SET /A Month = 1%LocalDate:~4,2% - 100
SET /A Day = 1%LocalDate:~6,2% - 100
SET /A Hour = 1%LocalTime:~0,2% - 100
SET /A Minute= 1%LocalTime:~2,2% - 100
IF !Month! EQU 0 CALL :Syntax Invalid date
IF !Month! GTR 12 CALL :Syntax Invalid date
IF !Day! EQU 0 CALL :Syntax Invalid date
IF !Day! GTR 31 CALL :Syntax Invalid date
)
:: Test for leap year
SET Leapyear=0
SET /A "Leaptest = %Year% %% 4"
IF %Leaptest% EQU 0 SET LeapYear=1
SET /A "Leaptest = %Year% %% 100"
IF %Leaptest% EQU 0 SET LeapYear=0
SET /A "Leaptest = %Year% %% 400"
IF %Leaptest% EQU 0 SET LeapYear=1
:: Array with days per month
SET MaxDay.1=31
SET /A Maxday.2 = 28 + %LeapYear%
SET MaxDay.3=31
SET MaxDay.4=30
SET MaxDay.5=31
SET MaxDay.6=30
SET MaxDay.7=31
SET MaxDay.8=31
SET MaxDay.9=30
SET MaxDay.10=31
SET MaxDay.11=30
SET MaxDay.12=31
:: Number of days for specified or current months
SET MaxDays=!MaxDay.%Month%!
IF %Day% GTR %MaxDays% CALL :Syntax Invalid date
:: Calculate DST start date and time
CALL :LastDow %DaylightDayOfWeek% %DaylightMonth% %Year% %DaylightDay%
SET /A DaylightStartDate = %DaylightMonth%00 + %LastDoW%
SET DaylightStartDate=0%DaylightStartDate%
SET DaylightStartDate=%Year%%DaylightStartDate:~-4%
SET /A DaylightStartTime = %DaylightHour%00 + %DaylightMinute%
SET DaylightStartTime=0%DaylightStartTime%
SET DaylightStartTime=%DaylightStartTime:~-4%
:: Calculate Standard Time start date and time
CALL :LastDow %StandardDayOfWeek% %StandardMonth% %Year% %StandardDay%
SET /A StandardStartDate = %StandardMonth%00 + %LastDow%
SET StandardStartDate=0%StandardStartDate%
SET StandardStartDate=%Year%%StandardStartDate:~-4%
SET /A StandardStartTime = %StandardHour%00 + %StandardMinute%
SET StandardStartTime=0%StandardStartTime%
SET StandardStartTime=%StandardStartTime:~-4%
:: For debugging only
::SET DaylightStart
::SET StandardStart
::SET LocalDate
::SET LocalTime
SET IsDST=2
IF %LocalDate% GTR %DaylightStartDate% (
IF %LocalDate% LSS %StandardStartDate% (
SET IsDST=0
)
)
IF %LocalDate% EQU %DaylightStartDate% (
IF %LocalTime% GEQ %DaylightStartTime% (
SET IsDST=0
)
)
IF %LocalDate% EQU %StandardStartDate% (
IF %LocalTime% LSS %StandardStartTime% (
SET IsDST=0
)
)
IF %IsDST% EQU 2 (
ECHO DST: No
) ELSE (
ECHO DST: Yes
)
ENDLOCAL & EXIT /B %IsDST%
:LastDoW
:: A REALLY DIRTY way of getting the DST and Standard Time start date/times:
:: set the system date to every date in the month and check the DayOfWeek,
:: then compare it to the TimeZone defined start date/time/dow.
:: Now let's just hope no policies on your system restrict you from changing the date...
SETLOCAL ENABLEDELAYEDEXPANSION
:: Required parameters
SET DoW=%1
SET Month=%2
SET Year=%3
SET WoM=%4
:: First of all save the current date
FOR %%A IN (%Date%) DO SET Today=%%A
:: Test the date formats M/D/Y and D-M-Y; add more yourself if required
SET DateFormat=
ECHO.| DATE 5/31/2010 >NUL 2>&1
DATE /T | FINDSTR /R /C:"5.31.2010" >NUL
IF NOT ERRORLEVEL 1 (
SET DateFormat=M/D/Y
) ELSE (
ECHO.| DATE 31-5-2010 >NUL 2>&1
DATE /T | FINDSTR /R /C:"31.0*5.2010" >NUL
IF NOT ERRORLEVEL 1 (
SET DateFormat=D-M-Y
)
)
:: Restore the current date
DATE %Today%
:: Abort if the local date format was not found
IF NOT DEFINED DateFormat CALL :Syntax Unknown date/time format
SET Count=0
FOR /L %%A IN (1,1,%MaxDays%) DO (
SET DayOfWeek=
REM Set the date ...
IF "%DateFormat%"=="D-M-Y" DATE %%A-%Month%-%Year%
IF "%DateFormat%"=="M/D/Y" DATE %Month%/%%A/%Year%
REM Get the DayOfWeek for the new date ...
FOR /F "skip=1" %%B IN ('WMIC Path Win32_LocalTime Get DayOfWeek') DO (
FOR %%C IN (%%B) DO (
SET DayOfWeek=%%C
)
)
REM If it wasn't found yet, check if the DayOfWeek
REM matches the requested DoW, and if so, save it
IF !Count! LSS %WoM% (
IF !DayOfWeek! EQU %DoW% (
SET /A Count += 1
SET LastDoW=%%A
)
)
)
:: Restore the current date
DATE %Today%
ENDLOCAL & SET LastDoW=%LastDoW%
GOTO:EOF
:Syntax
IF NOT "%*"=="" ECHO.
IF NOT "%*"=="" ECHO ERROR: %*
ECHO.
ECHO IsDST.bat, Version 1.03 for Windows XP professional and later
ECHO Check if the current or specified date/time is in Daylight Saving Time
ECHO.
ECHO Usage: ISDST [ YYYYMMDD [ HHmm ] ]
ECHO.
ECHO Notes: Displays the result on screen, and returns 'errorlevel' 0 if the
ECHO date is in DST, or 2 if not (1 is reserved for command line errors).
ECHO If no date is specified, the current date is assumed.
ECHO If a date but no time is specified, 00:00 is assumed.
ECHO DST is checked according to the timezone rules for the current
ECHO year, as found in the registry.
ECHO The script ignores the 'ambiguous hour' right after the transition
ECHO to Standard Time.
ECHO.
ECHO Written by Rob van der Woude
ECHO http://www.robvanderwoude.com"
:: To abort the entire script we CANNOT use EXIT's /B switch; if
:: the /B switch were used, the batch file would continue after
:: displaying this help text, which we certainly do not want.
IF "%OS%"=="Windows_NT" ENDLOCAL
IF "%OS%"=="Windows_NT" EXIT 1
page last modified: 2024-04-16; loaded in 0.0105 seconds