# 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
    yearday = wjd - to_jd(year, 1, 1)

    leap = isleap(year)

    if yearday < 58 + leap:
        leap_adj = 0
    elif leap:
        leap_adj = 1
    else:
        leap_adj = 2

    month = floor((((yearday + leap_adj) * 12) + 373) / 367)
    day = int(wjd - to_jd(year, month, 1)) + 1

    return (year, month, day)


def month_length(year, month):
    '''Calculate the length of a month in the Gregorian calendar'''
    return monthrange(year, month)[1]


def monthcalendar(year, month):
    '''
    Return a list of lists that describe the calender for one month. Each inner
    list have 7 items, one for each weekday, starting with Sunday. These items
    are either ``None`` or an integer, counting from 1 to the number of days in
    the month.
    For Gregorian, this is very similiar to the built-in :meth:``calendar.monthcalendar``.
    '''

# 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

    yearday = wjd - to_jd(year, 1, 1)

    leap = isleap(year)

    if yearday < 58 + leap:
        leap_adj = 0
    elif leap:
        leap_adj = 1
    else:
        leap_adj = 2

    month = floor((((yearday + leap_adj) * 12) + 373) / 367)
    day = int(wjd - to_jd(year, month, 1)) + 1

    return (year, month, day)


def month_length(year, month):
    '''Calculate the length of a month in the Gregorian calendar'''
    return monthrange(year, month)[1]


def monthcalendar(year, month):
    '''
    Return a list of lists that describe the calender for one month. Each inner
    list have 7 items, one for each weekday, starting with Sunday. These items
    are either ``None`` or an integer, counting from 1 to the number of days in
    the month.
    For Gregorian, this is very similiar to the built-in :meth:``calendar.monthcalendar``.
    '''

    start_weekday = jwday(to_jd(year, month, 1))