Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for easter.bat

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

  1. @ECHO OFF
  2. ECHO.
  3.  
  4. :: Windows NT 4 / 2000 / XP only
  5. IF NOT "%OS%"=="Windows_NT" GOTO Syntax
  6.  
  7. :: Localize environment and enable delayed variable expansion
  8. SETLOCAL ENABLEDELAYEDEXPANSION
  9.  
  10. :: Command line check
  11. IF NOT [%2]==[] GOTO Syntax
  12.  
  13. :: Determine current year
  14. CALL :ThisYear
  15.  
  16. :: If no year is specified, use current year
  17. IF "%~1"=="" (SET Y=%ThisYear%) ELSE (SET Y=%~1)
  18.  
  19. :: Is the specified year valid?
  20. :: Check if number
  21. FOR /F "tokens=1 delims=0123456789" %%A IN ('ECHO.%Y%') DO IF NOT "%%~A"=="" GOTO Syntax
  22. :: check if in range
  23. IF %Y%0 LSS 17520 GOTO Syntax
  24. IF %Y%0 GTR 30000 GOTO Syntax
  25.  
  26. :: Array of month names used
  27. SET Month.03=March
  28. SET Month.3=March
  29. SET Month.04=April
  30. SET Month.4=April
  31. SET Month.05=May
  32. SET Month.5=May
  33. SET Month.06=June
  34. SET Month.6=June
  35.  
  36. :: Calculate Easter Day using the instructions found at
  37. :: Simon Kershaw's "KEEPING THE FEAST"
  38. :: http://www.oremus.org/liturgy/etc/ktf/app/easter.html
  39. SET /A G  = ( %Y% %% 19 ) + 1
  40. SET /A S  = (( %Y% - 1600 ) / 100 ) - (( %Y% - 1600 ) / 400 )
  41. SET /A L  = ((( %Y% - 1400 ) / 100 ) * 8 ) / 25
  42. SET /A P1 = ( 30003 - 11 * %G% + %S% - %L% ) %% 30
  43. SET P=%P1%
  44. IF %P%==28 IF %G% GTR 11 SET P=27
  45. IF %P%==29 SET P=28
  46. SET /A D  = ( %Y% + ( %Y% / 4 ) - ( %Y% / 100 ) + ( %Y% / 400 )) %% 7
  47. SET /A D1 = ( 8 - %D% ) %% 7
  48. SET /A P2 = ( 70003 + %P% ) %% 7
  49. SET /A X  = (( 70004 - %D% - %P% ) %% 7 ) + 1
  50. SET /A E  = %P% + %X%
  51. IF %E% LSS 11 (
  52. 	SET /A ED = %E% + 21
  53. 	SET EM=3
  54. ) ELSE (
  55. 	SET /A ED = %E% - 10
  56. 	SET EM=4
  57. )
  58. IF %Y% LSS %ThisYear% SET IS=was
  59. IF %Y% EQU %ThisYear% SET IS=is
  60. IF %Y% GTR %ThisYear% SET IS=will be
  61.  
  62. :: Calculate Ascension and Pentecost dates
  63. CALL :JDate %Y% %EM% %ED%
  64. SET /A ADJ = %JDate% + 39
  65. SET /A PDJ = %JDate% + 49
  66. CALL :GDate %ADJ%
  67. FOR /F "tokens=2,3" %%A IN ("%GDate%") DO (
  68. 	SET AM=%%A
  69. 	SET AD=%%B
  70. )
  71. CALL :GDate %PDJ%
  72. FOR /F "tokens=2,3" %%A IN ("%GDate%") DO (
  73. 	SET PM=%%A
  74. 	SET PD=%%B
  75. )
  76.  
  77. :: Display the result
  78. ECHO In %Y% Easter Day %IS% !Month.%EM%! %ED%
  79. ECHO         Ascension Day %IS% !Month.%AM%! %AD%
  80. ECHO         Pentecost %IS% !Month.%PM%! %PD%
  81.  
  82. :: Done
  83. GOTO End
  84.  
  85.  
  86. :GDate
  87. :: Convert Julian date back to "normal" Gregorian date
  88. :: Argument : Julian date
  89. :: Returns  : YYYY MM DD
  90. ::
  91. :: Algorithm based on Fliegel-Van Flandern
  92. :: algorithm from the Astronomical Almanac,
  93. :: provided by Doctor Fenton on the Math Forum
  94. :: (http://mathforum.org/library/drmath/view/51907.html),
  95. :: and converted to batch code by Ron Bakowski.
  96. ::
  97. SET /A P      = %1 + 68569
  98. SET /A Q      = 4 * %P% / 146097
  99. SET /A R      = %P% - ( 146097 * %Q% +3 ) / 4
  100. SET /A S      = 4000 * ( %R% + 1 ) / 1461001
  101. SET /A T      = %R% - 1461 * %S% / 4 + 31
  102. SET /A U      = 80 * %T% / 2447
  103. SET /A V      = %U% / 11
  104. SET /A GYear  = 100 * ( %Q% - 49 ) + %S% + %V%
  105. SET /A GMonth = %U% + 2 - 12 * %V%
  106. SET /A GDay   = %T% - 2447 * %U% / 80
  107. :: Clean up the mess
  108. FOR %%A IN (P Q R S T U V) DO SET %%A=
  109. :: Add leading zeroes
  110. IF 1%GMonth% LSS 20 SET GMonth=0%GMonth%
  111. IF 1%GDay%   LSS 20 SET GDay=0%GDay%
  112. :: Return value
  113. SET GDate=%GYear% %GMonth% %GDay%
  114. GOTO:EOF
  115.  
  116.  
  117. :JDate
  118. :: Convert date to Julian
  119. :: Arguments : YYYY MM DD
  120. :: Returns   : Julian date
  121. ::
  122. :: Algorithm based on Fliegel-Van Flandern
  123. :: algorithm from the Astronomical Almanac,
  124. :: provided by Doctor Fenton on the Math Forum
  125. :: (http://mathforum.org/library/drmath/view/51907.html),
  126. :: and converted to batch code by Ron Bakowski.
  127. ::
  128. SET /A Month1 = ( %2 - 14 ) / 12
  129. SET /A Year1  = %1 + 4800
  130. SET /A JDate  = 1461 * ( %Year1% + %Month1% ) / 4 + 367 * ( %2 - 2 -12 * %Month1% ) / 12 - ( 3 * ( ( %Year1% + %Month1% + 100 ) / 100 ) ) / 4 + %3 - 32075
  131. FOR %%A IN (Month1 Year1) DO SET %%A=
  132. GOTO:EOF 
  133.  
  134.  
  135. :ThisYear
  136. :: Export registry settings to a temporary file
  137. START /WAIT REGEDIT /E "%Temp%.\_Temp.reg" "HKEY_CURRENT_USER\Control Panel\International"
  138. :: Read iDate and sDate from the exported data; more info on iDate can be found at
  139. :: http://technet2.microsoft.com/windowsserver/en/library/7dedbd31-40bd-4f47-a833-517a0b9ab9bb1033.mspx
  140. :: and more info on sDate at
  141. :: http://technet2.microsoft.com/windowsserver/en/library/072ad962-21c4-4070-9c6f-2720922d6d361033.mspx
  142. FOR /F "tokens=1* delims==" %%A IN ('TYPE "%Temp%.\_Temp.reg" ^| FINDSTR /R /B /C:"\"[is]Date\"="') DO SET %%~A=%%~B
  143. DEL "%Temp%.\_Temp.reg"
  144. :: Detemine current year depending on registry settings
  145. FOR %%A IN (%Date%) DO SET Today=%%A
  146. IF %iDate%==2 (SET Token=1) ELSE (SET Token=3)
  147. FOR /F "tokens=%Token% delims=%sDate%" %%A IN ("%Today%") DO SET ThisYear=%%A
  148. GOTO:EOF
  149.  
  150.  
  151.  
  152. :Syntax
  153. ECHO Easter.bat,  Version 3.00 for Windows NT 4 and later
  154. ECHO Calculate Easter day, Ascension day and Pentecost dates for the specified year.
  155. ECHO.
  156. ECHO Usage:  EASTER  [ year ]
  157. ECHO.
  158. ECHO Where:  year should be within the range of 1752..3000 (default: current year)
  159. ECHO.
  160. ECHO Note:   Easter day calculation based on Simon Kershaw's "KEEPING THE FEAST"
  161. ECHO         (http://www.oremus.org/liturgy/etc/ktf/app/easter.html).
  162. ECHO         Julian date math algorithms based on Fliegel-Van Flandern algorithm
  163. ECHO         from the Astronomical Almanac, provided by Doctor Fenton on the Math
  164. ECHO         Forum (http://mathforum.org/library/drmath/view/51907.html), and
  165. ECHO         converted to batch code by Ron Bakowski.
  166. ECHO.
  167. ECHO Written by Rob van der Woude
  168. ECHO http://www.robvanderwoude.com
  169.  
  170. :End
  171. IF "%OS%"=="Windows_NT" ENDLOCAL
  172.  

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