def iteryeardays2_Hebrew(self, year, cal_type): """ Like iteryeardates(), but will yield (day number, weekday number) tuples. For days outside the specified month the day number is 0. """ for date in self.iteryeardates(year): if date.year != year: fd = pcc.fixed_from_gregorian( [date.year, date.month, date.day]) d = pcc.hebrew_from_fixed(fd) day_value = pcc.standard_day(d) month_value = pcc.standard_month(d) year_value = pcc.standard_year(d) yield [(0, date.weekday()), (year_value, month_value)] else: fd = pcc.fixed_from_gregorian( [date.year, date.month, date.day]) if cal_type == "hebrew": d = pcc.hebrew_from_fixed(fd) day_value = pcc.standard_day(d) month_value = pcc.standard_month(d) year_value = pcc.standard_year(d) elif cal_type == "chinese": d = pcc.chinese_from_fixed(fd) day_value = pcc.chinese_day(d) month_value = pcc.chinese_month(d) else: print("type %s not implemented" % cal_type) return yield [(day_value, date.weekday()), (year_value, month_value)]
def main(year=2020, start=None): year = int(year) cal = candybar.LaTeXCandyBar(year) new_moons = cal.new_moons formatted_weeks = [] for w in cal.weeks["gregorian"]: output = "" if "new_moon" in w[0]: i = w[0]["new_moon_fixed"] moments = pcc.clock_from_moment(new_moons[i][2])[0:2] ts = ":".join(str(t) for t in moments) output += r"{}".format(ts) else: output += "" formatted_weeks.append(output) lunar_template_text = r"""\begin{tabular}{c} {% for w in weeks -%} {{ w }} \\ {%- endfor %} \end{tabular}""" lunar_template = Template(lunar_template_text) lunar_tab = lunar_template.render(weeks=formatted_weeks) # Hebrew calendar year hstart = pcc.standard_year( pcc.hebrew_from_fixed(pcc.fixed_from_gregorian([year, 1, 1]))) hend = pcc.standard_year( pcc.hebrew_from_fixed(pcc.fixed_from_gregorian([year, 12, 1]))) # Islamic calendar year istart = pcc.standard_year( pcc.islamic_from_fixed(pcc.fixed_from_gregorian([year, 1, 1]))) iend = pcc.standard_year( pcc.islamic_from_fixed(pcc.fixed_from_gregorian([year, 12, 1]))) year_display = r"{}& Phases & {}/{}& {}/{}&{}".format( year, hstart, hend, istart, iend, year) output = template.render( year_display=year_display, gregorian_data=cal.prweeks(cal.weeks["gregorian"], new_moons), lunar_data=lunar_tab, hebrew_data=cal.prweeks(cal.weeks["hebrew"], new_moons), islamic_data=cal.prweeks(cal.weeks["islamic"], new_moons), chinese_data=cal.prweeks(cal.weeks["chinese"], new_moons), ) outfile = "output/cal_" + str(year) + ".tex" with open(outfile, "w") as fp: fp.write(output)
def __init__(self, mdate, today=None): """ Initialize a date in lunar calendar with the given datetime.date object. :param mdate: the given datetime.date object. :param today: default is the current date (today), if not specified. :return: """ self._date = mdate if not today: self._today = date.today() else: # Useful when verifying functionality when running on a particular date. self._today = today self._chinese_date = self.lunar_from_regular(self._date) fixed_date = pycal.fixed_from_gregorian( (self._date.year, self._date.month, self._date.day)) self._chinese_date = pycal.chinese_from_fixed(fixed_date) cycle, year, month, leap_month, day = self._chinese_date self._year = self.normalize_lunar_year(cycle, year) self._month = month self._day = day # chinese_new_year_on_or_before does not work self._year_start = self.regular_from_lunar( (cycle, year, 1, leap_month, 1)) self._year_end = self.regular_from_lunar( (cycle, year + 1, 1, leap_month, 1)) - timedelta(1)
def new_moons_in_year(self, year): new_moon_tuple = namedtuple( "new_moon_tuple", "moon_since_1_1, moon_gregorian_date, moon_fixed_day, moon_sidereal_longitude", ) fixed_date = pcc.fixed_from_gregorian([year, 1, 1]) fudge_factor = 3 no_moons = self.many_moons(fixed_date) moon_rng = range( no_moons - 12 - (4 * self._wks_before + fudge_factor), no_moons + 13 + (4 * self._wks_after + fudge_factor), ) new_moons_data = [(n, pcc.nth_new_moon(n)) for n in moon_rng] new_moons = {} for n, nnm in new_moons_data: nm = pcc.gregorian_from_fixed(nnm) key = int(nnm) new_moons[key] = new_moon_tuple( moon_since_1_1=n, moon_gregorian_date=nm, moon_fixed_day=nnm, moon_sidereal_longitude=pcc.sidereal_lunar_longitude(nnm), ) return new_moons
def __init__(self, mdate, today=None): """ Initialize a date in lunar calendar with the given datetime.date object. :param mdate: the given datetime.date object. :param today: default is the current date (today), if not specified. :return: """ self._date = mdate if not today: self._today = date.today() else: # Useful when verifying functionality when running on a particular date. self._today = today self._chinese_date = self.lunar_from_regular(self._date) fixed_date = pycal.fixed_from_gregorian((self._date.year, self._date.month, self._date.day)) self._chinese_date = pycal.chinese_from_fixed(fixed_date) cycle, year, month, leap_month, day = self._chinese_date self._year = self.normalize_lunar_year(cycle, year) self._month = month self._day = day # chinese_new_year_on_or_before does not work self._year_start = self.regular_from_lunar((cycle, year, 1, leap_month, 1)) self._year_end = self.regular_from_lunar((cycle, year + 1, 1, leap_month, 1)) - timedelta(1)
def lunar_from_regular(self, rdate): """Get lunar date from regular date. :param rdate: Python datetime module's date class. :return: cdate: a tuple of format (cycle, offset, month, leap, day) defined by PyCalCal. """ fixed_date = pycal.fixed_from_gregorian((rdate.year, rdate.month, rdate.day)) chinese_date = pycal.chinese_from_fixed(fixed_date) return chinese_date
def add_anniv(db, ID_en, ID_cn, birth, ccal, gdate): date = pcc.fixed_from_gregorian((gdate.year, gdate.month, gdate.day)) c_date = CDate_from_fixed(date) crc = crc_dates(gdate, c_date.month, c_date.day) db.execute( 'insert into Anniv values (' '?, ?, ?, ?, ' '?, ?, ?, ?)', (ID_en, ID_cn, birth, ccal, gdate, c_date.month, c_date.day, crc)) db.commit()
def lunar_from_regular(self, rdate): """Get lunar date from regular date. :param rdate: Python datetime module's date class. :return: cdate: a tuple of format (cycle, offset, month, leap, day) defined by PyCalCal. """ fixed_date = pycal.fixed_from_gregorian( (rdate.year, rdate.month, rdate.day)) chinese_date = pycal.chinese_from_fixed(fixed_date) return chinese_date
def iterHebrewYearDates(self, year): """ Return an iterator for one year. The iterator will yield Hebrew dates corresponding to the gregorian year and will always iterate through complete weeks, so it will yield dates outside the specified year. """ date = datetime.date(year, 1, 1) # Go back to the beginning of the week days = (date.weekday() - self.firstweekday) % 7 date -= datetime.timedelta(days=days) oneday = datetime.timedelta(days=1) while True: yield pcc.hebrew_from_fixed( pcc.fixed_from_gregorian([date.year, date.month, date.day])) date += oneday if date.year != year and date.weekday() == self.firstweekday: break
def print_month(year, month, days, lang='en', enc='ascii', last_c_date=None, ext={}, f=sys.stdout): if lang == 'en': solterms = _en_solterms daynames = _en_daynames stems = _en_stems branches = _en_branches miscchar = _en_miscchar miscterm = _en_miscterm monhdfmt0 = '%(monname)s %(year)d (Year %(stem)s%(branch)s, ' + \ 'Month %(leap)s%(month)d%(length)s)' monhdfmt1 = '%(monname)s %(year)d (Year %(stem)s%(branch)s, ' + \ 'Month %(leap)s%(month)d%(length)s S%(day)d)' monhdfmt2 = '%(monname)s %(year)d (Year %(stem)s%(branch)s, ' + \ 'Month %(leap)s%(month)d%(length)s S%(day)d, ' + \ '%(aleap)s%(amonth)d%(alength)s S%(aday)d)' monhdfmt3 = '%(monname)s %(year)d (Year %(stem)s%(branch)s, ' + \ 'Month %(leap)s%(month)d%(length)s S%(day)d, ' + \ 'Year %(astem)s%(abranch)s, Month ' + \ '%(aleap)s%(amonth)d%(alength)s S%(aday)d)' dayowfmt = '%-10s' anniv_birth_fmt = _en_anniv_birth_fmt anniv_death_fmt = _en_anniv_death_fmt else: solterms = _chs_solterms daynames = _chs_daynames stems = _chs_stems branches = _chs_branches miscchar = _chs_miscchar miscterm = _chs_miscterm monhdfmt0 = u'%(monname)s %(year)d %(stem)s%(branch)s年' + \ u'%(leap)s%(month)s月%(length)s' monhdfmt1 = u'%(monname)s %(year)d %(stem)s%(branch)s年' + \ u'%(leap)s%(month)s月%(length)s%(day)d日始' monhdfmt2 = u'%(monname)s %(year)d %(stem)s%(branch)s年' + \ u'%(leap)s%(month)s月%(length)s%(day)d日始,' + \ u'%(aleap)s%(amonth)s月%(alength)s%(aday)d日始' monhdfmt3 = u'%(monname)s %(year)d %(stem)s%(branch)s年' + \ u'%(leap)s%(month)s月%(length)s%(day)d日始,' + \ u'%(astem)s%(abranch)s年' + \ u'%(aleap)s%(amonth)s月%(alength)s%(aday)d日始' dayowfmt = '%s ' anniv_birth_fmt = _chs_anniv_birth_fmt anniv_death_fmt = _chs_anniv_death_fmt date = pcc.fixed_from_gregorian((year, month, 1)) new_moon_date = pcc.chinese_new_moon_on_or_after(date) next_new_moon_date = pcc.chinese_new_moon_on_or_after(new_moon_date + 29) last_date = date + days - 1 #c_date = CDate_from_fixed(date) #c_new_moon_date = CDate_from_fixed(new_moon_date) #c_last_date = CDate_from_fixed(last_date) minor_solterm_date = pcc.minor_solar_term_on_or_after(date) major_solterm_date = pcc.major_solar_term_on_or_after(date) minor_solterm_date, major_solterm_date = map( pcc.fixed_from_moment, (minor_solterm_date, major_solterm_date)) if not last_c_date: c_date = CDate_from_fixed(date) else: if last_c_date.day < 29 or (last_c_date.day == 29 and date != new_moon_date): c_date = CDate(last_c_date.offset, last_c_date.offset, last_c_date.month, last_c_date.leap, last_c_date.day + 1) else: assert date == new_moon_date if last_c_date.month == 12: if last_c_date.offset == 60: c_date = CDate(last_c_date.cycle + 1, 1, 1, False, 1) else: c_date = CDate(last_c_date.cycle, last_c_date.offset + 1, 1, False, 1) else: c_date = CDate(last_c_date.cycle, last_c_date.offset, last_c_date.month + 1, False, 1) if new_moon_date == date: c_new_moon_date = c_date elif (new_moon_date <= major_solterm_date or new_moon_date + 5 > last_date): # next major solterm 19~24-29~30 if c_date.month == 12: if c_date.offset == 60: c_new_moon_date = CDate(c_date.cycle + 1, 1, 1, False, 1) else: c_new_moon_date = CDate(c_date.cycle, c_date.offset + 1, 1, False, 1) else: c_new_moon_date = CDate(c_date.cycle, c_date.offset, c_date.month + 1, False, 1) else: c_new_moon_date = CDate_from_fixed(new_moon_date) if last_date >= new_moon_date: if last_date < next_new_moon_date: c_last_date = CDate( c_new_moon_date.cycle, c_new_moon_date.offset, c_new_moon_date.month, c_new_moon_date.leap, c_new_moon_date.day + last_date - new_moon_date) else: c_last_date = CDate_from_fixed(last_date) else: c_last_date = CDate(c_date.cycle, c_date.offset, c_date.month, c_date.leap, c_date.day + last_date - date) if new_moon_date <= last_date: if (c_new_moon_date.month, c_new_moon_date.leap) != (c_last_date.month, c_last_date.leap): assert last_date - c_last_date.day + 1 == next_new_moon_date next_next_nmd = pcc.chinese_new_moon_on_or_after( next_new_moon_date + 29) if month == 1: assert c_new_moon_date.offset != c_last_date.offset monhdfmt = monhdfmt3 else: assert c_new_moon_date.offset == c_last_date.offset monhdfmt = monhdfmt2 monthhead = monhdfmt % dict( monname=_monnames[month - 1], year=year, stem=stems[(c_new_moon_date.offset - 1) % 10], branch=branches[(c_new_moon_date.offset - 1) % 12], month=month_name(c_new_moon_date, lang, miscchar), leap=c_new_moon_date.leap and miscchar[13] or '', length=miscchar[15 + int(next_new_moon_date - new_moon_date == 29)], day=new_moon_date - date + 1, astem=stems[(c_last_date.offset - 1) % 10], abranch=branches[(c_last_date.offset - 1) % 12], amonth=month_name(c_last_date, lang, miscchar), aleap=c_last_date.leap and miscchar[13] or '', alength=miscchar[15 + int(next_next_nmd - next_new_moon_date == 29)], aday=next_new_moon_date - date + 1, ) else: monthhead = monhdfmt1 % dict( monname=_monnames[month - 1], year=year, stem=stems[(c_new_moon_date.offset - 1) % 10], branch=branches[(c_new_moon_date.offset - 1) % 12], month=month_name(c_new_moon_date, lang, miscchar), leap=c_new_moon_date.leap and miscchar[13] or '', length=miscchar[15 + int(next_new_moon_date - new_moon_date == 29)], day=new_moon_date - date + 1, ) else: #last_new_moon_date = pcc.chinese_new_moon_before(date) last_new_moon_date = date - c_date.day + 1 assert month == 2 monthhead = monhdfmt0 % dict( monname=_monnames[month - 1], year=year, stem=stems[(c_date.offset - 1) % 10], branch=branches[(c_date.offset - 1) % 12], month=month_name(c_date, lang, miscchar), leap=c_date.leap and miscchar[13] or '', length=miscchar[15 + int(new_moon_date - last_new_moon_date == 29)], ) headlen = len(monthhead) + len(filter(lambda c: ord(c) > 0xFF, monthhead)) def println(s): print >> f, s.encode(enc) println(' ' * max((68 - headlen) / 2, 0) + monthhead) println(''.join([dayowfmt % (daynames[i], ) for i in xrange(7)])) dofw = pcc.day_of_week_from_fixed(date) if dofw > 4 and days == 31 or dofw > 5 and days == 30: weeks = 6 else: weeks = 5 dcnt, ldcnt = 1, c_date.day cmonth = c_date.month sameday = False show = ext.get('show') if show: stem, branch = pcc.chinese_day_name(date) stem -= 1 branch -= 1 anniv = ext.get('anniv') for w in xrange(weeks): ar = [] br = [] for i in xrange(7): if dcnt > days: break if w == 0 and i < dofw: ar.append(' ') br.append(' ') continue ar.append('%2d' % (dcnt, )) if not sameday and (date != minor_solterm_date and date != major_solterm_date and date != new_moon_date): ar.append(' %s ' % (day_name(ldcnt, lang, miscchar))) elif sameday or (date != minor_solterm_date and date != major_solterm_date and date == new_moon_date): s = month_name(c_new_moon_date, lang, miscchar) cmonth = c_new_moon_date.month if type(s) == int: ar.append( ' [%2d]Y%s ' % (s, c_new_moon_date.leap and miscchar[13] or ' ')) else: assert s if c_new_moon_date.leap: s = miscchar[13] + s s += miscchar[14] if len(s) == 2: ar.append(' %s ' % (s, )) elif len(s) == 3: ar.append(' %s ' % (s, )) else: ar.append(s) if sameday: sameday = False elif next_new_moon_date <= last_date: new_moon_date = next_new_moon_date c_new_moon_date = c_last_date ldcnt = 1 else: ldcnt = 1 else: if date == new_moon_date: cmonth = c_new_moon_date.month sameday = True ldcnt = 1 if next_new_moon_date <= last_date: new_moon_date = next_new_moon_date c_new_moon_date = c_last_date n = (month - 1) * 2 if date == major_solterm_date: n += 1 ar.append(' %s ' % (solterms[n], )) if show: s = stems[stem] + branches[branch] cr = get_anniv_on(anniv, year, month, dcnt, cmonth, ldcnt) if cr: if cr[0]: if len(cr[0]) > 1: cr[0] = anniv_birth_fmt % { 'ID': '', 'mul': 'x%d' % (len(cr[0]), ) } else: cr[0] = anniv_birth_fmt % { 'ID': cr[0][0][int(lang != 'en')], 'mul': '' } else: cr[0] = '' if cr[1]: if len(cr[1]) > 1: cr[1] = anniv_death_fmt % { 'ID': '', 'mul': 'x%d' % (len(cr[1]), ) } else: cr[1] = anniv_death_fmt % { 'ID': cr[1][0][int(lang != 'en')], 'mul': '' } else: cr[1] = '' s = _anniv_fmt % (''.join(cr), ) elif month == 6: # check RuMei if not ext.has_key('RuMei') or ext['RuMei'][0] != year: t, b = pcc.chinese_day_name(minor_solterm_date) if not ext.get('bencao'): if t <= 3: ext['RuMei'] = (year, minor_solterm_date + 3 - t) else: ext['RuMei'] = (year, minor_solterm_date + 13 - t) else: if t <= 9: ext['RuMei'] = (year, minor_solterm_date + 9 - t) else: ext['RuMei'] = (year, minor_solterm_date + 19 - t) if date == ext['RuMei'][1]: s = _miscterm_fmt % (miscterm[0], ) if not ext.has_key('XZ') or ext['XZ'][0] != year: t, b = pcc.chinese_day_name(major_solterm_date) ext['XZ'] = (year, major_solterm_date, t, b) elif month == 7: # check ChuMei, ChuFu, ZhongFu if not ext.has_key('ChuMei') or ext['ChuMei'][0] != year: t, b = pcc.chinese_day_name(minor_solterm_date) if not ext.get('bencao'): if b <= 8: ext['ChuMei'] = (year, minor_solterm_date + 8 - b) else: ext['ChuMei'] = (year, minor_solterm_date + 20 - b) else: if t <= 9: ext['ChuMei'] = (year, minor_solterm_date + 9 - t) else: ext['ChuMei'] = (year, minor_solterm_date + 19 - t) if not ext.has_key('XZ') or ext['XZ'][0] != year: d = pcc.fixed_from_moment( pcc.major_solar_term_on_or_after(date - 30)) t, b = pcc.chinese_day_name(d) ext['XZ'] = (year, d, t, b) if not ext.has_key('ChuFu') or ext['ChuFu'][0] != year: _, d, t, b = ext['XZ'] if t <= 7: ext['ChuFu'] = (year, d + 27 - t, d + 37 - t) else: ext['ChuFu'] = (year, d + 37 - t, d + 47 - t) if date == ext['ChuMei'][1]: s = _miscterm_fmt % (miscterm[1], ) elif date == ext['ChuFu'][1]: s = _miscterm_fmt % (miscterm[2], ) elif date == ext['ChuFu'][2]: s = _miscterm_fmt % (miscterm[3], ) elif month == 8: # check ZhongFu, MoFu if not ext.has_key('XZ') or ext['XZ'][0] != year: d = pcc.fixed_from_moment( pcc.major_solar_term_on_or_after(date - 61)) t, b = pcc.chinese_day_name(d) ext['XZ'] = (year, d, t, b) if not ext.has_key('ChuFu') or ext['ChuFu'][0] != year: _, d, t, b = ext['XZ'] if t <= 7: ext['ChuFu'] = (year, d + 27 - t, d + 37 - t) else: ext['ChuFu'] = (year, d + 37 - t, d + 47 - t) if not ext.has_key('MoFu') or ext['MoFu'][0] != year: t, b = pcc.chinese_day_name(minor_solterm_date) if t <= 7: ext['MoFu'] = (year, minor_solterm_date + 7 - t) else: ext['MoFu'] = (year, minor_solterm_date + 17 - t) if date == ext['ChuFu'][2]: s = _miscterm_fmt % (miscterm[3], ) elif date == ext['MoFu'][1]: s = _miscterm_fmt % (miscterm[4], ) elif month == 12 or month < 4: # check *Jiu if not ext.has_key('DZ') or ext['DZ'][0] != year - int( month != 12): if month == 12: d = major_solterm_date ext['DZ'] = (year, d) else: d = pcc.fixed_from_moment( pcc.major_solar_term_on_or_after( pcc.fixed_from_gregorian( (year - 1, 12, 1)))) ext['DZ'] = (year - 1, d) ext['Jiu'] = set([d + 9 * i for i in xrange(9)]) if date in ext['Jiu']: d = date - ext['DZ'][1] s = _miscterm_fmt % (miscterm[5 + d / 9], ) x = len(filter(lambda c: ord(c) > 0xFF, s)) if len(s) + x <= 10: s = s.center(10 - x) else: x = 8 - int(ord(s[-1]) > 0xFF) cr = [] for j in xrange(len(s) - 1): x -= 1 + int(ord(s[j]) > 0xFF) if x > 0: cr.append(s[j]) elif x == 0: break else: cr.append('.') break cr.append('..') cr.append(s[-1]) s = ''.join(cr) x = len(filter(lambda c: ord(c) > 0xFF, s)) assert len(s) + x == 10 br.append(s) stem += 1 if stem == 10: stem = 0 branch += 1 if branch == 12: branch = 0 date += 1 dcnt += 1 ldcnt += 1 println(''.join(ar)) if show: println(''.join(br)) return c_last_date
def chinese_from_gregorian(gdate): pcc_gdate = _pcc_gregorian_from_date(gdate) pcc_cdate = chinese_from_fixed(fixed_from_gregorian(pcc_gdate)) return _tuple_from_pcc_chinese(pcc_cdate)