按天、周、月、年平分时间 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平分===================
[{0101-00:00:00 0502-15:59:59} {0502-16:00:00 0901-07:59:59} {0901-08:00:00 1231-23:59:59}]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值