Esempio n. 1
0
    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)))
Esempio n. 2
0
    def test_timedelta(self):
        ld = LunarDate(1976, 8, 8)
        sd = date(2008, 1, 1)
        td = timedelta(days=10)

        self.assertEqual(timedelta(days=0), ld - ld)
        self.assertEqual(LunarDate(1976, 7, 27, 0), ld - td)
        self.assertEqual(timedelta(11444), sd - ld)
        self.assertEqual(LunarDate(1976, 8, 18, 0), ld + td)
        self.assertEqual(LunarDate(1976, 8, 18, 0), td + ld)
Esempio n. 3
0
    def test_convert_datetime(self):
        dt = LunarDate(1976, 8, 8, 1).to_solar_date()
        self.assertEqual(date(1976, 10, 1), dt)
        dt2 = LunarDate.from_solar_date(2033, 10, 23)
        self.assertTrue(LunarDate(2033, 10, 1, 0), dt2)

        # day out of range
        with self.assertRaises(ValueError):
            LunarDate(2004, 1, 30).to_solar_date()

        # year out of range [1900, 2100]
        with self.assertRaises(ValueError):
            LunarDate(2101, 1, 1).to_solar_date()
Esempio n. 4
0
def main(list):
    # list = [
    #     ['12-25', 'test1']  #第一个是农历生日,第二个是名字
    # ]

    # 获取今天的农历日期
    today = LunarDate.today()

    year = today.year
    month = today.month
    day = today.day

    for i, name in list:

        i = i.split('-')
        tmpMonth = int(i[0])
        tmpDay = int(i[1])
        tmpNow = LunarDate(year, tmpMonth, tmpDay)  # 今年的生日日期
        if tmpNow < today:
            tmpNow = LunarDate(year + 1, tmpMonth, tmpDay)  # 明年的生日日期
        # print(tmpNow)
        dis = today - tmpNow
        dis = dis.days

        message = None
        if dis == 0:
            message = '今天'
        elif dis == -1:
            message = '明天'
        elif dis == -2:
            message = '后天'
        elif dis == -3:
            message = '大后天'

        # print(message, dis)

        # 若在范围内
        if message:
            # 转化为公历
            solarDay = tmpNow.to_solar_date()
            try:
                message = message + '是' + name + '的生日' + '  具体日期是: ' + str(
                    solarDay)
                facade.update(message, flag=True)
            except:
                pass

        else:
            facade.update(message=name + '的生日 今天不需要通知')
Esempio n. 5
0
    def test_term_ganzhi_feature(self):
        ld = LunarDate(2018, 6, 26)
        self.assertEqual(datetime.date(2018, 8, 7), ld.to_solar_date())
        self.assertEqual(43287, ld._offset)
        self.assertEqual('立秋', ld.term)
        self.assertEqual('戊戌', ld.gz_year)
        self.assertEqual('庚申', ld.gz_month)
        self.assertEqual('辛未', ld.gz_day)
        self.assertEqual('二〇一八年六月廿六', ld.cn_str())
        self.assertEqual('戊戌年庚申月辛未日', ld.gz_str())

        ld1 = LunarDate(2018, 12, 20)
        self.assertEqual('戊戌', ld1.gz_year)
        self.assertEqual('狗', ld.animal)

        ld2 = LunarDate(2018, 12, 10)
        self.assertEqual('初十', ld2.cn_day)
Esempio n. 6
0
    def test_valid_format(self):
        ld = LunarDate(2018, 4, 3)
        self.assertEqual('2018-4-3', ld.strftime('%y-%m-%d'))
        self.assertEqual('二〇一八', ld.strftime('%Y'))
        self.assertEqual('2018%c', ld.strftime('%y%c'))  # Just ignore %c, no raise error

        ld2 = LunarDate(2018, 11, 23)
        self.assertEqual('二〇一八/冬/廿三', ld2.strftime('%Y/%M/%D'))

        ld3 = LunarDate(2017, 6, 3, 1)
        self.assertEqual('61', ld3.strftime('%m%l'))
        self.assertEqual('闰六', ld3.strftime('%L%M'))
        self.assertEqual(ld3.gz_str(), ld3.strftime('%G'))

        self.assertEqual('%y', ld3.strftime('%%y'))
        self.assertEqual('%2017', ld3.strftime('%%%y'))
        self.assertEqual('2017631', ld3.strftime('%y%m%d%l'))
        self.assertEqual('201706031', ld3.strftime('%y%A%B%l'))
Esempio n. 7
0
    def test_fuzzy_feature(self):
        ss = SolarSchema(month=4, day=1)
        with self.assertRaises(ValueError):
            ss.resolve()
        self.assertEqual(date(2018, 4, 1), ss.resolve(2018))

        dls = DayLunarSchema(month=12, day=1, reverse=1)
        self.assertEqual(1, (LunarDate(2019, 1, 1) - dls.resolve(2018)).days)
        schema = get_festival('元旦')
        self.assertTrue(schema.match(date(2019, 1, 1)))
