Python时间函数驱动&函数抽象化分离应用"以呈现日历实验为例"

打开CSDN看见一个这么可爱的广告,进去看了看
可可爱爱
这广告也太可爱= =,激动码字的心情一下子更,激动了哈哈哈哈
里面是怎样的你们自己去看叭,我点开看看,感觉还不够浪漫~
今天小白自己完成了一项工程,打印一个日历。
效果图:
效果图
老师说的好,很多年轻的程序员不经过计划,直接开始码字。
这次我系统的思考了如何完成这项工程。
分为两步

  1. 获得年份月份
  2. 打印出日历

是不是看上去很easy呢?
然而并不是,继续分析。

  1. 如何获得当前的年份?我们马上就会想到一个伟大的时间函数
    说来感觉很真实,每一台计算机自1970年1月1日起,都在默默地记着自己的秒数,坚毅,伟大(扯远扯远,但是1970年1月1日0秒是一个很重要的数字)
  2. 获得了秒数,我们知道要先//60得到分钟,再//60获得小时,再//24获得天数,那么如何将天数转换为年份、月份呢?
  3. 其中影响的因素有,闰年导致的天数不同,闰年以及月份导致的天数不同。

所以我们首先建立一个函数:叫做getYearandMonth()
这个函数要能得出现在的年份和月份,
在得出月份的时候,我们很明显需要判断年份是否为闰年。
所以在getYearandMonth()之后,
我们再建立一个isLeap()函数,判断是否为闰年。
isLeap()函数代码:

def isLeap(year):
    if year%400 == 0 or (year%4==0 and year%100!=0):
        return True
    else:
        return False

闰年即是,可以被400整除或者,被4整除但是不能被100整除。

好的,写好了闰年判断函数,现在获取年份。
来一个import time
对time进行操作(我顺手把系统时间也打出来了,time不用白不用嘛)
得到了天数,然后就是将年份从1970开始叠加叠加
每到年份为闰年时,对天数减366,平年减365
很快我们获得了年份,接下来就是月份。
分两种情况,用最简单粗暴的方法获得月份(月份这东西并没有专门的简单方法,你只能一步一步来)
为闰年、不为闰年。

getYearandMonth()函数代码如下:

def getYearandMonth():
    import time
    global year
    global month
    currentTime  = time.time()
    totalSeconds = int(currentTime)
    currentSecond = totalSeconds % 60
    totalMinutes = totalSeconds // 60
    currentMinute = totalMinutes % 60
    totalHours = totalMinutes // 60
    currentHour = totalHours % 24
    print("Current time is", currentHour + 8, ":", currentMinute, ":", currentSecond, "GMT")
    totalDays = totalSeconds//60//60//24
    year = 1970
    while totalDays>364:
        if isLeap(year):
            totalDays-=366
            year+=1
        else:
            totalDays-=365
            year+=1
    if totalDays<=31:
        month = 1
    elif 31<totalDays and isLeap(year) == True:
        if totalDays-31<=29:
            month =2
        elif totalDays-31-29 <= 31:
            month =3
        elif totalDays -31-29-31 <= 30:
            month =4
        elif totalDays -31-29-31-30<=31:
            month = 5
        elif totalDays -31-29-31-30-31<=30:
            month = 6
        elif totalDays -31-29-31-30-31-30<=31:
            month = 7
        elif totalDays -31-29-31-30-31-30-31<=31:
            month = 8
        elif totalDays -31-29-31-30-31-30-31-31<=30:
            month = 9
        elif totalDays -31-29-31-30-31-30-31-31-30<=31:
            month = 10
        elif totalDays -31-29-31-30-31-30-31-31-30-31<=30:
            month =11
        elif totalDays - 31 - 29 - 31 - 30 - 31 - 30 - 31 - 31 - 30 - 31 -30<=31:
            month =12
    else:
        if totalDays-31<=28:
            month =2
        elif totalDays-31-28 <= 31:
            month =3
        elif totalDays -31-28-31 <= 30:
            month =4
        elif totalDays -31-28-31-30<=31:
            month = 5
        elif totalDays -31-28-31-30-31<=30:
            month = 6
        elif totalDays -31-28-31-30-31-30<=31:
            month = 7
        elif totalDays -31-28-31-30-31-30-31<=31:
            month = 8
        elif totalDays -31-28-31-30-31-30-31-31<=30:
            month = 9
        elif totalDays -31-28-31-30-31-30-31-31-30<=31:
            month = 10
        elif totalDays -31-28-31-30-31-30-31-31-30-31<=30:
            month =11
        elif totalDays - 31 - 28 - 31 - 30 - 31 - 30 - 31 - 31 - 30 - 31 -30<=31:
            month =12

