今天,我发现了一个抽象的事情,就是Time库的输出格式很奇怪。因此,不满意自然是要动手重构。下面我写了一个模块,文件是Time_Overwrite.py。代码文件内容如下:
python
import subprocess
global character,Tips
Tips = (
"The Time Style is Error!!\nPlease you input '-' or '_' or '/'\nSet Time as "
"Year-Month-Day-Weekday or Year_Month_Day_Weekday or Year/Month/Day/Weekday"
)
def YMD_Time(character):
Dos_Time = subprocess.getoutput('ECHO %DATE%')
if character == '/':
DT = Dos_Time.replace(' ',f'{character}')
return DT
elif character == '-':
DT = Dos_Time.replace(' ',f'{character}').replace('/',f'{character}')
return DT
elif character == '_':
DT = Dos_Time.replace(' ',f'{character}').replace('/',f'{character}')
return DT
else:
return Tips
def HMS_Time():
DosTime = subprocess.getoutput("ECHO %Time%")
DosTime2 = DosTime.strip()
Time = DosTime2[:-3]
return Time
def All_Time(character=None):
Half_Time1 = YMD_Time(character)
if Half_Time1 != Tips:
Half_Time2 = HMS_Time()
Total_Time = Half_Time1 + ' ' + Half_Time2
return Total_Time
else:
return Tips
我在同一目录下的另一个example.py调用它,测试代码如下:
python
import Time_Overwrite as TO
Today_Time = TO.YMD_Time('_')
print(Today_Time)
Today_Time = TO.YMD_Time('-')
print(Today_Time)
Today_Time = TO.YMD_Time('/')
print(Today_Time)
Today_Time = TO.YMD_Time('%')
print(Today_Time)
print()
NowTime = TO.HMS_Time()
print(NowTime)
print()
Total_Time = TO.All_Time()
print(Total_Time)
Total_Time = TO.All_Time('_')
print(Total_Time)
Total_Time = TO.All_Time('/')
print(Total_Time)
Total_Time = TO.All_Time('-')
print(Total_Time)
Total_Time = TO.All_Time('%')
print(Total_Time)
测试结果:

总结
不满意就自己动手重构,没有人是完美的,前人设计的Time库值得肯定,不过能更新继续发展才是正道,而不是一边温水煮青蛙,还要一直硬着头皮坚持原来的旧路。