Esempio n. 8
0
    def test_match(self):
        md = SolarSchema(year=0, month=2, day=14)
        self.assertTrue(md.match(date(2019, 2, 14)))
        self.assertTrue(md.match(date(2020, 2, 14)))
        self.assertFalse(md.match(date(2019, 2, 1)))

        md1 = LunarSchema(year=0, month=1, day=2)
        self.assertTrue(md1.match(LunarDate(2018, 1, 2)))

        with self.assertRaises(TypeError):
            md1.match(2)
Esempio n. 9
0
def lunardate_sqlite3_demo():
    con = sqlite3.connect(":memory:", detect_types=sqlite3.PARSE_DECLTYPES)
    cur = con.cursor()
    cur.execute(
        'CREATE TABLE member (pid INT AUTO_INCREMENT PRIMARY KEY,birthday lunardate);'
    )
    ld = LunarDate(2018, 5, 3)
    cur.execute("INSERT INTO member(birthday) VALUES (?)", (ld, ))
    cur.execute("SELECT pid, birthday FROM member;")
    my_birthday = cur.fetchone()[1]
    cur.close()
    con.close()
    print(my_birthday)
 def test_custom_field(self):
     con = sqlite3.connect(":memory:", detect_types=sqlite3.PARSE_DECLTYPES)
     cur = con.cursor()
     cur.execute('CREATE TABLE member (pid INT AUTO_INCREMENT PRIMARY KEY,birthday lunardate);')
     ld = LunarDate(2018, 5, 3)
     cur.execute("INSERT INTO member(birthday) VALUES (?)", (ld,))
     cur.execute("SELECT pid, birthday FROM member;")
     my_birthday = cur.fetchone()[1]
     cur.close()
     con.close()
     self.assertEqual(LunarDate, type(my_birthday))
     self.assertEqual(2018, my_birthday.year)
     self.assertEqual(5, my_birthday.month)
     self.assertEqual(3, my_birthday.day)
     self.assertEqual(0, my_birthday.leap)
Esempio n. 11
0
def notifyBirthday():
    print(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())))
    birthdayDict = {
        "我": LunarDate(1986, 10, 3, 0),
        "婷婷": LunarDate(1987, 4, 2, 0),
        "东北爸": LunarDate(1967, 5, 28, 0),
        "东北妈": LunarDate(1965, 11, 27, 0),
        "湖南爸": LunarDate(1953, 9, 25, 0),
        "湖南妈": LunarDate(1957, 5, 29, 0),
    }
    notifyBirthday = NotifyBirthday(**birthdayDict)
    notifyBirthday.notifyBirthday()
Esempio n. 12
0
    def test_match(self):
        ls = LunarSchema(month=6, day=1)
        self.assertTrue(ls.match(LunarDate(2017, 6, 1, 0)))
        self.assertTrue(ls.match(LunarDate(2017, 6, 1, 1)))

        ls1 = LunarSchema(month=6, day=1, ignore_leap=0)
        self.assertTrue(ls1.match(LunarDate(2017, 6, 1, 0)))
        self.assertFalse(ls1.match(LunarDate(2017, 6, 1, 1)))

        ls2 = LunarSchema(month=6, day=1, leap=1, ignore_leap=0)
        self.assertFalse(ls2.match(LunarDate(2017, 6, 1, 0)))
        self.assertTrue(ls2.match(LunarDate(2017, 6, 1, 1)))
Esempio n. 13
0
    def test_schema_factory(self):
        md2 = DateSchemaFactory.from_string('1000006140')
        self.assertTrue(md2.match(LunarDate(2017, 6, 14)))
        self.assertTrue(md2.match(LunarDate(2017, 6, 14, 1)))
        sd = LunarDate(2017, 6, 14).to_solar_date()
        self.assertTrue(md2.match(sd))

        md3 = DateSchemaFactory.from_string('106140')
        self.assertTrue(md3.match(LunarDate(2017, 6, 14)))
        self.assertTrue(md3.match(LunarDate(2017, 6, 14, 1)))
        sd = LunarDate(2017, 6, 14).to_solar_date()
        self.assertTrue(md3.match(sd))
Esempio n. 14
0
    def test_comparison(self):
        ld = LunarDate(1976, 8, 8)
        ld2 = LunarDate.today()
        self.assertTrue(ld < ld2)
        self.assertTrue(ld <= ld2)
        self.assertTrue(ld2 > ld)
        self.assertTrue(ld2 >= ld)
        self.assertTrue(ld != ld2)
        self.assertFalse(ld == ld2)
        self.assertTrue(LunarDate.today() == LunarDate.today())

        # Compare with a integer
        self.assertFalse(LunarDate.today() == 5)
        with self.assertRaises(TypeError):
            LunarDate.today() < 5
        with self.assertRaises(TypeError):
            LunarDate.today() > 5
        with self.assertRaises(TypeError):
            LunarDate.today() >= 5
        with self.assertRaises(TypeError):
            LunarDate.today() >= 5