第一大块问题就完成了,我们获得了当前的年份与月份。
开始打印出来吧!
打印标题:

def Calentitle(year,month):
    print("       ",getmonth(month),year)
    print("----------------------------")
    print(" Sun Mon Tue Wed Thu Fri Sat")

开始针对第二块:
日历为两部分:一个是年月,还有是星期一到星期天。
这个可以用很简单很贱的print函数一步解决,
除了:月份最好换成英语的月份,看上去专业些。
所以我们建立一个转换月份的函数,比如把1转换为January。
函数名为getmonth()
代码:(是月份的代码都很简单、粗暴)


def getmonth(month):
    if month == 1:
        return "January"
    elif month == 2:
        return "Febuary"
    elif month == 3:
        return "March"
    elif month == 4:
        return "April"
    elif month == 5:
        return "May"
    elif month == 6:
        return "June"
    elif month == 7:
        return "July"
    elif month == 8:
        return "August"
    elif month == 9:
        return "September"
    elif month == 10:
        return "October"
    elif month == 11:
        return "November"
    else:
        return "December"

然后我们开始第二步,打印这个月的日历。
为了打印日历,我们要知道两件事:

  1. 第一天是星期几
  2. 这个月有几天

所以建立两个函数,getstartdays() 和 getnumberofdays()
一个月有几天挺方便,除了二月要根据闰年,其他不用考虑。
getnumberofdays()代码:

def getnumberofdays(year,month):
    if month == 2:
        return 29 if isLeap(year) else 28
    elif month==1 or month ==3 or month ==5 or month ==7 or month == 8 or month ==10\
            or month ==12:
        return 31
    else:
        return 30

那如何知道某个月的第一天是星期几呢?我们知道1800年1月1日是星期三,如果我们知道这个月的第一天距离1800.1.1有几天,用这个天数%7,余数就是星期(三+余数),比如余0,那么就是星期三。
所以很快我们又需要一个总共多少天的函数,命名gettotaldays()
这个也好写,只要用for函数,从1800到现在的年份,是闰年就加366,不是就加365。
那么很哈皮的又遇到一个问题,当年的日怎么算。
再建立一个函数,getdaysinthisyear()
这个函数就是把这个月之前的日子都算算,闰年,而且时间是三月以后么加1,不然就是加起来。简单粗暴(月份就是这样)
getdaysinthisyear():

def getdaysinthisyear(year,month):
    if month == 1:
        days =  0
    elif month == 2:
        days = 31
    elif month == 3:
        days = 28+31
    elif month == 4:
        days = 28+31+31
    elif month == 5:
        days = 28+31+31+30
    elif month == 6:
        days = 28+31+31+30+31
    elif month == 7:
        days = 28 + 31 + 31 + 30 + 31 + 30
    elif month == 8:
        days = 28 + 31 + 31 + 30 + 31 + 30 + 31
    elif month == 9:
        days = 28 + 31 + 31 + 30 + 31 + 30 + 31 + 31
    elif month == 10:
        days = 28 + 31 + 31 + 30 + 31 + 30 + 31 + 31 + 30
    elif month == 11:
        days = 28 + 31 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31
    elif month == 12:
        days = 28 + 31 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30
    if isLeap(year) == True and month >=3:
        return days+1
    else:
        return days

