def calculate_julian(year, month, day, hours=None, minutes=None, seconds=None):
    """
    Calculate the julian date value.
    :param year:    the year
    :param month:   the month
    :param day:     the day
    :param hours:   the hours
    :param minutes: the minutes
    :param seconds: the seconds
    :return:        the julian date
    """
    yr = mathutils.to_int(year)
    mth = mathutils.to_int(month)
    day = mathutils.to_int(day)
    hrs = mathutils.to_int(hours)
    mns = mathutils.to_int(minutes)
    scs = mathutils.to_float(seconds)
    if mth <= 2:
        yr -= 1
        mth += 12
    if yr >= 1582:
        A = yr / 100
        B = 2 - A + (A / 4)
    else:
        B = 0
    if yr < 0:
        C = int((365.25 * yr) - 0.75)
    else:
        C = int(365.25 * yr)
    D = int(30.6001 * (mth + 1))
    return 1720994.5 + B + C + D + day + (((
        (mathutils.fix(scs, TIME_SECONDS_PRECISION) / 60.0) + float(mns)) /
                                           60.0) + float(hrs)) / 24.0
def validate_date(year,
                  month,
                  day,
                  hours=None,
                  minutes=None,
                  seconds=None,
                  mode=None,
                  require_time=True):
    """
    Validate a date or date time.
    :return:    (year, month, day[, hours, minutes, seconds[, mode]]) or None
    """
    yr = mathutils.to_int(year, None)
    mth = mathutils.to_int(month, None)
    dy = mathutils.to_int(day, None)
    if yr is not None:
        if (mth is not None) and (mth >= 1) and (mth <= 12):
            if is_leap_year(yr) and (mth == 2):
                if dy > 29:
                    return None
            elif dy > LENGTH_OF_MONTH[mth]:
                return None
            elif dy < 1:
                return None
            if hours is not None:
                tm = validate_time(hours, minutes, seconds, mode)
                if tm is None:
                    return None
                if len(tm) == 4:
                    return yr, mth, dy, tm[0], tm[1], tm[2], tm[3]
                return yr, mth, dy, tm[0], tm[1], tm[2]
            elif require_time:
                return None
            return yr, mth, dy
    return None
def dd_from_dms(d, m, s):
    d = mathutils.to_int(d, None)
    m = mathutils.to_int(m, None)
    s = mathutils.to_float(s, None)
    if (d is not None) and (m is not None) and (s is not None):
        if d < 0:
            sgn = -1
        else:
            sgn = 1
        dd = sgn * ((((s / 60.0) + float(m)) / 60.0) + float(abs(d)))
        return dd
    return None
def to_day_of_week_from_julian(jd):
    """
    Converts a julian to day of the week.
    :param jd:  julian
    :return:    day of week
    """
    jul = mathutils.to_float(jd, None)
    if jul is not None:
        a = (jul + 1.5) / 7.0
        day = mathutils.to_int(round((a - mathutils.to_int(a)) * 7.0))
        return day
    return None
 def add_years(self, dy):
     """
     Add a year delta to the date.
     :param dy:  year delta
     """
     if self.year is not None:
         self.year += mathutils.to_int(dy, 0)
def get_date_of_easter(year):
    """
    Calculate the date of Easter.
    :param year:  year for which to calculate the date of Easter
    :return:      date tuple for Easter (None if not a valid year)(year, month, day)
    """
    y = mathutils.to_int(year)
    if y is not None:
        if y >= 1583:
            a = int(y % 19)
            b = int(y / 100)
            c = int(y % 100)
            d = int(b / 4)
            e = int(b % 4)
            f = int((b + 8) / 25)
            g = int((b - f + 1) / 3)
            h = int(((19 * a) + b - d - g + 15) % 30)
            i = int(c / 4)
            k = int(c % 4)
            l = int((32 + (2 * e) + (2 * i) - h - k) % 7)
            m = int((a + (11 * h) + (22 * l)) / 451)
            t = int((h + l - (7 * m) + 114))
            n = int(t / 31)
            p = int((t % 31) + 1)
            return y, n, p
    return None
