Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.11/site-packages/dateutil/easter.py: 26%

Shortcuts on this page

r m x   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

27 statements  

1""" 

2This module offers a generic Easter computing method for any given year, using 

3Western, Orthodox or Julian algorithms. 

4""" 

5 

6import datetime 

7 

8__all__ = ["easter", "EASTER_JULIAN", "EASTER_ORTHODOX", "EASTER_WESTERN"] 

9 

10EASTER_JULIAN = 1 

11EASTER_ORTHODOX = 2 

12EASTER_WESTERN = 3 

13 

14 

15def easter(year, method=EASTER_WESTERN): 

16 """ 

17 This method was ported from the work done by GM Arts, 

18 on top of the algorithm by Claus Tondering, which was 

19 based in part on the algorithm of Ouding (1940), as 

20 quoted in "Explanatory Supplement to the Astronomical 

21 Almanac", P. Kenneth Seidelmann, editor. 

22 

23 This algorithm implements three different Easter 

24 calculation methods: 

25 

26 1. Original calculation in Julian calendar, valid in 

27 dates after 326 AD 

28 2. Original method, with date converted to Gregorian 

29 calendar, valid in years 1583 to 4099 

30 3. Revised method, in Gregorian calendar, valid in 

31 years 1583 to 4099 as well 

32 

33 These methods are represented by the constants: 

34 

35 * ``EASTER_JULIAN = 1`` 

36 * ``EASTER_ORTHODOX = 2`` 

37 * ``EASTER_WESTERN = 3`` 

38 

39 The default method is method 3. 

40 

41 More about the algorithm may be found at: 

42 

43 `GM Arts: Easter Algorithms <http://www.gmarts.org/index.php?go=415>`_ 

44 

45 and 

46 

47 `The Calendar FAQ: Easter <https://www.tondering.dk/claus/cal/easter.php>`_ 

48 

49 """ 

50 

51 if not (1 <= method <= 3): 

52 raise ValueError("invalid method") 

53 

54 # g - Golden year - 1 

55 # c - Century 

56 # h - (23 - Epact) mod 30 

57 # i - Number of days from March 21 to Paschal Full Moon 

58 # j - Weekday for PFM (0=Sunday, etc) 

59 # p - Number of days from March 21 to Sunday on or before PFM 

60 # (-6 to 28 methods 1 & 3, to 56 for method 2) 

61 # e - Extra days to add for method 2 (converting Julian 

62 # date to Gregorian date) 

63 

64 y = year 

65 g = y % 19 

66 e = 0 

67 if method < 3: 

68 # Old method 

69 i = (19*g + 15) % 30 

70 j = (y + y//4 + i) % 7 

71 if method == 2: 

72 # Extra dates to convert Julian to Gregorian date 

73 e = 10 

74 if y > 1600: 

75 e = e + y//100 - 16 - (y//100 - 16)//4 

76 else: 

77 # New method 

78 c = y//100 

79 h = (c - c//4 - (8*c + 13)//25 + 19*g + 15) % 30 

80 i = h - (h//28)*(1 - (h//28)*(29//(h + 1))*((21 - g)//11)) 

81 j = (y + y//4 + i + 2 - c + c//4) % 7 

82 

83 # p can be from -6 to 56 corresponding to dates 22 March to 23 May 

84 # (later dates apply to method 2, although 23 May never actually occurs) 

85 p = i - j + e 

86 d = 1 + (p + 27 + (p + 6)//40) % 31 

87 m = 3 + (p + 26)//30 

88 return datetime.date(int(y), int(m), int(d))