|
@@ -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")
|
|
|
+}
|
|
|
+
|
|
|
+// 传入账期区间返回中间的月份 fmtData 1 - 返回"200601" 2 -返回"2006-01"
|
|
|
+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
|
|
|
+}
|
|
|
+
|
|
|
+// 传入period 返回 最后一天 201902-》
|
|
|
+func GetLastDay(period string) time.Time {
|
|
|
+ //获取本地location
|
|
|
+ toBeCharge := period //待转化为时间戳的字符串 注意 这里的小时和分钟还要秒必须写 因为是跟着模板走的 修改模板的话也可以不写
|
|
|
+ timeLayout := "200601" //转化所需模板
|
|
|
+ loc, _ := time.LoadLocation("Local") //重要:获取时区
|
|
|
+ theTime, _ := time.ParseInLocation(timeLayout, toBeCharge, loc) //使用模板在对应时区转化为time.time类型
|
|
|
+ sr := theTime.Unix() //转化为时间戳 类型是int64
|
|
|
+ //zaplog.LoggerS.Info(theTime) //打印输出theTime 2015-01-01 15:15:00 +0800 CST
|
|
|
+ //zaplog.LoggerS.Info(sr) //打印输出时间戳 1420041600
|
|
|
+
|
|
|
+ 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)
|
|
|
+ //return firstOfMonth.Format("2006-01-02"), lastOfMonth.Format("2006-01-02")
|
|
|
+ log.Info(firstOfMonth, lastOfMonth)
|
|
|
+ return lastOfMonth
|
|
|
+}
|
|
|
+
|
|
|
+// 传入period 返回 第一天 201902-》
|
|
|
+func GetFirstDay(period string) time.Time {
|
|
|
+ //获取本地location
|
|
|
+ toBeCharge := period //待转化为时间戳的字符串 注意 这里的小时和分钟还要秒必须写 因为是跟着模板走的 修改模板的话也可以不写
|
|
|
+ timeLayout := "200601" //转化所需模板
|
|
|
+ loc, _ := time.LoadLocation("Local") //重要:获取时区
|
|
|
+ theTime, _ := time.ParseInLocation(timeLayout, toBeCharge, loc) //使用模板在对应时区转化为time.time类型
|
|
|
+ sr := theTime.Unix() //转化为时间戳 类型是int64
|
|
|
+ //zaplog.LoggerS.Info(theTime) //打印输出theTime 2015-01-01 15:15:00 +0800 CST
|
|
|
+ //zaplog.LoggerS.Info(sr) //打印输出时间戳 1420041600
|
|
|
+
|
|
|
+ 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)
|
|
|
+ //return firstOfMonth.Format("2006-01-02"), lastOfMonth.Format("2006-01-02")
|
|
|
+ log.Info(firstOfMonth, lastOfMonth)
|
|
|
+ return firstOfMonth
|
|
|
+}
|