Browse Source

initial

master
Jeffrey Paul 1 year ago
commit
7fad5dc142
4 changed files with 67 additions and 0 deletions
  1. +15
    -0
      README.md
  2. +5
    -0
      go.mod
  3. +2
    -0
      go.sum
  4. +45
    -0
      util.go

+ 15
- 0
README.md View File

@@ -0,0 +1,15 @@
# sneak/goutil

This is a bunch of little general-purpose go utility functions that I don't
feel comfortable including as-is in my normal projects as they are too
general, so I put them here.

Suggestions are welcome.

# License

WTFPL (free software, no restrictions)

# Author

* [sneak@sneak.berlin](mailto:sneak@sneak.berlin)

+ 5
- 0
go.mod View File

@@ -0,0 +1,5 @@
module git.eeqj.de/sneak/goutil

go 1.14

require github.com/hako/durafmt v0.0.0-20191009132224-3f39dc1ed9f4

+ 2
- 0
go.sum View File

@@ -0,0 +1,2 @@
github.com/hako/durafmt v0.0.0-20191009132224-3f39dc1ed9f4 h1:60gBOooTSmNtrqNaRvrDbi8VAne0REaek2agjnITKSw=
github.com/hako/durafmt v0.0.0-20191009132224-3f39dc1ed9f4/go.mod h1:5Scbynm8dF1XAPwIwkGPqzkM/shndPm79Jd1003hTjE=

+ 45
- 0
util.go View File

@@ -0,0 +1,45 @@
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
}

Loading…
Cancel
Save