File: burmese.py

package info (click to toggle)
python-holidays 0.90-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 59,088 kB
  • sloc: python: 121,956; javascript: 85; makefile: 62
file content (318 lines) | stat: -rw-r--r-- 8,601 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
#  holidays
#  --------
#  A fast, efficient Python library for generating country, province and state
#  specific sets of holidays on the fly. It aims to make determining whether a
#  specific date is a holiday as fast and flexible as possible.
#
#  Authors: Vacanza Team and individual contributors (see CONTRIBUTORS file)
#           dr-prodigy <dr.prodigy.github@gmail.com> (c) 2017-2023
#           ryanss <ryanssdev@icloud.com> (c) 2014-2017
#  Website: https://github.com/vacanza/holidays
#  License: MIT (see LICENSE file)

from datetime import date
from functools import cache

from holidays.calendars.gregorian import _timedelta

MO = 1954168.050623  # Beginning of 0 ME for MMT.
SY = 1577917828.0 / 4320000.0  # Solar year (365.2587565 days).


class _BurmeseLunisolar:
    """Burmese Lunisolar calendar.

    References:
        * [Algorithm, Program and Calculation of Myanmar Calendar](https://web.archive.org/web/20250510011425/http://cool-emerald.blogspot.com/2013/06/algorithm-program-and-calculation-of.html)
    """

    START_DATE = date(1939, 3, 20)  # 1 Late Tagu 1301 Burmese Era.
    START_YEAR = 1939  # 1301 Burmese Era.
    END_YEAR = 2100  # 1463 Burmese Era.

    LITTLE_WATAT_YEARS_GREGORIAN = {
        1939,
        1948,
        1955,
        1958,
        1966,
        1972,
        1974,
        1982,
        1988,
        1993,
        1999,
        2004,
        2012,
        2018,
        2020,
        2029,
        2034,
        2039,
        2045,
        2050,
        2056,
        2061,
        2069,
        2075,
        2077,
        2086,
        2091,
        2096,
    }

    BIG_WATAT_YEARS_GREGORIAN = {
        1942,
        1945,
        1950,
        1953,
        1961,
        1964,
        1969,
        1977,
        1980,
        1985,
        1991,
        1996,
        2001,
        2007,
        2010,
        2015,
        2023,
        2026,
        2031,
        2037,
        2042,
        2048,
        2053,
        2058,
        2064,
        2067,
        2072,
        2080,
        2083,
        2088,
        2094,
        2099,
    }

    @staticmethod
    def jdn_to_gregorian(jdn: int) -> date:
        """Convert Julian Day Number to Gregorian date.

        Args:
            jdn: Julian Day Number.

        Returns:
            Gregorian date.
        """
        j = jdn - 1721119
        y, j = divmod(4 * j - 1, 146097)
        d = j // 4
        j, d = divmod(4 * d + 3, 1461)
        d = (d + 4) // 4
        m, d = divmod(5 * d - 3, 153)
        d = (d + 5) // 5
        y = 100 * y + j
        if m < 10:
            m += 3
        else:
            m -= 9
            y += 1

        return date(y, m, d)

    @cache
    def _get_start_date(self, year: int) -> date | None:
        if year < self.START_YEAR or year > self.END_YEAR:
            return None

        delta_days = 354 * (year - self.START_YEAR)
        for iter_year in range(self.START_YEAR, year):
            if iter_year in self.LITTLE_WATAT_YEARS_GREGORIAN:
                delta_days += 30
            elif iter_year in self.BIG_WATAT_YEARS_GREGORIAN:
                delta_days += 31

        return _timedelta(self.START_DATE, delta_days)

    def thingyan_dates(self, year: int) -> tuple[date | None, date | None]:
        """Calculate key dates of Thingyan (Myanmar New Year festival) - Akya day
            and Atat day.

        Args:
            year:
                Gregorian year.

        Returns:
            Akya day date, Atat day date.

        """
        if year < self.START_YEAR or year > self.END_YEAR:
            return None, None

        ja = SY * (year - 638) + MO
        jk = (ja - 2.169918982) if year >= 1950 else (ja - 2.1675)

        return self.jdn_to_gregorian(round(jk)), self.jdn_to_gregorian(round(ja))

    def kason_full_moon_date(self, year: int) -> date | None:
        """Calculate the Gregorian date of Full Moon Day of Kason.

        15th day of 2nd month (Kason).

        To calculate, we use the following time delta:

        * All years:
            29 [1] + 15 - 1 = 43.

        Args:
            year:
                Gregorian year.

        Returns:
            Gregorian date of Full Moon Day of Kason.
            `None` if the Gregorian year is out of supported range.
        """
        if not (start_date := self._get_start_date(year)):
            return None

        return _timedelta(start_date, +43)

    def waso_full_moon_date(self, year: int) -> date | None:
        """Calculate the Gregorian date of Full Moon Day of Waso.

        15th day of 4th month (Waso).

        To calculate, we use the following time delta:

        * All years:
            Year_length - 266 [4-12] + 15 - 1 = Year_length - 252.

        Args:
            year:
                Gregorian year.

        Returns:
            Gregorian date of Full Moon Day of Waso.
            `None` if the Gregorian year is out of supported range.
        """
        if not (next_year_start_date := self._get_start_date(year + 1)):
            return None

        return _timedelta(next_year_start_date, -252)

    def thadingyut_full_moon_date(self, year: int) -> date | None:
        """Calculate the Gregorian date of Full Moon Day of Thadingyut.

        15th day of 7th month (Thadingyut).

        To calculate, we use the following time delta:

        * All years:
            Year_length - 177 [7-12] + 15 - 1 = Year_length - 163.

        Args:
            year:
                Gregorian year.

        Returns:
            Gregorian date of Full Moon Day of Thadingyut.
            `None` if the Gregorian year is out of supported range.
        """
        if not (next_year_start_date := self._get_start_date(year + 1)):
            return None

        return _timedelta(next_year_start_date, -163)

    def tazaungmon_waxing_moon_date(self, year: int) -> date | None:
        """Calculate the Gregorian date of 1st Waxing Day of Tazaungmon.

        1st day of 8th month (Tazaungmon).

        To calculate, we use the following time delta:

        * All years:
            Year_length - 148 [8-12] + 1 - 1 = Year_length - 148.

        Args:
            year:
                Gregorian year.

        Returns:
            Gregorian date of 1st Waxing Day of Tazaungmon.
            `None` if the Gregorian year is out of supported range.
        """
        if not (next_year_start_date := self._get_start_date(year + 1)):
            return None

        return _timedelta(next_year_start_date, -148)

    def tazaungmon_full_moon_date(self, year: int) -> date | None:
        """Calculate the Gregorian date of Full Moon Day of Tazaungmon.

        15th day of 8th month (Tazaungmon).

        To calculate, we use the following time delta:

        * All years:
            Year_length - 148 [8-12] + 15 - 1 = Year_length - 134.

        Args:
            year:
                Gregorian year.

        Returns:
            Gregorian date of Full Moon Day of Tazaungmon.
            `None` if the Gregorian year is out of supported range.
        """
        if not (next_year_start_date := self._get_start_date(year + 1)):
            return None

        return _timedelta(next_year_start_date, -134)

    def pyatho_waxing_moon_date(self, year: int) -> date | None:
        """Calculate the Gregorian date of 1st Waxing Day of Pyatho.

        1st day of 10th month (Pyatho).

        To calculate, we use the following time delta:

        * All years:
            Year_length - 89 [10-12] + 1 - 1 = Year_length - 89.

        Args:
            year:
                Gregorian year.

        Returns:
            Gregorian date of 1st Waxing Day of Pyatho.
            `None` if the Gregorian year is out of supported range.
        """
        if not (next_year_start_date := self._get_start_date(year + 1)):
            return None

        return _timedelta(next_year_start_date, -89)

    def tabaung_full_moon_date(self, year: int) -> date | None:
        """Calculate the Gregorian date of Full Moon Day of Tabaung.

        15th day of 12th month (Tabaung).

        To calculate, we use the following time delta:

        * All years:
            Year_length - 30 [12] + 15 - 1 = Year_length - 16.

        Args:
            year:
                Gregorian year.

        Returns:
            Gregorian date of Full Moon Day of Tabaung.
            `None` if the Gregorian year is out of supported range.
        """
        if not (next_year_start_date := self._get_start_date(year + 1)):
            return None

        return _timedelta(next_year_start_date, -16)