Skip to content

Commit

Permalink
👔 up: strutil - update some encode and id generate logic
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Sep 1, 2023
1 parent 3e711a4 commit 1193638
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 11 deletions.
18 changes: 12 additions & 6 deletions strutil/encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,28 +111,34 @@ func B32Decode(str string) string {
return string(dec)
}

// base64 encoding with no padding
var (
B64Std = base64.StdEncoding.WithPadding(base64.NoPadding)
B64URL = base64.URLEncoding.WithPadding(base64.NoPadding)
)

// B64Encode base64 encode
func B64Encode(str string) string {
return base64.StdEncoding.EncodeToString([]byte(str))
return B64Std.EncodeToString([]byte(str))
}

// B64EncodeBytes base64 encode
func B64EncodeBytes(src []byte) []byte {
buf := make([]byte, base64.StdEncoding.EncodedLen(len(src)))
base64.StdEncoding.Encode(buf, src)
buf := make([]byte, B64Std.EncodedLen(len(src)))
B64Std.Encode(buf, src)
return buf
}

// B64Decode base64 decode
func B64Decode(str string) string {
dec, _ := base64.StdEncoding.DecodeString(str)
dec, _ := B64Std.DecodeString(str)
return string(dec)
}

// B64DecodeBytes base64 decode
func B64DecodeBytes(str []byte) []byte {
dbuf := make([]byte, base64.StdEncoding.DecodedLen(len(str)))
n, _ := base64.StdEncoding.Decode(dbuf, str)
dbuf := make([]byte, B64Std.DecodedLen(len(str)))
n, _ := B64Std.Decode(dbuf, str)
return dbuf[:n]
}

Expand Down
13 changes: 10 additions & 3 deletions strutil/id.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,18 @@ var (
)

// MicroTimeID generate.
// return like: 16074145697981929446(len: 20)
// - return like: 16074145697981929446(len: 20)
//
// Conv Base:
//
// mtId := MicroTimeID() // eg: 16935349145643425047 len: 20
// b16id := Base10Conv(mtId, 16) // eg: eb067252154a9d17 len: 16
// b32id := Base10Conv(mtId, 32) // eg: em1jia8akl78n len: 13
// b36id := Base10Conv(mtId, 36) // eg: 3ko088phiuoev len: 13
// b62id := Base10Conv(mtId, 62) // eg: kb24SKgsQ9V len: 11
func MicroTimeID() string {
ms := time.Now().UnixNano() / 1000
ri := mathutil.RandomInt(DefMinInt, DefMaxInt)

return strconv.FormatInt(ms, 10) + strconv.FormatInt(int64(ri), 10)
}

Expand All @@ -49,7 +56,7 @@ func DatetimeNo(prefix string) string {
bs = append(bs, prefix...)
}

// micro datatime
// micro datetime
bs = nt.AppendFormat(bs, "20060102150405.000000")
bs[14+pl] = '0'

Expand Down
11 changes: 11 additions & 0 deletions strutil/id_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,17 @@ func TestMicroTimeID(t *testing.T) {
fmt.Println(id, "len:", len(id))
assert.NotEmpty(t, id)
}

id := strutil.MicroTimeID()
fmt.Println("mtID :", id, "len:", len(id))
b16id := strutil.Base10Conv(id, 16)
fmt.Println("b16id:", b16id, "len:", len(b16id))
b32id := strutil.Base10Conv(id, 32)
fmt.Println("b32id:", b32id, "len:", len(b32id))
b36id := strutil.Base10Conv(id, 36)
fmt.Println("b36id:", b36id, "len:", len(b36id))
b62id := strutil.Base10Conv(id, 62)
fmt.Println("b62id:", b62id, "len:", len(b62id))
}

func TestMicroTimeHexID(t *testing.T) {
Expand Down
4 changes: 2 additions & 2 deletions strutil/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func MustToTime(s string, layouts ...string) time.Time {
// key is layout length.
var layoutMap = map[int][]string{
6: {"200601", "060102", time.Kitchen},
8: {"20060102"},
8: {"20060102", "06-01-02"},
10: {"2006-01-02"},
13: {"2006-01-02 15"},
15: {time.Stamp},
Expand Down Expand Up @@ -81,7 +81,7 @@ func ToTime(s string, layouts ...string) (t time.Time, err error) {
layout = strings.Replace(layout, "-", "/", -1)
}

t, err = time.Parse(layout, s)
t, err = time.ParseInLocation(layout, s, time.Local)
if err == nil {
return
}
Expand Down

0 comments on commit 1193638

Please sign in to comment.