如何通过各种条件生成随机时间

有这么一个需求:
生成时间时限为A到B的随机时间。

解决逻辑:以时间A作为开始时间,时间B作为结束时间。然后获取这个时间范围中的秒数。然后随机生成一个0到最大秒数范围内的随机时间,然后将时间A加上随机秒数即可。

from loguru import logger
import arrow
import random

start_time = arrow.get('2021-11-11')
end_time = arrow.get('2021-12-11')
for i in range(100):
    pass_second = (end_time - start_time).total_seconds()
    random_pass = random.randint(0, pass_second)
    random_time = start_time.shift(seconds=random_pass)
    logger.debug(random_time.format('YYYY-MM-DD HH:mm:ss'))

生成的随机时间:

2021-06-27 15:01:05.414 | DEBUG    | __main__:<module>:11 - 2021-11-23 03:31:32
2021-06-27 15:01:05.414 | DEBUG    | __main__:<module>:11 - 2021-11-24 07:36:16
2021-06-27 15:01:05.414 | DEBUG    | __main__:<module>:11 - 2021-11-23 00:00:05
2021-06-27 15:01:05.415 | DEBUG    | __main__:<module>:11 - 2021-11-22 11:14:14
2021-06-27 15:01:05.415 | DEBUG    | __main__:<module>:11 - 2021-12-06 20:43:19
2021-06-27 15:01:05.415 | DEBUG    | __main__:<module>:11 - 2021-11-25 07:04:23
2021-06-27 15:01:05.415 | DEBUG    | __main__:<module>:11 - 2021-11-20 14:51:00
2021-06-27 15:01:05.415 | DEBUG    | __main__:<module>:11 - 2021-11-19 23:52:33
2021-06-27 15:01:05.415 | DEBUG    | __main__:<module>:11 - 2021-11-23 09:49:21
2021-06-27 15:01:05.415 | DEBUG    | __main__:<module>:11 - 2021-11-18 10:06:58
2021-06-27 15:01:05.416 | DEBUG    | __main__:<module>:11 - 2021-11-19 05:22:14
2021-06-27 15:01:05.416 | DEBUG    | __main__:<module>:11 - 2021-12-04 08:28:48
2021-06-27 15:01:05.416 | DEBUG    | __main__:<module>:11 - 2021-11-22 15:17:15
2021-06-27 15:01:05.416 | DEBUG    | __main__:<module>:11 - 2021-11-26 14:41:12
2021-06-27 15:01:05.416 | DEBUG    | __main__:<module>:11 - 2021-11-17 12:59:22
2021-06-27 15:01:05.416 | DEBUG    | __main__:<module>:11 - 2021-12-02 00:49:39
2021-06-27 15:01:05.416 | DEBUG    | __main__:<module>:11 - 2021-11-25 23:44:37
2021-06-27 15:01:05.416 | DEBUG    | __main__:<module>:11 - 2021-12-04 22:21:50
2021-06-27 15:01:05.417 | DEBUG    | __main__:<module>:11 - 2021-12-04 03:17:46
2021-06-27 15:01:05.417 | DEBUG    | __main__:<module>:11 - 2021-12-09 06:21:12

进程完成,退出码 0

对时间进行排序:

通常情况下,文本是无法比较时间的大小的。
EXCEL转化成时间即可。
第二种,就是datetime格式比较时间大小。

from loguru import logger
import arrow
import random

start_time = arrow.get('2021-11-11')
end_time = arrow.get('2021-12-11')
time_list = []
for i in range(20):
    pass_second = (end_time - start_time).total_seconds()
    random_pass = random.randint(0, pass_second)
    random_time = start_time.shift(seconds=random_pass)
    # logger.debug(random_time.format('YYYY-MM-DD HH:mm:ss'))
    time_list.append(random_time)

time_list.sort(reverse=False)
for part in time_list:
    logger.debug(part.format('YYYY-MM-DD HH:mm:ss'))

生成从大到小排好序的随机时间。

