作者 yangfu

按月统计修改

... ... @@ -706,21 +706,33 @@ func histogramStatisticsByMonth(start time.Time) ([]queryItem, []string) {
var monthEnd = time.Date(year, month+1, 1, 0, 0, 0, 0, time.Local).Add(-time.Second)
var xAxisData []string
for {
if beginTime.AddDate(0, 0, increaseDay).After(monthEnd) {
currentIncreaseDay := increaseDay
leftDay := monthEnd.Day() - endTime.Day()
if leftDay == 0 {
break
}
// 不足5天的情况,比如二月 28、29天
if leftDay > 0 && leftDay < increaseDay {
currentIncreaseDay = leftDay
}
if beginTime.AddDate(0, 0, currentIncreaseDay).After(monthEnd) {
endTime = monthEnd
break
} else {
endTime = beginTime.AddDate(0, 0, increaseDay).Add(-time.Second)
endTime = beginTime.AddDate(0, 0, currentIncreaseDay).Add(-time.Second)
}
item := queryItem{
BeginTime: beginTime,
EndTime: beginTime.AddDate(0, 0, increaseDay).Add(-time.Second),
EndTime: beginTime.AddDate(0, 0, currentIncreaseDay).Add(-time.Second),
}
// 30号接近月末时,如果有31号 就取月末值
left := monthEnd.Day() - item.EndTime.Day()
if left <= 1 {
item.EndTime = monthEnd
endTime = monthEnd
}
xAxisData = append(xAxisData, item.EndTime.Format("01-02"))
beginTime = endTime
if endTime == monthEnd {
break
}
ret = append(ret, item)
}
return ret, xAxisData
... ...
package domain_service
import (
"fmt"
"log"
"testing"
"time"
)
func Test_histogramStatisticsByMonth(t *testing.T) {
input := time.Date(2021, 1, 1, 0, 0, 0, 0, time.Local)
for i := 0; i < 12; i++ {
y, x := histogramStatisticsByMonth(input)
log.Println(fmt.Sprintf("月份:%d", input.Month()), x)
log.Println(fmt.Sprintf("月份:%d", input.Month()), y)
input = input.AddDate(0, 1, 0)
}
}
... ...