def jd_to(jd): # The calendar is closely synchronized to the Gregorian Calendar, always # starting on the same day. We can use this and the regular sequence of # days in months to do a simple conversion by finding what day in the # Gregorian year the Julian Day number is, converting this to the day in # the Indian year and subtracting off the required number of months and # days to get the final date gregorianYear, gregorianMonth, gregorianDay = gregorian.jd_to(jd) jdGregorianFirstDayOfYear = gregorian.to_jd(gregorianYear, 1, 1) gregorianDayOfYear = jd - jdGregorianFirstDayOfYear + 1 # There is a fixed 78 year difference between year numbers, but the years # do not exactly match up, there is a fixed 80 day difference between the # first day of the year, if the Gregorian day of the year is 80 or less # then the equivalent Indian day actually falls in the preceding year if gregorianDayOfYear > 80: year = gregorianYear - 78 else: year = gregorianYear - 79 # If it is a leap year then the first month has 31 days, otherwise 30. if isLeap(year): daysInMonth1 = 31 else: daysInMonth1 = 30 # The Indian year always starts 80 days after the Gregorian year, # calculate the Indian day of the year, taking into account if it falls # into the previous Gregorian year if gregorianDayOfYear > 80: indianDayOfYear = gregorianDayOfYear - 80 else: indianDayOfYear = ( gregorianDayOfYear + daysInMonth1 + 5 * 31 + 6 * 30 - 80 ) # Then simply remove the whole months from the day of the year and you # are left with the day of month if indianDayOfYear <= daysInMonth1: month = 1 day = indianDayOfYear elif indianDayOfYear <= daysInMonth1 + 5 * 31: month = (indianDayOfYear - daysInMonth1 - 1) // 31 + 2 day = indianDayOfYear - daysInMonth1 - (month - 2) * 31 else: month = (indianDayOfYear - daysInMonth1 - 5 * 31 - 1) // 30 + 7 day = indianDayOfYear - daysInMonth1 - 5 * 31 - (month - 7) * 30 return (year, month, day)
def test_convert(self): startYear = 1950 endYear = 2050 for year in range(startYear, endYear): for month in range(1, 13): monthLen = gregorian.getMonthLen(year, month) for day in range(1, monthLen + 1): date = (year, month, day) jd = gregorian.to_jd(*date) ndate = gregorian.jd_to(jd) self.assertEqual( ndate, date, f"jd={jd}, date={date}, ndate={ndate}", )
def jd_to(jd): # The calendar is closely synchronized to the Gregorian Calendar, always # starting on the same day. We can use this and the regular sequence of # days in months to do a simple conversion by finding what day in the # Gregorian year the Julian Day number is, converting this to the day in # the Indian year and subtracting off the required number of months and # days to get the final date gregorianYear, gregorianMonth, gregorianDay = gregorian.jd_to(jd) jdGregorianFirstDayOfYear = gregorian.to_jd(gregorianYear, 1, 1) gregorianDayOfYear = jd - jdGregorianFirstDayOfYear + 1 # There is a fixed 78 year difference between year numbers, but the years # do not exactly match up, there is a fixed 80 day difference between the # first day of the year, if the Gregorian day of the year is 80 or less # then the equivalent Indian day actually falls in the preceding year if gregorianDayOfYear > 80: year = gregorianYear - 78 else: year = gregorianYear - 79 # If it is a leap year then the first month has 31 days, otherwise 30. if isLeap(year): daysInMonth1 = 31 else: daysInMonth1 = 30 # The Indian year always starts 80 days after the Gregorian year, # calculate the Indian day of the year, taking into account if it falls # into the previous Gregorian year if gregorianDayOfYear > 80: indianDayOfYear = gregorianDayOfYear - 80 else: indianDayOfYear = (gregorianDayOfYear + daysInMonth1 + 5 * 31 + 6 * 30 - 80) # Then simply remove the whole months from the day of the year and you # are left with the day of month if indianDayOfYear <= daysInMonth1: month = 1 day = indianDayOfYear elif indianDayOfYear <= daysInMonth1 + 5 * 31: month = (indianDayOfYear - daysInMonth1 - 1) // 31 + 2 day = indianDayOfYear - daysInMonth1 - (month - 2) * 31 else: month = (indianDayOfYear - daysInMonth1 - 5 * 31 - 1) // 30 + 7 day = indianDayOfYear - daysInMonth1 - 5 * 31 - (month - 7) * 30 return (year, month, day)
def isow(jd): ## iso week number year = gregorian.jd_to(jd - 3)[0] if jd >= iso_to_jd(year + 1, 1, 1): year += 1 return (jd - iso_to_jd(year, 1, 1)) // 7 + 1
def isow_year(jd): ## iso week year year = gregorian.jd_to(jd - 3)[0] if jd >= iso_to_jd(year + 1, 1, 1): year += 1 return year
def isow(jd):## iso week number year = gregorian.jd_to(jd - 3)[0] if jd>=iso_to_jd(year+1, 1, 1): year += 1 return (jd - iso_to_jd(year, 1, 1)) // 7 + 1
def isow_year(jd):## iso week year year = gregorian.jd_to(jd - 3)[0] if jd>=iso_to_jd(year+1, 1, 1): year += 1 return year