F:\Anacondas\python.exe F:/untitled4/323213.py
2021-06-27 15:07:00.329 | DEBUG    | __main__:<module>:17 - 2021-11-11 21:10:43
2021-06-27 15:07:00.329 | DEBUG    | __main__:<module>:17 - 2021-11-12 14:46:57
2021-06-27 15:07:00.329 | DEBUG    | __main__:<module>:17 - 2021-11-15 07:14:32
2021-06-27 15:07:00.329 | DEBUG    | __main__:<module>:17 - 2021-11-15 18:38:15
2021-06-27 15:07:00.329 | DEBUG    | __main__:<module>:17 - 2021-11-17 11:51:08
2021-06-27 15:07:00.329 | DEBUG    | __main__:<module>:17 - 2021-11-18 12:53:44
2021-06-27 15:07:00.329 | DEBUG    | __main__:<module>:17 - 2021-11-18 16:19:38
2021-06-27 15:07:00.329 | DEBUG    | __main__:<module>:17 - 2021-11-23 11:42:59
2021-06-27 15:07:00.330 | DEBUG    | __main__:<module>:17 - 2021-11-26 08:37:14
2021-06-27 15:07:00.330 | DEBUG    | __main__:<module>:17 - 2021-11-26 17:36:09
2021-06-27 15:07:00.330 | DEBUG    | __main__:<module>:17 - 2021-11-27 06:45:14
2021-06-27 15:07:00.330 | DEBUG    | __main__:<module>:17 - 2021-12-01 18:29:52
2021-06-27 15:07:00.330 | DEBUG    | __main__:<module>:17 - 2021-12-03 13:32:15
2021-06-27 15:07:00.330 | DEBUG    | __main__:<module>:17 - 2021-12-04 00:54:50
2021-06-27 15:07:00.330 | DEBUG    | __main__:<module>:17 - 2021-12-05 10:33:15
2021-06-27 15:07:00.330 | DEBUG    | __main__:<module>:17 - 2021-12-06 04:53:15
2021-06-27 15:07:00.330 | DEBUG    | __main__:<module>:17 - 2021-12-06 07:17:18
2021-06-27 15:07:00.330 | DEBUG    | __main__:<module>:17 - 2021-12-08 01:38:13
2021-06-27 15:07:00.330 | DEBUG    | __main__:<module>:17 - 2021-12-08 12:44:06
2021-06-27 15:07:00.330 | DEBUG    | __main__:<module>:17 - 2021-12-08 21:54:25

进程完成,退出码 0

生成一段时间范围内, 8:00–16:00的随机时间
分成两个部分:
生成随机天数。
生成随机秒数。
两者加起来。

from loguru import logger
import arrow
import random
from dateutil.parser import parse
start_time = arrow.get('2021-11-11')
end_time = arrow.get('2021-12-11')
time_list = []
for i in range(20):
    pass_days = (end_time - start_time).days
    random_pass = random.randint(0, pass_days)
    random_day = start_time.shift(days=random_pass)
    random_second = random.randint(60 * 60 * 8, 60 * 60 * 16)
    random_time = random_day.shift(seconds=random_second)
    logger.debug(random_time.format('YYYY-MM-DD HH:mm:ss'))
    time_list.append(random_time)

生成的时间:

F:\Anacondas\python.exe F:/untitled4/323213.py
2021-06-27 15:33:38.549 | DEBUG    | __main__:<module>:14 - 2021-11-17 13:00:09
2021-06-27 15:33:38.549 | DEBUG    | __main__:<module>:14 - 2021-12-01 10:20:32
2021-06-27 15:33:38.549 | DEBUG    | __main__:<module>:14 - 2021-11-20 12:02:11
2021-06-27 15:33:38.550 | DEBUG    | __main__:<module>:14 - 2021-11-29 13:24:45
2021-06-27 15:33:38.550 | DEBUG    | __main__:<module>:14 - 2021-11-28 13:48:23
2021-06-27 15:33:38.550 | DEBUG    | __main__:<module>:14 - 2021-11-17 13:38:06
2021-06-27 15:33:38.550 | DEBUG    | __main__:<module>:14 - 2021-11-19 09:31:09
2021-06-27 15:33:38.550 | DEBUG    | __main__:<module>:14 - 2021-12-03 15:30:11
2021-06-27 15:33:38.550 | DEBUG    | __main__:<module>:14 - 2021-12-09 09:31:03
2021-06-27 15:33:38.551 | DEBUG    | __main__:<module>:14 - 2021-11-25 08:16:43
2021-06-27 15:33:38.551 | DEBUG    | __main__:<module>:14 - 2021-12-04 12:37:42
2021-06-27 15:33:38.551 | DEBUG    | __main__:<module>:14 - 2021-11-27 08:54:47
2021-06-27 15:33:38.551 | DEBUG    | __main__:<module>:14 - 2021-12-10 13:09:25
2021-06-27 15:33:38.551 | DEBUG    | __main__:<module>:14 - 2021-11-22 12:29:15
2021-06-27 15:33:38.551 | DEBUG    | __main__:<module>:14 - 2021-11-26 09:36:58
2021-06-27 15:33:38.551 | DEBUG    | __main__:<module>:14 - 2021-11-18 12:25:34
2021-06-27 15:33:38.552 | DEBUG    | __main__:<module>:14 - 2021-11-17 13:20:20
2021-06-27 15:33:38.552 | DEBUG    | __main__:<module>:14 - 2021-12-10 10:39:17
2021-06-27 15:33:38.552 | DEBUG    | __main__:<module>:14 - 2021-11-15 15:49:51
2021-06-27 15:33:38.552 | DEBUG    | __main__:<module>:14 - 2021-11-30 10:50:20

