period.go 10 KB

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