|
@@ -1,7 +1,10 @@
|
|
|
package common
|
|
|
|
|
|
import (
|
|
|
+ "errors"
|
|
|
+ "github.com/go-kratos/kratos/v2/log"
|
|
|
"strconv"
|
|
|
+ "strings"
|
|
|
"time"
|
|
|
)
|
|
|
|
|
@@ -115,3 +118,104 @@ func GetCurrentPeriod() string {
|
|
|
period := firstOfMonth.AddDate(0, -1, 0).Format("200601")
|
|
|
return period
|
|
|
}
|
|
|
+
|
|
|
+func GetPeriod(f int) string {
|
|
|
+ t := time.Now()
|
|
|
+ year, month, _ := t.Date()
|
|
|
+ thisMonthFirstDay := time.Date(year, month, 1, 1, 1, 1, 1, t.Location())
|
|
|
+ a := thisMonthFirstDay.AddDate(0, f, 0).Format("200601")
|
|
|
+ return a
|
|
|
+}
|
|
|
+
|
|
|
+func GetNextPeriod(period string) string {
|
|
|
+ currentTime, _ := time.Parse("200601", period)
|
|
|
+ next := currentTime.AddDate(0, 1, 0)
|
|
|
+ return next.Format("200601")
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+func PeriodBetweenStartEnd(start, end string, fmtData int) (err error, periods []string) {
|
|
|
+ if start == "" || end == "" {
|
|
|
+ return errors.New("时间错误"), periods
|
|
|
+ }
|
|
|
+ var startTime time.Time
|
|
|
+ var endTime time.Time
|
|
|
+
|
|
|
+
|
|
|
+ if strings.Contains(start, "-") && strings.Contains(end, "-") {
|
|
|
+ timeTemplate := "2006-01"
|
|
|
+ startTime, _ = time.ParseInLocation(timeTemplate, start, time.Local)
|
|
|
+ endTime, _ = time.ParseInLocation(timeTemplate, end, time.Local)
|
|
|
+ } else if !strings.Contains(start, "-") && !strings.Contains(end, "-") {
|
|
|
+ timeTemplate := "200601"
|
|
|
+ startTime, _ = time.ParseInLocation(timeTemplate, start, time.Local)
|
|
|
+ endTime, _ = time.ParseInLocation(timeTemplate, end, time.Local)
|
|
|
+ } else {
|
|
|
+ return errors.New("传入参数格式错误"), periods
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ endTime = endTime.AddDate(0, 1, 0)
|
|
|
+
|
|
|
+
|
|
|
+ if !endTime.After(startTime) {
|
|
|
+ return errors.New("结束时间在开始时间之前"), periods
|
|
|
+ }
|
|
|
+
|
|
|
+ for {
|
|
|
+
|
|
|
+ if startTime.Equal(endTime) {
|
|
|
+ break
|
|
|
+ }
|
|
|
+ if fmtData == 1 {
|
|
|
+ periods = append(periods, startTime.Format("200601"))
|
|
|
+ }
|
|
|
+ if fmtData == 2 {
|
|
|
+ periods = append(periods, startTime.Format("2006-01"))
|
|
|
+ }
|
|
|
+ startTime = startTime.AddDate(0, 1, 0)
|
|
|
+ }
|
|
|
+ return
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+func GetLastDay(period string) time.Time {
|
|
|
+
|
|
|
+ toBeCharge := period
|
|
|
+ timeLayout := "200601"
|
|
|
+ loc, _ := time.LoadLocation("Local")
|
|
|
+ theTime, _ := time.ParseInLocation(timeLayout, toBeCharge, loc)
|
|
|
+ sr := theTime.Unix()
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ tm := time.Unix(sr, 0)
|
|
|
+ currentYear, currentMonth, _ := tm.Date()
|
|
|
+ currentLocation := tm.Location()
|
|
|
+ firstOfMonth := time.Date(currentYear, currentMonth, 1, 0, 0, 0, 0, currentLocation)
|
|
|
+ lastOfMonth := firstOfMonth.AddDate(0, 1, -1)
|
|
|
+
|
|
|
+ log.Info(firstOfMonth, lastOfMonth)
|
|
|
+ return lastOfMonth
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+func GetFirstDay(period string) time.Time {
|
|
|
+
|
|
|
+ toBeCharge := period
|
|
|
+ timeLayout := "200601"
|
|
|
+ loc, _ := time.LoadLocation("Local")
|
|
|
+ theTime, _ := time.ParseInLocation(timeLayout, toBeCharge, loc)
|
|
|
+ sr := theTime.Unix()
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ tm := time.Unix(sr, 0)
|
|
|
+ currentYear, currentMonth, _ := tm.Date()
|
|
|
+ currentLocation := tm.Location()
|
|
|
+ firstOfMonth := time.Date(currentYear, currentMonth, 1, 0, 0, 0, 0, currentLocation)
|
|
|
+ lastOfMonth := firstOfMonth.AddDate(0, 1, -1)
|
|
|
+
|
|
|
+ log.Info(firstOfMonth, lastOfMonth)
|
|
|
+ return firstOfMonth
|
|
|
+}
|