| | |
| | | package timex |
| | | |
| | | import ( |
| | | "silkserver/constvar" |
| | | "time" |
| | | ) |
| | | |
| | |
| | | return time.Now().Format(timeLayout) |
| | | } |
| | | |
| | | func NextDateTimestamp(base time.Time, unit constvar.InspectCycleUnit, cycle int) time.Time { |
| | | var t time.Time |
| | | switch unit { |
| | | case constvar.InspectCycleUnitWeek: |
| | | t = base.AddDate(0, 0, cycle*7) |
| | | case constvar.InspectCycleUnitMonth: |
| | | t = base.AddDate(0, cycle, 0) |
| | | case constvar.InspectCycleUnitDay: |
| | | t = base.AddDate(0, 0, cycle) |
| | | func GetDate(year int, month time.Month) int { |
| | | day := 0 |
| | | if month == time.February { |
| | | if (year%4 == 0 && year%100 != 0) || year%400 == 0 { |
| | | day = 29 |
| | | } else { |
| | | day = 28 |
| | | } |
| | | } else { |
| | | if month == time.January || month == time.March || month == time.May || month == time.July || |
| | | month == time.August || month == time.October || month == time.December { |
| | | day = 31 |
| | | } else { |
| | | day = 30 |
| | | } |
| | | } |
| | | return time.Date(t.Year(), t.Month(), t.Day(), 0, 0, 0, 0, t.Location()) |
| | | return day |
| | | } |
| | | |
| | | // Cycle2Seconds 周期换算成秒数 |
| | | func Cycle2Seconds(unit constvar.InspectCycleUnit, cycle int) int { |
| | | var s int |
| | | switch unit { |
| | | case constvar.InspectCycleUnitWeek: |
| | | s = cycle * 86400 * 7 |
| | | case constvar.InspectCycleUnitMonth: |
| | | s = cycle * 86400 * 30 |
| | | case constvar.InspectCycleUnitDay: |
| | | s = cycle * 86400 |
| | | // 获取一个月内的所有星期 |
| | | func GetWeeksOfMonth(year int, month time.Month) [][]time.Time { |
| | | firstDay := time.Date(year, month, 1, 0, 0, 0, 0, time.Local) |
| | | lastDay := firstDay.AddDate(0, 1, -1) // 下一个月的第一天减一,得到本月的最后一天 |
| | | |
| | | weeks := make([][]time.Time, 0) |
| | | currentWeek := make([]time.Time, 0) |
| | | current := firstDay |
| | | |
| | | for !current.After(lastDay) { |
| | | currentWeek = append(currentWeek, current) |
| | | current = current.AddDate(0, 0, 1) // 增加一天 |
| | | |
| | | // 如果当前周已经满了7天,或者已经到了月的最后一天,则保存这一周 |
| | | if len(currentWeek) == 7 || current.After(lastDay) { |
| | | weeks = append(weeks, currentWeek) |
| | | currentWeek = make([]time.Time, 0) // 重置当前周 |
| | | } |
| | | } |
| | | return s |
| | | |
| | | return weeks |
| | | } |