def get_days_in_month(month, year=None):
    """
    Determine the number of days in the month.
    :param month: month to analyze (1..12)
    :param year:  year to use with month (None for basic analysis)
    :return:      number of days in the month
    """
    d = 0
    m = mathutils.to_int(month, None)
    if m is not None:
        d = LENGTH_OF_MONTH[m]
        y = mathutils.to_int(year, None)
        if (y is not None) and (m == 2):
            if is_leap_year(y):
                d += 1
    return d
 def set_with_epochTD(self, epochTD):
     self.set_year(mathutils.to_int(epochTD))
     self.set_month(1)
     self.set_day(1)
     self.set_hours(0)
     self.set_minutes(0)
     self.set_seconds(0.0)
     self.set_mode(TIME_MODE_TDT)
def dh_from_hms(hours, minutes, seconds):
    """
    Converts a time to decimal hours.
    :param hours:   hours
    :param minutes: minutes
    :param seconds: seconds
    :return:        dh
    """
    h = mathutils.to_int(hours, None)
    m = mathutils.to_int(minutes, None)
    s = mathutils.to_float(seconds, None)
    if (h is not None) and (m is not None) and (s is not None):
        if h < 0:
            sgn = -1
        else:
            sgn = 1
        return sgn * ((((s / 60.0) + m) / 60.0) + abs(h))
    return None
 def set_zone_correction(self, zc):
     """
     Set the time zone correction to be used in date calculations.
     :param zc:  the time zone correction to set (- for west, + for east)(-24..24)
     """
     zc = mathutils.to_int(zc, 0)
     if zc is not None:
         zc = mathutils.normalize_hours(zc, -24.0, 24.0)
     self.zone_correction = zc
def validate_time(hours, minutes, seconds, mode=None):
    """
    Validate the time.
    :return:    (hours, minutes, seconds[, mode]) or None
    """
    hrs = mathutils.to_int(hours, None)
    mns = mathutils.to_int(minutes, None)
    scs = mathutils.to_float(seconds, None)
    if (hrs is not None) and (mns is not None) and (scs is not None):
        if (hrs >= 0) and (hrs < 24):
            if (mns >= 0) and (mns < 60):
                if (scs >= 0) and (scs < 60):
                    if mode is not None:
                        if mode in TIME_MODES:
                            return hrs, mns, scs, mode
                    else:
                        return hrs, mns, scs
    return None
 def set_year(self, year):
     """
     Set the year.
     :param year:    the year to set
     """
     y = mathutils.to_int(year, None)
     if y is not None:
         self.year = y
     if self.day is not None:
         if self.day > self.get_days_in_month():
             self.day = self.get_days_in_month()
 def set_hours(self, h=0):
     """
     Set the hours.
     :param h:   the hours to set (0..23)
     """
     h = mathutils.to_int(h, 0)
     if h is not None:
         if h < 0:
             h = 0
         if h > 23:
             h = 23
     self.hours = h
def is_leap_year(year):
    """
    Determine if the year is a leap year.
    :param year:  year     
    :return:      True or False
    """
    y = mathutils.to_int(year, None)
    if y is not None:
        if (y % 100) == 0:
            return (y % 400) == 0
        return (y % 4) == 0
    return None
 def set_minutes(self, minutes=0):
     """
     Set the minutes.
     :param minutes:   the minutes to set (0..59)
     """
     m = mathutils.to_int(minutes, 0)
     if m is not None:
         if m < 0:
             m = 0
         if m > 59:
             m = 59
     self.minutes = m
 def set_day(self, day=1):
     """
     Set the day.
     :param day:     the day to set (1..31)
     """
     d = 1
     if day is not None:
         d = mathutils.to_int(day, 1)
         if d < 1:
             d = 1
         if d > self.get_days_in_month():
             d = self.get_days_in_month()
     self.day = d
 def add_days(self, dd):
     """
     Add a day delta to the date.
     :param dd:  day delta
     """
     if self.day is not None:
         self.day += mathutils.to_int(dd, 0)
         dm = 0
         while self.day < 1:
             self.day += get_days_in_month(self.month, self.year)
             dm -= 1
         while self.day > get_days_in_month(self.month, self.year):
             self.day -= get_days_in_month(self.month, self.year)
             dm += 1
         self.add_months(dm)
 def add_hours(self, dh):
     """
     Add an hour delta to the date.
     :param dh:  hour delta
     """
     if self.hours is not None:
         self.hours += mathutils.to_int(dh, 0)
         dd = 0
         while self.hours < 1:
             self.hours += 24
             dd -= 1
         while self.hours >= 24:
             self.hours -= 24
             dd += 1
         self.add_days(dd)
 def add_minutes(self, dm):
     """
     Add a minute delta to the date.
     :param dm:  minute delta
     """
     if self.minutes is not None:
         self.minutes += mathutils.to_int(dm, 0)
         dh = 0
         while self.minutes < 1:
             self.minutes += 24
             dh -= 1
         while self.minutes >= 24:
             self.minutes -= 24
             dh += 1
         self.add_hours(dh)
 def add_months(self, dm):
     """
     Add a month delta to the date.
     :param dm:  month delta
     """
     if self.month is not None:
         self.month += mathutils.to_int(dm, 0)
         dy = 0
         while self.month < 1:
             self.month += 12
             dy -= 1
         while self.month > 12:
             self.month -= 12
             dy += 1
         self.add_years(dy)
 def set_month(self, month=1):
     """
     Set the month.
     :param month:   the month to set (1..12)
     """
     m = mathutils.to_int(month, 1)
     if m is not None:
         if m < 1:
             m = 1
         if m > 12:
             m = 12
     self.month = m
     if self.day is not None:
         if self.day > self.get_days_in_month():
             self.day = self.get_days_in_month()
