Python 获取午夜到当前时间之间的随机时间
我正在尝试获取午夜和当前时间之间的随机时间,以秒为单位 例子Python 获取午夜到当前时间之间的随机时间,python,datetime,Python,Datetime,我正在尝试获取午夜和当前时间之间的随机时间,以秒为单位 例子 12:01 AM->10:15 AM今天是2016年2月4日 def random_date(start, end): delta = end - start int_delta = (delta.days * 24 * 60 * 60) + delta.seconds random_second = randrange(int_delta) return start + timedelta(secon
12:01 AM->10:15 AM
今天是2016年2月4日
def random_date(start, end):
delta = end - start
int_delta = (delta.days * 24 * 60 * 60) + delta.seconds
random_second = randrange(int_delta)
return start + timedelta(seconds=random_second)
start = datetime.datetime.strptime('2016-04-02 12:00:00', '%Y-%m-%d %H:%M:%S')
end = datetime.datetime.strptime(strftime('%Y-%m-%d %H:%M:%S', gmtime()), '%Y-%m-%d %H:%M:%S')
print random_date(start, end)
结果
当前时间为上午10:22;我通过了当前时间。取当前时间,减去午夜的当前日期,得到一个
timedelta
给你秒数,使用random.randrange()
获得一个新的偏移量,并在timedelta
的帮助下再次将其转换回来:
from datetime import datetime, time, timedelta
import random
now = datetime.now()
midnight = datetime.combine(now.date(), time.min)
delta = int((now - midnight).total_seconds())
random_dt = midnight + timedelta(seconds=random.randrange(delta))
演示:
取当前时间,减去午夜的当前日期,得到一个
timedelta
给出秒数,使用random.randrange()
获得一个新的偏移量,并在timedelta
的帮助下再次将其转换回:
from datetime import datetime, time, timedelta
import random
now = datetime.now()
midnight = datetime.combine(now.date(), time.min)
delta = int((now - midnight).total_seconds())
random_dt = midnight + timedelta(seconds=random.randrange(delta))
演示:
以下是对的修改,以避免在DST转换周围产生偏差或不存在时间:
#!/usr/bin/env python
from datetime import datetime, timedelta
from random import randrange
from tzlocal import get_localzone # $ pip install tzlocal
local_timezone = get_localzone()
now = datetime.now(local_timezone)
today = datetime(now.year, now.month, now.day)
midnight = local_timezone.normalize(local_timezone.localize(today, is_dst=False))
max_seconds = int((now - midnight).total_seconds())
random_time = midnight + timedelta(seconds=randrange(max_seconds))
is_dst=False
用于避免在午夜出现dst转换时出现歧义。以下是对的修改,以避免在dst转换周围产生偏差或不存在时间:
#!/usr/bin/env python
from datetime import datetime, timedelta
from random import randrange
from tzlocal import get_localzone # $ pip install tzlocal
local_timezone = get_localzone()
now = datetime.now(local_timezone)
today = datetime(now.year, now.month, now.day)
midnight = local_timezone.normalize(local_timezone.localize(today, is_dst=False))
max_seconds = int((now - midnight).total_seconds())
random_time = midnight + timedelta(seconds=randrange(max_seconds))
is_dst=False
用于避免在午夜出现dst转换时出现歧义。因此,这就是您获取午夜的方式midnight=datetime.combine(now.date(),time())
?@ihue:yes,time()
没有参数,或者可能是time.MIN
,我想问一下,为什么在你的答案中包含>
,它在python世界中有什么意义吗?@ihue:这是python交互式解释器的输出;它向您显示了我给它的输入和它产生的输出,这样您就可以验证我的代码是否有效。@ihue:只需对生成的datetime
实例调用random\u dt.strftime(“%Y-%m-%d%H:%m:%S”)
这就是获取午夜midnight=datetime.combine(now.date(),time())
?@ihue:是的,time()
没有参数,或者可能是time.MIN
,它们都给你午夜时间。我可以问一下,你为什么在回答中包含>
,它在python世界中有什么意义吗?@ihue:这是python交互式解释器的输出;它向您显示我给它的输入和它产生的输出,这样您就可以验证我的代码是否有效。@ihue:只需对结果datetime
实例调用random\u dt.strftime(“%Y-%m-%d%H:%m:%S”)的strftime()方法即可
你能在顶部添加导入
吗,让我这样的新手了解函数的来源吗?可以,很抱歉。看看输出,你到底要在哪里超过这个范围?你能试着用am/pm符号打印12:36部分吗?如果有DST转换,并且结果随机时间在本地时间中不存在(“向前冲”转换),你想得到什么?你能在顶部添加import
,让像我这样的新手了解函数的来源吗,很抱歉。看看输出,你到底要在哪里超过这个范围?您可以尝试打印带有am/pm符号的12:36部分吗?如果存在DST转换,并且本地时间中不存在结果随机时间(“向前跳”转换),您希望得到什么?