For a given haab month and a julian day count, find the next start of that month on or after the JDC
501725
def next_haab(month, jd):
    ''''''
    if jd < EPOCH:
        raise IndexError("Input day is before Mayan epoch.")

    hday, hmonth = to_haab(jd)

    if hmonth == month:
        days = 1 - hday

    else:
        count1 = _haab_count(hday, hmonth)
        count2 = _haab_count(1, month)

        # Find number of days between haab of given jd and desired haab
        days = (count2 - count1) % 365

    # add in the number of days and return new jd
    return jd + days
784097
def to_jd2(year, month, day):
    ''''''
    # http://quasar.as.utexas.edu/BillInfo/JulianDatesG.html

    legal_date(year, month, day)

    if month <= 2:
        year = year - 1
        month = month + 12

    a = floor(year / 100)
    b = floor(a / 4)
    c = 2 - a + b
    e = floor(365.25 * (year + 4716))
    f = floor(30.6001 * (month + 1))
    return c + day + e + f - 1524.5
81517
@Deprecated
    protected int handleComputeMonthStart(int eyear, int month, boolean useMonth) {
        // If the month is out of range, adjust it into range, and
        // modify the extended year value accordingly.
        if (month < 0 || month > 11) {
            int[] rem = new int[1];
            eyear += floorDivide(month, 12, rem);
            month = rem[0];
        }

        int julianDay = PERSIAN_EPOCH - 1 + 365 * (eyear - 1) + floorDivide(8 * eyear + 21, 33);
        if (month != 0) {
            julianDay += MONTH_COUNT[month][2];
        }
        return julianDay;
    }
281842
def julian_day(t: date) -> int:
    """"""
    # Compute the number of days from January 1, 2000 to date t
    dt = t - julian_base_date
    # Add the julian base number to the number of days from the julian base date to date t
    return julian_base_number + dt.days
788618
def to_jd(year, month, day):
    ''''''

    legal_date(year, month, day)

    # Algorithm as given in Meeus, Astronomical Algorithms, Chapter 7, page 61

    if month <= 2:
        year -= 1
        month += 12

    return (trunc((365.25 * (year + 4716))) + trunc((30.6001 * (month + 1))) + day) - 1524.5
1006345
def from_jd(jd):
    ''''''

    start = 80
    # Day offset between Saka and Gregorian

    jd = trunc(jd) + 0.5
    greg = gregorian.from_jd(jd)  # Gregorian date for Julian day
    leap = isleap(greg[0])  # Is this a leap year?
    # Tentative year in Saka era
    year = greg[0] - SAKA_EPOCH
    # JD at start of Gregorian year
    greg0 = gregorian.to_jd(greg[0], 1, 1)
    yday = jd - greg0  # Day number (0 based) in Gregorian year

    if leap:
        Caitra = 31  # Days in Caitra this year
    else:
        Caitra = 30

    if yday < start:
        # Day is at the end of the preceding Saka year
        year -= 1
        yday += Caitra + (31 * 5) + (30 * 3) + 10 + start

    yday -= start
    if yday < Caitra:
        month = 1
        day = yday + 1
    else:
        mday = yday - Caitra
        if (mday < (31 * 5)):
            month = trunc(mday / 31) + 2
            day = (mday % 31) + 1
        else:
            mday -= 31 * 5
            month = trunc(mday / 30) + 7
            day = (mday % 30) + 1

    return (year, month, int(day))
546782
function(jd) {
			// Jean Meeus algorithm, "Astronomical Algorithms", 1991
			var z = Math.floor(jd + 0.5);
			var a = Math.floor((z - 1867216.25) / 36524.25);
			a = z + 1 + a - Math.floor(a / 4);
			var b = a + 1524;
			var c = Math.floor((b - 122.1) / 365.25);
			var d = Math.floor(365.25 * c);
			var e = Math.floor((b - d) / 30.6001);
			var day = b - d - Math.floor(e * 30.6001);
			var month = e - (e > 13.5 ? 13 : 1);
			var year = c - (month > 2.5 ? 4716 : 4715);
			if (year <= 0) { year--; } // No year zero
			return this.newDate(year, month, day);
		}