def __convert_date_time(res):
    """
    Convert the response array (usually from keyboard input) into a date time tuple.

    :param res: the response to convert
    :return:    an unvalidated date time tuple
    """
    dat = []
    i = 0
    while (i < len(res)) and (i < 7):
        if i < 5:
            dat.append(mathutils.to_int(res[i]))
        elif i == 5:
            dat.append(mathutils.fix(res[i], astrodate.TIME_SECONDS_PRECISION))
        else:
            dat.append(res[i])
        i += 1
    return dat
 def set_with_julian(self, jul, mode=TIME_MODE_UTC):
     """
     Set date from a julian day.
     :param jul:     julian
     :param mode:    the time mode (default: 'utc')
     """
     jul = mathutils.to_float(jul, None)
     if jul is not None:
         jd = 0.5 + jul
         I = mathutils.to_int(jd)
         F = jd - I
         if I > 2229160:
             A = mathutils.to_int((I - 1867216.25) / 36524.25)
             B = I + 1 + A - mathutils.to_int(A / 4.0)
         else:
             B = I
         C = B + 1524
         D = mathutils.to_int((C - 122.1) / 365.25)
         E = mathutils.to_int(365.25 * D)
         G = mathutils.to_int((C - E) / 30.6001)
         d = C - E + F - mathutils.to_int(30.6001 * G)
         if G < 13.5:
             mth = G - 1
         else:
             mth = G - 13
         if mth > 2.5:
             yr = D - 4716
         else:
             yr = D - 4715
         day = mathutils.to_int(d)
         h = (d - day) * 24
         hrs, mns, scs = hms_from_dh(h)
         self.set_year(yr)
         self.set_month(mth)
         self.set_day(day)
         self.set_hours(hrs)
         self.set_minutes(mns)
         self.set_seconds(scs)
         self.set_mode(mode)
 def set_min2(self, min2):
     m = 0
     if min2 is not None:
         m = mathutils.to_int(min2, 0)
     self.min2 = m
 def set_min1(self, min1):
     m = 0
     if min1 is not None:
         m = mathutils.to_int(min1, 0)
     self.min1 = m
 def set_deg2(self, deg2):
     d = 0
     if deg2 is not None:
         d = mathutils.to_int(deg2, 0)
     self.deg2 = d
 def set_deg1(self, deg1):
     d = 0
     if deg1 is not None:
         d = mathutils.to_int(deg1, 0)
     self.deg1 = d
def validate_hours(h, m, s):
    if (h is not None) and (h == mathutils.to_int(h)) and (h > -24.0) and (h <= 24.0):
        if (m is not None) and (m == mathutils.to_int(m)) and (m >= 0.0) and (m <= 59.0):
            if (s is not None) and (s == mathutils.to_float(s)) and (s >= 0.0) and (s < 60.0):
                return True
    return False
def validate_degrees(d, m, s):
    if (d is not None) and (d == mathutils.to_int(d)) and (d >= -360.0) and (d <= 360.0):
        if (m is not None) and (m == mathutils.to_int(m)) and (m >= 0.0) and (m <= 59.0):
            if (s is not None) and (s == mathutils.to_float(s)) and (s >= 0.0) and (s < 60.0):
                return True
    return False