forked from free-lancers/electricity_bill_calc_service
refactor(deps):将一些工具函数用工具库替换。
This commit is contained in:
16
tools/time.go
Normal file
16
tools/time.go
Normal file
@@ -0,0 +1,16 @@
|
||||
package tools
|
||||
|
||||
import (
|
||||
"time"
|
||||
)
|
||||
|
||||
func DifferenceInMonth(t1, t2 time.Time) int {
|
||||
var differYear, differMonth int
|
||||
differYear = t1.Year() - t2.Year()
|
||||
differMonth = int(t1.Month() - t2.Month())
|
||||
return differYear*12 + differMonth
|
||||
}
|
||||
|
||||
func IsNextMonth(origin, t time.Time) bool {
|
||||
return DifferenceInMonth(t, origin) == 1
|
||||
}
|
23
tools/utils.go
Normal file
23
tools/utils.go
Normal file
@@ -0,0 +1,23 @@
|
||||
package tools
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"github.com/mozillazg/go-pinyin"
|
||||
"github.com/samber/lo"
|
||||
)
|
||||
|
||||
func ContainsInsensitive(element string, slice []string) bool {
|
||||
lowercasedElement := strings.TrimSpace(strings.ToLower(element))
|
||||
return lo.Contains(slice, lowercasedElement)
|
||||
}
|
||||
|
||||
func PinyinAbbr(source string) string {
|
||||
abbr := pinyin.Pinyin(source, pinyin.NewArgs())
|
||||
var abbrCollect = make([]string, 0)
|
||||
for _, a := range abbr {
|
||||
abbrCollect = append(abbrCollect, a[0][0:1])
|
||||
}
|
||||
finalAbbr := strings.Join(abbrCollect, "")
|
||||
return finalAbbr
|
||||
}
|
36
tools/verify_code.go
Normal file
36
tools/verify_code.go
Normal file
@@ -0,0 +1,36 @@
|
||||
package tools
|
||||
|
||||
import (
|
||||
"math/rand"
|
||||
"time"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
const letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"
|
||||
|
||||
var src = rand.NewSource(time.Now().UnixNano())
|
||||
|
||||
const (
|
||||
// 6 bits to represent a letter index
|
||||
letterIdBits = 6
|
||||
// All 1-bits as many as letterIdBits
|
||||
letterIdMask = 1<<letterIdBits - 1
|
||||
letterIdMax = 63 / letterIdBits
|
||||
)
|
||||
|
||||
func RandStr(n int) string {
|
||||
b := make([]byte, n)
|
||||
// A rand.Int63() generates 63 random bits, enough for letterIdMax letters!
|
||||
for i, cache, remain := n-1, src.Int63(), letterIdMax; i >= 0; {
|
||||
if remain == 0 {
|
||||
cache, remain = src.Int63(), letterIdMax
|
||||
}
|
||||
if idx := int(cache & letterIdMask); idx < len(letters) {
|
||||
b[i] = letters[idx]
|
||||
i--
|
||||
}
|
||||
cache >>= letterIdBits
|
||||
remain--
|
||||
}
|
||||
return *(*string)(unsafe.Pointer(&b))
|
||||
}
|
Reference in New Issue
Block a user