period.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  1. package common
  2. import (
  3. "errors"
  4. "fmt"
  5. "math"
  6. "strconv"
  7. "strings"
  8. "time"
  9. "git.listensoft.net/tool/jspkit/logger"
  10. "github.com/go-rod/rod"
  11. )
  12. // GetTimestamp 时间戳
  13. func GetTimestamp(n int) string {
  14. unixNano := strconv.FormatInt(time.Now().UnixNano(), 10)
  15. return unixNano[0:n]
  16. }
  17. // BeginOrEedPeriod 获取当前账期第一天日期和最后一天日期 参数e 当前账期 201912 参数status 1获取月初日期 2获取月末日期
  18. func BeginOrEedPeriod(e string, status int) string {
  19. nian := StrToInt(e[0 : len(e)-2])
  20. yue := StrToInt(e[4 : len(e)-0])
  21. if nian%100 == 0 {
  22. if nian%400 == 0 {
  23. return leapYear(nian, yue, status, 1)
  24. } else {
  25. return leapYear(nian, yue, status, 2)
  26. }
  27. } else {
  28. if nian%4 == 0 {
  29. return leapYear(nian, yue, status, 1)
  30. } else {
  31. return leapYear(nian, yue, status, 2)
  32. }
  33. }
  34. }
  35. func leapYear(nian int, yue int, status int, yearStatus int) string {
  36. if yue == 1 || yue == 3 || yue == 5 || yue == 7 || yue == 8 || yue == 10 || yue == 12 {
  37. yues := IntToStr(yue)
  38. if yue < 10 {
  39. yues = "0" + yues
  40. }
  41. if status == 1 {
  42. return IntToStr(nian) + "-" + yues + "-01"
  43. } else {
  44. return IntToStr(nian) + "-" + yues + "-31"
  45. }
  46. } else if yue == 4 || yue == 6 || yue == 9 || yue == 11 {
  47. yues := IntToStr(yue)
  48. if yue < 10 {
  49. yues = "0" + yues
  50. }
  51. if status == 1 {
  52. return IntToStr(nian) + "-" + yues + "-01"
  53. } else {
  54. return IntToStr(nian) + "-" + yues + "-30"
  55. }
  56. } else {
  57. yues := IntToStr(yue)
  58. if yue < 10 {
  59. yues = "0" + yues
  60. }
  61. if status == 1 {
  62. return IntToStr(nian) + "-" + yues + "-01"
  63. } else {
  64. if yearStatus == 1 {
  65. return IntToStr(nian) + "-" + yues + "-29"
  66. } else {
  67. return IntToStr(nian) + "-" + yues + "-28"
  68. }
  69. }
  70. }
  71. }
  72. // GetBeginAndEndTime 获取本月开始和结束的时间
  73. func GetBeginAndEndTime(period string) (string, string) {
  74. loc, _ := time.LoadLocation("Local")
  75. the_time, _ := time.ParseInLocation("200601", period, loc)
  76. year, month, _ := the_time.Date()
  77. thisMonth := time.Date(year, month, 1, 0, 0, 0, 0, time.Local)
  78. start := thisMonth.AddDate(0, 0, 0).Format("2006-01-02")
  79. end := thisMonth.AddDate(0, 1, -1).Format("2006-01-02")
  80. return start, end
  81. }
  82. // 根据本账期查询上个账期 例如 201812 -> 201811
  83. func LastPeriod(startAccountPeriod string) string {
  84. if len(startAccountPeriod) < 6 {
  85. return ""
  86. }
  87. qian4 := startAccountPeriod[0 : len(startAccountPeriod)-2]
  88. hou2 := startAccountPeriod[4 : len(startAccountPeriod)-0]
  89. newPeriod := ""
  90. if StrToInt(hou2) == 1 {
  91. newPeriod = IntToStr(StrToInt(qian4)-1) + "12"
  92. } else {
  93. newPeriod = IntToStr(StrToInt(startAccountPeriod) - 1)
  94. }
  95. return newPeriod
  96. }
  97. func GetCurrentDate() string {
  98. tm := time.Unix(time.Now().Unix(), 0)
  99. currentYear, currentMonth, _ := tm.Date()
  100. currentLocation := tm.Location()
  101. firstOfMonth := time.Date(currentYear, currentMonth, 10, 0, 0, 0, 0, currentLocation)
  102. period := firstOfMonth.AddDate(0, 0, 0).Format("200601") // 当前日期
  103. return period
  104. }
  105. // 获取当前账期
  106. func GetCurrentPeriod() string { //仅本期报税使用
  107. tm := time.Unix(time.Now().Unix(), 0)
  108. currentYear, currentMonth, _ := tm.Date()
  109. currentLocation := tm.Location()
  110. firstOfMonth := time.Date(currentYear, currentMonth, 10, 0, 0, 0, 0, currentLocation)
  111. period := firstOfMonth.AddDate(0, -1, 0).Format("200601") // 当前账期
  112. return period
  113. }
  114. func GetPeriod(f int) string {
  115. t := time.Now()
  116. year, month, _ := t.Date()
  117. thisMonthFirstDay := time.Date(year, month, 1, 1, 1, 1, 1, t.Location())
  118. a := thisMonthFirstDay.AddDate(0, f, 0).Format("200601")
  119. return a
  120. }
  121. func GetNextPeriod(period string) string {
  122. currentTime, _ := time.Parse("200601", period)
  123. next := currentTime.AddDate(0, 1, 0)
  124. return next.Format("200601")
  125. }
  126. // 传入账期区间返回中间的月份 fmtData 1 - 返回"200601" 2 -返回"2006-01"
  127. func PeriodBetweenStartEnd(start, end string, fmtData int) (err error, periods []string) {
  128. if start == "" || end == "" {
  129. return errors.New("时间错误"), periods
  130. }
  131. var startTime time.Time
  132. var endTime time.Time
  133. //格式化时间
  134. if strings.Contains(start, "-") && strings.Contains(end, "-") {
  135. timeTemplate := "2006-01"
  136. startTime, _ = time.ParseInLocation(timeTemplate, start, time.Local)
  137. endTime, _ = time.ParseInLocation(timeTemplate, end, time.Local)
  138. } else if !strings.Contains(start, "-") && !strings.Contains(end, "-") {
  139. timeTemplate := "200601"
  140. startTime, _ = time.ParseInLocation(timeTemplate, start, time.Local)
  141. endTime, _ = time.ParseInLocation(timeTemplate, end, time.Local)
  142. } else {
  143. return errors.New("传入参数格式错误"), periods
  144. }
  145. //结束日期包含本月
  146. endTime = endTime.AddDate(0, 1, 0)
  147. //判断结束时间在开始时间之后
  148. if !endTime.After(startTime) {
  149. return errors.New("结束时间在开始时间之前"), periods
  150. }
  151. for {
  152. //如果时间相等 则跳出
  153. if startTime.Equal(endTime) {
  154. break
  155. }
  156. if fmtData == 1 {
  157. periods = append(periods, startTime.Format("200601"))
  158. }
  159. if fmtData == 2 {
  160. periods = append(periods, startTime.Format("2006-01"))
  161. }
  162. startTime = startTime.AddDate(0, 1, 0)
  163. }
  164. return
  165. }
  166. // 传入period 返回 最后一天 201902-》
  167. func GetLastDay(period string) time.Time {
  168. //获取本地location
  169. toBeCharge := period //待转化为时间戳的字符串 注意 这里的小时和分钟还要秒必须写 因为是跟着模板走的 修改模板的话也可以不写
  170. timeLayout := "200601" //转化所需模板
  171. loc, _ := time.LoadLocation("Local") //重要:获取时区
  172. theTime, _ := time.ParseInLocation(timeLayout, toBeCharge, loc) //使用模板在对应时区转化为time.time类型
  173. sr := theTime.Unix() //转化为时间戳 类型是int64
  174. //logger.Info(theTime) //打印输出theTime 2015-01-01 15:15:00 +0800 CST
  175. //logger.Info(sr) //打印输出时间戳 1420041600
  176. tm := time.Unix(sr, 0)
  177. currentYear, currentMonth, _ := tm.Date()
  178. currentLocation := tm.Location()
  179. firstOfMonth := time.Date(currentYear, currentMonth, 1, 0, 0, 0, 0, currentLocation)
  180. lastOfMonth := firstOfMonth.AddDate(0, 1, -1)
  181. //return firstOfMonth.Format("2006-01-02"), lastOfMonth.Format("2006-01-02")
  182. logger.Info(firstOfMonth, lastOfMonth)
  183. return lastOfMonth
  184. }
  185. // 传入period 返回 第一天 201902-》
  186. func GetFirstDay(period string) time.Time {
  187. //获取本地location
  188. toBeCharge := period //待转化为时间戳的字符串 注意 这里的小时和分钟还要秒必须写 因为是跟着模板走的 修改模板的话也可以不写
  189. timeLayout := "200601" //转化所需模板
  190. loc, _ := time.LoadLocation("Local") //重要:获取时区
  191. theTime, _ := time.ParseInLocation(timeLayout, toBeCharge, loc) //使用模板在对应时区转化为time.time类型
  192. sr := theTime.Unix() //转化为时间戳 类型是int64
  193. //logger.Info(theTime) //打印输出theTime 2015-01-01 15:15:00 +0800 CST
  194. //logger.Info(sr) //打印输出时间戳 1420041600
  195. tm := time.Unix(sr, 0)
  196. currentYear, currentMonth, _ := tm.Date()
  197. currentLocation := tm.Location()
  198. firstOfMonth := time.Date(currentYear, currentMonth, 1, 0, 0, 0, 0, currentLocation)
  199. lastOfMonth := firstOfMonth.AddDate(0, 1, -1)
  200. //return firstOfMonth.Format("2006-01-02"), lastOfMonth.Format("2006-01-02")
  201. logger.Info(firstOfMonth, lastOfMonth)
  202. return firstOfMonth
  203. }
  204. func GetPrevQuarterStartTimeAndEndTime(period string) (string, string) {
  205. quarterStart := ""
  206. quarterEnd := ""
  207. quarter := math.Ceil(StrToFloat(period[4:6])/3) - 1
  208. if quarter == 1 { //第一季度
  209. quarterStart = period[0:4] + "-01-01"
  210. quarterEnd = period[0:4] + "-03-31"
  211. }
  212. if quarter == 2 { //第二季度
  213. quarterStart = period[0:4] + "-04-01"
  214. quarterEnd = period[0:4] + "-06-30"
  215. }
  216. if quarter == 3 { //第三季度
  217. quarterStart = period[0:4] + "-07-01"
  218. quarterEnd = period[0:4] + "-09-30"
  219. }
  220. if quarter == 0 { //第四季度
  221. quarterStart = IntToStr(StrToInt(period[0:4])-1) + "-10-01"
  222. quarterEnd = IntToStr(StrToInt(period[0:4])-1) + "-12-31"
  223. }
  224. return quarterStart, quarterEnd
  225. }
  226. // 根据本账期查询上季度末账期 例如 201812 -> 201809
  227. func LastJiDu(startAccountPeriod string) string {
  228. if len(startAccountPeriod) < 6 {
  229. return ""
  230. }
  231. qian4 := startAccountPeriod[0 : len(startAccountPeriod)-2]
  232. hou2 := startAccountPeriod[4 : len(startAccountPeriod)-0]
  233. newPeriod := ""
  234. if StrToInt(hou2) == 1 || StrToInt(hou2) == 2 || StrToInt(hou2) == 3 {
  235. newPeriod = IntToStr(StrToInt(qian4)-1) + "12"
  236. } else if StrToInt(hou2) == 4 || StrToInt(hou2) == 5 || StrToInt(hou2) == 6 {
  237. newPeriod = IntToStr(StrToInt(qian4)-0) + "03"
  238. } else if StrToInt(hou2) == 7 || StrToInt(hou2) == 8 || StrToInt(hou2) == 9 {
  239. newPeriod = IntToStr(StrToInt(qian4)-0) + "06"
  240. } else if StrToInt(hou2) == 10 || StrToInt(hou2) == 11 || StrToInt(hou2) == 12 {
  241. newPeriod = IntToStr(StrToInt(qian4)-0) + "09"
  242. }
  243. return newPeriod
  244. }
  245. // 根据本账期查询上个账期 例如 201812 -> 2018-11
  246. func LastPeriod1(startAccountPeriod string) string {
  247. if len(startAccountPeriod) < 6 {
  248. return ""
  249. }
  250. qian4 := startAccountPeriod[0 : len(startAccountPeriod)-2]
  251. hou2 := startAccountPeriod[4 : len(startAccountPeriod)-0]
  252. newPeriod := ""
  253. if StrToInt(hou2) == 1 {
  254. newPeriod = IntToStr(StrToInt(qian4)-1) + "12"
  255. } else {
  256. newPeriod = IntToStr(StrToInt(startAccountPeriod) - 1)
  257. }
  258. return newPeriod[:4] + "-" + newPeriod[4:]
  259. }
  260. // GetLastBeginAndEndTime 获取上个账期的起止时间
  261. func GetLastBeginAndEndTime(period string) (string, string) {
  262. lastP := getCurrentPeriod(period)
  263. return GetBeginAndEndTime(lastP)
  264. }
  265. // 获取上月
  266. func getCurrentPeriod(period string) string {
  267. parse, _ := time.Parse(`200601`, period)
  268. period = parse.AddDate(0, -1, 0).Format("200601") // 获取上月
  269. return period
  270. }
  271. func GetFirstAndLastDayOfPeriod(period string) (string, string) {
  272. t, _ := time.Parse("200601", period)
  273. firstDay := t.AddDate(0, 0, 0)
  274. lastDay := t.AddDate(0, 1, -1)
  275. return firstDay.Format("2006-01-02"), lastDay.Format("2006-01-02")
  276. }
  277. // RFC3339 = "2006-01-02T15:04:05Z07:00"
  278. const TimeFormat = "2006-01-02 15:04:05"
  279. // DateFormatter 将YYYYMMDD格式改为YYYY-MM-DD格式
  280. func DateFormatter(date string) string {
  281. if len(date) != 8 {
  282. return "" // 返回空字符串表示输入格式不正确
  283. }
  284. return fmt.Sprintf("%s-%s-%s", date[:4], date[4:6], date[6:])
  285. }
  286. // 获取某一天的0点时间
  287. func GetNextDateTime(d time.Time) time.Time {
  288. return time.Date(d.Year(), d.Month(), d.Day()+1, 0, 0, 0, 0, d.Location())
  289. }
  290. // 获取去年的第一天和最后一天
  291. func GetLastYearFirstTimeAndEndTime() (string, string) {
  292. now := time.Now()
  293. logger.Info(now)
  294. currentYear, _, _ := now.Date()
  295. currentLocation := now.Location()
  296. logger.Info(currentLocation)
  297. firstOfYear := time.Date(currentYear-1, time.January, 1, 0, 0, 0, 0, currentLocation)
  298. lastOfYear := firstOfYear.AddDate(1, 0, -1)
  299. return firstOfYear.Format("2006-01-02"), lastOfYear.Format("2006-01-02")
  300. }
  301. // 获取今年的第一天和最后一天
  302. func GetCurrentYearFirstTimeAndEndTime() (string, string) {
  303. now := time.Now()
  304. logger.Info(now)
  305. currentYear, _, _ := now.Date()
  306. currentLocation := now.Location()
  307. logger.Info(currentLocation)
  308. firstOfYear := time.Date(currentYear, time.January, 1, 0, 0, 0, 0, currentLocation)
  309. lastOfYear := firstOfYear.AddDate(1, 0, -1)
  310. return firstOfYear.Format("2006-01-02"), lastOfYear.Format("2006-01-02")
  311. }
  312. // 获取上个月的最后一天
  313. func GetLastMonthLastDate() string {
  314. now := time.Now()
  315. firstOfMonth := time.Date(now.Year(), now.Month(), 1, 0, 0, 0, 0, time.Local)
  316. return firstOfMonth.AddDate(0, 0, -1).Format(`2006-01-02`)
  317. }
  318. // GetLastYear 获取上一年 202210 => 2021
  319. func GetLastYear() string {
  320. now := time.Now()
  321. return now.AddDate(-1, 0, 0).Format(`2006`)
  322. }
  323. // 等待 xpath 对应的元素加载,相当于:
  324. //
  325. // page.Timeout(5*time.Second).MustElementX(xpath).MustWaitVisible()
  326. //
  327. // 注意:加载失败 panic 的是税局卡顿错误。非税局采集任务不应使用该函数。
  328. //
  329. // Deprecated: 直接替换成上面的代码即可。超时错误有 HandleError 兜底。
  330. func WaitElementX5(page *rod.Page, xpath string) *rod.Element {
  331. return WaitElementXFoTimes(page, xpath, 5)
  332. }
  333. // 获取上一年的最后一天
  334. func GetLastYearLastDate() string {
  335. now := time.Now()
  336. lastYear := now.AddDate(-1, 0, 0)
  337. lastDayOfLastYear := time.Date(lastYear.Year(), 12, 31, 0, 0, 0, 0, time.Local)
  338. return lastDayOfLastYear.Format(`2006-01-02`)
  339. }
  340. // 获取本账期开始和结束日期
  341. func GetQuarterStartTimeAndEndTime(period string) (string, string) {
  342. quarterStart := ""
  343. quarterEnd := ""
  344. quarter := math.Ceil(StrToFloat(period[4:6])/3) - 1
  345. if quarter == 0 { //第一季度
  346. quarterStart = period[0:4] + "-01-01"
  347. quarterEnd = period[0:4] + "-03-31"
  348. }
  349. if quarter == 1 { //第二季度
  350. quarterStart = period[0:4] + "-04-01"
  351. quarterEnd = period[0:4] + "-06-30"
  352. }
  353. if quarter == 2 { //第三季度
  354. quarterStart = period[0:4] + "-07-01"
  355. quarterEnd = period[0:4] + "-09-30"
  356. }
  357. if quarter == 3 { //第四季度
  358. quarterStart = period[0:4] + "-10-01"
  359. quarterEnd = period[0:4] + "-12-31"
  360. }
  361. return quarterStart, quarterEnd
  362. }
  363. func GetCurrentPeriod3() string { //仅本期报税使用
  364. tm := time.Unix(time.Now().Unix(), 0)
  365. currentYear, currentMonth, _ := tm.Date()
  366. currentLocation := tm.Location()
  367. firstOfMonth := time.Date(currentYear, currentMonth, 10, 0, 0, 0, 0, currentLocation)
  368. period := firstOfMonth.AddDate(0, -1, 0).Format("2006-01") // 当前账期-1
  369. return period
  370. }
  371. func GetThisPrevQuarterStartTimeAndEndTimeNew(period string) (string, string) {
  372. quarterStart := ""
  373. quarterEnd := ""
  374. m := period[4:6]
  375. switch m {
  376. case "03":
  377. quarterStart = period[0:4] + "-01-01"
  378. quarterEnd = period[0:4] + "-03-31"
  379. case "06":
  380. quarterStart = period[0:4] + "-04-01"
  381. quarterEnd = period[0:4] + "-06-30"
  382. case "09":
  383. quarterStart = period[0:4] + "-07-01"
  384. quarterEnd = period[0:4] + "-09-30"
  385. case "12":
  386. quarterStart = IntToStr(StrToInt(period[0:4])) + "-10-01"
  387. quarterEnd = IntToStr(StrToInt(period[0:4])) + "-12-31"
  388. default:
  389. quarterStart, quarterEnd = GetBeginAndEndTime(period)
  390. }
  391. return quarterStart, quarterEnd
  392. }
  393. // 是否是季度报
  394. func IsJdSb(period string) bool {
  395. if len(period) == 6 && (period[4:] == "03" || period[4:] == "06" || period[4:] == "09" || period[4:] == "12") {
  396. return true
  397. }
  398. return false
  399. }
  400. func GetMonthEnd(t time.Time) time.Time {
  401. return t.AddDate(0, 1, -t.Day())
  402. }
  403. func GetCurrentDate1() string {
  404. tm := time.Unix(time.Now().Unix(), 0)
  405. currentYear, currentMonth, _ := tm.Date()
  406. currentLocation := tm.Location()
  407. firstOfMonth := time.Date(currentYear, currentMonth, 10, 0, 0, 0, 0, currentLocation)
  408. period := firstOfMonth.AddDate(0, 0, 0).Format("2006-01") // 当前日期
  409. return period
  410. }
  411. // 获取本月开始和结束的时间
  412. func GetBeginAndEndTime1(period string) (string, string) {
  413. loc, _ := time.LoadLocation("Local")
  414. the_time, _ := time.ParseInLocation("200601", period, loc)
  415. year, month, _ := the_time.Date()
  416. thisMonth := time.Date(year, month, 1, 0, 0, 0, 0, time.Local)
  417. start := thisMonth.AddDate(0, 0, 0).Format("2006-1-2")
  418. end := thisMonth.AddDate(0, 1, -1).Format("2006-1-2")
  419. return start, end
  420. }
  421. // 根据时间戳获取 月初 月末 0代表本月
  422. func GetMonthBeginTimeAndEndTime(num int64) (string, string) {
  423. if num == 0 {
  424. num = time.Now().Unix()
  425. }
  426. tm := time.Unix(num, 0)
  427. currentYear, currentMonth, _ := tm.Date()
  428. currentLocation := tm.Location()
  429. firstOfMonth := time.Date(currentYear, currentMonth, 1, 0, 0, 0, 0, currentLocation)
  430. lastOfMonth := firstOfMonth.AddDate(0, 1, -1)
  431. return firstOfMonth.Format("2006-01-02"), lastOfMonth.Format("2006-01-02")
  432. }
  433. // 获取当前账期
  434. func GetThisYearMonth() string {
  435. return time.Now().Format("2006-01")
  436. }