zoukankan      html  css  js  c++  java
  • 关于实现今天到一年中任意一天两者之间的天数差的计算

    昨天的某时某刻突发奇想,想用自己现阶段所学的python知识来制作一个小程序。大致功能为:开机时像某安全软件一样,弹出窗口,窗口上能提示你,“距xxxx年xx月xx日还有多少天”。

            在开始前,因为涉及到日期的计算(如标题所说),首先我想到了标准库中的datetime模块,该模块有个today()函数能返回今天的日期(格式:xxxx-xx-xx),将函数返回的日期转换成字符串后,日期就成了一个长度为10的字符串,对字符串进行切片后分别转换为整型数,就可得到year,month,day参数,具体代码如下

     1 from datetime import date
     2 
     3 
     4 def get_date():
     5     today_date = date.today()
     6     str_t_d = str(today_date)
     7     year = int(str_t_d[:4])
     8     month = int(str_t_d[5:7])
     9     day = int(str_t_d[-2:])
    10     return year, month, day

    调用get_date()后会返回元组(year, month, day)

    下一步开始计算天数差了,因为考虑到对于每一年来说,每个月份天数除了2月以外都是固定的,2月天数的变化关系受到年份影响,因此需要判断闰年或平年,代码如下

     1 def is29days(year=get_date()[0]):
     2     months = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
     3     if year % 100 == 0:
     4         if year % 400 == 0:
     5             months[1] = 29
     6             return months
     7         else:
     8             return months
     9     elif year % 4 == 0:
    10         months[1] = 29
    11         return months
    12     else:
    13         return months

    在上述代码中,将一年的十二个月对应天数按顺序放在列表months中,这个列表中二月对应的天数为28天,即默认为平年,用 if-elif-else语句判断是否为闰年,是就改变months[1]的值为29,返回新的months列表,否返回默认列表,在调用is29days()时将返回月份列表

             有了月份列表,就可也计算了,假定,给定一个日期:3月6日,今天的日期:get_date()[1] 月get_date()[2](今天日期为2月12日,没错我是今天写的)日,利用两个日期的月份对months列表切片,结合列表索引值将其与月份对应,如果当月为2月,则对应的months列表索引值为2-1=1!!!,切片为:months[2-1 : 3],这样列表中就包含了2月,3月两个月,用sum计算切片后的列表元素和后,减去两头的天数后就可以得到3月6日与2月12日之间相差的天数了,代码如下

     1 def count_days(num_month=3, d_day=6):
     2     if num_month < get_date()[1] and d_day < get_date()[2]:
     3         '''在上一月的情况'''
     4         msg = 'error'
     5         month = 'error'
     6         year = 'error'
     7         x_day = 'error'
     8         return msg, month, year, x_day
     9     elif num_month == get_date()[1] and d_day < get_date()[2]:
    10         '''在当月前些天的情况'''
    11         msg = 'error'
    12         month = 'error'
    13         year = 'error'
    14         x_day = 'error'
    15         return msg, month, year, x_day
    16     else:
    17         months = is29days()[get_date()[1]-1:num_month]
    18         x_day = d_day
    19         year = get_date()[0]
    20         month = num_month
    21         days = sum(months) - get_date()[2] - months[-1] + x_day
    22         msg = '%3s天' % days
    23         return msg, month, year, x_day

    考虑到会有选择的日期在当日日期之前的情况,加入判断语句,调用count_days()后返回元组(msg,  month,  year,  x_day),(若不传递实参 ,则默认实参为num_month=3, d_day=6)

    现在就完成了天数差的计算,但开头所讲我们还需一个能弹出窗口(GUI界面),鉴于我仅仅接触过 tkinter 所以我使用了它,代码如下

     1 deadline = Tk()
     2 deadline.title('DeadLine')
     4 deadline.geometry('220x300+1678+732')
     6 label = Label(deadline, text='距%4s年%2s月%2s日还有' % (count_days()[2], count_days()[1], count_days()[3])
     7 label.grid(row=0, columnspan=2)
     8 label_1 = Label(deadline, justify='left', text="""
     9 Don`t waste time any more !
    10
    11 " So do you want to take a
    12 leap of faith,or become an
    13 old man,filled with regret
    14 waiting to die alone? "
    15 """)
    16 label_1.grid(row=2, columnspan=2)
    17 display = Listbox(deadline, font=100, width=5, height=1)
    18 display.insert(END, count_days()[0])
    19 display.grid(row=1, columnspan=2)
    22 button_2 = Button(deadline, text='Exit', command=deadline.destroy)
    23 button_2.grid(columnspan=2)
    24 deadline.mainloop()

    效果如下

    好像差不多了啊

            然后开始将其封装成一个.exe的文件,在这里使用第三方库PyInstaller,因为不是python自带的需要自己安装,安装完成后,在控制台输入命令pyinstaller -F -w filename.py,这里需要注意如果不是在filename.py文件所在的目录下打开的控制台,是需要给定路径的,同时F 大写,w小写,如果不加上-w选项,在运行程序时会弹出控制器界面,同时py文件名不能用汉字,否则会报错,完成后在当前目录下,会有一个dist文件件,打开会发现一个filename.exe文件,大功告成!!

    还没完,我想每天打开电脑都能看到上图的GUI界面,就像某安全软件在屏幕右下角弹出界面一样(好像安全软件都弹),上网查了一下,找到一个方法,如下

    https://jingyan.baidu.com/article/54b6b9c0d103662d593b474e.html

    完整的代码如下:

     1 #!/usr/bin/env python3
     2 # coding=utf-8
     3 # Date: 2019/2/12
     4 
     5 from tkinter import *
     6 from datetime import date
     7 
     8 
     9 def get_date():
    10     today_date = date.today()
    11     str_t_d = str(today_date)
    12     year = int(str_t_d[:4])
    13     month = int(str_t_d[5:7])
    14     day = int(str_t_d[-2:])
    15     return year, month, day
    16 
    17 
    18 def is29days(year=get_date()[0]):
    19     months = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
    20     if year % 100 == 0:
    21         if year % 400 == 0:
    22             months[1] = 29
    23             return months
    24         else:
    25             return months
    26     elif year % 4 == 0:
    27         months[1] = 29
    28         return months
    29     else:
    30         return months
    31 
    32 
    33 def count_days(num_month=3, d_day=6):
    34     if num_month < get_date()[1] and d_day < get_date()[2]:
    35         '''在上一月的情况'''
    36         msg = 'error'
    37         month = 'error'
    38         year = 'error'
    39         x_day = 'error'
    40         return msg, month, year, x_day
    41     elif num_month == get_date()[1] and d_day < get_date()[2]:
    42         '''在当月前些天的情况'''
    43         msg = 'error'
    44         month = 'error'
    45         year = 'error'
    46         x_day = 'error'
    47         return msg, month, year, x_day
    48     else:
    49         months = is29days()[get_date()[1]-1:num_month]
    50         x_day = d_day
    51         year = get_date()[0]
    52         month = num_month
    53         days = sum(months) - get_date()[2] - months[-1] + x_day
    54         msg = '%3s天' % days
    55         return msg, month, year, x_day
    56 
    57 
    58 deadline = Tk()
    59 deadline.title('DeadLine')
    60 deadline.geometry('220x300+1678+732')
    61 label = Label(deadline, text=
    62 '距%4s年%2s月%2s日还有' % (count_days()[2], count_days()[1], count_days()[3]))
    63 label.grid(row=0, columnspan=2)
    64 label_1 = Label(deadline, justify='left', text="""
    65 Don`t waste time any more !
    66 
    67 " So do you want to take a
    68 leap of faith,or become an
    69 old man,filled with regret
    70 waiting to die alone? "
    71 """)
    72 label_1.grid(row=2, columnspan=2)
    73 display = Listbox(deadline, font=100, width=5, height=1)
    74 display.insert(END, count_days()[0])
    75 display.grid(row=1, columnspan=2)
    76 button_2 = Button(deadline, text='Exit', command=deadline.destroy)
    77 button_2.grid(columnspan=2)
    78 deadline.mainloop()

    写在最后:

      上述代码只是实现了最简单的情况,我想以此为基础制作一个能计算任意两个日期天数差的程序,一步一步来,day day up,Come on ,code newbie!

    我有一个特点,我不擅长倒下
  • 相关阅读:
    Hive学习之路 (三)Hive元数据信息对应MySQL数据库表
    Hive学习之路 (二)Hive安装
    Hive学习之路 (一)Hive初识
    MyBatis+Spring SQL效率测试报告
    Java NIO
    Endianess(字节次序)简介
    Maven重复类的解决
    [转]Unicode和UTF-8的关系
    Java IO 体系结构
    建造者模式(Builder)
  • 原文地址:https://www.cnblogs.com/wangwenhao072093/p/10364697.html
Copyright © 2011-2022 走看看