From 87ccbff5edea9ead68ac6f93a75b5f22bd66710e Mon Sep 17 00:00:00 2001 From: liuzx Date: Tue, 26 Nov 2024 15:37:03 +0800 Subject: [PATCH] feat: upgrade --- algox/algorithm_test.go | 29 +++++++++++ asyncx/asyncx_test.go | 9 ++++ convertx/convert.go | 105 ++++++++++++++++++++++++++++++++++++++++ convertx/num.go | 24 --------- timex/time.go | 13 +++++ 5 files changed, 156 insertions(+), 24 deletions(-) create mode 100644 algox/algorithm_test.go create mode 100644 asyncx/asyncx_test.go create mode 100644 convertx/convert.go delete mode 100644 convertx/num.go create mode 100644 timex/time.go diff --git a/algox/algorithm_test.go b/algox/algorithm_test.go new file mode 100644 index 0000000..8cab059 --- /dev/null +++ b/algox/algorithm_test.go @@ -0,0 +1,29 @@ +package algox + +import "testing" + +func TestBase64Encode(t *testing.T) { + t.Log(Base64Encode("1234567")) +} + +func TestBase64Decode(t *testing.T) { + t.Log(Base64Decode("MTIzNDU2Nw==")) +} + +func TestMD5(t *testing.T) { + t.Log(MD5("1")) +} + +func TestSHA256(t *testing.T) { + t.Log(Sha256("1")) +} + +func TestRandomInt(t *testing.T) { + for range 10 { + t.Log(RandomInt(0, 101111111)) + } +} + +func TestUUID2(t *testing.T) { + t.Log(UUID()) +} diff --git a/asyncx/asyncx_test.go b/asyncx/asyncx_test.go new file mode 100644 index 0000000..4b4c55f --- /dev/null +++ b/asyncx/asyncx_test.go @@ -0,0 +1,9 @@ +package asyncx + +import "testing" + +func TestAsync(t *testing.T) { + WithGoroutine(func() { + t.Log("print") + }, 3) +} diff --git a/convertx/convert.go b/convertx/convert.go new file mode 100644 index 0000000..af1d8f0 --- /dev/null +++ b/convertx/convert.go @@ -0,0 +1,105 @@ +package convertx + +import ( + "errors" + "strconv" + "strings" + "time" +) + +func StringToInt64(v string) (int64, error) { + return strconv.ParseInt(v, 10, 64) +} + +func StringToInt(v string) (int, error) { + return strconv.Atoi(v) +} +func StringToInt8(v string) (int8, error) { + // 先将字符串转换为 int64 + i, err := strconv.ParseInt(v, 10, 8) + if err != nil { + return 0, err + } + + // 检查是否在 int8 范围内 + if i < -128 || i > 127 { + return 0, errors.New("value out of range for int8") + } + + // 转换为 int8 并返回 + return int8(i), nil +} + +// IntToString 整数转字符串 +func IntToString(i int) string { + return strconv.Itoa(i) +} + +// StringToFloat64 字符串转浮点数 +func StringToFloat64(s string) (float64, error) { + return strconv.ParseFloat(s, 64) +} + +// Float64ToString 浮点数转字符串 +func Float64ToString(f float64) string { + return strconv.FormatFloat(f, 'f', -1, 64) +} + +// StringToFloat32 字符串转 float32 +func StringToFloat32(v string) (float32, error) { + f, err := strconv.ParseFloat(v, 32) + if err != nil { + return 0, err + } + return float32(f), nil +} + +// Float32ToString float32 转字符串 +func Float32ToString(f float32) string { + return strconv.FormatFloat(float64(f), 'f', -1, 32) +} + +// StringToBool 字符串转布尔值 +func StringToBool(s string) (bool, error) { + return strconv.ParseBool(s) +} + +// BoolToString 布尔值转字符串 +func BoolToString(b bool) string { + return strconv.FormatBool(b) +} + +// ToLower 字符串转小写 +func ToLower(s string) string { + return strings.ToLower(s) +} + +// ToUpper 字符串转大写 +func ToUpper(s string) string { + return strings.ToUpper(s) +} + +// StringToTime 字符串转时间 +func StringToTime(s string, layout string) (time.Time, error) { + return time.Parse(layout, s) +} + +// TimeToString 时间转字符串 +func TimeToString(t time.Time, layout string) string { + return t.Format(layout) +} + +// NowTimeString 获取当前时间的字符串表示 +func NowTimeString(layout string) string { + return time.Now().Format(layout) +} + +// TimestampToTime 时间戳转时间 +func TimestampToTime(timestamp int64) time.Time { + return time.Unix(timestamp, 0) +} + +// TimeToTimestamp 时间转时间戳 +func TimeToTimestamp(t time.Time) int64 { + return t.Unix() +} diff --git a/convertx/num.go b/convertx/num.go deleted file mode 100644 index 5c911ba..0000000 --- a/convertx/num.go +++ /dev/null @@ -1,24 +0,0 @@ -package convertx - -import ( - "github.com/kiririx/krutils/algox" - "strconv" -) - -var RandomInt = algox.RandomInt - -type Integer interface { - int | int32 | int8 | int64 | int16 | uint | uint16 | uint8 | uint32 | uint64 -} - -func ToInt64() { - -} - -func ToInt(v string) int { - _v, _ := strconv.Atoi(v) - return _v -} -func ToInt8[T Integer](v T) int32 { - return ToInt8(v) -} diff --git a/timex/time.go b/timex/time.go new file mode 100644 index 0000000..5cc00d1 --- /dev/null +++ b/timex/time.go @@ -0,0 +1,13 @@ +package timex + +import "time" + +// DaysBetween 计算两个时间之间的差值(天数) +func DaysBetween(t1, t2 time.Time) int { + return int(t2.Sub(t1).Hours() / 24) +} + +// AddDays 给时间加上指定天数 +func AddDays(t time.Time, days int) time.Time { + return t.AddDate(0, 0, days) +}