Home > Mobile >  How do i find the duration between two timings for multiple events?
How do i find the duration between two timings for multiple events?

Time:04-18

My task is to find out how long the bus is charged for, and when charging type = 1, it means that the charger is plugged in. How do i find the duration from this? Also i need to find out the charging frequency, is there a way i can do that too?

Here is the code currently:

#Duration loop
foundEnd = False

for i in range(len(dfDur01Mar22)):
    #only display bus 'SG3079S'
    if dfDur01Mar22.iloc[i,1] == 'SG3079S':
        #print 'end' when first '0' appears
        if not foundEnd and dfDur01Mar22.iloc[i,2] == 0:
            print('end')
            foundEnd = True
        #if charging type is 1
        elif dfDur01Mar22.iloc[i,2] == 1:
            print(dfDur01Mar22.iloc[i,0],dfDur01Mar22.iloc[i,1],dfDur01Mar22.iloc[i,2])
            foundEnd = False

and here is the output from it : enter image description here

CodePudding user response:

You can use the datetime module to figure out the amount of time between 2 events:

from datetime import datetime

start = datetime.now()
#Func here
end = datetime.now()

total = end - start

seconds_elapsed = total.total_seconds()

print (seconds_elapsed)
  • Related