我想知道是否有办法将整数的值转换为一种时间戳,就像使用strftime("%H:%M", gmtime())
输出当前的小时和分钟一样。
我想修复的代码片段如下所示:
#The full program includes an input system which makes this snippet run.
import time
initial_time = time.time()
print "It has been " + str(int(time.time()-initial_time)) + " since you said that."
假设程序运行时,整数的值为596,大约为9分56秒。我需要程序做的是输出09:56而不是596.如果有办法做到这一点,请告诉我。
P.S。如果有办法使用格式化运算符str(int(time.time()-initial_time))
将%
引入字符串,请告诉我。
答案 0 :(得分:1)
In [1]: import datetime
In [2]: str(datetime.timedelta(seconds=596))
Out[2]: '0:09:56'
答案 1 :(得分:1)
您可以使用datetime.timedelta
:
>>> import datetime
>>> str(datetime.timedelta(seconds=596))
'0:09:56'
答案 2 :(得分:1)
In [86]: import time
In [87]: initial_time = time.time()
In [88]: end_time = time.time()
In [89]: print "It has been %d:%d since you said that" %(int(end_time-initial_time)/60, int(end_time-initial_time)%60)
It has been 0:56 since you said that
另外,要解决你的PS:
In [90]: print "It has been", str(int(end_time-initial_time)/60) + ":" + str(int(end_time-initial_time)%60), "since you said that"
It has been 0:56 since you said that