当我遇到(另一个线程)关于 Pythondatetime
和timedelta
对象的问题时,这个问题就产生了。
我关注了更新jimgardener
并阅读了评论eyquem
,并尝试了一些python代码..我很难理解这里的工作方式(由于我的新手到python状态)..我认为问一个问题是合适的新问题
import datetime
#for t1=23:30:00 PM
t1 = datetime.time(23,30,00)
#for t1=00:15:30 AM
t2 = datetime.time(0,15,30)
td1 = datetime.timedelta(hours=t1.hour,minutes = t1.minute,seconds=t1.second)
td2 = datetime.timedelta(hours=t2.hour,minutes = t2.minute,seconds=t2.second)
#substarcting timedeltas
tdiff = td2-td1
打印这些变量产生
td1 ==> datetime.timedelta(0, 84600)
td1.seconds ==> 84600
td2 ==> datetime.timedelta(0, 930)
td2.seconds ==> 930
tdiff ==> datetime.timedelta(-1, 2730)
当我查看这些结果时,我注意到
td1.seconds (ie 84600) is equivalent to
84600/60 ==> 1410 minutes
1410/60 ==> 23.5 hours
or in short,td1 represents the duration **from previous midnight** to 23:30 PM
现在 ,
td2.seconds (ie 930) is equivalent to
930/60 ==> 15.5 minutes or 15 minutes and 30 seconds
which means td2 represents the duration from **that midnight**
to 00:15:30 AM
当检查 tdiff 时,我发现
tdiff ==> timedelta(-1,2730)
tdiff.seconds ==> 2730
tdiff.seconds/60 ==>45 minutes
这与duration between t1(23:30:00 PM) and t2(00:15:30 AM)
假设 t2 跟随 t1相同
My question is,since td1 is the duration from previous midnight to 23:30:00 PM and td2 is the duration from that midnight to 00:15:30 AM , how can their difference represent the duration between t2 and t1 ?
Can some python gurus explain