要求:python3.x
模块:datetime
测试平台:ArchLinux
#!/usr/bin/env python3
import time
def currenttime():
"""
凌晨:0-5
早晨:5-8
上午:8-11
中午:11-13
下午:13-16
傍晚:16-19
晚上:19-24
"""
# H:M:S
time_table = [('0',"凌晨"),
('5',"早晨"),
('8',"上午"),
('11',"中午"),
('13',"下午"),
('16',"傍晚"),
('19',"晚上")
]
date = time.asctime().split(' ')[3].split(':')
#print('date: ',date)
date_mark = ''
hour = ''
for i in time_table:
if int(date[0]) > int(i[0]):
if time_table.index(i) == 6:
date_mark = time_table[-1][1]
break
continue
else:
if int(date[0])==int(i[0]):
date_mark = i[1]
break
date_mark = time_table[time_table.index(i)-1][1]
break
if int(date[0]) < 12:
hour = str(int(date[0]))
else:
hour = str(int(date[0])-12)
str_time = '现在是%s %s点 %s分'%(date_mark,hour,date[1])#,date[2])
print(str_time)
if __name__ == "__main__":
currenttime()
打印结果:
$ python3 currenttime.py
现在是 晚上 10点 50分