package utils import ( "errors" "fmt" "regexp" "strconv" "strings" "time" ) func StrToTime(s string) (int64, error) { // delete all not int characters if s == "" { return time.Now().Unix(), nil } r := make([]rune, 14) l := 0 // 过滤除数字以外的字符 for _, v := range s { if '0' <= v && v <= '9' { r[l] = v l++ if l == 14 { break } } } for l < 14 { r[l] = '0' // 补0 l++ } t, err := time.Parse("20060102150405", string(r)) if err != nil { return 0, err } return t.Unix(), nil } func TimeToStr(unixSecTime interface{}, layout ...string) string { i := AnyToInt64(unixSecTime) if i == 0 { return "" } f := "2006-01-02 15:04:05" if len(layout) > 0 { f = layout[0] } return time.Unix(i, 0).Format(f) } func Time2String(date time.Time, format string) string { if format == "" { format = "2006-01-02 15:04:05" } timeS := date.Format(format) if timeS == "0001-01-01 00:00:00" { return "" } return timeS } func FormatNanoUnix() string { return strings.Replace(time.Now().Format("20060102150405.0000000"), ".", "", 1) } func TimeParse(format, src string) (time.Time, error) { return time.ParseInLocation(format, src, time.Local) } func TimeParseStd(src string) time.Time { t, _ := TimeParse("2006-01-02 15:04:05", src) return t } func TimeParseDateStd(src string) time.Time { t, _ := TimeParse("2006-01-02", src) return t } func TimeStdParseUnix(src string) int64 { t, err := TimeParse("2006-01-02 15:04:05", src) if err != nil { return 0 } return t.Unix() } // 获取一个当前时间 时间间隔 时间戳 func GetTimeInterval(unit string, amount int) (startTime, endTime int64) { t := time.Now() nowTime := t.Unix() tmpTime := int64(0) switch unit { case "years": tmpTime = time.Date(t.Year()+amount, t.Month(), t.Day(), t.Hour(), 0, 0, 0, t.Location()).Unix() case "months": tmpTime = time.Date(t.Year(), t.Month()+time.Month(amount), t.Day(), t.Hour(), 0, 0, 0, t.Location()).Unix() case "days": tmpTime = time.Date(t.Year(), t.Month(), t.Day()+amount, t.Hour(), 0, 0, 0, t.Location()).Unix() case "hours": tmpTime = time.Date(t.Year(), t.Month(), t.Day(), t.Hour()+amount, 0, 0, 0, t.Location()).Unix() } if amount > 0 { startTime = nowTime endTime = tmpTime } else { startTime = tmpTime endTime = nowTime } return } // 几天前 func TimeInterval(newTime int) string { now := time.Now().Unix() newTime64 := AnyToInt64(newTime) if newTime64 >= now { return "刚刚" } interval := now - newTime64 switch { case interval < 60: return AnyToString(interval) + "秒前" case interval < 60*60: return AnyToString(interval/60) + "分前" case interval < 60*60*24: return AnyToString(interval/60/60) + "小时前" case interval < 60*60*24*30: return AnyToString(interval/60/60/24) + "天前" case interval < 60*60*24*30*12: return AnyToString(interval/60/60/24/30) + "月前" default: return AnyToString(interval/60/60/24/30/12) + "年前" } } // 时分秒字符串转时间戳,传入示例:8:40 or 8:40:10 func HmsToUnix(str string) (int64, error) { t := time.Now() arr := strings.Split(str, ":") if len(arr) < 2 { return 0, errors.New("Time format error") } h, _ := strconv.Atoi(arr[0]) m, _ := strconv.Atoi(arr[1]) s := 0 if len(arr) == 3 { s, _ = strconv.Atoi(arr[3]) } formatted1 := fmt.Sprintf("%d%02d%02d%02d%02d%02d", t.Year(), t.Month(), t.Day(), h, m, s) res, err := time.ParseInLocation("20060102150405", formatted1, time.Local) if err != nil { return 0, err } else { return res.Unix(), nil } } // 获取特定时间范围 func GetTimeRange(s string) map[string]int64 { t := time.Now() var stime, etime time.Time switch s { case "today": stime = time.Date(t.Year(), t.Month(), t.Day(), 0, 0, 0, 0, t.Location()) etime = time.Date(t.Year(), t.Month(), t.Day()+1, 0, 0, 0, 0, t.Location()) case "yesterday": stime = time.Date(t.Year(), t.Month(), t.Day()-1, 0, 0, 0, 0, t.Location()) etime = time.Date(t.Year(), t.Month(), t.Day(), 0, 0, 0, 0, t.Location()) case "within_seven_days": // 前6天0点 stime = time.Date(t.Year(), t.Month(), t.Day()-6, 0, 0, 0, 0, t.Location()) // 明天 0点 etime = time.Date(t.Year(), t.Month(), t.Day()+1, 0, 0, 0, 0, t.Location()) case "current_month": stime = time.Date(t.Year(), t.Month(), 0, 0, 0, 0, 0, t.Location()) etime = time.Date(t.Year(), t.Month()+1, 0, 0, 0, 0, 0, t.Location()) case "last_month": stime = time.Date(t.Year(), t.Month()-1, 0, 0, 0, 0, 0, t.Location()) etime = time.Date(t.Year(), t.Month(), 0, 0, 0, 0, 0, t.Location()) } return map[string]int64{ "start": stime.Unix(), "end": etime.Unix(), } } // 获取特定时间范围 func GetDateTimeRangeStr(s string) (string, string) { t := time.Now() var stime, etime time.Time switch s { case "today": stime = time.Date(t.Year(), t.Month(), t.Day(), 0, 0, 0, 0, t.Location()) etime = time.Date(t.Year(), t.Month(), t.Day()+1, 0, 0, 0, 0, t.Location()) case "yesterday": stime = time.Date(t.Year(), t.Month(), t.Day()-1, 0, 0, 0, 0, t.Location()) etime = time.Date(t.Year(), t.Month(), t.Day(), 0, 0, 0, 0, t.Location()) case "within_seven_days": // 前6天0点 stime = time.Date(t.Year(), t.Month(), t.Day()-6, 0, 0, 0, 0, t.Location()) // 明天 0点 etime = time.Date(t.Year(), t.Month(), t.Day()+1, 0, 0, 0, 0, t.Location()) case "current_month": stime = time.Date(t.Year(), t.Month(), 0, 0, 0, 0, 0, t.Location()) etime = time.Date(t.Year(), t.Month()+1, 0, 0, 0, 0, 0, t.Location()) case "last_month": stime = time.Date(t.Year(), t.Month()-1, 0, 0, 0, 0, 0, t.Location()) etime = time.Date(t.Year(), t.Month(), 0, 0, 0, 0, 0, t.Location()) } return stime.Format("2006-01-02 15:04:05"), etime.Format("2006-01-02 15:04:05") } // 获取传入的时间所在月份的第一天,即某月第一天的0点。如传入time.Now(), 返回当前月份的第一天0点时间。 func GetFirstDateOfMonth(d time.Time) time.Time { d = d.AddDate(0, 0, -d.Day()+1) return GetZeroTime(d) } // 获取传入的时间所在月份的最后一天,即某月最后一天的0点。如传入time.Now(), 返回当前月份的最后一天0点时间。 func GetLastDateOfMonth(d time.Time) time.Time { return GetFirstDateOfMonth(d).AddDate(0, 1, -1) } // 获取某一天的0点时间 func GetZeroTime(d time.Time) time.Time { return time.Date(d.Year(), d.Month(), d.Day(), 0, 0, 0, 0, d.Location()) } // GetWeekInfo 获取周数、开始时间、结束时间 func GetWeekInfo(dateStr string) (string, string, string, string) { date := TimeParseStd(dateStr) year, week := date.ISOWeek() location, _ := time.LoadLocation("Asia/Shanghai") // 计算给定年份1月1日是星期几 startOfYear := time.Date(year, time.January, 1, 0, 0, 0, 0, location) daysOffset := int(startOfYear.Weekday()) - int(time.Monday) + 1 // 计算给定年份的第一周的开始日期 firstWeekStart := startOfYear.AddDate(0, 0, -daysOffset+1) // 计算给定周的开始日期 weekStart := firstWeekStart.AddDate(0, 0, (week-1)*7) weekEnd := weekStart.AddDate(0, 0, 6) return IntToStr(year), IntToStr(week), weekStart.Format("2006-01-02"), weekEnd.Format("2006-01-02") } // GetWeekInfoByDate 获取周数、开始时间、结束时间 func GetWeekInfoByDate(dateStr string) (string, string, string, string) { date := TimeParseDateStd(dateStr) year, week := date.ISOWeek() location, _ := time.LoadLocation("Asia/Shanghai") // 计算给定年份1月1日是星期几 startOfYear := time.Date(year, time.January, 1, 0, 0, 0, 0, location) daysOffset := int(startOfYear.Weekday()) - int(time.Monday) + 1 // 计算给定年份的第一周的开始日期 firstWeekStart := startOfYear.AddDate(0, 0, -daysOffset+1) // 计算给定周的开始日期 weekStart := firstWeekStart.AddDate(0, 0, (week-1)*7) weekEnd := weekStart.AddDate(0, 0, 6) return IntToStr(year), IntToStr(week), weekStart.Format("2006-01-02"), weekEnd.Format("2006-01-02") } func IsTimeFormat(s string) bool { // 正则表达式匹配HH:MM格式 re := regexp.MustCompile(`^(\d{2}):(\d{2})$`) return re.MatchString(s) }