123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265 |
- package common
- import (
- "errors"
- "git.listensoft.net/tool/jspkit/logger"
- "math"
- "strconv"
- "strings"
- "time"
- )
- func GetTimestamp(n int) string {
- unixNano := strconv.FormatInt(time.Now().UnixNano(), 10)
- return unixNano[0:n]
- }
- 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"
- }
- }
- }
- }
- 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
- }
- 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")
- }
- 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)
-
- logger.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)
-
- 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
- }
- func LastJiDu(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 || StrToInt(hou2) == 2 || StrToInt(hou2) == 3 {
- newPeriod = IntToStr(StrToInt(qian4)-1) + "12"
- } else if StrToInt(hou2) == 4 || StrToInt(hou2) == 5 || StrToInt(hou2) == 6 {
- newPeriod = IntToStr(StrToInt(qian4)-0) + "03"
- } else if StrToInt(hou2) == 7 || StrToInt(hou2) == 8 || StrToInt(hou2) == 9 {
- newPeriod = IntToStr(StrToInt(qian4)-0) + "06"
- } else if StrToInt(hou2) == 10 || StrToInt(hou2) == 11 || StrToInt(hou2) == 12 {
- newPeriod = IntToStr(StrToInt(qian4)-0) + "09"
- }
- return newPeriod
- }
|