鱼C论坛

 找回密码
 立即注册
12
返回列表 发新帖
楼主: 冬雪雪冬

[技术交流] Python:每日一题 142

[复制链接]
发表于 2018-1-17 14:55:48 | 显示全部楼层
start = '17:55:31'
stop = '4:21:57'
startList = start.split(':')
stopList = stop.split(':')

#将str change to int
for i in range(len(startList)):
    startList[i] = int(startList[i])
for j in range(len(stopList)):
    stopList[j] = int(stopList[j])

#计算秒:
if  stopList[2]-startList[2]<0:
    if(stopList[1]>0):
        stopList[1] = stopList[1]-1
        temp2 = stopList[2]-startList[2]+60
    else:
        stopList[1] = stopList[1]-1+60
        temp2 = stopList[2]-startList[2]+60
else:
    temp2 = stopList[2]-startList[2]
#计算分:
if stopList[1] - startList[1]<0:
    if(stopList[0]>0):
        stopList[0] = stopList[0] -1
        temp1 =( stopList[1] - startList[1]+60)*60
    else:
        stopList[0] = stopList[0] -1+24
        temp1 =( stopList[1] - startList[1]+60)*60
else:
    temp1 =( stopList[1] - startList[1])*60
#计算时:
if(stopList[0]-startList[0]>=0):
    temp0 =(stopList[0]-startList[0])*3600
else:
    temp0 =(stopList[0]-startList[0]+24)*3600
print('时间差为:',temp0+temp1+temp2)


时间差为: 37586

评分

参与人数 1荣誉 +3 鱼币 +3 收起 理由
冬雪雪冬 + 3 + 3

查看全部评分

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-1-17 16:08:09 | 显示全部楼层
  1. def time_diff(start,stop):
  2.         start_list = start.split(':')
  3.         stop_list = stop.split(':')
  4.         print(start_list,stop_list)
  5.         start_out = int(start_list[0])*3600 + int(start_list[1])*60 + int(start_list[2])
  6.         stop_out = int(stop_list[0])*3600 + int(stop_list[1])*60 + int(stop_list[2])
  7.         if int(start_list[0]) <= int(stop_list[0]):
  8.                 result = stop_out - start_out
  9.         else:
  10.                 result = 24*3600 + stop_out - start_out

  11.         return result

  12. start = input('请输入开始时间(例如:2:30:50):')
  13. stop = input('请输入结束时间(例如:3:20:45):')
  14. out = time_diff(start,stop)
  15. print('时间差为%ds'%out)
复制代码

评分

参与人数 1荣誉 +3 鱼币 +3 收起 理由
冬雪雪冬 + 3 + 3

查看全部评分

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-1-17 18:01:24 | 显示全部楼层
start = [17,55,31]
stop = [4,21,57]
diff = (stop[2]-start[2])+(stop[1]-start[1])*60+(stop[0]-start[0])*3600
if stop < start:
    diff = diff + 24*3600
print(diff)

评分

参与人数 1荣誉 +3 鱼币 +3 收起 理由
冬雪雪冬 + 3 + 3

查看全部评分

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-1-17 18:08:43 | 显示全部楼层
start = input("请输入开始时间(h:m:s):")
stop = input("请输入结束时间(h:m:s):")
start = start.split(":")
stop = stop.split(":")
if int(stop[0]) < int(start[0]):
    h = int(stop[0]) - int(start[0])+24
else:
    h =int(stop[0])-int(start[0])
m = int(stop[1])-int(start[1])
s = int(stop[2])-int(start[2])
time = h*3600+m*60+s
print("时间差为:",time,"s")

评分

参与人数 1荣誉 +3 鱼币 +3 收起 理由
冬雪雪冬 + 3 + 3

查看全部评分

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-1-17 21:10:29 | 显示全部楼层
x1 = int(input('开始时间的时'))
x2 = int(input('开始时间的分'))
x3 = int(input('开始时间的秒'))
y1 = int(input('结束时间的时'))
y2 = int(input('结束时间的分'))
y3 = int(input('结束时间的秒'))
print('start = %d:%d:%d' %(x1,x2,x3))
print('stop = %d:%d:%d' %(y1,y2,y3))
if 0 < x1 < 24 and 0 < y1 < 24 and 0 < x2 < 60 and 0 < y2 < 60 and 0 < x3 < 60 and 0 < y3 < 60:
    if x1 < y1:
        time_diff = y1 * 3600 + y2 * 60 + y3 - x1 * 3600 - x2 * 60 - x3
        time_diff = str(time_diff)
        print(time_diff+'s')
    else:
        time_diff = (y1 + 24) * 3600 + y2 * 60 + y3 - x1 * 3600 - x2 * 60 - x3
        print(str(time_diff)+'s')
