您好,登錄后才能下訂單哦!
內(nèi)容如題
原因:solaris等os不能做時(shí)間的運(yùn)算處理,個(gè)人愛好。之前用c實(shí)現(xiàn)了一版,這里再次用python實(shí)現(xiàn)一版,后期應(yīng)該還有改進(jìn)版
改進(jìn):1 代碼優(yōu)化
2 添加了指定獲取月份最后一天的功能
第四版:
添加了-d選項(xiàng),-d date date_format,既可以指定時(shí)間字符串和時(shí)間格式,格式可以不指定默認(rèn)為 %Y%m%d或%Y-%m-%d
第五版:
1 修進(jìn)了一些bug
2 把入口函數(shù)添加到類中,方便其他python的調(diào)用,而不是單純的腳本
第六版:
在類中使用了初始化函數(shù),方便調(diào)用,可以在初始化函數(shù)中直接設(shè)置需要輸入的參數(shù),而不必對(duì)內(nèi)部變量作出設(shè)置
第七版:
修改了-d參數(shù)從對(duì)時(shí)間戳的支持,例:-d "
1526606217
" "%s"
第八版:
修正版了部分代碼,減少代碼行數(shù)。
腳本下載地址:https://github.com/raysuen/rdate
#!/usr/bin/env python # _*_coding:utf-8_*_ # Auth by raysuen # version v8.0 import datetime import time import calendar import sys import re # 時(shí)間計(jì)算的類 class DateColculation(object): rdate = { "time_tuple": time.localtime(), "time_format": "%Y-%m-%d %H:%M:%S %A", "colculation_string": None, "last_day": False, "input_time": None, "input_format": None } def __init__(self,time_tuple=None,out_format=None,col_string=None,isLastday=None,in_time=None,in_format=None): if time_tuple != None: self.rdate["time_tuple"] = time_tuple if out_format != None: self.rdate["time_format"] = out_format if col_string != None: self.rdate["colculation_string"] = col_string if isLastday != None: self.rdate["last_day"] = isLastday if in_time != None: self.rdate["input_time"] = in_time if in_format != None: self.rdate["input_format"] = in_format # 月計(jì)算的具體實(shí)現(xiàn)函數(shù) def __R_MonthAdd(self, col_num, add_minus, lastday, time_truct): R_MA_num = 0 # 記錄計(jì)算的月的數(shù)字 R_ret_tuple = None # 返回值,None或者時(shí)間元組 R_MA_datetime = None # 臨時(shí)使用的datetime類型 if type(col_num) != int: # 判斷傳入的參數(shù)是否為數(shù)字 print("the parameter type is wrong!") exit(5) if time_truct == None: R_MA_datetime = datetime.datetime.now() # 獲取當(dāng)前時(shí)間 else: R_MA_datetime = datetime.datetime.fromtimestamp(time.mktime(time_truct)) if add_minus.lower() == "add": # 判斷是否為+ R_MA_num = R_MA_datetime.month + col_num if R_MA_num > 12: # 判斷相加后的月份數(shù)是否大于12,如果大于12,需要在年+1 while R_MA_num > 12: R_MA_datetime = R_MA_datetime.replace(year=R_MA_datetime.year + 1) R_MA_num = R_MA_num - 12 R_ret_tuple = self.__days_add(R_MA_datetime, R_MA_num, lastday).timetuple() else: R_ret_tuple = self.__days_add(R_MA_datetime, R_MA_num, lastday).timetuple() elif add_minus.lower() == "minus": # 判斷是否為- while col_num >= 12: # 判斷傳入的參數(shù)是否大于12,如果大于12則對(duì)年做處理 R_MA_datetime = R_MA_datetime.replace(year=R_MA_datetime.year - 1) col_num = col_num - 12 # R_MA_num = 12 + (R_MA_datetime.month - col_num) # 獲取將要替換的月份的數(shù)字 if R_MA_datetime.month - col_num < 0: # 判斷當(dāng)前月份數(shù)字是否大于傳入?yún)?shù)(取模后的),小于0表示,年需要減1,并對(duì)月份做處理 if R_MA_datetime.day > calendar.monthrange(R_MA_datetime.year - 1, R_MA_datetime.month)[ 1]: # 如果年減一后,當(dāng)前日期的天數(shù)大于年減一后的天數(shù),則在月份加1,天變更為當(dāng)前日期天數(shù)減變更后的月份天數(shù) R_MA_datetime = R_MA_datetime.replace(year=R_MA_datetime.year - 1, month=R_MA_datetime.month + 1, day=(R_MA_datetime.day > calendar.monthrange(R_MA_datetime.year - 1, R_MA_datetime.month)[1])) # 年減1 else: R_MA_datetime = R_MA_datetime.replace(year=R_MA_datetime.year - 1) # 年減1 R_MA_datetime = self.__days_add(R_MA_datetime, 12 - abs(R_MA_datetime.month - col_num), lastday) elif R_MA_datetime.month - col_num == 0: # 判斷當(dāng)前月份數(shù)字是否等于傳入?yún)?shù)(取模后的),等于0表示,年減1,月份替換為12,天數(shù)不變(12月為31天,不可能會(huì)存在比31大的天數(shù)) R_MA_datetime = R_MA_datetime.replace(year=R_MA_datetime.year - 1, month=12) elif R_MA_datetime.month - col_num > 0: # 默認(rèn)表示當(dāng)前月份-傳入?yún)?shù)(需要減去的月數(shù)字)大于0,不需要處理年 R_MA_datetime = self.__days_add(R_MA_datetime, R_MA_datetime.month - col_num, lastday) R_ret_tuple = R_MA_datetime.timetuple() return R_ret_tuple # 返回時(shí)間元組 def __days_add(self, formal_MA_datetime, formal_MA_num, lastday): R_MA_datetime = formal_MA_datetime R_MA_num = formal_MA_num if lastday: # 如果計(jì)算月最后一天,則直接把月份替換,天數(shù)為月份替換后的最后一天 R_MA_datetime = R_MA_datetime.replace(month=R_MA_num, day=calendar.monthrange(R_MA_datetime.year, R_MA_num)[ 1]) # 月份替換,天數(shù)為替換月的最后一天 else: if R_MA_datetime.day > \ calendar.monthrange(R_MA_datetime.year, R_MA_num)[ 1]: # 判斷當(dāng)前日期的天數(shù)是否大于替換后的月份天數(shù),如果大于,月份在替換后的基礎(chǔ)上再加1,天數(shù)替換為當(dāng)前月份天數(shù)減替換月份天數(shù) R_MA_datetime = R_MA_datetime.replace(month=R_MA_num + 1, day=R_MA_datetime.day - calendar.monthrange(R_MA_datetime.year, R_MA_num)[ 1]) # 月份在替換月的數(shù)字上再加1,天數(shù)替換為當(dāng)前月份天數(shù)減替換月份天數(shù) else: R_MA_datetime = R_MA_datetime.replace(month=R_MA_num) # 獲取替換月份,day不變 return R_MA_datetime # 月計(jì)算的入口函數(shù) def R_Month_Colculation(self, R_ColStr, lastday, time_truct): R_ret_tuple = None if R_ColStr.find("-") != -1: # 判斷-是否存在字符串 col_num = R_ColStr.split("-")[-1].strip() # 獲取需要計(jì)算的數(shù)字 if col_num.strip().isdigit(): # 判斷獲取的數(shù)字是否為正整數(shù) R_ret_tuple = self.__R_MonthAdd(int(col_num.strip()), "minus", lastday, time_truct) # 獲取tuple time時(shí)間格式 else: # 如果獲取的數(shù)字不為正整數(shù),則退出程序 print("Please enter right format symbol!!") print("If you don't kown what values is avalable,please use -h to get help!") exit(4) elif R_ColStr.find("+") != -1: # 判斷+是否存在字符串 col_num = R_ColStr.split("+")[-1].strip() # 獲取需要計(jì)算的數(shù)字 if col_num.strip().isdigit(): # 判斷獲取的數(shù)字是否為正整數(shù) R_ret_tuple = self.__R_MonthAdd(int(col_num.strip()), "add", lastday, time_truct) # 獲取tuple time時(shí)間格式 else: print("Please enter right format symbol!!") print("If you don't kown what values is avalable,please use -h to get help!") exit(4) return R_ret_tuple # 秒,分,時(shí),日,周計(jì)算的實(shí)現(xiàn)函數(shù) def R_General_Colculation(self, R_ColStr, time_truct, cal_parm): R_ret_tuple = None if time_truct == None: # 判斷是否指定了輸入時(shí)間,沒指定則獲取當(dāng)前時(shí)間,否則使用指定的輸入時(shí)間 R_Datatime = datetime.datetime.now() else: R_Datatime = datetime.datetime.fromtimestamp(time.mktime(time_truct)) if R_ColStr.find("-") != -1: # 判斷-是否存在字符串 col_num = R_ColStr.split("-")[-1].strip() # 獲取需要計(jì)算的數(shù)字 if col_num.strip().isdigit(): # 判斷獲取的數(shù)字是否為正整數(shù) if R_ColStr.strip().lower().find("second") != -1: R_ret_tuple = (R_Datatime + datetime.timedelta( seconds=-int(col_num.strip()))).timetuple() # 獲取tuple time時(shí)間格式 elif R_ColStr.strip().lower().find("minute") != -1: R_ret_tuple = (R_Datatime + datetime.timedelta( minutes=-int(col_num.strip()))).timetuple() # 獲取tuple time時(shí)間格式 elif R_ColStr.strip().lower().find("hour") != -1: R_ret_tuple = (R_Datatime + datetime.timedelta( hours=-int(col_num.strip()))).timetuple() # 獲取tuple time時(shí)間格式 elif R_ColStr.strip().lower().find("day") != -1: R_ret_tuple = (R_Datatime + datetime.timedelta( days=-int(col_num.strip()))).timetuple() # 獲取tuple time時(shí)間格式 elif R_ColStr.strip().lower().find("week") != -1: R_ret_tuple = (R_Datatime + datetime.timedelta( weeks=-int(col_num.strip()))).timetuple() # 獲取tuple time時(shí)間格式 # R_ret_tuple = (R_Datatime + datetime.timedelta(cal_parm=-int(col_num.strip()))).timetuple() # 獲取tuple time時(shí)間格式 else: # 如果獲取的數(shù)字不為正整數(shù),則退出程序 print("Please enter right format symbol!!") print("If you don't kown what values is avalable,please use -h to get help!") exit(4) elif R_ColStr.find("+") != -1: # 判斷+是否存在字符串 col_num = R_ColStr.split("+")[-1].strip() # 獲取需要計(jì)算的數(shù)字 if col_num.strip().isdigit(): # 判斷獲取的數(shù)字是否為正整數(shù) if R_ColStr.strip().lower().find("second") != -1: R_ret_tuple = (R_Datatime + datetime.timedelta( seconds=int(col_num.strip()))).timetuple() # 獲取tuple time時(shí)間格式 elif R_ColStr.strip().lower().find("minute") != -1: R_ret_tuple = (R_Datatime + datetime.timedelta( minutes=int(col_num.strip()))).timetuple() # 獲取tuple time時(shí)間格式 elif R_ColStr.strip().lower().find("hour") != -1: R_ret_tuple = (R_Datatime + datetime.timedelta( hours=int(col_num.strip()))).timetuple() # 獲取tuple time時(shí)間格式 elif R_ColStr.strip().lower().find("day") != -1: R_ret_tuple = (R_Datatime + datetime.timedelta( days=int(col_num.strip()))).timetuple() # 獲取tuple time時(shí)間格式 elif R_ColStr.strip().lower().find("week") != -1: R_ret_tuple = (R_Datatime + datetime.timedelta( weeks=int(col_num.strip()))).timetuple() # 獲取tuple time時(shí)間格式 else: print("Please enter right format symbol!!") print("If you don't kown what values is avalable,please use -h to get help!") exit(4) return R_ret_tuple # 年計(jì)算的實(shí)現(xiàn)函數(shù) def R_Year_Colculation(self, R_ColStr, time_truct): R_ret_tuple = None if time_truct == None: # 判斷是否指定了輸入時(shí)間,沒指定則獲取當(dāng)前時(shí)間,否則使用指定的輸入時(shí)間 R_Y_Datatime = datetime.datetime.now() else: R_Y_Datatime = datetime.datetime.fromtimestamp(time.mktime(time_truct)) if R_ColStr.find("-") != -1: # 判斷-是否存在字符串 col_num = R_ColStr.split("-")[-1].strip() # 獲取需要計(jì)算的數(shù)字 if col_num.strip().isdigit(): # 判斷獲取的數(shù)字是否為正整數(shù) # 判斷當(dāng)前時(shí)間是否為閏年并且為二月29日,如果是相加/減后不為閏年則在月份加1,日期加1 if calendar.isleap( R_Y_Datatime.year) and R_Y_Datatime.month == 2 and R_Y_Datatime.day == 29 and calendar.isleap( R_Y_Datatime.year - int(col_num.strip())) == False: R_ret_tuple = ( R_Y_Datatime.replace(year=R_Y_Datatime.year - int(col_num.strip()), month=R_Y_Datatime.month + 1, day=1)).timetuple() # 獲取tuple time時(shí)間格式 else: R_ret_tuple = ( R_Y_Datatime.replace( year=R_Y_Datatime.year - int(col_num.strip()))).timetuple() # 獲取tuple time時(shí)間格式 else: # 如果獲取的數(shù)字不為正整數(shù),則退出程序 print("Please enter right format symbol!!") print("If you don't kown what values is avalable,please use -h to get help!") exit(4) elif R_ColStr.find("+") != -1: # 判斷+是否存在字符串 col_num = R_ColStr.split("+")[-1].strip() # 獲取需要計(jì)算的數(shù)字 if col_num.strip().isdigit(): # 判斷獲取的數(shù)字是否為正整數(shù) # 判斷當(dāng)前時(shí)間是否為閏年并且為二月29日,如果是相加/減后不為閏年則在月份加1,日期加1 if calendar.isleap( R_Y_Datatime.year) and R_Y_Datatime.month == 2 and R_Y_Datatime.day == 29 and calendar.isleap( R_Y_Datatime.year + col_num.strip()) == False: R_ret_tuple = ( R_Y_Datatime.replace(year=R_Y_Datatime.year - int(col_num.strip()), month=R_Y_Datatime.month + 1, day=1)).timetuple() # 獲取tuple time時(shí)間格式 else: R_ret_tuple = ( R_Y_Datatime.replace( year=R_Y_Datatime.year + int(col_num.strip()))).timetuple() # 獲取tuple time時(shí)間格式 else: print("Please enter right format symbol!!") print("If you don't kown what values is avalable,please use -h to get help!") exit(4) return R_ret_tuple # 獲取月的最后一天 def R_Month_lastday(self, time_tuple): R_MA_datetime = datetime.datetime.fromtimestamp(time.mktime(time_tuple)) # time_tuple R_MA_datetime = R_MA_datetime.replace(day=(calendar.monthrange(R_MA_datetime.year, R_MA_datetime.month)[1])) return R_MA_datetime.timetuple() def R_colculation(self): ret_tupletime = None ColStr = self.rdate["colculation_string"] lastday = self.rdate["last_day"] input_time = None if ColStr != None: if type(ColStr) != str: print("Please enter right format symbol!!") print("If you don't kown what values is avalable,please use -h to get help!") exit(3) if (ColStr.find("-") != -1) and (ColStr.find("+") != -1): print("Please enter right format symbol!!") print("If you don't kown what values is avalable,please use -h to get help!") exit(3) if self.rdate["input_time"] != None: if self.rdate["input_format"] == None: i = 1 while 1: try: if i < 2: input_time = time.strptime(self.rdate["input_time"], "%Y%m%d") else: input_time = time.strptime(self.rdate["input_time"], "%Y-%m-%d") break except ValueError as e: if i < 2: i+=1 continue print("The input time and format do not match.") exit(98) elif self.rdate["input_format"] == "%s": if self.rdate["input_time"].isdigit(): input_time = time.localtime(int(self.rdate["input_time"])) else: print("The input time must be number.") exit(97) else: try: input_time = time.strptime(self.rdate["input_time"], self.rdate["input_format"]) except ValueError as e: print("The input time and format do not match.") exit(98) if lastday: if ColStr == None: if input_time != None: ret_tupletime = self.R_Month_lastday(input_time) else: ret_tupletime = self.R_Month_lastday(time.localtime()) # second的計(jì)算 # elif ColStr.strip().lower().find("second") != -1: # 判斷是否傳入的字符串中是否存在hour關(guān)鍵字 # ret_tupletime = self.R_General_Colculation(ColStr.strip().lower(), input_time,"seconds") # # minute的計(jì)算 # elif ColStr.strip().lower().find("minute") != -1: # 判斷是否傳入的字符串中是否存在hour關(guān)鍵字 # # ret_tupletime = self.R_Minute_Colculation(ColStr.strip().lower(), input_time) # ret_tupletime = self.R_General_Colculation(ColStr.strip().lower(), input_time, "minutes") # # hour的計(jì)算 # elif ColStr.strip().lower().find("hour") != -1: # 判斷是否傳入的字符串中是否存在hour關(guān)鍵字 # # ret_tupletime = self.R_Hour_Colculation(ColStr.strip().lower(), input_time) # ret_tupletime = self.R_General_Colculation(ColStr.strip().lower(), input_time, "hours") # # day的計(jì)算 # elif ColStr.strip().lower().find("day") != -1: # 判斷是否傳入的字符串中是否存在day關(guān)鍵字 # # ret_tupletime = self.R_Month_lastday(self.R_Day_Colculation(ColStr.strip().lower(), input_time)) # ret_tupletime = self.R_General_Colculation(ColStr.strip().lower(), input_time, "days") # # week的計(jì)算 # elif ColStr.strip().lower().find("week") != -1: # 判斷是否傳入的字符串中是否存在day關(guān)鍵字 # # ret_tupletime = self.R_Month_lastday(self.R_Week_Colculation(ColStr.strip().lower(), input_time)) # ret_tupletime = self.R_General_Colculation(ColStr.strip().lower(), input_time, "weeks") elif ColStr.strip().lower().find("second") != -1 or ColStr.strip().lower().find("minute") != -1 or ColStr.strip().lower().find("hour") != -1 or ColStr.strip().lower().find("day") != -1 or ColStr.strip().lower().find("week") != -1: ret_tupletime = self.R_General_Colculation(ColStr.strip().lower(), input_time) # month的計(jì)算 elif ColStr.strip().lower().find("month") != -1: # 判斷是否傳入的字符串中是否存在day關(guān)鍵字 ret_tupletime = self.R_Month_lastday(self.R_Month_Colculation(ColStr.strip().lower(), lastday, input_time)) # year的計(jì)算 elif ColStr.strip().lower().find("year") != -1: # 判斷是否傳入的字符串中是否存在day關(guān)鍵字 ret_tupletime = self.R_Month_lastday(self.R_Year_Colculation(ColStr.strip().lower(), input_time)) else: print("Please enter right format symbol of -c.") print("If you don't kown what values is avalable,please use -h to get help!") exit(3) else: if ColStr == None: if self.rdate["input_time"] != None: ret_tupletime = input_time else: ret_tupletime = time.localtime() # second的計(jì)算 elif ColStr.strip().lower().find("second") != -1: # 判斷是否傳入的字符串中是否存在hour關(guān)鍵字 ret_tupletime = self.R_General_Colculation(ColStr.strip().lower(), input_time, "seconds") # minute的計(jì)算 elif ColStr.strip().lower().find("minute") != -1: # 判斷是否傳入的字符串中是否存在hour關(guān)鍵字 # ret_tupletime = self.R_Minute_Colculation(ColStr.strip().lower(), input_time) ret_tupletime = self.R_General_Colculation(ColStr.strip().lower(), input_time, "minutes") # hour的計(jì)算 elif ColStr.strip().lower().find("hour") != -1: # 判斷是否傳入的字符串中是否存在hour關(guān)鍵字 # ret_tupletime = self.R_Hour_Colculation(ColStr.strip().lower(), input_time) ret_tupletime = self.R_General_Colculation(ColStr.strip().lower(), input_time, "hours") # day的計(jì)算 elif ColStr.strip().lower().find("day") != -1: # 判斷是否傳入的字符串中是否存在day關(guān)鍵字 # ret_tupletime = self.R_Month_lastday(self.R_Day_Colculation(ColStr.strip().lower(), input_time)) ret_tupletime = self.R_General_Colculation(ColStr.strip().lower(), input_time, "days") # week的計(jì)算 elif ColStr.strip().lower().find("week") != -1: # 判斷是否傳入的字符串中是否存在day關(guān)鍵字 # ret_tupletime = self.R_Month_lastday(self.R_Week_Colculation(ColStr.strip().lower(), input_time)) ret_tupletime = self.R_General_Colculation(ColStr.strip().lower(), input_time, "weeks") # month的計(jì)算 elif ColStr.strip().lower().find("month") != -1: # 判斷是否傳入的字符串中是否存在day關(guān)鍵字 ret_tupletime = self.R_Month_Colculation(ColStr.strip().lower(), lastday, input_time) # year的計(jì)算 elif ColStr.strip().lower().find("year") != -1: # 判斷是否傳入的字符串中是否存在day關(guān)鍵字 ret_tupletime = self.R_Year_Colculation(ColStr.strip().lower(), input_time) else: print("Please enter right format symbol of -c.") print("If you don't kown what values is avalable,please use -h to get help!") exit(3) return ret_tupletime def func_help(): print(""" NAME: rdate --display date and time SYNOPSIS: rdate [-f] [time format] [-c] [colculation format] [-d] [input_time] [input_time_format] DESCRIPTION: -c: value is hour/day/week/month/year,plus +/-,plus a number which is number to colculate -l: obtain a number which is last day of month -d: input_time: enter a time string input_time_format: enter a time format for input time,default %Y%m%d or %Y-%m-%d -f: %A is replaced by national representation of the full weekday name. %a is replaced by national representation of the abbreviated weekday name. %B is replaced by national representation of the full month name. %b is replaced by national representation of the abbreviated month name. %C is replaced by (year / 100) as decimal number; single digits are preceded by a zero. %c is replaced by national representation of time and date. %D is equivalent to ``%m/%d/%y''. %d is replaced by the day of the month as a decimal number (01-31). %E* %O* POSIX locale extensions. The sequences %Ec %EC %Ex %EX %Ey %EY %Od %Oe %OH %OI %Om %OM %OS %Ou %OU %OV %Ow %OW %Oy are supposed to provide alternate representations. Additionally %OB implemented to represent alternative months names (used standalone, without day mentioned). %e is replaced by the day of the month as a decimal number (1-31); single digits are preceded by a blank. %F is equivalent to ``%Y-%m-%d''. %G is replaced by a year as a decimal number with century. This year is the one that contains the greater part of the week (Monday as the first day of the week). %g is replaced by the same year as in ``%G'', but as a decimal number without century (00-99). %H is replaced by the hour (24-hour clock) as a decimal number (00-23). %h the same as %b. %I is replaced by the hour (12-hour clock) as a decimal number (01-12). %j is replaced by the day of the year as a decimal number (001-366). %k is replaced by the hour (24-hour clock) as a decimal number (0-23); single digits are preceded by a blank. %l is replaced by the hour (12-hour clock) as a decimal number (1-12); single digits are preceded by a blank. %M is replaced by the minute as a decimal number (00-59). %m is replaced by the month as a decimal number (01-12). %n is replaced by a newline. %O* the same as %E*. %p is replaced by national representation of either ante meridiem (a.m.) or post meridiem (p.m.) as appropriate. %R is equivalent to ``%H:%M''. %r is equivalent to ``%I:%M:%S %p''. %S is replaced by the second as a decimal number (00-60). %s is replaced by the number of seconds since the Epoch, UTC (see mktime(3)). %T is equivalent to ``%H:%M:%S''. %t is replaced by a tab. %U is replaced by the week number of the year (Sunday as the first day of the week) as a decimal number (00-53). %u is replaced by the weekday (Monday as the first day of the week) as a decimal number (1-7). %V is replaced by the week number of the year (Monday as the first day of the week) as a decimal number (01-53). If the week containing January 1 has four or more days in the new year, then it is week 1; otherwise it is the last week of the previous year, and the next week is week 1. %v is equivalent to ``%e-%b-%Y''. %W is replaced by the week number of the year (Monday as the first day of the week) as a decimal number (00-53). %w is replaced by the weekday (Sunday as the first day of the week) as a decimal number (0-6). %X is replaced by national representation of the time. %x is replaced by national representation of the date. %Y is replaced by the year with century as a decimal number. %y is replaced by the year without century as a decimal number (00-99). %Z is replaced by the time zone name. %z is replaced by the time zone offset from UTC; a leading plus sign stands for east of UTC, a minus sign for west of UTC, hours and minutes follow with two digits each and no delimiter between them (common form for RFC 822 date headers). %+ is replaced by national representation of the date and time (the format is similar to that produced by date(1)). %-* GNU libc extension. Do not do any padding when performing numerical outputs. %_* GNU libc extension. Explicitly specify space for padding. %0* GNU libc extension. Explicitly specify zero for padding. %% is replaced by `%'. EXAMPLE: rdate --2017-10-23 11:04:51 Monday rdate -f "%Y-%m_%d" --2017-10-23 rdate -f "%Y-%m_%d" -c "day-3" --2017-10-20 rdate -f "%Y-%m_%d" -c "day+3" --2017-10-26 rdate -f "%Y-%m_%d" -c "month+3" --2017-7-23 rdate -f "%Y-%m_%d" -c "year+3" --2020-7-23 rdate -c "week - 1" -f "%Y-%m-%d %V" --2018-02-15 07 rdate -c "day - 30" -f "%Y-%m-%d" -l --2018-01-31 rdate -d "1972-01-31" "%Y-%m-%d" --1972-01-31 00:00:00 Monday """) if __name__ == "__main__": d1 = DateColculation() if len(sys.argv) > 1: i = 1 while i < len(sys.argv): if sys.argv[i] == "-h": # 判斷輸入的參數(shù)是否為-h,既獲取幫助 func_help() exit(0) elif sys.argv[i] == "-f": # -f表示format,表示指定的輸出時(shí)間格式 i = i + 1 if i >= len(sys.argv): # 判斷-f的值的下標(biāo)是否大于等于參數(shù)個(gè)數(shù),如果為真則表示沒有指定-f的值 print("The value of -f must be specified!!!") exit(1) elif sys.argv[i] == "-c": print("The value of -f must be specified!!!") exit(1) elif re.match("^-", sys.argv[i]) != None: # 判斷-f的值,如果-f的下個(gè)參數(shù)以-開頭,表示沒有指定-f值 print("The value of -f must be specified!!!") exit(1) d1.rdate["time_format"] = sys.argv[i] # 獲取輸出時(shí)間格式 elif sys.argv[i] == "-c": # -c表示colculation,計(jì)算 i = i + 1 if i >= len(sys.argv): # 判斷-f的值的下標(biāo)是否大于等于參數(shù)個(gè)數(shù),如果為真則表示沒有指定-f的值 print("The value of -c must be specified!!!") exit(2) elif sys.argv[i] == "-f": print("The value of -c must be specified!!!") exit(2) elif (re.match("^-", sys.argv[i]) != None): # 判斷-f的值,如果-f的下個(gè)參數(shù)以-開頭,表示沒有指定-f值 print("The value of -c must be specified!!!") exit(2) d1.rdate["colculation_string"] = sys.argv[i] # 獲取需要計(jì)算的字符串參數(shù)內(nèi)容 elif sys.argv[i] == "-d": # -d date 表示指定輸入的時(shí)間和輸入的時(shí)間格式 i += 1 if i >= len(sys.argv): # 判斷-d的值的下標(biāo)是否大于等于參數(shù)個(gè)數(shù),如果為真則表示沒有指定-的值 print("The value of -d must be specified!!!") exit(3) elif (re.match("^-", sys.argv[i]) != None): # 判斷-d的值,如果-df的下個(gè)參數(shù)以-開頭,表示沒有指定-df值 print("The value of -c must be specified!!!") exit(3) d1.rdate["input_time"] = sys.argv[i] if (i+1 < len(sys.argv) and re.match("^-", sys.argv[(i+1)]) == None): d1.rdate["input_format"] = sys.argv[i+1] i+=1 elif sys.argv[i] == "-l": # -l表示獲取月份的最后一天 d1.rdate["last_day"] = True else: print("You must enter right parametr.") print("If you don't kown what values is avalable,please use -h to get help!") exit(3) i = i + 1 d1.rdate["time_tuple"] = d1.R_colculation() # 獲取時(shí)間的元組,通過R_colculation函數(shù),R_colculation參數(shù)為傳入一個(gè)需要計(jì)算的時(shí)間字符串 print(time.strftime(d1.rdate["time_format"], d1.rdate["time_tuple"])) exit(0) else: # 如果不輸入?yún)?shù),則輸出默認(rèn)格式化的本地時(shí)間 print(time.strftime(d1.rdate["time_format"], d1.rdate["time_tuple"])) exit(0)
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。