|
发表于 2018-1-31 12:12:16
|
显示全部楼层
- def getSeconds(start_time,stop_time):
- '''给出两个时间算出时间差(以秒计)
- 如果结束时间小于开始时间则结束时间在下一天
- 开始和结束时间不超过1天'''
- if start_time.count(':')==2:
- if stop_time.count(':')==2:
- #计算时间差
- #先分割出时分秒
- (sta_hours,sta_mins,sta_sec)=start_time.split(':',2)
- (stop_hours,stop_mins,stop_sec)=stop_time.split(':',2)
- #把这些数字通通转化为int类型
- sta_hours = int(sta_hours)
- sta_mins = int(sta_mins)
- sta_sec = int(sta_sec)
- stop_hours = int(stop_hours)
- stop_mins = int(stop_mins)
- stop_sec = int(stop_sec)
- #分别用时分秒对应相减
- #计算秒钟差
- if stop_sec<sta_sec:
- stop_sec+=60
- stop_mins-=1
- get_sec=stop_sec-sta_sec
- #计算分钟差
- if stop_mins<sta_mins:
- stop_mins+=60
- stop_hours-=1
- get_mins=stop_mins-sta_mins
- #计算小时差
- if stop_hours<sta_hours:
- day=1
- get_hours=sta_hours-stop_hours
- else:
- get_hours=stop_hours-sta_hours
- #将时分秒按照输入时的格式输出
- if day==1:
- print('%dday:%dh:%dmin:%ds'%(day,get_hours,get_mins,get_sec))
- else:
- print('%dh:%dmin:%ds'%(get_hours,get_mins,get_sec))
- #将时分秒转换为秒钟
- if day==1:
- final_sec=3600*24+get_hours*3600+get_mins*60+get_sec
- else:
- final_sec=get_hours*3600+get_mins*60+get_sec
- print(str(final_sec))
- else:
- print('输入结束时间格式错误!')
- else:
- print('输入开始时间格式错误!')
-
-
- start_time = input('请输入开始时间,格式xx:xx:xx\n')
- stop_time = input('请输入结束时间,格式同上\n')
- getSeconds(start_time,stop_time)
复制代码 |
评分
-
查看全部评分
|