period.go 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. package common
  2. import (
  3. "errors"
  4. "github.com/go-kratos/kratos/v2/log"
  5. "strconv"
  6. "strings"
  7. "time"
  8. )
  9. // GetTimestamp 时间戳
  10. func GetTimestamp(n int) string {
  11. unixNano := strconv.FormatInt(time.Now().UnixNano(), 10)
  12. return unixNano[0:n]
  13. }
  14. // BeginOrEedPeriod 获取当前账期第一天日期和最后一天日期 参数e 当前账期 201912 参数status 1获取月初日期 2获取月末日期
  15. func BeginOrEedPeriod(e string, status int) string {
  16. nian := StrToInt(e[0 : len(e)-2])
  17. yue := StrToInt(e[4 : len(e)-0])
  18. if nian%100 == 0 {
  19. if nian%400 == 0 {
  20. return leapYear(nian, yue, status, 1)
  21. } else {
  22. return leapYear(nian, yue, status, 2)
  23. }
  24. } else {
  25. if nian%4 == 0 {
  26. return leapYear(nian, yue, status, 1)
  27. } else {
  28. return leapYear(nian, yue, status, 2)
  29. }
  30. }
  31. }
  32. func leapYear(nian int, yue int, status int, yearStatus int) string {
  33. if yue == 1 || yue == 3 || yue == 5 || yue == 7 || yue == 8 || yue == 10 || yue == 12 {
  34. yues := IntToStr(yue)
  35. if yue < 10 {
  36. yues = "0" + yues
  37. }
  38. if status == 1 {
  39. return IntToStr(nian) + "-" + yues + "-01"
  40. } else {
  41. return IntToStr(nian) + "-" + yues + "-31"
  42. }
  43. } else if yue == 4 || yue == 6 || yue == 9 || yue == 11 {
  44. yues := IntToStr(yue)
  45. if yue < 10 {
  46. yues = "0" + yues
  47. }
  48. if status == 1 {
  49. return IntToStr(nian) + "-" + yues + "-01"
  50. } else {
  51. return IntToStr(nian) + "-" + yues + "-30"
  52. }
  53. } else {
  54. yues := IntToStr(yue)
  55. if yue < 10 {
  56. yues = "0" + yues
  57. }
  58. if status == 1 {
  59. return IntToStr(nian) + "-" + yues + "-01"
  60. } else {
  61. if yearStatus == 1 {
  62. return IntToStr(nian) + "-" + yues + "-29"
  63. } else {
  64. return IntToStr(nian) + "-" + yues + "-28"
  65. }
  66. }
  67. }
  68. }
  69. // GetBeginAndEndTime 获取本月开始和结束的时间
  70. func GetBeginAndEndTime(period string) (string, string) {
  71. loc, _ := time.LoadLocation("Local")
  72. the_time, _ := time.ParseInLocation("200601", period, loc)
  73. year, month, _ := the_time.Date()
  74. thisMonth := time.Date(year, month, 1, 0, 0, 0, 0, time.Local)
  75. start := thisMonth.AddDate(0, 0, 0).Format("2006-01-02")
  76. end := thisMonth.AddDate(0, 1, -1).Format("2006-01-02")
  77. return start, end
  78. }
  79. // 根据本账期查询上个账期 例如 201812 -> 201811
  80. func LastPeriod(startAccountPeriod string) string {
  81. if len(startAccountPeriod) < 6 {
  82. return ""
  83. }
  84. qian4 := startAccountPeriod[0 : len(startAccountPeriod)-2]
  85. hou2 := startAccountPeriod[4 : len(startAccountPeriod)-0]
  86. newPeriod := ""
  87. if StrToInt(hou2) == 1 {
  88. newPeriod = IntToStr(StrToInt(qian4)-1) + "12"
  89. } else {
  90. newPeriod = IntToStr(StrToInt(startAccountPeriod) - 1)
  91. }
  92. return newPeriod
  93. }
  94. func GetCurrentDate() string {
  95. tm := time.Unix(time.Now().Unix(), 0)
  96. currentYear, currentMonth, _ := tm.Date()
  97. currentLocation := tm.Location()
  98. firstOfMonth := time.Date(currentYear, currentMonth, 10, 0, 0, 0, 0, currentLocation)
  99. period := firstOfMonth.AddDate(0, 0, 0).Format("200601") // 当前日期
  100. return period
  101. }
  102. // 获取当前账期
  103. func GetCurrentPeriod() string { //仅本期报税使用
  104. tm := time.Unix(time.Now().Unix(), 0)
  105. currentYear, currentMonth, _ := tm.Date()
  106. currentLocation := tm.Location()
  107. firstOfMonth := time.Date(currentYear, currentMonth, 10, 0, 0, 0, 0, currentLocation)
  108. period := firstOfMonth.AddDate(0, -1, 0).Format("200601") // 当前账期
  109. return period
  110. }
  111. func GetPeriod(f int) string {
  112. t := time.Now()
  113. year, month, _ := t.Date()
  114. thisMonthFirstDay := time.Date(year, month, 1, 1, 1, 1, 1, t.Location())
  115. a := thisMonthFirstDay.AddDate(0, f, 0).Format("200601")
  116. return a
  117. }
  118. func GetNextPeriod(period string) string {
  119. currentTime, _ := time.Parse("200601", period)
  120. next := currentTime.AddDate(0, 1, 0)
  121. return next.Format("200601")
  122. }
  123. // 传入账期区间返回中间的月份 fmtData 1 - 返回"200601" 2 -返回"2006-01"
  124. func PeriodBetweenStartEnd(start, end string, fmtData int) (err error, periods []string) {
  125. if start == "" || end == "" {
  126. return errors.New("时间错误"), periods
  127. }
  128. var startTime time.Time
  129. var endTime time.Time
  130. //格式化时间
  131. if strings.Contains(start, "-") && strings.Contains(end, "-") {
  132. timeTemplate := "2006-01"
  133. startTime, _ = time.ParseInLocation(timeTemplate, start, time.Local)
  134. endTime, _ = time.ParseInLocation(timeTemplate, end, time.Local)
  135. } else if !strings.Contains(start, "-") && !strings.Contains(end, "-") {
  136. timeTemplate := "200601"
  137. startTime, _ = time.ParseInLocation(timeTemplate, start, time.Local)
  138. endTime, _ = time.ParseInLocation(timeTemplate, end, time.Local)
  139. } else {
  140. return errors.New("传入参数格式错误"), periods
  141. }
  142. //结束日期包含本月
  143. endTime = endTime.AddDate(0, 1, 0)
  144. //判断结束时间在开始时间之后
  145. if !endTime.After(startTime) {
  146. return errors.New("结束时间在开始时间之前"), periods
  147. }
  148. for {
  149. //如果时间相等 则跳出
  150. if startTime.Equal(endTime) {
  151. break
  152. }
  153. if fmtData == 1 {
  154. periods = append(periods, startTime.Format("200601"))
  155. }
  156. if fmtData == 2 {
  157. periods = append(periods, startTime.Format("2006-01"))
  158. }
  159. startTime = startTime.AddDate(0, 1, 0)
  160. }
  161. return
  162. }
  163. // 传入period 返回 最后一天 201902-》
  164. func GetLastDay(period string) time.Time {
  165. //获取本地location
  166. toBeCharge := period //待转化为时间戳的字符串 注意 这里的小时和分钟还要秒必须写 因为是跟着模板走的 修改模板的话也可以不写
  167. timeLayout := "200601" //转化所需模板
  168. loc, _ := time.LoadLocation("Local") //重要:获取时区
  169. theTime, _ := time.ParseInLocation(timeLayout, toBeCharge, loc) //使用模板在对应时区转化为time.time类型
  170. sr := theTime.Unix() //转化为时间戳 类型是int64
  171. //zaplog.LoggerS.Info(theTime) //打印输出theTime 2015-01-01 15:15:00 +0800 CST
  172. //zaplog.LoggerS.Info(sr) //打印输出时间戳 1420041600
  173. tm := time.Unix(sr, 0)
  174. currentYear, currentMonth, _ := tm.Date()
  175. currentLocation := tm.Location()
  176. firstOfMonth := time.Date(currentYear, currentMonth, 1, 0, 0, 0, 0, currentLocation)
  177. lastOfMonth := firstOfMonth.AddDate(0, 1, -1)
  178. //return firstOfMonth.Format("2006-01-02"), lastOfMonth.Format("2006-01-02")
  179. log.Info(firstOfMonth, lastOfMonth)
  180. return lastOfMonth
  181. }
  182. // 传入period 返回 第一天 201902-》
  183. func GetFirstDay(period string) time.Time {
  184. //获取本地location
  185. toBeCharge := period //待转化为时间戳的字符串 注意 这里的小时和分钟还要秒必须写 因为是跟着模板走的 修改模板的话也可以不写
  186. timeLayout := "200601" //转化所需模板
  187. loc, _ := time.LoadLocation("Local") //重要:获取时区
  188. theTime, _ := time.ParseInLocation(timeLayout, toBeCharge, loc) //使用模板在对应时区转化为time.time类型
  189. sr := theTime.Unix() //转化为时间戳 类型是int64
  190. //zaplog.LoggerS.Info(theTime) //打印输出theTime 2015-01-01 15:15:00 +0800 CST
  191. //zaplog.LoggerS.Info(sr) //打印输出时间戳 1420041600
  192. tm := time.Unix(sr, 0)
  193. currentYear, currentMonth, _ := tm.Date()
  194. currentLocation := tm.Location()
  195. firstOfMonth := time.Date(currentYear, currentMonth, 1, 0, 0, 0, 0, currentLocation)
  196. lastOfMonth := firstOfMonth.AddDate(0, 1, -1)
  197. //return firstOfMonth.Format("2006-01-02"), lastOfMonth.Format("2006-01-02")
  198. log.Info(firstOfMonth, lastOfMonth)
  199. return firstOfMonth
  200. }