576209
function d2j(jdn) {
    var gy = d2g(jdn).gy // Calculate Gregorian year (gy).
        , jy = gy - 621
        , r = jalCal(jy)
        , jdn1f = g2d(gy, 3, r.march)
        , jd
        , jm
        , k;

    // Find number of days that passed since 1 Farvardin.
    k = jdn - jdn1f;
    if (k >= 0) {
        if (k <= 185) {
            // The first 6 months.
            jm = 1 + div(k, 31);
            jd = mod(k, 31) + 1;
            return  { jy: jy
                , jm: jm
                , jd: jd
            };
        } else {
            // The remaining months.
            k -= 186;
        }
    } else {
        // Previous Jalali year.
        jy -= 1;
        k += 179;
        if (r.leap === 1)
            k += 1;
    }
    jm = 7 + div(k, 30);
    jd = mod(k, 30) + 1;
    return  { jy: jy
        , jm: jm
        , jd: jd
    };
}
683731
private static int getMonthDays(int month, int year) {
        Integer[] newMonths = getAdjustedMonthDays(year);
        return newMonths[month].intValue();
    }
708690
def daterange_start(value):
    ''''''
    if not value:
        return None
    elif isinstance(value, datetime):
        return value.date()
    elif isinstance(value, date):
        return value

    result = parse_dt(value).date()
    dashes = value.count('-')

    if dashes >= 2:
        return result
    elif dashes == 1:
        # Year/Month only
        return result.replace(day=1)
    else:
        # Year only
        return result.replace(day=1, month=1)
1136619
public static void jdToCE(int julianDay, int jdEpochOffset, int[] fields) {
        int c4; // number of 4 year cycle (1461 days)
        int[] r4 = new int[1]; // remainder of 4 year cycle, always positive

        c4 = floorDivide(julianDay - jdEpochOffset, 1461, r4);

        // exteded year
        fields[0] = 4 * c4 + (r4[0]/365 - r4[0]/1460); // 4 * <number of 4year cycle> + <years within the last cycle>

        int doy = (r4[0] == 1460) ? 365 : (r4[0] % 365); // days in present year

        // month
        fields[1] = doy / 30; // 30 -> Coptic/Ethiopic month length up to 12th month
        // day
        fields[2] = (doy % 30) + 1; // 1-based days in a month
    }
1134531
static int countInPeriod(DayOfWeek dow, DayOfWeek dow0, int nDays) {
		//two cases:
		//   (1a) dow >= dow0: count === (nDays - (dow - dow0)) / 7
		//   (1b) dow < dow0:  count === (nDays - (7 - dow0 - dow)) / 7
		if (dow.getCalendarConstant() >= dow0.getCalendarConstant()) {
			return 1 + ((nDays - (dow.getCalendarConstant() - dow0.getCalendarConstant()) - 1) / 7);
		} else {
			return 1 + ((nDays - (7 - (dow0.getCalendarConstant() - dow.getCalendarConstant())) - 1) / 7);
		}
	}
723449
def date_range(start_date, end_date, increment, period):
    
    next = start_date
    delta = relativedelta.relativedelta(**{period:increment})
    while next <= end_date:
        yield next
        next += delta
1016882
function(jd) {
			jd = Math.floor(jd) + 0.5;
			var year = Math.floor(((jd - this.jdEpoch) * 98496.0) / 35975351.0) - 1;
			while (jd >= this.toJD((year === -1 ? +1 : year + 1), 7, 1)) {
				year++;
			}
			var month = (jd < this.toJD(year, 1, 1)) ? 7 : 1;
			while (jd > this.toJD(year, month, this.daysInMonth(year, month))) {
				month++;
			}
			var day = jd - this.toJD(year, month, 1) + 1;
			return this.newDate(year, month, day);
		}
1186226
def end_of_month(self) -> datetime:
        
        # Increase month by 1,
        result = self.value + relativedelta(months=1)
        # take the 1st day of the (next) month,
        result = result.replace(day=1)
        # subtract one day
        result = result - relativedelta(days=1)
        self.value = result
        return self.value
781916
public function jdToYmd($julian_day)
    {
        // Find the year, by adding one month at a time to use up the remaining days.
        $year  = $this->jdToY($julian_day);
        $month = 1;
        $day   = $julian_day - $this->yToJd($year) + 1;

        while ($day > $this->daysInMonth($year, $month)) {
            $day -= $this->daysInMonth($year, $month);
            $month++;
        }

        // PHP 5.4 and earlier converted non leap-year Adar into month 6, instead of month 7.
        $month -= ($month === 7 && $this->options[self::EMULATE_BUG_54254] && !$this->isLeapYear($year)) ? 1 : 0;

        return array($year, $month, $day);
    }
62093
def next_day(self):
        """"""
        return HDate(self.gdate + datetime.timedelta(1), self.diaspora,
                     self.hebrew)