Powered by GeSHi

Source code for leapyear.bat

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

  1. @ECHO OFF
  2. :: Windows NT 4 and later only
  3. IF NOT "%OS%"=="Windows_NT" GOTO Syntax
  4.  
  5. SETLOCAL
  6. :: Use current year as default
  7. IF [%1]==[] GOTO ThisYear
  8. :: Only 1 parameter allowed
  9. IF NOT [%2]==[] GOTO Syntax
  10. :: Check for ?
  11. ECHO.%1 | FIND "?" >NUL
  12. IF NOT ERRORLEVEL 1 GOTO Syntax
  13. :: Check if parameter is a number
  14. IF 1%1 LSS 2 GOTO Syntax
  15. :: Check if the number is within range
  16. IF 1%1 LSS 10 GOTO Syntax
  17. IF 1%1 GTR 19999 GOTO Syntax
  18. :: OK, continue
  19. SET yy=%1
  20. GOTO Calculate
  21.  
  22. :ThisYear
  23. :: Code to extract day, month and year by Simon Sheppard
  24. :: http://www.ss64.com
  25. FOR /f "tokens=2-4 skip=1 delims=(/-)" %%G IN ('ECHO.^|DATE') DO (
  26. 	FOR /f "tokens=2 delims= " %%A IN ('DATE /T') DO (
  27. 		SET v_first=%%G
  28. 		SET v_second=%%H
  29. 		SET v_third=%%I
  30. 		SET v_all=%%A
  31. 	)
  32. )
  33. SET %v_first%=%v_all:~0,2%
  34. SET %v_second%=%v_all:~3,2%
  35. SET %v_third%=%v_all:~6,4%
  36.  
  37. :Calculate
  38. :: Default to not a leap year
  39. SET LeapYear=0
  40. :: If year is a multiple of 4 then it is a leap year ...
  41. SET /A test = %yy% / 4
  42. SET /A test = %test% * 4
  43. IF %test% EQU %yy% SET LeapYear=1
  44. :: ... except if it is a multiple of 100 ...
  45. SET /A test = %yy% / 100
  46. SET /A test = %test% * 100
  47. IF %test% EQU %yy% SET LeapYear=0
  48. :: ... unless it is a multiple of 400!
  49. SET /A test = %yy% / 400
  50. SET /A test = %test% * 400
  51. IF %test% EQU %yy% SET LeapYear=1
  52.  
  53. :: Display the result
  54. SET is=IS
  55. IF %LeapYear%==0 SET is=is NOT
  56. ECHO %yy% %is% a leap year
  57.  
  58. :: Done
  59. ENDLOCAL
  60. GOTO End
  61.  
  62. :Syntax
  63. ECHO.
  64. ECHO LeapYear.bat, Version 1.01 for Windows NT 4 / 2000 / XP
  65. ECHO Check if the specified year is a leap year or not.
  66. ECHO.
  67. ECHO Usage:  LEAPYEAR  [ year ]
  68. ECHO Where:  "year" is a year between 0 and 9999.
  69. ECHO         If no year is specified, the current year is assumed.
  70. ECHO.
  71. ECHO Written by Rob van der Woude
  72. ECHO http://www.robvanderwoude.com
  73. ECHO.
  74. ECHO Code to extract the current year written by Simon Sheppard
  75. ECHO http://www.ss64.com
  76.  
  77. :End
  78.