subtraction of a string representing the time in Python

Asked 2 years ago, Updated 2 years ago, 340 views

I am currently looking for a way to find the most recent departure time.
I thought it would be better to convert the current time and timetable into four digits and subtract them, but the following problems have occurred.
I would appreciate it if someone could tell me a good way.

↓ Time data status

arrived_time=['1216', '1337', '1018', '1809', '2058', '1919'...]
time_table=['1210', '1230', '1305', '1328', '1400', '1411'...]

For example, subtracting 1216 to 1210 from 1216 shows a six-minute difference, but subtracting 1130 from 1216 makes it 86 and impossible to calculate.

python

2022-09-30 21:55

3 Answers

The problem is that the time is calculated in decimal.
You can get the expected results by dividing the units into minutes and subtracting them.

 from datetime import datetime as dt
t1 = dt.strptime ('1216', '%H%M')
t2 = dt.strptime ('1130', '%H%M')
d=(t1.hour*60+t1.minute)-(t2.hour*60+t2.minute)


2022-09-30 21:55

I've commented a lot, but if you're thinking about a transfer guide as @payaneco showed, it's a normal case.

The time difference calculation is introduced in the comments or, as previously answered, use the datetime module (library?).
For example, @akiraejiri-san and @payaneco-san can do the following:

 diff=str((dt.strptime (departure time, "%H%M") - dt.strptime (currently "%H%M")).total_seconds()//60))

However, under the conditions presented, it is not necessary to calculate the difference after converting all the data into time, and there is no problem to calculate after finding the most recent departure time (just after).
The time information is a four-digit number, so you can simply sort it up and down the string.

 from datetime import datetime as dt

arrived_time=['1216', '1337', '1018', '1809', '2058', '1919']
time_table=['1210', '1230', '1305', '1328', '1400', '1411']

#### combine each time with the attributes of which information
t_arv = [[a,1] for a learned_time]
t_dpt = [[d,2] for in time_table]
t_arv.extend(t_dpt)

#### Sort by time information (because it is a 4-digit number, it can be sorted simply as above and below a string)
t_mix=sorted(t_arv, key=lambdax:x[0])
t_mix_atr=[e[1]for e int_mix]####List only current/departure attributes for search

#### The process of examining the current and most recent departure time pairs and differences
list_result=[ ]
items=len(t_mix)
for i in range (items):
    if t_mix[i][1] == 1:##################################################
        try:
            j=t_mix_atr[i:].index(2)#### Get the index of the most recent departure time after
            arv=t_mix[i][0]
            dpt=t_mix[i+j][0]
            #### Convert string to time, calculate difference value in minutes, and string
            diff=str((dt.strptime(dpt, "%H%M")-dt.strptime(arv, "%H%M")).total_seconds()//60))
            list_result.append([arv, dpt, diff])#### Add to result list
        except:
            pass

For in list_result:
    print(f'Current: {r[0]}, Departure Time: {r[1]}, Difference: {r[2]} Minutes')


2022-09-30 21:55

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.