Home > other >  How to increment a list of time strings by one day?
How to increment a list of time strings by one day?

Time:11-04

I have a very large list of timestamps. Here is a sample:

time_str = ["23:00", "11:00", "1:00", "5:00", "7:00"]

I am trying to iterate over the list elements, convert each element to datetime, then compare two subsequent elements, and if the latter time stamp is smaller than the earlier one, then add one day to the latter one.

Here is what I have written, but seems not to be working:

from datetime import datetime, timedelta


time_str = ["23:00", "11:00", "1:00", "5:00", "7:00"]

time_date_lst = []

elem = 0
early = datetime.strptime(time_str[0], "%H:%M")

while elem <= len(time_str)-2:

    early = datetime.strptime(time_str[elem], "%H:%M")
    later = datetime.strptime(time_str[elem 1], "%H:%M")
    if later < early:
        later  = timedelta(days=1)
    time_date_lst.append(early)
    time_date_lst.append(later)

    elem  = 1

print(time_date_lst)
[datetime.datetime(1900, 1, 1, 23, 0), datetime.datetime(1900, 1, 2, 11, 0), datetime.datetime(1900, 1, 1, 11, 0), datetime.datetime(1900, 1, 2, 1, 0), datetime.datetime(1900, 1, 1, 1, 0), datetime.datetime(1900, 1, 1, 5, 0), datetime.datetime(1900, 1, 1, 5, 0), datetime.datetime(1900, 1, 1, 7, 0)]

CodePudding user response:

I changed the approach a bit.

  • First I convert all the string times to datetimes directly, before entering any loop.

  • The while loop is replaced by a for loop.

  • No secondary list is created because otherwise your third element for example will not take into consideration that you added days to your second element, etc. We modify in place the given list.


from datetime import datetime, timedelta

# Base data converted to datetime before any processing.
time_str = ["23:00", "11:00", "1:00", "5:00", "7:00"]
time_str = [datetime.strptime(x, "%H:%M") for x in time_str]
 
for n in range(len(time_str) - 1):
    early = time_str[n]
    later = time_str[n 1]
    while later < early:
        later  = timedelta(days=1) # Add as many days as needed.
    time_str[n 1] = later # Store the modified date in the list. 

print("\n".join(map(str, time_str)))
# 1900-01-01 23:00:00
# 1900-01-02 11:00:00
# 1900-01-03 01:00:00
# 1900-01-03 05:00:00
# 1900-01-03 07:00:00

CodePudding user response:

Looking at your code...so you pick two items (early and later) and then you compare them and you append both of them to the list. For example, you compare 23:00 and 11:00 and add both to the list after adding a day to 11:00. Next up, you compare 11:00 and 1:00 and the problem is that you already added 11:00, but you append it again with 1:00.

I modified your while loop to for loop and code below does what I think you want.


HOUR_MIN_FORMAT = "%H:%M"

time_str = ["23:00", "11:00", "1:00", "5:00", "7:00", '3:00']
time_date_lst = []

time = datetime.strptime(time_str[0], "%H:%M")
time_date_lst.append(time)

for index, time in enumerate(time_str):
    if index == len(time_str)-1:
        break
    time = datetime.strptime(time, "%H:%M")
    next_time = datetime.strptime(time_str[index 1], "%H:%M")

    if next_time < time:
        next_time  = timedelta(days=1)
    #time_date_lst.append(time)
    time_date_lst.append(next_time)

print(time_date_lst)
>> [datetime.datetime(1900, 1, 1, 23, 0), datetime.datetime(1900, 1, 2, 11, 0), datetime.datetime(1900, 1, 2, 1, 0), datetime.datetime(1900, 1, 1, 5, 0), datetime.datetime(1900, 1, 1, 7, 0), datetime.datetime(1900, 1, 2, 3, 0)]
  • Related