package util import ( "bytes" "fmt" "math" "strconv" "strings" "time" ) func CurrentMaxFrameId() int32 { currentHour := time.Now().Hour() return int32(math.Floor(float64(currentHour/2))) * 10 } func ArrayInt32ToStrings(cowIds []int32, cutset string) string { var cows bytes.Buffer if len(cowIds) <= 0 { return cows.String() } for i, v := range cowIds { if i > 0 { cows.WriteString(cutset) } cows.WriteString(strconv.Itoa(int(v))) // 将整数转换为字符串 } return cows.String() } // ConvertCowIdsToInt64Slice 将逗号拼接的字符串转换为 int64 切片 func ConvertCowIdsToInt64Slice(input string) ([]int64, error) { // 如果输入为空,直接返回空切片 if input == "" { return []int64{}, nil } // 按逗号分割字符串 parts := strings.Split(input, ",") // 初始化结果切片 result := make([]int64, 0, len(parts)) // 遍历每个部分,转换为 int64 for _, part := range parts { // 去除空格 part = strings.TrimSpace(part) if part == "" { continue // 忽略空字符串 } // 转换为 int64 num, err := strconv.ParseInt(part, 10, 64) if err != nil { return nil, fmt.Errorf("invalid number: %s", part) } // 添加到结果切片 result = append(result, num) } return result, nil } // GetMonthStartAndEndTimestamp 获取当前月份的开始时间戳和结束时间戳 func GetMonthStartAndEndTimestamp() (startTimestamp, endTimestamp int64) { // 获取当前时间 now := time.Now() // 获取当前月份的第一天 startOfMonth := time.Date(now.Year(), now.Month(), 1, 0, 0, 0, 0, now.Location()) // 获取下一个月份的第一天,然后减去一秒,得到当前月份的最后一天 endOfMonth := startOfMonth.AddDate(0, 1, 0).Add(-time.Second) // 转换为时间戳 startTimestamp = startOfMonth.Unix() endTimestamp = endOfMonth.Unix() return startTimestamp, endTimestamp } // SubDays 计算两个日期(时间戳)之间的天数差 func SubDays(startDay, endDay int64) int32 { s1 := time.Unix(startDay, 0) s2 := time.Unix(endDay, 0) return int32(s2.Sub(s1).Hours() / 24) } func TimeParseLocal(layout, dateTime string) (time.Time, error) { loc, _ := time.LoadLocation("Local") return time.ParseInLocation(layout, dateTime, loc) } func Substr(s string, start, length int) string { // 转换为rune切片以正确处理Unicode字符 runes := []rune(s) l := start + length if l > len(runes) { l = len(runes) } return string(runes[start:l]) }