# Path: src/convertdate/utils.py
# def floor(x):
#     return int(math.floor(x))
# 
# def jwday(j):
#     '''Calculate day of week from Julian day. Consider using ``calendar.weekday``!'''
#     return math.trunc((j + 0.5)) % 7
# 
# def monthcalendarhelper(start_weekday, month_length):
#     end_weekday = start_weekday + (month_length - 1) % 7
# 
#     lpad = (start_weekday + 1) % 7
#     rpad = (5 - end_weekday % 7) % 6
# 
#     days = [None] * lpad + list(range(1, 1 + month_length)) + rpad * [None]
# 
#     return [days[i : i + 7] for i in range(0, len(days), 7)]


from calendar import isleap, monthrange
from datetime import date
from .utils import floor, jwday, monthcalendarhelper
def to_jd(year, month, day):
    '''Convert gregorian date to julian day count.'''
    legal_date(year, month, day)

    if month <= 2:
        leap_adj = 0
    elif isleap(year):
        leap_adj = -1
    else:
        leap_adj = -2

    return (
        EPOCH
        - 1
        + (YEAR_DAYS * (year - 1))
        + floor((year - 1) / LEAP_CYCLE_YEARS)
        + (-floor((year - 1) / LEAP_SUPPRESSION_YEARS))
        + floor((year - 1) / INTERCALATION_CYCLE_YEARS)
        + floor((((367 * month) - 362) / 12) + leap_adj + day)
    )


def from_jd(jd):
    '''Return Gregorian date in a (Y, M, D) tuple'''
    wjd = floor(jd - 0.5) + 0.5
    depoch = wjd - EPOCH

    quadricent = floor(depoch / INTERCALATION_CYCLE_DAYS)
    dqc = depoch % INTERCALATION_CYCLE_DAYS


# Path: src/convertdate/utils.py
# def floor(x):
#     return int(math.floor(x))
# 
# def jwday(j):
#     '''Calculate day of week from Julian day. Consider using ``calendar.weekday``!'''
#     return math.trunc((j + 0.5)) % 7
# 
# def monthcalendarhelper(start_weekday, month_length):
#     end_weekday = start_weekday + (month_length - 1) % 7
# 
#     lpad = (start_weekday + 1) % 7
#     rpad = (5 - end_weekday % 7) % 6
# 
#     days = [None] * lpad + list(range(1, 1 + month_length)) + rpad * [None]
# 
#     return [days[i : i + 7] for i in range(0, len(days), 7)]


# Path: src/convertdate/gregorian.py
from calendar import isleap, monthrange
from datetime import date
from .utils import floor, jwday, monthcalendarhelper

def to_jd(year, month, day):
    '''Convert gregorian date to julian day count.'''
    legal_date(year, month, day)

    if month <= 2:
        leap_adj = 0
    elif isleap(year):
        leap_adj = -1
    else:
        leap_adj = -2

    return (
        EPOCH
        - 1
        + (YEAR_DAYS * (year - 1))
        + floor((year - 1) / LEAP_CYCLE_YEARS)
        + (-floor((year - 1) / LEAP_SUPPRESSION_YEARS))
        + floor((year - 1) / INTERCALATION_CYCLE_YEARS)
        + floor((((367 * month) - 362) / 12) + leap_adj + day)
    )


def from_jd(jd):
    '''Return Gregorian date in a (Y, M, D) tuple'''
    wjd = floor(jd - 0.5) + 0.5
    depoch = wjd - EPOCH

    quadricent = floor(depoch / INTERCALATION_CYCLE_DAYS)
    dqc = depoch % INTERCALATION_CYCLE_DAYS


    cent = floor(dqc / LEAP_SUPPRESSION_DAYS)