samedi 1 août 2020

Generating random time with intervals of 5 minutes

I'm trying to get an output that looks like that:

12:15
07:55
02:20
04:35

I managed to get the following output:

2020-07-03 03:53:32
2020-07-20 08:01:15
2020-07-29 10:04:11
2020-07-07 07:17:24
2020-07-20 12:13:32

by using this code:

import datetime
import time
import random

MINTIME = datetime.datetime(2020,7,1,0,0,0)
MAXTIME = datetime.datetime(2020,7,31,0,0,0)

mintime_ts = int(time.mktime(MINTIME.timetuple())) #convert date into int
maxtime_ts = int(time.mktime(MAXTIME.timetuple())) #convert date into int

for RECORD in range(100):
    random_ts = random.randint(mintime_ts, maxtime_ts)
    RANDOMTIME = datetime.datetime.fromtimestamp(random_ts)
    print(RANDOMTIME)

So, basically I have two issues. The first is I want to get the time in format of (hh:mm) only. The second is that I want the step to be 5 minutes (something like 14:50 or 04:35 .. etc).

Thanks




Aucun commentaire:

Enregistrer un commentaire