else:
    print('输入错误!')

评分

参与人数 1荣誉 +3 鱼币 +3 收起 理由
冬雪雪冬 + 3 + 3

查看全部评分

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-1-17 23:44:41 | 显示全部楼层
  1. def fun2(start, stop):
  2.     startList = start.split(":")
  3.     stopList = stop.split(":")
  4.     startSecond = int(startList[0]) * 3600 + int(startList[1]) * 60 + int(startList[2])
  5.     stopSecond = int(stopList[0]) * 3600 + int(stopList[1]) * 60 + int(stopList[2])

  6.     if startSecond > stopSecond:
  7.         stopSecond += 24 * 3600
  8.     return stopSecond - startSecond
复制代码


刚申请的帐号,等了半个小时才可以回复,睡觉去~

评分

参与人数 1荣誉 +3 鱼币 +3 收起 理由
冬雪雪冬 + 3 + 3

查看全部评分

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-1-19 09:30:51 | 显示全部楼层
  1. def fun142(start,end):
  2.     start = int(start.split(':')[0])*3600 + int(start.split(':')[1])*60 + int(start.split(':')[2])
  3.     end = int(end.split(':')[0])*3600 + int(end.split(':')[1])*60 + int(end.split(':')[2])
  4.     if end >= start:
  5.         return end - start
  6.     return 86400 - start + end

  7. if __name__ == '__main__':
  8.     start = input('输入开始时间:')
  9.     end = input('输入结束时间:')
  10.     print('时间差为:%ss'%fun142(start,end))
复制代码

评分

参与人数 1荣誉 +3 鱼币 +3 收起 理由
冬雪雪冬 + 3 + 3

查看全部评分

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-1-19 15:55:50 | 显示全部楼层
import time

def time_difference(start,stop):
    tmp_date = "2010-01-10 "
    # 开始时间的时间戳
    start_date = tmp_date + start
    start_st = time.strptime(start_date, '%Y-%m-%d %H:%M:%S')
    start_time_stamp = time.mktime(start_st)

    # 结束时间的时间戳
    stop_date = tmp_date + stop
    stop_st = time.strptime(stop_date, '%Y-%m-%d %H:%M:%S')
    stop_time_stamp = time.mktime(stop_st)

    if start_time_stamp <= stop_time_stamp:
        timeis = int(stop_time_stamp - start_time_stamp)
        print(stop_time_stamp,start_time_stamp)
        print("时间差是 %d" % timeis)
    else:
        timeis = int(stop_time_stamp + 86400 - start_time_stamp)
        print(stop_time_stamp,start_time_stamp)
        print("时间差是 %d" % timeis)

评分

参与人数 1荣誉 +3 鱼币 +3 收起 理由
冬雪雪冬 + 3 + 3

查看全部评分

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-1-21 04:17:33 | 显示全部楼层
a = input('start=')
b = input('stop=')
h1,m1,s1=a.split(':')
h2,m2,s2=b.split(':')
if int(h1) < int(h2):
    S1 = int(h1)*3600 + int(m1)*60 + int(s1)
    S2 = int(h2)*3600 + int(m2)*60 + int(s2)
    print('x')
else:
    S1 = int(h1) * 3600 + int(m1) * 60 + int(s1)
    S2 = (int(h2)+24) * 3600 + int(m2) * 60 + int(s2)

D = S2-S1
print('时差为:%d s'% D)

评分

参与人数 1荣誉 +3 鱼币 +3 收起 理由
冬雪雪冬 + 3 + 3

查看全部评分

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-1-21 11:33:08 | 显示全部楼层
start = list(map(int,input().split(":")))
stop = list(map(int,input().split(":")))
start_s = start[0]*3600+start[1]*60+start[2]
stop_s = stop[0]*3600 + stop[1] * 60 +stop[2]
if stop_s >= start_s:
    sub_s = stop_s-start_s
else:
    sub_s = 24*3600+stop_s-start_s
print(str(sub_s)+"s")

评分

参与人数 1荣誉 +3 鱼币 +3 收起 理由
冬雪雪冬 + 3 + 3

