daycompute.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. package common
  2. import (
  3. "runtime/debug"
  4. "strings"
  5. "time"
  6. "bet24.com/log"
  7. )
  8. const SecondsOfDay = 86400 // 每天秒数
  9. const FirstWeek_days = 4 // 1970.1.1为周四
  10. const FirstWeek_Secondes = 432000 // 周日为每周起点,减去5天
  11. const SecondsOfWeek = 607800
  12. const Layout = "2006-01-02 15:04:05"
  13. // 获取当前时间
  14. func GetNowTime() time.Time {
  15. return time.Now()
  16. }
  17. // 获取当前时间串
  18. func GetNowTimeStr() string {
  19. return time.Now().Format(Layout)
  20. }
  21. // 时间字符串转换时间格式
  22. func ParseTime(value string) time.Time {
  23. if value == "" {
  24. value = "1970-01-01 00:00:00"
  25. }
  26. t, err := time.ParseInLocation(Layout, value, time.Now().Location())
  27. if err != nil {
  28. log.Error("daycompute.ParseTime error %+v", err)
  29. log.Error("%s", debug.Stack())
  30. }
  31. return t
  32. }
  33. func GetDifferSec(beforeSec int) int {
  34. now := GetTimeStamp()
  35. t1 := time.Unix(int64(beforeSec), 0)
  36. t2 := time.Unix(int64(now), 0)
  37. diff := t2.Sub(t1)
  38. sec := int(diff.Seconds())
  39. return sec
  40. }
  41. // 获取时间戳
  42. func GetTimeStamp() int {
  43. return int(time.Now().Unix())
  44. }
  45. // 从数据库读取的 time 已经加有时区,这里不需要再处理 Timezone_add
  46. func GetStamp(t time.Time) int {
  47. return int(t.Unix())
  48. }
  49. func timeStampToLocal(timeStamp int) int {
  50. nowStr := time.Unix(int64(timeStamp), 0).Format(Layout)
  51. u, err := time.Parse(Layout, nowStr)
  52. if err != nil {
  53. log.Error("daycompute.timeStampToLocal error %+v", err)
  54. log.Error("%s", debug.Stack())
  55. return timeStamp
  56. }
  57. return int(u.Unix())
  58. }
  59. func TimeStampToString(timeStamp int64) string {
  60. nowStr := time.Unix(timeStamp, 0).Format(Layout)
  61. u, err := time.Parse(Layout, nowStr)
  62. if err != nil {
  63. return nowStr
  64. }
  65. return u.Format(Layout)
  66. }
  67. func TimeStampToShortString(timeStamp int64) string {
  68. ret := TimeStampToString(timeStamp)
  69. ret = strings.ReplaceAll(ret, "2023-", "")
  70. return strings.ReplaceAll(ret, "2024-", "")
  71. }
  72. func TimeStampToTime(timeStamp int64) time.Time {
  73. now := time.Unix(timeStamp, 0)
  74. nowStr := now.Format(Layout)
  75. u, err := time.Parse(Layout, nowStr)
  76. if err != nil {
  77. return now
  78. }
  79. return u
  80. }
  81. func GetDayIndex(timeStamp int) int {
  82. return timeStampToLocal(timeStamp) / SecondsOfDay
  83. }
  84. func GetNowDayIndex() int {
  85. return GetDayIndex(int(time.Now().Unix()))
  86. }
  87. func GetWeekIndex(timeStamp int) int {
  88. return (timeStampToLocal(timeStamp)-FirstWeek_Secondes)/SecondsOfWeek + 1
  89. }
  90. func GetMonthIndex(timeStamp int) int {
  91. t := time.Unix(int64(timeStamp), 0)
  92. // 5 + 202300(年份加月份解决唯一且不重复的问题)
  93. return int(t.Month()) + t.Year()*100
  94. }
  95. func IsContinueDay(timeStamp1, timeStamp2 int) bool {
  96. day1 := GetDayIndex(timeStamp1)
  97. day2 := GetDayIndex(timeStamp2)
  98. if day1 == day2-1 || day1 == day2+1 {
  99. return true
  100. }
  101. return false
  102. }
  103. func IsSameDay(timeStamp1, timeStamp2 int) bool {
  104. return GetDayIndex(timeStamp1) == GetDayIndex(timeStamp2)
  105. }
  106. func IsSameWeek(timeStamp1, timeStamp2 int) bool {
  107. return GetWeekIndex(timeStamp1) == GetWeekIndex(timeStamp2)
  108. }