Update every nth element of given a range

81 Views Asked by At

I have N=12 and between 1 to 12 (in N) I want every 3 elements to be updated.

For example: I am calculating the current hour(say 6am) and add 4 hours to start with "6-10" for the 1st 3 rows and add 4hrs for the next 3 and so on...

So The expected output is :

1 6-10
2 6-10
3 6-10
4 10-14
5 10-14
6 10-14
7 14-18
8 14-18
9 14-18
10 18-22
11 18-22
12 18-22

I have somethinglike this:

#utc_hour is the current utc hour calculated
from datetime import datetime
utc_time = datetime.utcnow()
utc_year = utc_time.year
utc_month = utc_time.month
utc_day = utc_time.day
utc_hour = utc_time.hour
utc_minute = utc_time.minute
minute = []
hour =[]
day = []
month = []
week = []
_hour = utc_hour
count = 0
n= 3
jobs = 12
while count <= int(jobs):
    if layer == 'get' :  
        
        minute.append('0-59')
        incr_hour = _hour+4
        gen_hour = str(_hour)+'-'+str(incr_hour)   
        a = [hour.append(gen_hour)]
        print(count,x,n,hour)
    count+=1

and the output i get is all the 12 elements with 6-10 (the same hour)

3

There are 3 best solutions below

0
SIGHUP On BEST ANSWER

The currently recommended way to get the current hour differs from what's in your code. datetime.utcnow() is deprecated.

Try this:

import datetime

hour = datetime.datetime.now(datetime.UTC).hour

N = 12

for i in range(N):
    hh = (hour + 4 * (i//3)) % 24
    print(f"{i+1:>2} {hh:02d}-{(hh+4)%24:02d}")

Output:

 1 11-15
 2 11-15
 3 11-15
 4 15-19
 5 15-19
 6 15-19
 7 19-23
 8 19-23
 9 19-23
10 23-03
11 23-03
12 23-03
0
S.B On

You can let datetime module handle it for you. You can create a datetime object and add timedelta object:

from datetime import datetime, timedelta, timezone

n = 12
current_datetime = datetime.now(tz=timezone.utc)
for i in range(1, n + 1, 3):
    added_four = current_datetime + timedelta(hours=4)
    for j in range(3):
        print(f"{i+j} {current_datetime.hour}-{added_four.hour}")
    current_datetime += timedelta(hours=4)

output(in UTC):

1 11-15
2 11-15
3 11-15
4 15-19
5 15-19
6 15-19
7 19-23
8 19-23
9 19-23
10 23-3
11 23-3
12 23-3
0
Suramuthu R On

I've split my code into smaller components which will make you understand the concept of datetime module easily.

#Apart from datetime, import timedelta which is meant for adding time
from datetime import datetime, timedelta

#Import module for timezone
import pytz

#Get your time zone(I guess you're from India)
tz_ch = pytz.timezone('Asia/Kolkata')

#Get current time of your time zone
tz_ch = datetime.now(tz_ch)

#iterate in the range of 12
for i in range(12):
    
    #if i is divided by 3 add 4 hrs to current_time(also i should not be 0)
    if i != 0 and i % 3 == 0: tz_ch = tz_ch + timedelta(hours=4)

    #Always add 4 hours to to_time
    to_time = tz_ch + timedelta(hours=4)

    #Get the time component inturn hour component of from time
    fr = tz_ch.time()
    fr = f'{fr:%H}'

    #Get the time component inturn hour component of to_time
    to = to_time.time()
    to = f'{to:%H}'
    
    #time range
    time_range = f'{fr} - {to}'
    print(i+1, time_range)

'''Output:
1 20 - 23
2 20 - 23
3 20 - 23
4 23 - 02
5 23 - 02
6 23 - 02
7 02 - 05
8 02 - 05
9 02 - 05
10 05 - 08
11 05 - 08
12 05 - 08
'''