This commit is contained in:
ideaa
2024-08-14 09:47:00 +08:00
parent 5c4cf1b2c0
commit 8ab720505e
6 changed files with 77 additions and 13 deletions

View File

@@ -6,7 +6,7 @@ import (
)
func Bites(size float64) string {
unit := []string{"b", "kb", "mb", "gb", "tb", "pb"}
unit := []string{"B", "KB", "MB", "GB", "TB", "PB"}
s := math.Floor(math.Log(size) / math.Log(1024))
i := int(s)

View File

@@ -0,0 +1,15 @@
package format
import (
"log"
"testing"
"time"
)
func TestTimeSince(t *testing.T) {
past := time.Now()
time.Sleep(time.Millisecond)
a := TimeSince(past)
log.Println(a)
}

View File

@@ -0,0 +1,28 @@
package format
import (
"fmt"
"time"
)
func TimeSince(t time.Time) string {
now := time.Now()
duration := now.Sub(t)
seconds := int(duration.Seconds())
minutes := int(duration.Minutes())
hours := int(duration.Hours())
days := hours / 24
if seconds < 1 {
return fmt.Sprintf("%.2f秒", duration.Seconds())
} else if seconds < 60 {
return fmt.Sprintf("%d秒", seconds)
} else if minutes < 60 {
return fmt.Sprintf("%d分钟%d秒", minutes, seconds%60)
} else if hours < 24 {
return fmt.Sprintf("%d小时%d分钟", hours, minutes%60)
} else {
return fmt.Sprintf("%d天%d小时%d分钟", days, hours%24, minutes%60)
}
}

View File

@@ -1,38 +1,47 @@
package utils
import (
"fmt"
"strconv"
"strings"
"golang.org/x/exp/constraints"
)
func StringToIntSlice(str string) []uint {
// Integer 限制 T 只能是整数类型。
type Integer interface {
constraints.Integer
}
// StringToSlice 将字符串转换为去重后的整数切片。
func StringToSlice[T Integer](str string) []T {
parts := strings.Split(str, ",")
seen := make(map[uint]bool)
nums := make([]uint, 0, len(parts))
seen := make(map[T]bool)
nums := make([]T, 0, len(parts))
for _, part := range parts {
part = strings.TrimSpace(part)
if part == "" {
continue
}
num, err := strconv.Atoi(part)
num, err := strconv.ParseInt(part, 10, 64)
if err != nil {
continue
}
uNum := uint(num)
if !seen[uNum] {
nums = append(nums, uNum)
seen[uNum] = true
typedNum := T(num)
if !seen[typedNum] {
nums = append(nums, typedNum)
seen[typedNum] = true
}
}
return nums
}
func IntSliceToString(nums ...uint) string {
func IntSliceToString[T Integer](nums ...T) string {
var strBuilder strings.Builder
seen := make(map[uint]bool)
seen := make(map[T]bool)
for i, num := range nums {
if seen[num] {
continue
@@ -41,7 +50,8 @@ func IntSliceToString(nums ...uint) string {
if i > 0 {
strBuilder.WriteString(",")
}
strBuilder.WriteString(strconv.FormatUint(uint64(num), 10))
strBuilder.WriteString(fmt.Sprintf("%d", num))
seen[num] = true
}
return strBuilder.String()

11
utils/string2id_test.go Normal file
View File

@@ -0,0 +1,11 @@
package utils
import (
"log"
"testing"
)
func TestIntSliceToString(t *testing.T) {
slice := StringToSlice[int64]("574159,513036,500295,492558,480641,480174,423891,385470,360901,246633")
log.Println(slice)
}

View File

@@ -109,7 +109,7 @@ func (c *Client) Write() {
c.Log("->", string(msg))
case <-timer.C:
err := wsutil.WriteServerMessage(c.Conn, ws.OpPing, nil)
err := wsutil.WriteServerMessage(c.Conn, ws.OpPing, []byte("ping"))
if err != nil {
c.Log("xx", "Error actively pinging the client", err.Error())
return