36 lines
732 B
Go
36 lines
732 B
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"strconv"
|
|
"time"
|
|
)
|
|
|
|
const timeFmt = "2006-01-02 15:04:05"
|
|
|
|
func now() string {
|
|
return time.Now().Format(timeFmt)
|
|
}
|
|
|
|
func since(t time.Time) string {
|
|
s := time.Since(t)
|
|
u := uint64(s)
|
|
if u <= 0 {
|
|
return "0s"
|
|
}
|
|
str := s.String()
|
|
switch {
|
|
case u >= uint64(time.Second):
|
|
f, _ := strconv.ParseFloat(str[:len(str)-1], 64)
|
|
return fmt.Sprintf("%.fs", f)
|
|
case u < uint64(time.Microsecond):
|
|
f, _ := strconv.ParseFloat(str[:len(str)-2], 64)
|
|
return fmt.Sprintf("%.fns", f)
|
|
case u < uint64(time.Millisecond):
|
|
f, _ := strconv.ParseFloat(str[:len(str)-3], 64)
|
|
return fmt.Sprintf("%.fµs", f)
|
|
default:
|
|
f, _ := strconv.ParseFloat(str[:len(str)-2], 64)
|
|
return fmt.Sprintf("%.fms", f)
|
|
}
|
|
}
|