package util import ( "fmt" "math" "time" ) const ( LocationName = "Asia/Shanghai" LayoutTime = "2006-01-02 15:04:05" Layout = "2006-01-02" ) // TimeParseLocalUnix 获取当天零点的时间戳 // eg 2023-02-22 => 1676995200 func TimeParseLocalUnix(DayTime string) int64 { value := DayTime if len(DayTime) <= 11 { value = fmt.Sprintf("%s 00:00:00", DayTime) } loc, _ := time.LoadLocation("Local") theTime, _ := time.ParseInLocation(LayoutTime, value, loc) return theTime.Unix() } // TimeParseLocalEndUnix 获取当天24点的时间戳 // eg 2023-02-22 => 1676995200 func TimeParseLocalEndUnix(DayTime string) int64 { value := DayTime if len(DayTime) <= 11 { value = fmt.Sprintf("%s 23:59:59", DayTime) } loc, _ := time.LoadLocation("Local") theTime, _ := time.ParseInLocation(LayoutTime, value, loc) return theTime.Unix() } // ConvertParseLocalUnix 字符串转换当天时间戳 // eg 15:04:05 => 1676998245 func ConvertParseLocalUnix(timeParse string) (int64, error) { loc, err := time.LoadLocation("Local") if err != nil { return 0, err } value := fmt.Sprintf("%s %s", time.Now().Format(Layout), timeParse) theTime, err := time.ParseInLocation(LayoutTime, value, loc) if err != nil { return 0, err } return theTime.Unix(), nil } // GetMonthRemainDay 获取当前月还剩几天 func GetMonthRemainDay() int { now := time.Now() lastDayOfMonth := time.Date(now.Year(), now.Month()+1, 0, 23, 59, 59, 999999999, now.Location()) return int(lastDayOfMonth.Sub(now).Hours()/24) + 1 } // Ceil 向上取整函数 func Ceil(x float64) float64 { // 使用 math.Floor 计算小于或等于 x 的最大整数 // 然后检查 x 是否为整数,如果不是,则结果加 1 // 注意:math.Floor 返回的是 float64 类型,所以我们需要进行比较 // 来确定是否需要加 1 intPart := math.Floor(x) if x-intPart > 0 { return intPart + 1 } return intPart }