def validate_lunar(): t1 = time.time() total = 0 fail = 0 records = [] for sd in iter_solar_date(): ld = LunarDate.from_solar(sd) # test target actual = ld.strftime('%y,%m,%d,%l') solar = Solar(sd.year, sd.month, sd.day) solar_date_str = '{},{},{}'.format(sd.year, sd.month, sd.day) lunar = converter.SolarToLunar(solar) expected = '{},{},{},{}'.format(lunar.lunarYear, lunar.lunarMonth, lunar.lunarDay, int(lunar.isleap)) # solar_date_str = sd.strftime("%Y,%m,%d") # rsp = urllib.request.urlopen(url='http://localhost:1337/?src={}'.format(solar_date_str)) # expected = rsp.read().decode('utf8') total += 1 if actual != expected: records.append('{} {} {}'.format(solar_date_str, expected, actual)) fail += 1 if fail > 0: with open('fail_record.txt', 'w') as fp: fp.write('\n'.join(records)) t2 = time.time() print('Completed! total:{}, fail:{};time {}s'.format(total, fail, int(t2 - t1)))
def test_delta(self): sd = date(2018, 12, 1) self.assertEqual(-1, LCalendars.delta(sd, date(2018, 12, 2))) self.assertEqual( -1, LCalendars.delta(LunarDate.from_solar(sd), date(2018, 12, 2))) self.assertEqual( 4, LCalendars.delta(LunarDate(2018, 1, 6), LunarDate(2018, 1, 2)))
def _normalize(self, date_obj): date_class = self._get_date_class() if not isinstance(date_obj, (date, LunarDate)): raise TypeError('Unsupported type: {}'.format( date_obj.__class__.__name__)) if isinstance(date_obj, date_class): return date_obj if isinstance(date_class, date): return date_obj.to_solar_date() else: return LunarDate.from_solar(date_obj)
def __render_one_day_info(self, row, date, info_frame=None): if info_frame is None: info_frame = self.info_frame for widget in info_frame.winfo_children(): widget.destroy() str_date = date.strftime('%Y-%m-%d') # 渲染某个日期的基本信息 tk.Label(info_frame, text='-' * Constants.SPLIT_LINE, font=(Constants.FONT, Constants.FONT_SIZE_14, Constants.FONT_WEIGHT)).grid(row=row + 1, column=0, columnspan=7, sticky=tk.N) tk.Label(info_frame, text=date.day, width=2, height=1, fg=Constants.WHITE_COLOR, bg=Constants.ORANGE_COLOR, font=(Constants.FONT, 50, Constants.FONT_WEIGHT)).grid(row=row + 2, column=0, rowspan=3, columnspan=2, sticky=tk.W, padx=3) solar_date = date.strftime('%Y{0}%m{1}%d{2} {3}{4}').format( '年', '月', '日', '星期', Constants.WEEKS[date.weekday()]) tk.Label(info_frame, text=solar_date, fg=Constants.BLACK_COLOR, font=(Constants.FONT, Constants.FONT_SIZE_12, Constants.FONT_WEIGHT)).grid(row=row + 2, column=1, columnspan=6, sticky=tk.NW, padx=20) lunar_date = LunarDate.from_solar(date) lunar_date = lunar_date.strftime('{0}%M{1}%D %G'.format('农历:', '月')) tk.Label(info_frame, text=lunar_date, fg=Constants.BLACK_COLOR, font=(Constants.FONT, Constants.FONT_SIZE_10, Constants.FONT_WEIGHT)).grid(row=row + 3, column=1, columnspan=6, sticky=tk.NW, padx=21) # 渲染24节气 twenty_four_day = self.tf.get_festivals(str_date) if twenty_four_day: twenty_four_day = twenty_four_day[0] + '\r' + twenty_four_day[1] tk.Label(info_frame, text=twenty_four_day, fg=Constants.WHITE_COLOR, bg=Constants.GREEN_COLOR, font=(Constants.FONT, Constants.FONT_SIZE_10)).grid(row=row + 4, column=1, sticky=tk.NW, padx=22) # 渲染农历节日 lunar_day = LunarDate.from_solar(date).strftime('%M%D') lunar_festival = self.lf.get_festivals(lunar_day) if lunar_festival: tk.Label(info_frame, text=lunar_festival, fg=Constants.WHITE_COLOR, bg=Constants.ORANGE_COLOR, font=(Constants.FONT, Constants.FONT_SIZE_10, Constants.FONT_WEIGHT)).grid(row=row + 4, column=1, sticky=tk.E) # 母亲节 父亲节 感恩节 如果与公历节日冲突则覆盖公历节日 parents_day = self.pd.get_festivals(str_date) if parents_day: tk.Label(info_frame, text=parents_day, fg=Constants.WHITE_COLOR, bg=Constants.RED_COLOR, font=(Constants.FONT, Constants.FONT_SIZE_10, Constants.FONT_WEIGHT)).grid(row=row + 4, column=2, sticky=tk.W) # 渲染公历节日 solar_day = self.sf.get_festivals_desc(date.strftime('%m-%d')) if solar_day: tk.Label(info_frame, text=solar_day, fg=Constants.WHITE_COLOR, bg=Constants.RED_COLOR, font=(Constants.FONT, Constants.FONT_SIZE_10, Constants.FONT_WEIGHT)).grid(row=row + 4, column=3, sticky=tk.W, columnspan=4) # 渲染日程信息 tk.Label(info_frame, text='-' * Constants.SPLIT_LINE, font=(Constants.FONT, Constants.FONT_SIZE_14, Constants.FONT_WEIGHT)).grid(row=row + 6, column=0, columnspan=7, sticky=tk.N) sch_list = self.sl.get_today_schedulers(str_date) if sch_list and len(sch_list) > 0: index = 7 sch_bg = Constants.SCHEDULER_COLOR if date < self.today: sch_bg = Constants.GREY_COLOR tk.Label(info_frame, text='待办事项:', fg=Constants.WHITE_COLOR, bg=Constants.SCHEDULER_COLOR, font=(Constants.FONT, Constants.FONT_SIZE_10)).grid(row=row + index, column=0, columnspan=6, sticky=tk.NW, padx=3) for sch in sch_list: sch_info = '%02s:%02s - %s:%s' % (str( sch.get('hour')), str(sch.get('minute')), sch.get('title'), sch.get('describ')) tk.Label(info_frame, text=sch_info, fg=Constants.WHITE_COLOR, bg=sch_bg, font=(Constants.FONT, Constants.FONT_SIZE_10, Constants.FONT_WEIGHT)).grid(row=row + index + 1, column=1, columnspan=6, sticky=tk.W, pady=3) index += 1
def __render_lunar_day_and_festival(self, every_day, lunar_font, bg_color, row, wk_day, festival_font_color): # 渲染公历节日 solar_day = self.sf.get_festivals(every_day.strftime('%m-%d')) if solar_day: tk.Label(self.dynamic_frame, text=solar_day, fg=festival_font_color, bg=bg_color, font=(Constants.FONT, Constants.FONT_SIZE_10, Constants.FONT_WEIGHT)).grid(row=row, column=wk_day, sticky=tk.N, pady=4) # 渲染农历日期 lunar_date = LunarDate.from_solar(every_day).strftime('%D') if lunar_date == '初一': lunar_date = LunarDate.from_solar(every_day).strftime('%M') + '月' tk.Label(self.dynamic_frame, text=lunar_date, fg=lunar_font, bg=bg_color, font=(Constants.FONT, Constants.FONT_SIZE_9, Constants.FONT_WEIGHT)).grid(row=row, column=wk_day, sticky=tk.S, pady=15) # 渲染农历节日 lunar_day = LunarDate.from_solar(every_day).strftime('%M%D') lunar_festival = self.lf.get_festivals(lunar_day) if lunar_festival: tk.Label(self.dynamic_frame, text=lunar_festival, fg=festival_font_color, bg=bg_color, font=(Constants.FONT, Constants.FONT_SIZE_10, Constants.FONT_WEIGHT)).grid(row=row, column=wk_day, sticky=tk.N, pady=4) str_every_day = every_day.strftime('%Y-%m-%d') # 渲染24节气 twenty_four_day = self.tf.get_festivals(str_every_day) if twenty_four_day: twenty_four_day = twenty_four_day[0] + '\r' + twenty_four_day[1] tk.Label(self.dynamic_frame, text=twenty_four_day, fg=Constants.WHITE_COLOR, bg=Constants.GREEN_COLOR, font=(Constants.FONT, Constants.FONT_SIZE_9, Constants.FONT_WEIGHT)).grid(row=row, column=wk_day, sticky=tk.E, padx=2) # 母亲节 父亲节 感恩节 如果与阳历节日冲突则覆盖掉阳历节日 parents_day = self.pd.get_festivals(str_every_day) if parents_day: tk.Label(self.dynamic_frame, text=parents_day, fg=festival_font_color, bg=bg_color, font=(Constants.FONT, Constants.FONT_SIZE_10, Constants.FONT_WEIGHT)).grid(row=row, column=wk_day, sticky=tk.N, pady=4) # 渲染日程 if self.sl.get_today_schedulers(str_every_day) and len( self.sl.get_today_schedulers(str_every_day)) > 0: sch_text = '待' sch_bg = Constants.SCHEDULER_COLOR # 今天以前的事项标记为已办 if every_day < self.today: sch_text = '已' sch_bg = Constants.GREY_COLOR tk.Label(self.dynamic_frame, text=sch_text, fg=Constants.WHITE_COLOR, bg=sch_bg, font=(Constants.FONT, Constants.FONT_SIZE_8)).grid(row=row, column=wk_day, sticky=tk.SE, padx=2, pady=4) # 渲染调休/上班标记 public_holiday = self.ph.get_festivals(str_every_day) if public_holiday: ph_bg = Constants.RED_COLOR if public_holiday == '班': ph_bg = Constants.BLACK_COLOR tk.Label(self.dynamic_frame, text=public_holiday, fg=Constants.WHITE_COLOR, bg=ph_bg, font=(Constants.FONT, Constants.FONT_SIZE_9)).grid(row=row, column=wk_day, sticky=tk.NW, padx=2, pady=2)