package common import ( "errors" "git.listensoft.net/tool/jspkit/logger" "math" "strconv" "strings" "time" ) // GetTimestamp 时间戳 func GetTimestamp(n int) string { unixNano := strconv.FormatInt(time.Now().UnixNano(), 10) return unixNano[0:n] } // BeginOrEedPeriod 获取当前账期第一天日期和最后一天日期 参数e 当前账期 201912 参数status 1获取月初日期 2获取月末日期 func BeginOrEedPeriod(e string, status int) string { nian := StrToInt(e[0 : len(e)-2]) yue := StrToInt(e[4 : len(e)-0]) if nian%100 == 0 { if nian%400 == 0 { return leapYear(nian, yue, status, 1) } else { return leapYear(nian, yue, status, 2) } } else { if nian%4 == 0 { return leapYear(nian, yue, status, 1) } else { return leapYear(nian, yue, status, 2) } } } func leapYear(nian int, yue int, status int, yearStatus int) string { if yue == 1 || yue == 3 || yue == 5 || yue == 7 || yue == 8 || yue == 10 || yue == 12 { yues := IntToStr(yue) if yue < 10 { yues = "0" + yues } if status == 1 { return IntToStr(nian) + "-" + yues + "-01" } else { return IntToStr(nian) + "-" + yues + "-31" } } else if yue == 4 || yue == 6 || yue == 9 || yue == 11 { yues := IntToStr(yue) if yue < 10 { yues = "0" + yues } if status == 1 { return IntToStr(nian) + "-" + yues + "-01" } else { return IntToStr(nian) + "-" + yues + "-30" } } else { yues := IntToStr(yue) if yue < 10 { yues = "0" + yues } if status == 1 { return IntToStr(nian) + "-" + yues + "-01" } else { if yearStatus == 1 { return IntToStr(nian) + "-" + yues + "-29" } else { return IntToStr(nian) + "-" + yues + "-28" } } } } // GetBeginAndEndTime 获取本月开始和结束的时间 func GetBeginAndEndTime(period string) (string, string) { loc, _ := time.LoadLocation("Local") the_time, _ := time.ParseInLocation("200601", period, loc) year, month, _ := the_time.Date() thisMonth := time.Date(year, month, 1, 0, 0, 0, 0, time.Local) start := thisMonth.AddDate(0, 0, 0).Format("2006-01-02") end := thisMonth.AddDate(0, 1, -1).Format("2006-01-02") return start, end } // 根据本账期查询上个账期 例如 201812 -> 201811 func LastPeriod(startAccountPeriod string) string { if len(startAccountPeriod) < 6 { return "" } qian4 := startAccountPeriod[0 : len(startAccountPeriod)-2] hou2 := startAccountPeriod[4 : len(startAccountPeriod)-0] newPeriod := "" if StrToInt(hou2) == 1 { newPeriod = IntToStr(StrToInt(qian4)-1) + "12" } else { newPeriod = IntToStr(StrToInt(startAccountPeriod) - 1) } return newPeriod } func GetCurrentDate() string { tm := time.Unix(time.Now().Unix(), 0) currentYear, currentMonth, _ := tm.Date() currentLocation := tm.Location() firstOfMonth := time.Date(currentYear, currentMonth, 10, 0, 0, 0, 0, currentLocation) period := firstOfMonth.AddDate(0, 0, 0).Format("200601") // 当前日期 return period } // 获取当前账期 func GetCurrentPeriod() string { //仅本期报税使用 tm := time.Unix(time.Now().Unix(), 0) currentYear, currentMonth, _ := tm.Date() currentLocation := tm.Location() firstOfMonth := time.Date(currentYear, currentMonth, 10, 0, 0, 0, 0, currentLocation) 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") logger.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") logger.Info(firstOfMonth, lastOfMonth) return firstOfMonth } func GetPrevQuarterStartTimeAndEndTime(period string) (string, string) { quarterStart := "" quarterEnd := "" quarter := math.Ceil(StrToFloat(period[4:6])/3) - 1 if quarter == 1 { //第一季度 quarterStart = period[0:4] + "-01-01" quarterEnd = period[0:4] + "-03-31" } if quarter == 2 { //第二季度 quarterStart = period[0:4] + "-04-01" quarterEnd = period[0:4] + "-06-30" } if quarter == 3 { //第三季度 quarterStart = period[0:4] + "-07-01" quarterEnd = period[0:4] + "-09-30" } if quarter == 0 { //第四季度 quarterStart = IntToStr(StrToInt(period[0:4])-1) + "-10-01" quarterEnd = IntToStr(StrToInt(period[0:4])-1) + "-12-31" } return quarterStart, quarterEnd }