gowiki/pkg/util/helper.go
2018-09-04 23:15:47 +02:00

47 lines
975 B
Go

package util
import (
"fmt"
"strconv"
"strings"
"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)
}
}
func StoreTitle(section, title string) string {
return section + "/" + title
}
func UnstoreTitle(storeTitle string) (section, title string) {
if a := strings.Split(storeTitle, "/"); len(a) == 2 {
return a[0], a[1]
}
return
}