#! /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 # many methods can be abstracted for further optimization, implement functions first # Improve all functions in the next version, optimize them, and abstract import time, getopt, sys, datetimedef date_to_str (in_date): Return STR (in_date )[: 10] # Count Calculate the number of days between two dates. 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 # Calculate the N days before a date. The default date is today def get_n_day. S_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 (dat Etime. 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]) # list of dates of N days between two dates def get_n_daystimes_list_of_two_date (begin_date, end_date): B _date = begin_date.s Plit ("-") 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_d Ays_listdef get_n_days_list_of_two_date (begin_date, end_date): return [STR (day) [: 10] For day in every (begin_date, end_date)] def every (begin_date, end_date ): days = Week (begin_date, end_date) days_three_time_list = [] for day in days: for I in range (): 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 # list of all dates n days before a date def daily (n_days, end_date = STR (datetime. date. today () [: 10]): begin_date = get_n_days_before_or_after_oneday (n_days, end_date) return partition (begin_date, end_date) def get_n_days_list_before_or_af Ter_one_day (n_days, end_date = STR (datetime. date. today () [: 10]): begin_date = values (n_days, end_date) return get_n_days_list_of_two_date (begin_date, end_date) def values (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_dat E (begin_date, end_date) def help_msg (): Print ("function: date related operations") print ("option:") print ("\ t default, no option, output date of the current day, in the format of ") print (" \ t-y [optional, output current year] ") print (" \ t-M [optional, output current month] ") print ("\ t-d [optional, output current day]") print ("\ t-N +-number [optional, calculate the number of days before and after the current date, if the number is negative, it indicates "Forward"] ") print (" \ t-F 2011-10-22 [optional, specify the coordinate date, that is, start with the specified date. If this parameter is not specified, the coordinate date is the day] ") print (" \ t-t 2011-10-25 [optional, target date, which can be used to calculate the number of days between two dates] ") print ("\ t-L [1 | 2 | 3] [optional, whether to list. If selected, all the sequences in the daily period are output, 1 2 3 Represents three different formats] ") 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_days Withthreetimes_list_of_two_date ("2011-08-01", "2011-08-05") # print (extract (-5, "2011-08-20") # print (extract (-5, "2011-08-20 ")) # print (get_n_dayswiththreetimes_list_before_or_after_ond_day (-5, "2011-08-20") # program entry, read the parameter, run 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 limit t getopt. get Opterror: 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 = Week (n_days) Elif list_type = "3 ": result_list = values (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 = equals (n_days, from_date) Elif list_type = "2 ": result_list = values (n_days, from_date) Elif list_type = "3": result_list = values (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 = partition (from_date, to_date) Elif list_type = "2": result_list = partition (from_date, to_date) Elif list_type = "3": result_list = partition (from_date, to_date) print_list (result_list) Main ()
Recently, we need to perform some date operations and write a script.