Example #1
0
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
Example #2
0
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
Example #3
0
def iso_to_jd(year, week, day):
	"""
	Return Julian day of given ISO year, week, and 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
Example #4
0
	def test_to_jd(self):
		for date, jd in self.dateToJdDict.items():
			jdActual = gregorian.to_jd(*date)
			self.assertEqual(
				jdActual,
				jd,
				f"date={date}, jd={jd}, jdActual={jdActual}",
			)
Example #5
0
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)
Example #6
0
	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}",
					)
Example #7
0
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)
Example #8
0
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
Example #9
0
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