period.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. package common
  2. import (
  3. "errors"
  4. "fmt"
  5. "git.listensoft.net/tool/jspkit/logger"
  6. "github.com/go-rod/rod"
  7. "math"
  8. "strconv"
  9. "strings"
  10. "time"
  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. // DateFormatter 将YYYYMMDD格式改为YYYY-MM-DD格式
  278. func DateFormatter(date string) string {
  279. if len(date) != 8 {
  280. return "" // 返回空字符串表示输入格式不正确
  281. }
  282. return fmt.Sprintf("%s-%s-%s", date[:4], date[4:6], date[6:])
  283. }
  284. // 获取某一天的0点时间
  285. func GetNextDateTime(d time.Time) time.Time {
  286. return time.Date(d.Year(), d.Month(), d.Day()+1, 0, 0, 0, 0, d.Location())
  287. }
  288. // 获取去年的第一天和最后一天
  289. func GetLastYearFirstTimeAndEndTime() (string, string) {
  290. now := time.Now()
  291. logger.Info(now)
  292. currentYear, _, _ := now.Date()
  293. currentLocation := now.Location()
  294. logger.Info(currentLocation)
  295. firstOfYear := time.Date(currentYear-1, time.January, 1, 0, 0, 0, 0, currentLocation)
  296. lastOfYear := firstOfYear.AddDate(1, 0, -1)
  297. return firstOfYear.Format("2006-01-02"), lastOfYear.Format("2006-01-02")
  298. }
  299. // 获取今年的第一天和最后一天
  300. func GetCurrentYearFirstTimeAndEndTime() (string, string) {
  301. now := time.Now()
  302. logger.Info(now)
  303. currentYear, _, _ := now.Date()
  304. currentLocation := now.Location()
  305. logger.Info(currentLocation)
  306. firstOfYear := time.Date(currentYear, time.January, 1, 0, 0, 0, 0, currentLocation)
  307. lastOfYear := firstOfYear.AddDate(1, 0, -1)
  308. return firstOfYear.Format("2006-01-02"), lastOfYear.Format("2006-01-02")
  309. }
  310. // 获取上个月的最后一天
  311. func GetLastMonthLastDate() string {
  312. now := time.Now()
  313. firstOfMonth := time.Date(now.Year(), now.Month(), 1, 0, 0, 0, 0, time.Local)
  314. return firstOfMonth.AddDate(0, 0, -1).Format(`2006-01-02`)
  315. }
  316. // GetLastYear 获取上一年 202210 => 2021
  317. func GetLastYear() string {
  318. now := time.Now()
  319. return now.AddDate(-1, 0, 0).Format(`2006`)
  320. }
  321. func WaitElementX5(page *rod.Page, xpath string) *rod.Element {
  322. return WaitElementXFoTimes(page, xpath, 5)
  323. }
  324. // 获取上一年的最后一天
  325. func GetLastYearLastDate() string {
  326. now := time.Now()
  327. lastYear := now.AddDate(-1, 0, 0)
  328. lastDayOfLastYear := time.Date(lastYear.Year(), 12, 31, 0, 0, 0, 0, time.Local)
  329. return lastDayOfLastYear.Format(`2006-01-02`)
  330. }
  331. // 获取本账期开始和结束日期
  332. func GetQuarterStartTimeAndEndTime(period string) (string, string) {
  333. quarterStart := ""
  334. quarterEnd := ""
  335. quarter := math.Ceil(StrToFloat(period[4:6])/3) - 1
  336. if quarter == 0 { //第一季度
  337. quarterStart = period[0:4] + "-01-01"
  338. quarterEnd = period[0:4] + "-03-31"
  339. }
  340. if quarter == 1 { //第二季度
  341. quarterStart = period[0:4] + "-04-01"
  342. quarterEnd = period[0:4] + "-06-30"
  343. }
  344. if quarter == 2 { //第三季度
  345. quarterStart = period[0:4] + "-07-01"
  346. quarterEnd = period[0:4] + "-09-30"
  347. }
  348. if quarter == 3 { //第四季度
  349. quarterStart = period[0:4] + "-10-01"
  350. quarterEnd = period[0:4] + "-12-31"
  351. }
  352. return quarterStart, quarterEnd
  353. }
  354. func GetCurrentPeriod3() string { //仅本期报税使用
  355. tm := time.Unix(time.Now().Unix(), 0)
  356. currentYear, currentMonth, _ := tm.Date()
  357. currentLocation := tm.Location()
  358. firstOfMonth := time.Date(currentYear, currentMonth, 10, 0, 0, 0, 0, currentLocation)
  359. period := firstOfMonth.AddDate(0, -1, 0).Format("2006-01") // 当前账期-1
  360. return period
  361. }