easter.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. # -*- coding: utf-8 -*-
  2. """
  3. This module offers a generic easter computing method for any given year, using
  4. Western, Orthodox or Julian algorithms.
  5. """
  6. import datetime
  7. __all__ = ["easter", "EASTER_JULIAN", "EASTER_ORTHODOX", "EASTER_WESTERN"]
  8. EASTER_JULIAN = 1
  9. EASTER_ORTHODOX = 2
  10. EASTER_WESTERN = 3
  11. def easter(year, method=EASTER_WESTERN):
  12. """
  13. This method was ported from the work done by GM Arts,
  14. on top of the algorithm by Claus Tondering, which was
  15. based in part on the algorithm of Ouding (1940), as
  16. quoted in "Explanatory Supplement to the Astronomical
  17. Almanac", P. Kenneth Seidelmann, editor.
  18. This algorithm implements three different easter
  19. calculation methods:
  20. 1 - Original calculation in Julian calendar, valid in
  21. dates after 326 AD
  22. 2 - Original method, with date converted to Gregorian
  23. calendar, valid in years 1583 to 4099
  24. 3 - Revised method, in Gregorian calendar, valid in
  25. years 1583 to 4099 as well
  26. These methods are represented by the constants:
  27. * ``EASTER_JULIAN = 1``
  28. * ``EASTER_ORTHODOX = 2``
  29. * ``EASTER_WESTERN = 3``
  30. The default method is method 3.
  31. More about the algorithm may be found at:
  32. `GM Arts: Easter Algorithms <http://www.gmarts.org/index.php?go=415>`_
  33. and
  34. `The Calendar FAQ: Easter <https://www.tondering.dk/claus/cal/easter.php>`_
  35. """
  36. if not (1 <= method <= 3):
  37. raise ValueError("invalid method")
  38. # g - Golden year - 1
  39. # c - Century
  40. # h - (23 - Epact) mod 30
  41. # i - Number of days from March 21 to Paschal Full Moon
  42. # j - Weekday for PFM (0=Sunday, etc)
  43. # p - Number of days from March 21 to Sunday on or before PFM
  44. # (-6 to 28 methods 1 & 3, to 56 for method 2)
  45. # e - Extra days to add for method 2 (converting Julian
  46. # date to Gregorian date)
  47. y = year
  48. g = y % 19
  49. e = 0
  50. if method < 3:
  51. # Old method
  52. i = (19*g + 15) % 30
  53. j = (y + y//4 + i) % 7
  54. if method == 2:
  55. # Extra dates to convert Julian to Gregorian date
  56. e = 10
  57. if y > 1600:
  58. e = e + y//100 - 16 - (y//100 - 16)//4
  59. else:
  60. # New method
  61. c = y//100
  62. h = (c - c//4 - (8*c + 13)//25 + 19*g + 15) % 30
  63. i = h - (h//28)*(1 - (h//28)*(29//(h + 1))*((21 - g)//11))
  64. j = (y + y//4 + i + 2 - c + c//4) % 7
  65. # p can be from -6 to 56 corresponding to dates 22 March to 23 May
  66. # (later dates apply to method 2, although 23 May never actually occurs)
  67. p = i - j + e
  68. d = 1 + (p + 27 + (p + 6)//40) % 31
  69. m = 3 + (p + 26)//30
  70. return datetime.date(int(y), int(m), int(d))