按天、周、月、年平分时间 Go 实现


原文地址声明:https://blog.csdn.net/qq_23179075/article/details/99415124

golang 实现按天、周、月、年平分时间

type TimeInterval struct {
	StartTime string
	EntTime   string
}

/*
 * @Author: 郑亮
 * @Description: 按天,周,月,年平分时间
 * @Date: 2019/8/13 11:09
 * @param: divideType: day:天 ; week:周;month:月;year:年
 * @param: divideCount: 分割的份数
 * @param: layout: 因为按月、年平分时间时是传递的具体时间,可以自定义时间格式化模板
 * @param: curreTime: 需要分割的时间,可变参数,当按月、年分割时必填
 * @return: err ,tis
 */
func TimeDivide(divideType string, divideCount int, layout string, curreTime ...time.Time) (err error, tis []TimeInterval) {
	const dayMillisecond = 24 * 60 * 60 * 1000

	start, _ := time.ParseInLocation("15:04", "00:00", time.Local)
	totalMillisecond := 0

	//按天
	if divideType == "day" {
		totalMillisecond = dayMillisecond
		if layout == "" {
			layout = "15:04"
		}
	}

	//周
	if divideType == "week" {
		totalMillisecond = 7 * dayMillisecond
		if layout == "" {
			layout = "星期2-15:04"
		}
	}

	//月
	if divideType == "month" {
		if len(curreTime) == 0 {
			return errors.New("当按月分割时间段时,请传入具体要分割的时间"), nil
		}
		t := curreTime[0]
		start = time.Date(t.Year(), t.Month(), 1, 0, 0, 0, 0, time.Local)
		totalMillisecond = MonthDataCount(t.Year(), int(t.Month())) * dayMillisecond
		if layout == "" {
			layout = "02号-15:04:05"
		}
	}
	//年
	if divideType == "year" {
		if len(curreTime) == 0 {
			return errors.New("当按年分割时间段时,请传入具体要分割的时间"), nil
		}
		t := curreTime[0]
		start = time.Date(t.Year(), 1, 1, 0, 0, 0, 0, time.Local)
		totalMillisecond = YearDataCount(t.Year()) * dayMillisecond
		if layout == "" {
			layout = "01月02日-15:04:05"
		}
	}

	for i := 0; i < divideCount; i++ {
		startDivider := totalMillisecond / divideCount * i
		endDivider := totalMillisecond / divideCount * (i + 1)
		tis = append(tis, TimeInterval{
			start.Add(time.Duration(startDivider) * time.Millisecond).Format(layout),
			start.Add(time.Duration(endDivider) * time.Millisecond).Add(-1 * time.Millisecond).Format(layout),
		})
	}
	return
}

/**
 * 根据年月获取,月份天数
 */
func MonthDataCount(year int, month int) (days int) {
	if month != 2 {
		if month == 4 || month == 6 || month == 9 || month == 11 {
			days = 30
		} else {
			days = 31
		}
	} else {
		if ((year%4) == 0 && (year%100) != 0) || (year%400) == 0 {
			days = 29
		} else {
			days = 28
		}
	}
	return
}

/**
 * 根据获取年天数
 */
func YearDataCount(year int) (days int) {
	if ((year%4) == 0 && (year%100) != 0) || (year%400) == 0 {
		days = 366
	} else {
		days = 365
	}
	return
}

测试运行

func main() {
	fmt.Println("=====================按day平分====================")
	_, day := TimeDivide("day", 3, "")
	fmt.Println(day)
	fmt.Println("=====================按week平分===================")
	_, week := TimeDivide("week", 3, "")
	fmt.Println(week)
	fmt.Println("=====================按month平分===================")
	_, month := TimeDivide("month", 3, "", time.Now())
	fmt.Println(month)
	fmt.Println("=====================按year平分===================")
	_, year := TimeDivide("year", 3, "", time.Now())
	fmt.Println(year)
}
=====================按day平分====================
[{00:00 07:59} {08:00 15:59} {16:00 23:59}]
=====================按week平分===================
[{星期1-00:00 星期3-07:59} {星期3-08:00 星期5-15:59} {星期5-16:00 星期7-23:59}]
=====================按month平分===================
[{01号-00:00:00 11号-07:59:59} {11号-08:00:00 21号-15:59:59} {21号-16:00:00 31号-23:59:59}]
=====================按year平分===================
[{01月01日-00:00:00 05月02日-15:59:59} {05月02日-16:00:00 09月01日-07:59:59} {09月01日-08:00:00 12月31日-23:59:59}]