Files
opencloud/vendor/github.com/gookit/goutil/strutil/id.go
dependabot[bot] 5ebc596352 Bump github.com/gookit/config/v2 from 2.1.8 to 2.2.2
Bumps [github.com/gookit/config/v2](https://github.com/gookit/config) from 2.1.8 to 2.2.2.
- [Release notes](https://github.com/gookit/config/releases)
- [Commits](https://github.com/gookit/config/compare/v2.1.8...v2.2.2)

---
updated-dependencies:
- dependency-name: github.com/gookit/config/v2
  dependency-type: direct:production
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <support@github.com>
2023-06-13 10:54:58 +02:00

69 lines
1.4 KiB
Go

package strutil
import (
"hash/crc32"
"math/rand"
"os"
"strconv"
"time"
"github.com/gookit/goutil/mathutil"
)
// global id:
//
// https://github.com/rs/xid
// https://github.com/satori/go.uuid
var (
DefMinInt = 1000
DefMaxInt = 9999
)
// MicroTimeID generate.
// return like: 16074145697981929446(len: 20)
func MicroTimeID() string {
ms := time.Now().UnixNano() / 1000
ri := mathutil.RandomInt(DefMinInt, DefMaxInt)
return strconv.FormatInt(ms, 10) + strconv.FormatInt(int64(ri), 10)
}
// MicroTimeHexID generate.
// return like: 5b5f0588af1761ad3(len: 16-17)
func MicroTimeHexID() string {
ms := time.Now().UnixNano() / 1000
ri := mathutil.RandomInt(DefMinInt, DefMaxInt)
return strconv.FormatInt(ms, 16) + strconv.FormatInt(int64(ri), 16)
}
// DatetimeNo generate. can use for order-no.
//
// No prefix, return like: 2023041410484904074285478388(len: 28)
func DatetimeNo(prefix string) string {
nt := time.Now()
pl := len(prefix)
bs := make([]byte, 0, 28+pl)
if pl > 0 {
bs = append(bs, prefix...)
}
// micro datatime
bs = nt.AppendFormat(bs, "20060102150405.000000")
bs[14+pl] = '0'
// host
name, err := os.Hostname()
if err != nil {
name = "default"
}
c32 := crc32.ChecksumIEEE([]byte(name)) // eg: 4006367001
bs = strconv.AppendUint(bs, uint64(c32%99), 10)
// rand 10000 - 99999
rand.Seed(nt.UnixNano())
bs = strconv.AppendInt(bs, 10000+rand.Int63n(89999), 10)
return string(bs)
}