查看全部评分

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-1-21 12:50:28 | 显示全部楼层
来晚了,也写个自己的答案
  1. def timeCal(t1,t2):
  2.     l1=t1.split(':',3)
  3.     l2=t2.split(':',3)
  4.     L1=[int(l1[0]),int(l1[1]),int(l1[2])]
  5.     L2=[int(l2[0]),int(l2[1]),int(l2[2])]
  6.     s1=L1[0]*3600+L1[1]*60+L1[2]
  7.     s2=L2[0]*3600+L2[1]*60+L2[2]
  8.     if s2>=s1:
  9.         time=s2-s1
  10.     else:
  11.         time=s2+24*3600-s1
  12.     print("时间差:%d秒"%time)
  13. start='17:55:31'
  14. stop='4:21:57'
  15. timeCal(start,stop)
复制代码

评分

参与人数 1荣誉 +3 鱼币 +3 收起 理由
冬雪雪冬 + 3 + 3

查看全部评分

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-1-31 12:12:16 | 显示全部楼层
  1. def getSeconds(start_time,stop_time):
  2.     '''给出两个时间算出时间差(以秒计)
  3.     如果结束时间小于开始时间则结束时间在下一天
  4.     开始和结束时间不超过1天'''
  5.     if start_time.count(':')==2:
  6.         if stop_time.count(':')==2:
  7.             #计算时间差
  8.             #先分割出时分秒
  9.             (sta_hours,sta_mins,sta_sec)=start_time.split(':',2)
  10.             (stop_hours,stop_mins,stop_sec)=stop_time.split(':',2)
  11.             #把这些数字通通转化为int类型
  12.             sta_hours = int(sta_hours)
  13.             sta_mins = int(sta_mins)
  14.             sta_sec = int(sta_sec)
  15.             stop_hours = int(stop_hours)
  16.             stop_mins = int(stop_mins)
  17.             stop_sec = int(stop_sec)
  18.             #分别用时分秒对应相减
  19.             #计算秒钟差
  20.             if stop_sec<sta_sec:
  21.                 stop_sec+=60
  22.                 stop_mins-=1
  23.             get_sec=stop_sec-sta_sec
  24.             #计算分钟差
  25.             if stop_mins<sta_mins:
  26.                 stop_mins+=60
  27.                 stop_hours-=1
  28.             get_mins=stop_mins-sta_mins
  29.             #计算小时差
  30.             if stop_hours<sta_hours:
  31.                 day=1
  32.                 get_hours=sta_hours-stop_hours
  33.             else:
  34.                 get_hours=stop_hours-sta_hours
  35.             #将时分秒按照输入时的格式输出
  36.             if day==1:
  37.                 print('%dday:%dh:%dmin:%ds'%(day,get_hours,get_mins,get_sec))
  38.             else:
  39.                 print('%dh:%dmin:%ds'%(get_hours,get_mins,get_sec))
  40.             #将时分秒转换为秒钟
  41.             if day==1:
  42.                 final_sec=3600*24+get_hours*3600+get_mins*60+get_sec
  43.             else:
  44.                 final_sec=get_hours*3600+get_mins*60+get_sec
  45.             print(str(final_sec))
  46.         else:
  47.             print('输入结束时间格式错误!')
  48.     else:
  49.         print('输入开始时间格式错误!')
  50.         
  51.    
  52. start_time = input('请输入开始时间,格式xx:xx:xx\n')
  53. stop_time = input('请输入结束时间,格式同上\n')
  54. getSeconds(start_time,stop_time)
复制代码

评分

参与人数 1荣誉 +3 鱼币 +3 收起 理由
冬雪雪冬 + 3 + 3

