def lunar_to_solar(self, today, thisYear): lunarDate = self._date calendar = KoreanLunarCalendar() if thisYear or self._mmdd: calendar.setLunarDate(today.year, lunarDate.month, lunarDate.day, self._intercalation) if calendar.SolarIsoFormat() == '0000-00-00': lunarDate2 = lunarDate - timedelta(1) calendar.setLunarDate(today.year, lunarDate2.month, lunarDate2.day, self._intercalation) _LOGGER.warn("Non-existent date correction : %s -> %s", lunarDate, calendar.SolarIsoFormat()) else: calendar.setLunarDate(lunarDate.year, lunarDate.month, lunarDate.day, self._intercalation) return dt_util.parse_date(calendar.SolarIsoFormat())
class DateConverter(object): def __init__(self, year, month, day, lunar): self._year = year self._month = month self._day = day self._lunar = lunar self._intercalation = None self._calendar = KoreanLunarCalendar() @property def year(self): return self._year @property def month(self): return self._month @property def day(self): return self._day @property def intercalation(self): return self._get_intercalation() @property def solar_day(self): return self._get_solar_day() @property def lunar_day(self): return self._get_lunar_day() def _get_intercalation(self): if self._lunar == True: if self._date.year % 4 == 0 or self._date.year % 400 == 0: self._intercalation = True else: self._intercalation = False else: self._intercalation = None return self._intercalation def _get_solar_day(self): self._calendar.setLunarDate(self._year, self._month, self._day, self._intercalation) solar_date = self._calendar.SolarIsoFormat() return solar_date def _get_lunar_day(self): self._calendar.setSolarDate(self._year, self._month, self._day) lunar_date = self._calendar.LunarIsoFormat() return lunar_date def __del__(self): del self._calendar
def printInformation(year, is_intercalation=False): today = datetime.today() year = today.year month = today.month day = today.day calendar = KoreanLunarCalendar() calendar.setSolarDate(year, month, day) print('Year: {}'.format(year)) # 할머니: (음) 5/23 calendar.setLunarDate(year, 5, 23, is_intercalation) print('할머니 제사 (음) 5/23') print('* Solar: {}'.format(calendar.SolarIsoFormat())) print('* Lunar: {}'.format(calendar.LunarIsoFormat())) print('* Hangul: {}'.format(calendar.getGapJaString())) print('* Hanja: {}'.format(calendar.getChineseGapJaString())) calendar.setLunarDate(year, 5, 1, is_intercalation) print('초하루 (음) 5/1') print('* Solar: {}'.format(calendar.SolarIsoFormat())) print('* Lunar: {}'.format(calendar.LunarIsoFormat())) print('* Hangul: {}'.format(calendar.getGapJaString())) print('* Hanja: {}'.format(calendar.getChineseGapJaString())) print('------------------------------') # 할아버지: (음) 8/6 calendar.setLunarDate(year, 8, 6, is_intercalation) print('할아버지 제사 (음) 8/6') print('* Solar: {}'.format(calendar.SolarIsoFormat())) print('* Lunar: {}'.format(calendar.LunarIsoFormat())) print('* Hangul: {}'.format(calendar.getGapJaString())) print('* Hanja: {}'.format(calendar.getChineseGapJaString())) print('초하루 (음) 8/1') calendar.setLunarDate(year, 8, 1, is_intercalation) print('* Solar: {}'.format(calendar.SolarIsoFormat())) print('* Lunar: {}'.format(calendar.LunarIsoFormat())) print('* Hangul: {}'.format(calendar.getGapJaString())) print('* Hanja: {}'.format(calendar.getChineseGapJaString()))
def lunar_to_solar(self, lunarDate, intercal): calendar = KoreanLunarCalendar() calendar.setLunarDate(lunarDate.year, lunarDate.month, lunarDate.day, intercal) return dt_util.parse_date(calendar.SolarIsoFormat())
class CalendarCalculator: def __init__(self): self.lunarCalculator = KoreanLunarCalendar() def isLunarMonth(self, year: int) -> bool: ''' Judge lunar month :param year: the year :return: boolean type data ''' if year % 4 == 0 and year % 100 != 0 or year % 400 == 0: return True #if ((i % 4 == 0 & & i % 100 != 0) | | i % 400 == 0) #elif year % 400 == 0: #return True else: return False def toSolarDate(self, year: int, month: int, day: int): ''' Convert from lunar date to solar date :param year: lunar date of year :param month: lunar date of month :param day: lunar date of day :return: lunar date with tuple which have 3 arguments ''' self.lunarCalculator.setLunarDate(year, month, day, self.isLunarMonth(year)) formatedDate = self.formatDate(self.lunarCalculator.SolarIsoFormat()) return formatedDate def toLunarDate(self, year: int, month: int, day: int) -> tuple: ''' Convert from solar date to lunar date :param year: solar date of year :param month: solar date of month :param day: solar date of day :return: solar date with tuple which have 3 arguments ''' self.lunarCalculator.setSolarDate(year, month, day) formatedDate = self.formatDate(self.lunarCalculator.LunarIsoFormat()) return formatedDate def formatDate(self, date: str) -> tuple: ''' Format string type to tuple :param date: string type value date ex) '2018-12-9' :return: tuple which have 3 arguments ex) (2018, 12, 9) ''' return tuple(map(int, date.split('-'))) def getSolarHoliday(self, year: int, month: int, day: int, holiday: str) -> tuple: rawDate = self.toSolarDate(year, month, day) rawDate = (rawDate[1], rawDate[2], holiday) return rawDate def getToLunarDate(self, year, month, day): self.lunarCalculator.setSolarDate(year, month, day) return self.lunarCalculator.LunarIsoFormat()