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()
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章