43 lines
701 B
Go
43 lines
701 B
Go
package utils
|
|
|
|
import (
|
|
"crypto/rand"
|
|
"encoding/hex"
|
|
"math"
|
|
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
func Now() time.Time {
|
|
return time.Now().UTC()
|
|
}
|
|
|
|
func StripAfterDot(s string) string {
|
|
if idx := strings.Index(s, "."); idx != -1 {
|
|
return s[:idx]
|
|
}
|
|
return s
|
|
}
|
|
|
|
func NameFromEmail(email string) string {
|
|
parts := strings.SplitN(email, "@", 2)
|
|
if len(parts) > 0 {
|
|
return parts[0]
|
|
}
|
|
return ""
|
|
}
|
|
|
|
func RoundFloat(val float32, precision uint) float32 {
|
|
ratio := math.Pow(10, float64(precision))
|
|
return float32(math.Round(float64(val)*ratio) / ratio)
|
|
}
|
|
|
|
func MustRandToken(n int) string {
|
|
b := make([]byte, n)
|
|
if _, err := rand.Read(b); err != nil {
|
|
panic(err)
|
|
}
|
|
return hex.EncodeToString(b)
|
|
}
|