gpt4 book ai didi

python - 在给定范围内生成随机时间戳?

转载 作者:行者123 更新时间:2023-11-28 22:30:16 27 4
gpt4 key购买 nike

我正在尝试做类似的事情:

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))

输出:

>>> 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)))

关于python - 在给定范围内生成随机时间戳?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42416383/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com