how am i able to round down time to the nearest 15mins clock mark. the code below rounds to the nearest 15mins clock mark, i.e if time is 5:08 to becomes 5:15. however i would like to make this round down not up and become 5:00 please help
here is my code
fajr_jamaat = ceil(fajr_jamaat_date).strftime('%H:%M:%S')
and the function is
def ceil(dt):
if dt.minute % 15 or dt.second:
return dt + timedelta(minutes = 15 - dt.minute % 15, seconds = -(dt.second % 60))
else:
return dt
this should work.
import datetime
def ceil(dt):
minut = int(dt.strftime('%M'))*60
secon = int(dt.strftime('%S'))
h = int(dt.strftime('%H'))
tot = minut + secon
#print(tot)
nearest = round(tot/900)*900
#print('time is %d:%d'%(tot/60, tot%60), 'nearest fifteen mark is %d:%d'%(nearest/60, nearest%60))
if nearest/60 == 60 and h < 23:
nr_15_mark = dt.replace(hour=h+1, minute=0, second=0)
elif nearest/60 == 60 and h == 23:
#wrapping the hour back to 0
nr_15_mark = dt.replace(hour=0, minute=0, second=0)
else:
nr_15_mark = dt.replace(minute=int(nearest/60), second=nearest%60)
return nr_15_mark
#fajr_jamaat_date = datetime.datetime(2017,1,1,23,58,0)
fajr_jamaat = ceil(fajr_jamaat_date).strftime('%H:%M:%S')
#print(fajr_jamaat)
Please note that as we are considering only hours, minutes and seconds, i'm ignoring the date addition when we are finding the nearest mark when the time of fajr_jamaat_date exceeds 23:47:30.