进程完成,退出码 0

解析不同格式时间:

import arrow
import datetime
from dateutil.parser import parse
import re


def time_format(time_):
    # 替换中文符号
    time_ = time_.replace(':', ':').replace(',', ',').replace('\\', '/').replace('。', ':    ')
    # 将时间转化为标准格式
    time_ = time_.replace('年', '-').replace('月', '-').replace('日', ' ').replace('时', ':').replace('点', ':').replace('分',
                                                                                                                    ':').replace(
        '秒', ' ')
    # 对时间进行转化
    time_ = time_.replace('五十九', '59').replace('五十八', '五十八').replace('五十七', '57').replace('五十六', '56').replace('五十五',
                                                                                                               '55') \
        .replace('五十四', '54').replace('五十三', '53').replace('五十二', '52').replace('五十一', '51').replace('五十三', '53') \
        .replace('五十二', '52').replace('五十一', '51').replace('五十', '50').replace('四十九', '49').replace('四十八',
                                                                                                    '四十八').replace(
        '四十七', '47').replace('四十六', '46').replace('四十五', '45') \
        .replace('四十四', '44').replace('四十三', '43').replace('四十二', '42').replace('四十一', '41').replace('四十三', '43') \
        .replace('四十二', '42').replace('四十一', '41').replace('四十', '40').replace('三十九', '39').replace('三十八',
                                                                                                    '38').replace('三十七',
                                                                                                                  '37').replace(
        '三十六', '36').replace('三十五', '35') \
        .replace('三十四', '34').replace('三十三', '33').replace('三十二', '32').replace('三十一', '31').replace('三十三', '33') \
        .replace('三十二', '32')
    time_ = time_.replace('三十一', '31').replace('三十', '三十').replace('二十九', '二十九').replace('二十八', '28').replace('二十七',
                                                                                                              '27') \
        .replace('二十六', '26').replace('二十五', '25').replace('二十四', '24').replace('二十三', '23').replace('二十二', '22') \
        .replace('二十一', '21').replace('二十', '20').replace('十九', '19').replace('十八', '18').replace('十七', '17').replace(
        '十六', '16') \
        .replace('十五', '15').replace('十四', '14').replace('十三', '13').replace('十二', '12').replace('十一', '11').replace(
        '十', '10')
    # 替换周
    time_ = time_.replace('周一', 'Mon').replace('星期一', 'Mon').replace('周二', 'Tue').replace('星期二', 'Tue').replace('周三',
                                                                                                                'Wed') \
        .replace('星期三', 'Wed').replace('周四', 'Thu').replace('星期四', 'Thu').replace('周五', 'Fri').replace('星期五', 'Fri') \
        .replace('周六', 'Sat').replace('星期六', 'Sat').replace('星期日', 'Sun').replace('周日', 'Sun')
    time_ = time_.replace('一', '1').replace('零', '0').replace('〇', '0').replace('二', '2').replace('三', '3').replace('四',
                                                                                                                    '4') \
        .replace('五', '5').replace('六', '6').replace('七', '7').replace('八', '8').replace('九', '9')
    # 补齐少时间的格式
    time_ = re.sub('-$', '-1', time_)
    time_ = re.sub(':$', ':0', time_)
    # 上午下午必须添加到最后似乎才准确
    if time_.find('上午') > 0 or time_.find('凌晨') > 0:
        time_ = time_ + 'am'
    if time_.find('下午') > 0 or time_.find('晚上') > 0 or time_.find('深夜') > 0:
        time_ = time_ + 'pm'
    time_ = re.sub('[\u4e00-\u9fa5()]', ' ', time_)
    time_ = parse(time_)
    time_ = arrow.get(time_, tzinfo='Asia/Shanghai').format('YYYY-MM-DD HH:mm:ss')
    return time_

使用这个函数,可以解析绝大多数时间格式。
把这个函数添加到路径,就可以随时调用了。

批量生成连续时间

start_time = arrow.get('2021-11-11')
end_time = arrow.get('2021-12-11')
time_list = []


