File: mandaean.py

package info (click to toggle)
python-holidays 0.86-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 57,296 kB
  • sloc: python: 117,830; javascript: 85; makefile: 59
file content (58 lines) | stat: -rw-r--r-- 1,950 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
#  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 holidays.calendars.gregorian import _timedelta


class _Mandaean:
    """
    Mandaean calendar for 1901-2100 years.
    https://en.wikipedia.org/wiki/Mandaean_calendar
    """

    # Begin of 445271 Mandaean year.
    START_DATE = date(1901, 8, 16)
    START_YEAR = 1901
    END_YEAR = 2100

    def new_year_date(self, year: int) -> date | None:
        """
        Return Gregorian date of Mandaean new year (1 Dowla) in a given Gregorian year.
        """
        if year < _Mandaean.START_YEAR or year > _Mandaean.END_YEAR:
            return None

        return _timedelta(_Mandaean.START_DATE, 365 * (year - _Mandaean.START_YEAR))

    def mandaean_to_gregorian(self, year: int, month: int, day: int) -> date | None:
        """
        Return Gregorian date of Mandaean day and month of the year that begins in a given
        Gregorian year.
        Extra 5 days inserted after 8th month are considered as 13th month.
        """
        start_date = self.new_year_date(year)
        if not start_date:
            return None

        if not (1 <= month <= 13) or not (1 <= day <= 30) or (month == 13 and not (1 <= day <= 5)):
            return None

        if month < 9:
            delta = 30 * (month - 1)
        elif month == 13:
            delta = 30 * 8
        else:
            delta = 30 * (month - 1) + 5

        return _timedelta(start_date, delta + day - 1)