package egg_system_rules import ( "errors" "fmt" "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 String2TimeV2(timeStr string) time.Time { toTime, err := time.ParseInLocation("2006-01-02", timeStr, time.Local) if err != nil { return time.Now() } return toTime } func StringToTime(timeStr string) (time.Time, error) { toTime, err := time.ParseInLocation("2006-01-02 15:04:05", timeStr, time.Local) return toTime, err } 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 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 TimeStdParseUnix(src string) int64 { t, err := TimeParse("2006-01-02 15:04:05", src) if err != nil { return 0 } return t.Unix() } func TimeStdParseUnixDate(src string) int64 { t, err := TimeParse("2006-01-02", 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) < 3 { 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[2]) } 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 GetTimes(s string) map[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 map[string]string{ "start": stime.Format("2006-01-02 15:04:05"), "end": 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) } func GetAnyFirstDateOfMonth(d time.Time, monthDiff int) time.Time { year, month, _ := d.Date() thisMonth := time.Date(year, month, 1, 0, 0, 0, 0, time.Local) monthOneDay := thisMonth.AddDate(0, monthDiff, 0) return monthOneDay } // 当天时间戳 func GetDateTime(date string) (int64, int64) { //获取当前时区 loc, _ := time.LoadLocation("Local") //日期当天0点时间戳(拼接字符串) startDate := date + "_00:00:00" startTime, _ := time.ParseInLocation("2006-01-02_15:04:05", startDate, loc) //日期当天23时59分时间戳 endDate := date + "_23:59:59" end, _ := time.ParseInLocation("2006-01-02_15:04:05", endDate, loc) //返回当天0点和23点59分的时间戳 return startTime.Unix(), end.Unix() } // 获取某一天的0点时间 func GetZeroTime(d time.Time) time.Time { return time.Date(d.Year(), d.Month(), d.Day(), 0, 0, 0, 0, d.Location()) } // getYearMonthToDay 查询指定年份指定月份有多少天 // @params year int 指定年份 // @params month int 指定月份 func GetYearMonthToDay(year int, month int) int { // 有31天的月份 day31 := map[int]bool{ 1: true, 3: true, 5: true, 7: true, 8: true, 10: true, 12: true, } if day31[month] == true { return 31 } // 有30天的月份 day30 := map[int]bool{ 4: true, 6: true, 9: true, 11: true, } if day30[month] == true { return 30 } // 计算是平年还是闰年 if (year%4 == 0 && year%100 != 0) || year%400 == 0 { // 得出2月的天数 return 29 } // 得出2月的天数 return 28 }