46 lines
928 B
Go
46 lines
928 B
Go
package goutil
|
|
|
|
import (
|
|
"errors"
|
|
"math"
|
|
"os"
|
|
"regexp"
|
|
"time"
|
|
|
|
"github.com/hako/durafmt"
|
|
)
|
|
|
|
func FilterToAlnum(input string) string {
|
|
re := regexp.MustCompile("[^a-zA-Z0-9]+")
|
|
return re.ReplaceAllString(input, "")
|
|
}
|
|
|
|
func TimeDiffHuman(first time.Time, second time.Time) string {
|
|
if first.Before(second) {
|
|
return durafmt.ParseShort(second.Sub(first)).String()
|
|
} else {
|
|
return durafmt.ParseShort(first.Sub(second)).String()
|
|
}
|
|
}
|
|
|
|
// Does anyone else use uints for things that are always >=0 like counts and
|
|
func TimeDiffAbsSeconds(first time.Time, second time.Time) uint {
|
|
return uint(math.Abs(first.Sub(second).Truncate(time.Second).Seconds()))
|
|
}
|
|
|
|
func Mkdirp(p string) error {
|
|
src, err := os.Stat(p)
|
|
if os.IsNotExist(err) {
|
|
errDir := os.MkdirAll(p, 0755)
|
|
if errDir != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|
|
if src.Mode().IsRegular() {
|
|
return errors.New("file already exists at path")
|
|
}
|
|
return nil
|
|
}
|