period.go 8.3 KB

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