说起来这个也可以用for函数,把每个月遍历一遍,但我懒得搞,用函数套函数,一个月一个月加,注意别把当前的月的天数加进去就行。

然后我们的totaldays函数就能用了。
gettotaldays():

def gettotaldays (year,month):
    daysinthisyear = getdaysinthisyear(year,month)
    total = 0
    for i in range (1800,year):
        if isLeap(i):
            total+=366
        else:
            total+=365
    total+=daysinthisyear
    return total

好的,接下来就是最激动的时刻,打印主体部分。

def Calenbody(year,month):
    startdays = getstartday(year,month)
    numberofdays = getnumberofdays(year,month)
    for a in range(startdays):
        print("    ",end="")
    count = startdays
    for i in range (1,numberofdays+1):
        print(format(i,"4d"),end="")
        count+=1
        if count % 7 ==0:
            print()
    print()

只要注意到了第七个数换行,还有刚开始有些空就好,空要根据第一天是星期几来操作,因为我这里是星期天一二三四五六,所以提前换了行,如果你是一二三四五六天,那么count应该在print()之后哦

最后就是一个驱动程序,还有半驱动程序啦。
驱动:

def main():
    getYearandMonth()
    print("The calender of current month is : ")
    Calen(year,month)
#    print("The calender of this year is(Jan. to Dec.) ↓: ")
#    for i in range (1,month+1):
#        Calen(year,i)
#    year = eval(input("Enter the year of the calender (e.g.:1999): "))
#    month = eval(input("Enter the month of the calender (e.g.: 1): "))
#    if month <1 or month >12:
#        print("Don't kidding please.")
#    else:
#        Calen(year,month)
main()

这里我隐藏了可以自定义,还有可以展示一年的日历。
有兴趣可以自己去掉玩玩看
半驱动:

def Calen(year,month):
    Calentitle(year,month)
    Calenbody(year,month)

终于写完~
这个亲爱的小仙女还是有点可爱~
小仙女
这是完整的代码:

def getYearandMonth():
    import time
    global year
    global month
    currentTime  = time.time()
    totalSeconds = int(currentTime)
    currentSecond = totalSeconds % 60
    totalMinutes = totalSeconds // 60
    currentMinute = totalMinutes % 60
    totalHours = totalMinutes // 60
    currentHour = totalHours % 24
    print("Current time is", currentHour + 8, ":", currentMinute, ":", currentSecond, "GMT")
    totalDays = totalSeconds//60//60//24
    year = 1970
    while totalDays>364:
        if isLeap(year):
            totalDays-=366
            year+=1
        else:
            totalDays-=365
            year+=1
    if totalDays<=31:
        month = 1
    elif 31<totalDays and isLeap(year) == True:
        if totalDays-31<=29:
            month =2
        elif totalDays-31-29 <= 31:
            month =3
        elif totalDays -31-29-31 <= 30:
            month =4
        elif totalDays -31-29-31-30<=31:
            month = 5
        elif totalDays -31-29-31-30-31<=30:
            month = 6
        elif totalDays -31-29-31-30-31-30<=31:
            month = 7
        elif totalDays -31-29-31-30-31-30-31<=31:
            month = 8
        elif totalDays -31-29-31-30-31-30-31-31<=30:
            month = 9
        elif totalDays -31-29-31-30-31-30-31-31-30<=31:
            month = 10
        elif totalDays -31-29-31-30-31-30-31-31-30-31<=30:
            month =11
        elif totalDays - 31 - 29 - 31 - 30 - 31 - 30 - 31 - 31 - 30 - 31 -30<=31:
            month =12
    else:
        if totalDays-31<=28:
            month =2
        elif totalDays-31-28 <= 31:
            month =3
        elif totalDays -31-28-31 <= 30:
            month =4
        elif totalDays -31-28-31-30<=31:
            month = 5
        elif totalDays -31-28-31-30-31<=30:
            month = 6
        elif totalDays -31-28-31-30-31-30<=31:
            month = 7
        elif totalDays -31-28-31-30-31-30-31<=31:
            month = 8
        elif totalDays -31-28-31-30-31-30-31-31<=30:
            month = 9
        elif totalDays -31-28-31-30-31-30-31-31-30<=31:
            month = 10
        elif totalDays -31-28-31-30-31-30-31-31-30-31<=30:
            month =11
        elif totalDays - 31 - 28 - 31 - 30 - 31 - 30 - 31 - 31 - 30 - 31 -30<=31:
            month =12


