Parse ps' “etime” output and convert it into seconds

后端 未结 14 2161
难免孤独
难免孤独 2020-12-30 12:43

These are possible output formats for ps h -eo etime

21-18:26:30
   15:28:37
      48:14
      00:01

How to parse them into se

14条回答
  •  误落风尘
    2020-12-30 13:04

    A version for Python:

    ex=[
        '21-18:26:30',
        '06-00:15:30',
        '15:28:37',
        '48:14',
        '00:01'
        ]
    
    def etime_to_secs(e):
        t=e.replace('-',':').split(':')
        t=[0]*(4-len(t))+[int(i) for i in t]
        return t[0]*86400+t[1]*3600+t[2]*60+t[3]
    
    for e in ex:
        print('{:11s}: {:d}'.format(e, etime_to_secs(e)))
    

提交回复
热议问题