def to_jd(year, month, day): ## 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 ## the Julian Day number of the first day of the year and adding on the required number of months ## and days to get the final Julian Day number ## Calculate the jd of 1 Chaitra for this year and how many days are in Chaitra this year ## If a Leap Year, then 1 Chaitra == 21 March of the Gregorian year and Chaitra has 31 days ## If not a Leap Year, then 1 Chaitra == 22 March of the Gregorian year and Chaitra has 30 days ## Need to use dateToJulianDay() to calculate instead of setDate() to avoid the year 9999 validation if isLeap(year): jdFirstDayOfYear = gregorian.to_jd(year+78, 3, 21) daysInMonth1 = 31 else: jdFirstDayOfYear = gregorian.to_jd(year+78, 3, 22) daysInMonth1 = 30 ## Add onto the jd of the first day of the year the number of days required ## Calculate the number of days in the months before the required month ## Then add on the required days ## The first month has 30 or 31 days depending on if it is a Leap Year (determined above) ## The second to sixth months have 31 days each ## The seventh to twelth months have 30 days each ## Note: could be expressed more efficiently, but I think this is clearer if month==1: jd = jdFirstDayOfYear + day - 1 elif month<=6: jd = jdFirstDayOfYear + daysInMonth1 + (month-2)*31 + day - 1 else: ## month > 6 jd = jdFirstDayOfYear + daysInMonth1 + 5*31 + (month-7)*30 + day - 1 return jd
def to_jd(year, month, day): ## 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 ## the Julian Day number of the first day of the year and adding on the required number of months ## and days to get the final Julian Day number ## Calculate the jd of 1 Chaitra for this year and how many days are in Chaitra this year ## If a Leap Year, then 1 Chaitra == 21 March of the Gregorian year and Chaitra has 31 days ## If not a Leap Year, then 1 Chaitra == 22 March of the Gregorian year and Chaitra has 30 days ## Need to use dateToJulianDay() to calculate instead of setDate() to avoid the year 9999 validation if isLeap(year): jdFirstDayOfYear = gregorian.to_jd(year + 78, 3, 21) daysInMonth1 = 31 else: jdFirstDayOfYear = gregorian.to_jd(year + 78, 3, 22) daysInMonth1 = 30 ## Add onto the jd of the first day of the year the number of days required ## Calculate the number of days in the months before the required month ## Then add on the required days ## The first month has 30 or 31 days depending on if it is a Leap Year (determined above) ## The second to sixth months have 31 days each ## The seventh to twelth months have 30 days each ## Note: could be expressed more efficiently, but I think this is clearer if month == 1: jd = jdFirstDayOfYear + day - 1 elif month <= 6: jd = jdFirstDayOfYear + daysInMonth1 + (month - 2) * 31 + day - 1 else: ## month > 6 jd = jdFirstDayOfYear + daysInMonth1 + 5 * 31 + (month - 7) * 30 + day - 1 return jd
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 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 iso_to_jd(year, week, day): #assert week>0 and day>0 and day<=7 jd0 = gregorian.to_jd(year-1, 12, 28) return day + 7*week + jd0 - jd0%7 - 1
def iso_to_jd(year, week, day): #assert week>0 and day>0 and day<=7 jd0 = gregorian.to_jd(year - 1, 12, 28) return day + 7 * week + jd0 - jd0 % 7 - 1