def Calen(year,month):
    Calentitle(year,month)
    Calenbody(year,month)

def Calentitle(year,month):
    print("       ",getmonth(month),year)
    print("----------------------------")
    print(" Sun Mon Tue Wed Thu Fri Sat")

def getmonth(month):
    if month == 1:
        return "January"
    elif month == 2:
        return "Febuary"
    elif month == 3:
        return "March"
    elif month == 4:
        return "April"
    elif month == 5:
        return "May"
    elif month == 6:
        return "June"
    elif month == 7:
        return "July"
    elif month == 8:
        return "August"
    elif month == 9:
        return "September"
    elif month == 10:
        return "October"
    elif month == 11:
        return "November"
    else:
        return "December"

def Calenbody(year,month):
    startdays = getstartday(year,month)
    numberofdays = getnumberofdays(year,month)
    for a in range(startdays):
        print("    ",end="")
    count = startdays
    for i in range (1,numberofdays+1):
        print(format(i,"4d"),end="")
        count+=1
        if count % 7 ==0:
            print()
    print()
def getstartday(year,month):
    totaldays = gettotaldays (year,month)
    return (totaldays+3)%7

def gettotaldays (year,month):
    daysinthisyear = getdaysinthisyear(year,month)
    total = 0
    for i in range (1800,year):
        if isLeap(i):
            total+=366
        else:
            total+=365
    total+=daysinthisyear
    return total
def getdaysinthisyear(year,month):
    if month == 1:
        days =  0
    elif month == 2:
        days = 31
    elif month == 3:
        days = 28+31
    elif month == 4:
        days = 28+31+31
    elif month == 5:
        days = 28+31+31+30
    elif month == 6:
        days = 28+31+31+30+31
    elif month == 7:
        days = 28 + 31 + 31 + 30 + 31 + 30
    elif month == 8:
        days = 28 + 31 + 31 + 30 + 31 + 30 + 31
    elif month == 9:
        days = 28 + 31 + 31 + 30 + 31 + 30 + 31 + 31
    elif month == 10:
        days = 28 + 31 + 31 + 30 + 31 + 30 + 31 + 31 + 30
    elif month == 11:
        days = 28 + 31 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31
    elif month == 12:
        days = 28 + 31 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30
    if isLeap(year) == True and month >=3:
        return days+1
    else:
        return days

def isLeap(year):
    if year%400 == 0 or (year%4==0 and year%100!=0):
        return True
    else:
        return False
def getnumberofdays(year,month):
    if month == 2:
        return 29 if isLeap(year) else 28
    elif month==1 or month ==3 or month ==5 or month ==7 or month == 8 or month ==10\
            or month ==12:
        return 31
    else:
        return 30

def main():
    getYearandMonth()
    print("The calender of current month is : ")
    Calen(year,month)
#    print("The calender of this year is(Jan. to Dec.) ↓: ")
#    for i in range (1,month+1):
#        Calen(year,i)
#    year = eval(input("Enter the year of the calender (e.g.:1999): "))
#    month = eval(input("Enter the month of the calender (e.g.: 1): "))
#    if month <1 or month >12:
#        print("Don't kidding please.")
#    else:
#        Calen(year,month)
main()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值