Way to take diff of timestamp in python

I am trying to measure bootloader timing where in I need to measure SPL, U-Boot and total time ( SPL + U-Boot). My minicom console log entries looks as follows

$ cat u-boot-2017-01 | grep -e 'U-Boot SPL 2017' -e 'U-Boot 2017.01' -e 'Starting kernel ...'
[2018-10-11 15:05:11.021] U-Boot SPL 2017.01-05786-ge0aa2fcb13 (Oct 10 2018 - 13:53:01)
[2018-10-11 15:05:11.294] U-Boot 2017.01-05786-ge0aa2fcb13 (Oct 10 2018 - 13:53:01 -0400)
[2018-10-11 15:05:12.706] Starting kernel ...

Now

SPL boot time is : timestamp diff of line 1 and line 2 ( SPL start and U-Boot start)
U-Boot boot time is : timestamp diff of line 2 and line 3 ( U-Boot start and starting kernel)
Total time is : timestamp diff of line 1 and line 3.

I see that python timedelta supports Year, month, day, hour, minute, second, microseconds, milliseconds but not sure how do i convert this timestamp to timedelta. I am learning python so want to try it in there. can someone suggest how it should be done. I have to do these measurement for multiple products and multiple versions of bootloader so scripting would be nice.

1 answer

  • answered 2018-10-11 19:48 DeepSpace

    This is pretty much straightforward by using datetime.strptime, just need to make sure to use the correct format.

    See the available directives here.

    from datetime import datetime
    
    start_time = '2018-10-11 15:05:11.021'
    end_time = '2018-10-11 15:05:11.294'
    
    FORMAT = '%Y-%m-%d %H:%M:%S.%f'
    
    print(datetime.strptime(end_time, FORMAT) - datetime.strptime(start_time, FORMAT))
    # 0:00:00.273000