Separating Python date and time into time cutouts

I have an instance of datetime.datetime , d and a datetime.timedelta instance of td , and I'm trying to write a function that splits the range (d, d+td) into [(d,x1),(x1,x2),...,(xn,d+td)] with variables xn all aligned with the hour.

For example, if

 d = datetime.datetime(2012, 9, 8, 18, 53, 34) td = datetime.timedelta(hours=2, minutes=34, seconds=5) 

I need a list

 [(datetime.datetime(..., 18, 53, 34), datetime.datetime(..., 19, 0, 0)), (datetime.datetime(..., 19, 0, 0), datetime.datetime(..., 20, 0, 0)), (datetime.datetime(..., 20, 0, 0), datetime.datetime(..., 21, 0, 0)), (datetime.datetime(..., 21, 0, 0), datetime.datetime(..., 21, 27, 39))] 

Can anyone suggest a good, Pythonic, way to accomplish this?

+4
source share
2 answers

Using dateutil , you can generate a list using rrule :

 import dateutil.rrule as rrule import datetime def hours_aligned(start, end, inc = True): if inc: yield start rule = rrule.rrule(rrule.HOURLY, byminute = 0, bysecond = 0, dtstart=start) for x in rule.between(start, end, inc = False): yield x if inc: yield end d = datetime.datetime(2012, 9, 8, 18, 53, 34) td = datetime.timedelta(hours=2, minutes=34, seconds=5) for x in hours_aligned(d,d+td): print(x) 

gives

 2012-09-08 18:53:34 2012-09-08 19:00:00 2012-09-08 20:00:00 2012-09-08 21:00:00 2012-09-08 21:27:39 
+6
source
 chunks = [] end = d + td current = d # Set next_current to the next hour-aligned datetime next_current = (d + datetime.timedelta(hours=1)).replace(minute=0, second=0) # Grab the start block (that ends on an hour alignment) # and then any full-hour blocks while next_current < end: chunks.append( (current, next_current) ) # Advance both current and next_current to the following hour-aligned spots current = next_current next_current += datetime.timedelta(hours=1) # Grab any remainder as the last segment chunks.append( (current, end) ) 

The basic assumption is that your initial given timedelta is not negative. You will get a list with one number [(x,y)] , where y < x , if you do.

+1
source

Source: https://habr.com/ru/post/1433159/


All Articles