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 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 __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 is_valid_chinese_date(cdate): pcc_cdate = _pcc_chinese_from_tuple(cdate) cdate2 = chinese_from_fixed(fixed_from_chinese(pcc_cdate)) if cdate2[0] == pcc_cdate[0] - 1 and \ cdate2[1] == 60 and \ pcc_cdate[1] == 0: return cdate2[2:] == pcc_cdate[2:] return cdate2 == pcc_cdate
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 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 CDate_from_fixed(date): return CDate(*pcc.chinese_from_fixed(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)