python实现日历功能_Python方法完成农历日历功能代码

count+= 1

if d ==0:print print_str+"\t",continue(ly, lm, ld)=get_ludar_date(datetime(tm.year, tm.month, d))if count % 7 ==0:printd_str=str(d)if d ==tm.day:

d_str= u"*" +d_strprint d_str + d_lunar(ld) + u"\t",print

defthis_month():

show_month(datetime.now())#www.iplaypython.com

defweek_str(tm):

a= u'星期一 星期二 星期三 星期四 星期五 星期六 星期日'.split()returna[tm.weekday()]defd_lunar(ld):

a= u'初一 初二 初三 初四 初五 初六 初七 初八 初九 初十\

十一 十二 十三 十四 十五 十六 十七 十八 十九 廿十\

廿一 廿二 廿三 廿四 廿五 廿六 廿七 廿八 廿九 三十'.split()return a[ld - 1]defm_lunar(lm):

a= u'正月 二月 三月 四月 五月 六月 七月 八月 九月 十月 十一月 十二月'.split()return a[lm - 1]defy_lunar(ly):

y=ly

tg= u'甲 乙 丙 丁 戊 己 庚 辛 壬 癸'.split()

dz= u'子 丑 寅 卯 辰 巳 午 未 申 酉 戌 亥'.split()

sx= u'鼠 牛 虎 免 龙 蛇 马 羊 猴 鸡 狗 猪'.split()return tg[(y - 4) % 10] + dz[(y - 4) % 12] + u' ' + sx[(y - 4) % 12] + u'年'

defdate_diff(tm):return (tm -datetime(1901, 1, 1)).daysdefget_leap_month(lunar_year):

flag= g_lunar_month[(lunar_year - START_YEAR) / 2]if (lunar_year - START_YEAR) % 2:return flag & 0x0f

else:return flag >> 4

deflunar_month_days(lunar_year, lunar_month):if (lunar_year get_leap_month(lunar_year) andget_leap_month(lunar_year)):

iBit-= 1

if (g_lunar_month_day[lunar_year - START_YEAR] & (1 <

low+= 1

if (lunar_month ==get_leap_month(lunar_year)):if (g_lunar_month_day[lunar_year - START_YEAR] & (1 << (iBit -1))):

high= 30

else:

high= 29

return(high, low)deflunar_year_days(year):

days=0for i in range(1, 13):

(high, low)=lunar_month_days(year, i)

days+=high

days+=lowreturndaysdefget_ludar_date(tm):

span_days=date_diff(tm)#阳历1901年2月19日为阴历1901年正月初一

#阳历1901年1月1日到2月19日共有49天

if (span_days <49):

year= START_YEAR - 1

if (span_days <19):

month= 11;

day= 11 +span_dayselse:

month= 12;

day= span_days - 18

return(year, month, day)#下面从阴历1901年正月初一算起

span_days -= 49year, month, day= START_YEAR, 1, 1

#计算年

tmp =lunar_year_days(year)while span_days >=tmp:

span_days-=tmp

year+= 1tmp=lunar_year_days(year)#计算月

(foo, tmp) =lunar_month_days(year, month)while span_days >=tmp:

span_days-=tmpif (month ==get_leap_month(year)):

(tmp, foo)=lunar_month_days(year, month)if (span_days

span_days-=tmp

month+= 1(foo, tmp)=lunar_month_days(year, month)#计算日

day +=span_daysreturn(year, month, day)#功能简单,只打印当月的

this_month()

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值