2

Is there a simple way to convert a start and end time input into a list of evenly separated times? the input can be string or integer with format 1000,"1000",or "10:00" in 2400hr format. I've managed to accomplish this in a messy looking way, is there a tighter more efficient way to create this list? As you'll notice I created an array first and then called .tolist() to make the time transformation iteration easier. The problem is that an input of 1030 or 1015 would need to be translated into 1050 or 1025 to create the right spacing but if there were a way I could call a datetime.timedelta or something and cleanly make the array?

    start="1000"
    end="1600"
    total_minutes=(int(end[:2])*60)+int(end[2:])-(int(start[:2])*60)- 
    int(start[2:])
        dog=list(range(0,int(total_minutes),25))
        walk=dog_df["Walk Length"][dog_df.index[dog_df["Name"]==self.name][0]]

        if walk=='half':
            self.dogarr=np.array([(x-25,x,x+25,x+50) for x in dog])
        elif walk=='full':
            self.dogarr=np.array([(x-25,x,x+25,x+50,x+75,x+100) for x in dog])
        else:
            self.dogarr=np.array([(x,x+25,x+50) for x in dog])
        if int(start[2])!=0:
            start=start[:2]+str(int(int(start[2:])*1.667))
        self.dogarr+=(int(start))
        self.dogarr=self.dogarr.tolist()
        z=0
        while z<len(self.dogarr):
            for timespot in self.dogarr[z].copy():
                self.dogarr[z][self.dogarr[z].index(timespot)]=time.strftime('%H%M', time.gmtime(self.dogarr[z][self.dogarr[z].index(timespot)]*36))
            z+=1
        self.dogarr=np.array(self.dogarr)```

array([['1115', '1130', '1145', '1200'],
       ['1130', '1145', '1200', '1215'],
       ['1145', '1200', '1215', '1230'],
       ['1200', '1215', '1230', '1245'],
       ['1215', '1230', '1245', '1300']], dtype='<U4')


Bugbeeb
  • 2,021
  • 1
  • 9
  • 26
  • Is your question asking [Given a date range how can we break it up into N contiguous sub-intervals?](https://stackoverflow.com/questions/29721228/given-a-date-range-how-can-we-break-it-up-into-n-contiguous-sub-intervals) – chickity china chinese chicken Jan 19 '19 at 01:47

1 Answers1

0

I'm sure you can figure out to parse times from any number of existing questions. The crux of your question seems to be how to create evenly separated times within a range. Here's a simple way:

start = datetime.datetime(2018,12,20,10) # or use strptime etc.
end = datetime.datetime(2018,12,24,18)
count = 10
interval = (end - start) / count
dt = start
while dt <= end:
    print(dt)
    dt += interval

The output is:

2018-12-20 10:00:00
2018-12-20 20:24:00
2018-12-21 06:48:00
2018-12-21 17:12:00
2018-12-22 03:36:00
2018-12-22 14:00:00
2018-12-23 00:24:00
2018-12-23 10:48:00
2018-12-23 21:12:00
2018-12-24 07:36:00
2018-12-24 18:00:00
John Zwinck
  • 239,568
  • 38
  • 324
  • 436
  • Thank you for the response. I crafted a solution very similar to yours, only I used datetime.timedelta and converted total_seconds to minutes and so forth. – Bugbeeb Jan 20 '19 at 03:55
  • @Bugbeeb: That's good, you can post it as an answer to your own question. – John Zwinck Jan 21 '19 at 00:57