time_add = start_time
while (time_add - end_time).total_seconds() < 0:
    time_add = time_add.shift(seconds=5 * 60 * 24)
    logger.debug(time_add.format('YYYY-MM-DD HH:mm:ss'))
2021-06-27 15:43:30.261 | DEBUG    | __main__:<module>:14 - 2021-11-11 02:00:00
2021-06-27 15:43:30.261 | DEBUG    | __main__:<module>:14 - 2021-11-11 04:00:00
2021-06-27 15:43:30.261 | DEBUG    | __main__:<module>:14 - 2021-11-11 06:00:00
2021-06-27 15:43:30.261 | DEBUG    | __main__:<module>:14 - 2021-11-11 08:00:00
2021-06-27 15:43:30.262 | DEBUG    | __main__:<module>:14 - 2021-11-11 10:00:00
2021-06-27 15:43:30.262 | DEBUG    | __main__:<module>:14 - 2021-11-11 12:00:00
2021-06-27 15:43:30.262 | DEBUG    | __main__:<module>:14 - 2021-11-11 14:00:00
2021-06-27 15:43:30.262 | DEBUG    | __main__:<module>:14 - 2021-11-11 16:00:00
2021-06-27 15:43:30.262 | DEBUG    | __main__:<module>:14 - 2021-11-11 18:00:00
2021-06-27 15:43:30.262 | DEBUG    | __main__:<module>:14 - 2021-11-11 20:00:00
2021-06-27 15:43:30.262 | DEBUG    | __main__:<module>:14 - 2021-11-11 22:00:00
2021-06-27 15:43:30.262 | DEBUG    | __main__:<module>:14 - 2021-11-12 00:00:00
2021-06-27 15:43:30.262 | DEBUG    | __main__:<module>:14 - 2021-11-12 02:00:00
2021-06-27 15:43:30.263 | DEBUG    | __main__:<module>:14 - 2021-11-12 04:00:00
2021-06-27 15:43:30.263 | DEBUG    | __main__:<module>:14 - 2021-11-12 06:00:00
2021-06-27 15:43:30.263 | DEBUG    | __main__:<module>:14 - 2021-11-12 08:00:00
2021-06-27 15:43:30.263 | DEBUG    | __main__:<module>:14 - 2021-11-12 10:00:00
2021-06-27 15:43:30.263 | DEBUG    | __main__:<module>:14 - 2021-11-12 12:00:00
2021-06-27 15:43:30.263 | DEBUG    | __main__:<module>:14 - 2021-11-12 14:00:00
2021-06-27 15:43:30.263 | DEBUG    | __main__:<module>:14 - 2021-11-12 16:00:00
2021-06-27 15:43:30.263 | DEBUG    | __main__:<module>:14 - 2021-11-12 18:00:00
2021-06-27 15:43:30.264 | DEBUG    | __main__:<module>:14 - 2021-11-12 20:00:00
2021-06-27 15:43:30.264 | DEBUG    | __main__:<module>:14 - 2021-11-12 22:00:00
2021-06-27 15:43:30.264 | DEBUG    | __main__:<module>:14 - 2021-11-13 00:00:00
2021-06-27 15:43:30.264 | DEBUG    | __main__:<module>:14 - 2021-11-13 02:00:00
2021-06-27 15:43:30.264 | DEBUG    | __main__:<module>:14 - 2021-11-13 04:00:00
2021-06-27 15:43:30.264 | DEBUG    | __main__:<module>:14 - 2021-11-13 06:00:00
2021-06-27 15:43:30.264 | DEBUG    | __main__:<module>:14 - 2021-11-13 08:00:00
2021-06-27 15:43:30.264 | DEBUG    | __main__:<module>:14 - 2021-11-13 10:00:00
2021-06-27 15:43:30.265 | DEBUG    | __main__:<module>:14 - 2021-11-13 12:00:00
2021-06-27 15:43:30.265 | DEBUG    | __main__:<module>:14 - 2021-11-13 14:00:00
2021-06-27 15:43:30.265 | DEBUG    | __main__:<module>:14 - 2021-11-13 16:00:00
2021-06-27 15:43:30.265 | DEBUG    | __main__:<module>:14 - 2021-11-13 18:00:00
2021-06-27 15:43:30.265 | DEBUG    | __main__:<module>:14 - 2021-11-13 20:00:00
2021-06-27 15:43:30.265 | DEBUG    | __main__:<module>:14 - 2021-11-13 22:00:00
2021-06-27 15:43:30.265 | DEBUG    | __main__:<module>:14 - 2021-11-14 00:00:00
2021-06-27 15:43:30.266 | DEBUG    | __main__:<module>:14 - 2021-11-14 02:00:00
2021-06-27 15:43:30.266 | DEBUG    | __main__:<module>:14 - 2021-11-14 04:00:00
2021-06-27 15:43:30.266 | DEBUG    | __main__:<module>:14 - 2021-11-14 06:00:00
2021-06-27 15:43:30.266 | DEBUG    | __main__:<module>:14 - 2021-11-14 08:00:00
2021-06-27 15:43:30.266 | DEBUG    | __main__:<module>:14 - 2021-11-14 10:00:00
2021-06-27 15:43:30.266 | DEBUG    | __main__:<module>:14 - 2021-11-14 12:00:00
2021-06-27 15:43:30.266 | DEBUG    | __main__:<module>:14 - 2021-11-14 14:00:00
2021-06-27 15:43:30.266 | DEBUG    | __main__:<module>:14 - 2021-11-14 16:00:00
2021-06-27 15:43:30.267 | DEBUG    | __main__:<module>:14 - 2021-11-14 18:00:00
2021-06-27 15:43:30.267 | DEBUG    | __main__:<module>:14 - 2021-11-14 20:00:00
2021-06-27 15:43:30.267 | DEBUG    | __main__:<module>:14 - 2021-11-14 22:00:00
2021-06-27 15:43:30.267 | DEBUG    | __main__:<module>:14 - 2021-11-15 00:00:00
2021-06-27 15:43:30.267 | DEBUG    | __main__:<module>:14 - 2021-11-15 02:00:00
2021-06-27 15:43:30.267 | DEBUG    | __main__:<module>:14 - 2021-11-15 04:00:00
2021-06-27 15:43:30.267 | DEBUG    | __main__:<module>:14 - 2021-11-15 06:00:00
2021-06-27 15:43:30.267 | DEBUG    | __main__:<module>:14 - 2021-11-15 08:00:00
2021-06-27 15:43:30.267 | DEBUG    | __main__:<module>:14 - 2021-11-15 10:00:00
2021-06-27 15:43:30.268 | DEBUG    | __main__:<module>:14 - 2021-11-15 12:00:00
2021-06-27 15:43:30.268 | DEBUG    | __main__:<module>:14 - 2021-11-15 14:00:00
2021-06-27 15:43:30.268 | DEBUG    | __main__:<module>:14 - 2021-11-15 16:00:00
2021-06-27 15:43:30.268 | DEBUG    | __main__:<module>:14 - 2021-11-15 18:00:00
2021-06-27 15:43:30.268 | DEBUG    | __main__:<module>:14 - 2021-11-15 20:00:00
2021-06-27 15:43:30.268 | DEBUG    | __main__:<module>:14 - 2021-11-15 22:00:00
2021-06-27 15:43:30.268 | DEBUG    | __main__:<module>:14 - 2021-11-16 00:00:00
2021-06-27 15:43:30.268 | DEBUG    | __main__:<module>:14 - 2021-11-16 02:00:00
2021-06-27 15:43:30.269 | DEBUG    | __main__:<module>:14 - 2021-11-16 04:00:00
2021-06-27 15:43:30.269 | DEBUG    | __main__:<module>:14 - 2021-11-16 06:00:00
2021-06-27 15:43:30.269 | DEBUG    | __main__:<module>:14 - 2021-11-16 08:00:00
2021-06-27 15:43:30.269 | DEBUG    | __main__:<module>:14 - 2021-11-16 10:00:00
2021-06-27 15:43:30.269 | DEBUG    | __main__:<module>:14 - 2021-11-16 12:00:00
2021-06-27 15:43:30.269 | DEBUG    | __main__:<module>:14 - 2021-11-16 14:00:00
2021-06-27 15:43:30.269 | DEBUG    | __main__:<module>:14 - 2021-11-16 16:00:00
2021-06-27 15:43:30.269 | DEBUG    | __main__:<module>:14 - 2021-11-16 18:00:00
2021-06-27 15:43:30.269 | DEBUG    | __main__:<module>:14 - 2021-11-16 20:00:00
2021-06-27 15:43:30.270 | DEBUG    | __main__:<module>:14 - 2021-11-16 22:00:00
2021-06-27 15:43:30.270 | DEBUG    | __main__:<module>:14 - 2021-11-17 00:00:00
2021-06-27 15:43:30.270 | DEBUG    | __main__:<module>:14 - 2021-11-17 02:00:00
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值