Python實踐——datetime日期操作腳本,最近需要做一些日期的操作,寫了一個腳本.
- #!/usr/bin/python
- # -*- coding: utf-8 -*-
- #countDays.py
- # version 0.10 edited by lingyue.wkl 20110819 11:00:00
- # version 0.11 modified by lingyue.wkl 20110820 11:37:00 add functions for days list
- #this script count days,between two date or one date and the days between them
-
- #考慮下,很多方法可以抽象出來,進一步優化,先期先實現功能吧
- #下一個版本 改進所有函數,優化之,抽象之
-
-
- import time,getopt,sys,datetime
-
- def date_to_str(in_date):
- return str(in_date)[:10]
- #計算兩個日期之間相隔天數
- def get_count_between_two_date(begin_date,end_date):
- b_date = begin_date.split("-")
- b_date = [int(num) for num in b_date]
- b_date_time = datetime.datetime(b_date[0],b_date[1],b_date[2])
-
- e_date = end_date.split("-")
- e_date = [int(num) for num in e_date]
- e_date_time = datetime.datetime(e_date[0],e_date[1],e_date[2])
-
- return (e_date_time - b_date_time).days
-
- #計算某個日期前n天是哪一天 默認日期是今天
- def get_n_days_before_or_after_oneday(n_days,in_date=str(datetime.date.today())[:10]):
- begin_date = in_date.split("-")
- begin_date = [int(num) for num in begin_date]
- return str(datetime.datetime(begin_date[0],begin_date[1],begin_date[2]) + datetime.timedelta(days=n_days))[:10]
-
-
- def get_year():
- return str(datetime.date.today())[:4]
-
- def get_month():
- return str(datetime.date.today())[5:7]
-
- def get_day():
- return str(datetime.date.today())[8:]
-
- def get_now():
- return datetime.datetime.now()
-
- def get_today():
- return datetime.date.today()
-
- def get_yesterday():
- return get_n_days_before_or_after_oneday(-1,str(datetime.date.today())[:10])
-
- def get_tomorrow():
- return get_n_days_before_or_after_oneday(1,str(datetime.date.today())[:10])
-
- #兩個日期之間 n天的日期列表
-
- def get_n_daystimes_list_of_two_date(begin_date,end_date):
- b_date = begin_date.split("-")
- b_date = [int(num) for num in b_date]
- b_date_time = datetime.datetime(b_date[0],b_date[1],b_date[2])
-
- e_date = end_date.split("-")
- e_date = [int(num) for num in e_date]
- e_date_time = datetime.datetime(e_date[0],e_date[1],e_date[2])
-
- days = (e_date_time - b_date_time).days
- n_days_list = []
- for i in range(0,days+1):
- n_days_list.append(str(b_date_time + datetime.timedelta(days=i)))
- return n_days_list
-
- def get_n_days_list_of_two_date(begin_date,end_date):
- return [str(day)[:10] for day in get_n_daystimes_list_of_two_date(begin_date,end_date)]
-
- def get_n_dayswiththreetimes_list_of_two_date(begin_date,end_date):
- days = get_n_days_list_of_two_date(begin_date,end_date)
- days_three_time_list = []
- for day in days:
- for i in range(0,3):
- if i == 0:
- days_three_time_list.append(day+" 00:00:00")
- elif i == 1:
- days_three_time_list.append(day+" 12:00:00")
- else:
- days_three_time_list.append(day+" 23:59:59")
- return days_three_time_list
-
- #某個日期之前n天 所有日期列表
-
- def get_n_daystimes_list_before_or_after_one_day(n_days,end_date=str(datetime.date.today())[:10]):
- begin_date = get_n_days_before_or_after_oneday(n_days,end_date)
- return get_n_daystimes_list_of_two_date(begin_date,end_date)
-
- def get_n_days_list_before_or_after_one_day(n_days,end_date=str(datetime.date.today())[:10]):
- begin_date = get_n_days_before_or_after_oneday(n_days,end_date)
- return get_n_days_list_of_two_date(begin_date,end_date)
-
- def get_n_dayswiththreetimes_list_before_or_after_one_day(n_days,end_date=str(datetime.date.today())[:10]):
- begin_date = get_n_days_before_or_after_oneday(n_days,end_date)
- return get_n_dayswiththreetimes_list_of_two_date(begin_date,end_date)
-
- def help_msg():
- print("功能:日期相關操作")
- print("選項:")
- print("\t 默認,無選項,輸出當天日期,格式2011-08-20")
- print("\t -y [可選,輸出當前年份]")
- print("\t -m [可選,輸出當前月份]")
- print("\t -d [可選,輸出當前日]")
- print("\t -n +-數字 [可選,計算當前日期前後多少天的日期,數字為負表示往前]")
- print("\t -f 2011-10-22[可選,指定坐標日期,即以指定日期開始計算,若不指定,坐標日期為當天]")
- print("\t -t 2011-10-25 [可選,目標日期,可用於計算兩個日期相隔天數]")
- print("\t -l [1|2|3] [可選,是否列表,若選定,輸出日期間的所有序列,1 2 3 代表三種不同格式]")
- sys.exit(0)
-
- def print_list(l):
- for i in l:
- print(i)
- #print(get_year())
- #print(get_month())
- #print(get_day())
- #print(get_now())
- #print(get_today())
- #print(get_yesterday())
- #print(get_tomorrow())
- #print(get_n_days_before_or_after_oneday(2,"2011-08-20"))
- #print(get_n_daystimes_list_of_two_date("2011-08-01","2011-08-05"))
- #print(get_n_days_list_of_two_date("2011-08-01","2011-08-05"))
- #print(get_n_dayswiththreetimes_list_of_two_date("2011-08-01","2011-08-05"))
- #print(get_n_daystimes_list_before_or_after_ond_day(-5,"2011-08-20"))
- #print(get_n_days_list_before_or_after_ond_day(-5,"2011-08-20"))
- #print(get_n_dayswiththreetimes_list_before_or_after_ond_day(-5,"2011-08-20"))
-
- #程序入口,讀入參數,執行
- def main():
- is_list = False
- try:
- opts,args = getopt.getopt(sys.argv[1:],"n:f:t:o:ymdhrl:")
-
- if len(opts) == 0:
- print(get_today())
- sys.exit(0)
-
- for op,value in opts:
- if op in ("-h","-H","--help"):
- help_msg()
- if op == "-y":
- print(get_year())
- sys.exit(0)
- elif op == "-m":
- print(get_month())
- sys.exit(0)
- elif op == "-d":
- print(get_day())
- sys.exit(0)
- elif op == "-n":
- n_days = int(value)
- elif op == "-f":
- from_date = value
- elif op == "-t":
- to_date = value
- elif op == "-l":
- is_list = True
- list_type = value
-
- except getopt.GetoptError:
- print(sys.argv[0]+" : params are not defined well!")
-
- #if "n_days" not in dir() and "from_date" not in dir() and "to_date" not in dir():
- # print(result_str)
-
- if "n_days" in dir() and "from_date" not in dir() and "to_date" not in dir():
- if not is_list:
- print(get_n_days_before_or_after_today(n_days))
- else:
- if list_type == "1":
- result_list = get_n_days_list_before_or_after_one_day(n_days)
- elif list_type == "2":
- result_list = get_n_daystimes_list_before_or_after_one_day(n_days)
- elif list_type == "3":
- result_list = get_n_dayswiththreetimes_list_before_or_after_one_day(n_days)
- print_list(result_list)
-
- if "n_days" in dir() and "from_date" in dir() and "to_date" not in dir():
- if not is_list:
- print(get_n_days_before_or_after_oneday(n_days,from_date))
- else:
- if list_type == "1":
- result_list = get_n_days_list_before_or_after_one_day(n_days,from_date)
- elif list_type == "2":
- result_list = get_n_daystimes_list_before_or_after_one_day(n_days,from_date)
- elif list_type == "3":
- result_list = get_n_dayswiththreetimes_list_before_or_after_one_day(n_days,from_date)
- print_list(result_list)
-
-
- if "n_days" not in dir() and "from_date" in dir() and "to_date" in dir():
- if not is_list:
- print(get_count_between_two_date(from_date,to_date))
- else:
- if list_type == "1":
- result_list = get_n_days_list_of_two_date(from_date,to_date)
- elif list_type == "2":
- result_list = get_n_daystimes_list_of_two_date(from_date,to_date)
- elif list_type == "3":
- result_list = get_n_dayswiththreetimes_list_of_two_date(from_date,to_date)
- print_list(result_list)
-
-
-
- main()