/* @Time : 2019-07-08 11:12 @Author : zr */ package utils import ( "math" "math/rand" "time" "video_course/errors" ) func TimeFormatter() string { return "2006-01-02 15:04:05" } func TimeFormatterDate() string { return "2006-01-02" } func TimeStdQueryBegin(stdTimeStr string) (newTime time.Time, err error) { if stdTimeStr == "" { return time.Parse(TimeFormatter(), "2000-01-01 00:00:00") } newTime, err = time.Parse(TimeFormatter(), stdTimeStr) if err != nil { return newTime, err } newTimeStr := newTime.Format("2006-01-02") + " 00:00:00" return time.Parse(TimeFormatter(), newTimeStr) } func TimeStdQueryEnd(stdTimeStr string) (newTime time.Time, err error) { if stdTimeStr == "" { newTime = time.Now() } else { newTime, err = time.Parse(TimeFormatter(), stdTimeStr) if err != nil { return newTime, err } } newTimeStr := newTime.Format("2006-01-02") + " 23:59:59" return time.Parse(TimeFormatter(), newTimeStr) } func TimeDayBegin(day time.Time) time.Time { dayBeginStr := day.Format("2006-01-02") + " 00:00:00" dayBegin, _ := time.Parse(TimeFormatter(), dayBeginStr) return dayBegin } func TimeTodayBegin() time.Time { timeNow := time.Now() dayBeginStr := timeNow.Format("2006-01-02") + " 00:00:00" dayBegin, _ := time.Parse(TimeFormatter(), dayBeginStr) return dayBegin } func TimeDayEnd(day time.Time) time.Time { dayStr := day.Format("2006-01-02") + " 23:59:59" day, _ = time.Parse(TimeFormatter(), dayStr) return day } func RandomInt(n int) int { min := int(math.Pow(10, float64(n-1))) max := min*9 - 1 rand1 := rand.New(rand.NewSource(time.Now().UnixNano())) code := rand1.Intn(max) + min return code } func GetAgeByBirthday(b time.Time) (age int) { //b, err := time.Parse(TimeFormatter(), birthday) //if err != nil { // return 0, err //} year := b.Year() month := int(b.Month()) day := b.Day() nowYear := time.Now().Year() nowMonth := int(time.Now().Month()) nowDay := time.Now().Day() if nowYear < year { panic(errors.ErrBirthday) } age = nowYear - year if month > nowMonth { // 当生日月份>当前月份的时候,年龄-1 age = age - 1 } if month == nowMonth && day > nowDay { age = age - 1 // 当生日月份与当前月份相等的时候,如果生日日期大于当天日期,则生日未到,年龄-1 } return } func GetStaticHrByAge(age int) (staticHr int) { switch age { case 2: staticHr = 80 case 3: staticHr = 80 case 4: staticHr = 80 case 5: staticHr = 75 case 6: staticHr = 75 case 7: staticHr = 70 case 8: staticHr = 70 case 9: staticHr = 70 default: if age >= 10 { staticHr = 60 } else { panic(errors.ErrStaticHr) } } return }