Esempio n. 15
0
 def notifyBirthday(self):  # 距离生日7 、3 、1 、0天时通知
     flag = False  #标记今天没有人过生日
     for name, birthday in self.__birthdayDict.items():
         ##获取今天的农历日期
         todayNongli = LunarDate.today()
         birthday = LunarDate(todayNongli.year, birthday.month,
                              birthday.day,
                              0)  #将生日的年份改成今年,方便计算还有多少天过生日,直接与当前农历日志相减
         howMuchDays = birthday.offset - todayNongli.offset
         if (howMuchDays == 7 or howMuchDays == 3 or howMuchDays == 1
                 or howMuchDays == 0):
             text = '{name}的生日还有{howMuchDays}天,要祝福呦~。'.format(
                 name=name, howMuchDays=howMuchDays)
             desp = '###{name},祝你生日快乐,健健康康~~'.format(
                 name=name)  # 内容支持markdown
             serverJiangNoify = ServerJiangNoify(text, desp)  #初始化.
             print(serverJiangNoify.sendMessage())
             flag = True  #无人过生日
     text = "加油工作,加油学习~" + str(time.time())
     desp = "###没钱不行,没钱不行,没钱不行~"
     serverJiangNoify = ServerJiangNoify(text, desp)
     print(serverJiangNoify.sendMessage())
Esempio n. 16
0
 def test_new_date(self):
     ld = LunarDate(2018, 12, 10)
     ld1 = ld.replace(year=2017, month=6, day=23, leap=1)
     self.assertEqual(2017, ld1.year)
Esempio n. 17
0
 def test_term(self):
     ld = LunarDate(2020, 3, 23)
     self.assertEqual('tem:-', ld.strftime('tem:%t'))
Esempio n. 18
0
 def test_immutable_feature(self):
     ld1 = LunarDate(2018, 6, 1)
     ld2 = LunarDate(2018, 6, 1)
     self.assertEqual(1, len({ld1, ld2}))
Esempio n. 19
0
 def test_cn_calendar_day(self):
     ld = LunarDate(2017, 6, 1, 1)
     self.assertEqual('闰六', ld.strftime('%F'))
     ld1 = LunarDate(2017, 11, 1, 0)
     self.assertEqual('冬月', ld1.strftime('%F'))
Esempio n. 20
0
 def test_nominal_age(self):
     birthday = LunarDate(2017, 6, 16, 1)
     self.assertEqual(1, nominal_age(birthday, LunarDate(2017, 6, 21, 1)))
     self.assertEqual(1, nominal_age(birthday, LunarDate(2017, 12, 29)))
     self.assertEqual(2, nominal_age(birthday, LunarDate(2018, 1, 1)))
Esempio n. 21
0
 def test_create_date(self):
     ld = LunarDate(1976, 8, 8, 1)
     self.assertEqual(1976, ld.year)
     self.assertEqual(8, ld.month)
     self.assertEqual(8, ld.day)
     self.assertEqual(True, ld.leap)
Esempio n. 22
0
 def test_leap_countdown(self):
     ls = LunarSchema(month=6, day=1)
     ls.delta(LunarDate(2017, 6, 27))
Esempio n. 23
0
 def _resolve(self, year):
     return LunarDate(year, self.month, self.day, self.leap)
Esempio n. 24
0
 def test_countdown_lunar(self):
     ls = LunarSchema(year=0, month=4, day=2)
     self.assertEqual(1, ls.countdown(LunarDate(2019, 4, 1)))
     self.assertEqual(0, ls.countdown(LunarDate(2019, 4, 2)))
     self.assertEqual(353, ls.countdown(LunarDate(2019, 4, 3)))
Esempio n. 25
0
 def _resolve(self, year):
     if self._reverse == 0:
         day = self.day
     else:
         day = LCalendars.ndays(year, self.month) - self.day + 1
     return LunarDate(year, self.month, day)
Esempio n. 26
0
    '12-26',  # 农历生日
]

# 获取今天的农历日期
today = LunarDate.today()
today_string = today.strftime('%m-%d')

year = today.year
month = today.month
day = today.day

for i in list:
    i = i.split('-')
    tmpMonth = int(i[0])
    tmpDay = int(i[1])
    tmpNow = LunarDate(year, tmpMonth, tmpDay)
    # print(tmpNow)
    dis = today - tmpNow
    dis = dis.days
    message = None
    if dis == 0:
        message = '今天'
    elif dis == -1:
        message = '明天'
    elif dis == -2:
        message = '后天'
    elif dis == -3:
        message = '大后天'

    # 若在范围内
    if message: