Is there any method to append minute per specific interval?

74 Views Asked by At

What I want to do is to make specific minute be added from now. For example.

Let's assume that it's 10:09 now. Then the minute is gonna be 9. And we assume that the interval is 5 minutes. Then the list has to be like this:

[9, 14, 19, 24, 29, 34, 39, 44, 49, 54, 59, 4, 9, ....]

Does anyone has solution?

3

There are 3 best solutions below

0
On

This is what you are looking for?

import datetime

interval = 5
result_size = 10
current_minute = datetime.datetime.now().minute
result = [(current_minute + i * interval) % 60 for i in range(result_size)]
0
On

One way to think about it is that each element is the starting minute plus the index times the interval, and then take the remainder from dividing by 60 (an hour). Put it all together and you get:

start = 9
interval = 5
minutes = [(start + i * interval) % 60 for i in range(1000)]
1
On
def minutes_generator(start, delta=5):
  x = start
  while True:
    yield x
    x = (x + delta) % 60

g = minutes_generator(9, 5)
lst  = [next(g) for _ in range(20)]
print(lst)  # --> [9, 14, 19, 24, 29, 34, 39, 44, 49, 54, 59, 4, 9, 14, 19, 24, 29, 34, 39, 44]