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

后端 未结 14 2181
难免孤独
难免孤独 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:03

    Ruby version:

    def psETime2Seconds(etime)
      running_secs = 0
      if etime.match(/^(([\d]+)-)?(([\d]+):)?([\d]+):([\d]+)$/)
        running_secs += $2.to_i * 86400 # days
        running_secs += $4.to_i * 3600  # hours
        running_secs += $5.to_i * 60    # minutes
        running_secs += $6.to_i         # seconds
      end
      return running_secs
    end
    

提交回复
热议问题