Add hour handling in convertCPUTimes function

This commit add hour handling in convertCPUTimes function.

The time string usually comes from macOS command line:
ps -a -o stime,utime -p <pid>

which could contain hour string.
pull/655/head
Liam Xu 6 years ago
parent 381f7cc0bf
commit b3670f8027

@ -389,12 +389,31 @@ func convertCPUTimes(s string) (ret float64, err error) {
var _tmp string
if strings.Contains(s, ":") {
_t := strings.Split(s, ":")
hour, err := strconv.Atoi(_t[0])
if err != nil {
return ret, err
}
t += hour * 60 * 100
_tmp = _t[1]
if len(_t) > 3 {
return ret, err
} else if len(_t) == 3 {
hour, err := strconv.Atoi(_t[0])
if err != nil {
return ret, err
}
t += hour * 60 * 60 * ClockTicks
mins, err := strconv.Atoi(_t[1])
if err != nil {
return ret, err
}
t += mins * 60 * ClockTicks
_tmp = _t[2]
} else if len(_t) == 2 {
mins, err := strconv.Atoi(_t[0])
if err != nil {
return ret, err
}
t += mins * 60 * ClockTicks
_tmp = _t[1]
} else {
_tmp = s
}
} else {
_tmp = s
}
@ -404,7 +423,7 @@ func convertCPUTimes(s string) (ret float64, err error) {
return ret, err
}
h, err := strconv.Atoi(_t[0])
t += h * 100
t += h * ClockTicks
h, err = strconv.Atoi(_t[1])
t += h
return float64(t) / ClockTicks, nil

Loading…
Cancel
Save