我正在尝试做类似的事情:
start=input("select starting date, format example : Jan 01 00:00:00")
end=input("select ending date")
if start>end:
start,end=end,start
print(randomize_time(start,end))
Output:
>>> Aug 05 13:15:59
我尝试过使用 random.randint,但如果我选择一个范围,例如:
00:00:00 && 01:00:00,唯一随机的部分是小时、分钟和秒将被忽略(因为我正在执行 random.randint(0,0))。
我该如何正确地做到这一点?
提前致谢!
您可以使用如下内容:
from random import randrange
import time
start_timestamp = time.mktime(time.strptime('Jun 1 2010 01:33:00', '%b %d %Y %I:%M:%S'))
end_timestamp = time.mktime(time.strptime('Jun 1 2017 12:33:00', '%b %d %Y %I:%M:%S'))
def randomize_time(start_timestamp,end_timestamp):
return time.strftime('%b %d %Y %I:%M:%S', time.localtime(randrange(start_timestamp,end_timestamp)))
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)