代码之家  ›  专栏  ›  技术社区  ›  ART

在python中获取时间戳差异的方法

  •  0
  • ART  · 技术社区  · 7 年前

    我正在尝试测量引导加载程序计时,在这里我需要测量SPL、U-Boot和总时间(SPL+U-Boot)。我的minicom控制台日志条目如下

    $ 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 ...
    

    现在

    SPL启动时间为:第1行和第2行的时间戳差异(SPL开始和结束)
    U-Boot引导时间为:第2行和 第3行(U-Boot启动和启动内核)
    总时间为:时间戳 1号线和3号线的差异。

    我看到那条蟒蛇了 timedelta 支持年、月、日、时、分、秒、微秒、毫秒,但不确定如何将此时间戳转换为timedelta。我正在学python,所以想在那里试试。有人能建议怎么做吗。我必须为多个产品和多个版本的bootloader做这些度量,所以脚本编写会很好。

    1 回复  |  直到 7 年前
        1
  •  2
  •   DeepSpace    7 年前

    通过使用 datetime.strptime ,只需要确保使用正确的格式。

    请参阅可用指令 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