查看全部评分

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-2-3 10:54:01 From FishC Mobile | 显示全部楼层
不会的
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-2-7 15:43:34 | 显示全部楼层
  1. def dif(start, stop):
  2.     s0 = [int(x) for x in start.split(':')]
  3.     s1 = [int(x) for x in stop.split(':')]
  4.     dif_L = [s1[i] - s0[i] for i in range(3)]
  5.     dif_int = dif_L[0]*3600 + dif_L[1]*60 + dif_L[2]
  6.     if dif_int < 0:
  7.         dif_int += 60*60*24
  8.     return '时间差为:%ds' % dif_int
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-4-2 21:55:14 | 显示全部楼层
  1. def get_data(st):
  2.     for i in range(len(st)):
  3.         if st[i].isdecimal() or st[i] == ':':
  4.             t = st.partition(':')
  5.             h = int(t[0])
  6.             t = t[2].partition(':')
  7.             m = int(t[0])
  8.             s = int(t[2])
  9.             time = [h,m,s]
  10.             return time
  11.         else:
  12.             return 'Input Error'   
  13. def calc_sec(lis1,lis2):
  14.     if lis1[0] <= lis2[0]:
  15.         result = 60 - lis1[2] + (60 - (lis1[1] + 1)) * 60 + lis2[2] + lis2[1] * 60 + (lis2[0] - (lis1[0] + 1)) * 3600
  16.         return result
  17.     else:
  18.         temp = 60 - lis1[2] + (60 - (lis1[1] + 1)) * 60 + (24 - lis1[0] -1) * 3600
  19.         temp1 = lis2[0] * 3600 + lis2[1] * 60 + lis2[2]
  20.         result = temp + temp1
  21.         return result
  22. print('时间格式为:hh:mm:ss')
  23. start = input('请输入起始时间:\n')
  24. start_lis = get_data(start)
  25. end = input('请输入截至时间:\n')
  26. end_lis = get_data(end)
  27. result = calc_sec(start_lis,end_lis)
  28. print('时间差为:%ds' % (result))
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-5-8 06:31:15 | 显示全部楼层
def fun(start, stop):
    start = start.split(":")
    stop = stop.split(":")
    for i in start:
        start.append(int(i))
    for i in start:
        if type(i) == str:
            start.remove(i)
    for i in stop:
        stop.append(int(i))
    for i in stop:
        if type(i) == str:            
            stop.remove(i)
    time_lsit = [3600, 60]
    sumnum = 0
    if start[0] > stop[0]:
        sumnum = (24 - start[0])*3600 + (60 - start[1])*60 + (60- start[2])
        sumnum += stop[0]*3600 + stop[1]*60 + stop[2]
    else:
        a = start[0]*3600 + start[1]*60 + start[2]
        b = stop[0]*3600 + stop[1]*60 + stop[2]
        sumnum = a - b
    return sumnum
start = "17:55:31"
stop = "4:21:57"
fun(start, stop)
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-1-5 17:14:03 | 显示全部楼层
  1. def timeCount(start, stop) :
  2.         startHourValue = int(start.split(':')[0])
  3.         stopHourValue = int(stop.split(':')[0])
  4.         startMinuValue = int(start.split(':')[1])
  5.         stopMinuValue = int(stop.split(':')[1])
  6.         startSecValue = int(start.split(':')[2])
  7.         stopSecValue = int(stop.split(':')[2])
  8.         tempValue, tempValue1, tempValue2 = 0, 0, 0
  9.         tempValue1 = startHourValue * 3600 + startMinuValue * 60 + startSecValue
  10.         tempValue2 = stopHourValue * 3600 + stopMinuValue * 60 + stopSecValue
  11.         if (stopHourValue > startHourValue) or (stopHourValue == startHourValue and stopMinuValue > startMinuValue) or (stopHourValue == startHourValue and stopMinuValue == startMinuValue and stopSecValue > startSecValue) :
  12.                 tempValue = tempValue2 - tempValue1
  13.                 print('时间相隔 :' + str(tempValue) + 's')
  14.                 print('换算一下为:%d小时,%d分,%d秒' % (tempValue // 3600, (tempValue % 3600) // 60, tempValue % 3600 % 60))
  15.         else :
  16.                 tempValue = tempValue2 + (86400 - tempValue1)
  17.                 print('时间相隔 :' + str(tempValue) + 's')
  18.                 print('换算一下为:%d小时,%d分,%d秒' % (tempValue // 3600, (tempValue % 3600) // 60, tempValue % 3600 % 60))

  19. start = input('请按\’18:02:34\'的格式输入开始时间:')
  20. stop = input('请按以上格式输入结束时间:')
  21. timeCount(start, stop)
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-3-27 20:17:21 | 显示全部楼层
  1. from datetime import datetime,timedelta

  2. def fun142(t1,t2):
  3.     t1 = datetime.strptime(t1,"%X")
  4.     t2 = datetime.strptime(t2,"%X")
  5.     if t1 > t2:
  6.         t2 += timedelta(days=1)
  7.     dalay = (t2 - t1).total_seconds()
  8.     return dalay
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-3-11 16:13:38 | 显示全部楼层
  1. def fun142(start, stop):
  2.     sta = start.split(':')
  3.     A = int(sta[0]) * 3600 + int(sta[1]) * 60 + int(sta[2])
  4.     end = stop.split(':')
  5.     B = int(end[0]) * 3600 + int(end[1]) * 60 + int(end[2])
  6.     return '时间差为:' + str(B - A if B > A else 86400 + B - A) + 's'
  7.    
  8.         
  9. start = '17:55:31'
  10. stop = '24:21:57'

  11. print(fun142(start, stop))
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-20 06:03

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表