time.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. package otherutils
  2. import "time"
  3. // GetYearDay returns a formatted integer representing year and day number (format: YYYYDDD)
  4. func GetYearDay(tm *time.Time) int {
  5. year := tm.Year()
  6. day := tm.YearDay()
  7. return year*1000 + day
  8. }
  9. func GetYearDayByTs(ts int64) int {
  10. tm := time.Unix(ts, 0)
  11. return GetYearDay(&tm)
  12. }
  13. func GetYearWeek(tm *time.Time) int {
  14. y, w := tm.ISOWeek()
  15. return y*100 + w
  16. }
  17. func GetYearWeekByTs(ts int64) int {
  18. tm := time.Unix(ts, 0)
  19. return GetYearWeek(&tm)
  20. }
  21. func GetYearMonth(tm *time.Time) int {
  22. y, m, _ := tm.Date()
  23. return y*100 + int(m)
  24. }
  25. func GetYearMonthByTs(ts int64) int {
  26. tm := time.Unix(ts, 0)
  27. return GetYearMonth(&tm)
  28. }
  29. // 20141016
  30. func GetYearMonthDay(tm *time.Time) int {
  31. y, m, d := tm.Date()
  32. return y*10000 + int(m)*100 + d
  33. }
  34. func IsSameDate(a *time.Time, b *time.Time) bool {
  35. a1 := GetYearMonthDay(a)
  36. b1 := GetYearMonthDay(b)
  37. return a1 == b1
  38. }
  39. // IsSameDateByTs checks if two timestamps belong to the same date
  40. func IsSameDateByTs(a int64, b int64) bool {
  41. if a == b {
  42. return true
  43. }
  44. a1 := time.Unix(a, 0)
  45. b1 := time.Unix(b, 0)
  46. return IsSameDate(&a1, &b1)
  47. }
  48. // IsSameWeekByTs checks if two timestamps belong to the same ISO week
  49. func IsSameWeekByTs(a int64, b int64) bool {
  50. if a == b {
  51. return true
  52. }
  53. a1 := time.Unix(a, 0)
  54. b1 := time.Unix(b, 0)
  55. return GetYearWeek(&a1) == GetYearWeek(&b1)
  56. }
  57. // GetZeroTime 获取指定时间的0时
  58. func GetZeroTime(t time.Time) int64 {
  59. t2 := t.Unix()
  60. _, offsetSeconds := t.Zone()
  61. seconds := (t2 + int64(offsetSeconds)) % 86400
  62. return t2 - seconds
  63. }
  64. // DiffNatureDays 判断两个时间戳相差几天
  65. func DiffNatureDays(t1, t2 int64) int {
  66. var secondsOfDay int64 = 86400
  67. if t1 == t2 {
  68. return 0
  69. }
  70. if t1 > t2 {
  71. t1, t2 = t2, t1
  72. }
  73. diffDays := 0
  74. secDiff := t2 - t1
  75. if secDiff > secondsOfDay {
  76. tmpDays := int(secDiff / secondsOfDay)
  77. t1 += int64(tmpDays) * secondsOfDay
  78. diffDays += tmpDays
  79. }
  80. st := time.Unix(t1, 0)
  81. et := time.Unix(t2, 0)
  82. dateFormatTpl := "20060102"
  83. if st.Format(dateFormatTpl) != et.Format(dateFormatTpl) {
  84. diffDays += 1
  85. }
  86. return diffDays
  87. }
  88. // GetWeekDay 时间是周几?范围1~7
  89. func GetWeekDay(t time.Time) int {
  90. dayOfWeek := t.Weekday()
  91. // 将周日转换为7,其余依次加1
  92. dayNumber := int(dayOfWeek)
  93. if dayNumber == 0 { // 周日
  94. dayNumber = 7
  95. }
  96. return dayNumber
  97. }
  98. // GetWhichDayOfPeriod 活动开始后第几天,包括结束日
  99. func CalcWhichDayOfPeriod(startTime int64, endTime int64) int64 {
  100. n := CalcDaysBetweenTs(startTime, endTime)
  101. return n + 1
  102. }
  103. // 计算从start日0点到endTime日0点,中间有几天,只计算日差值
  104. func CalcDaysBetweenTime(startTime time.Time, endTime time.Time) int64 {
  105. endTime = time.Date(endTime.Year(), endTime.Month(), endTime.Day(), 0, 0, 0, 0, time.Local)
  106. startTime = time.Date(startTime.Year(), startTime.Month(), startTime.Day(), 0, 0, 0, 0, time.Local)
  107. return int64(endTime.Sub(startTime).Hours() / 24)
  108. }
  109. // 计算从start日0点到endTime日0点,中间有几天,只计算日差值
  110. func CalcDaysBetweenTs(startTime int64, endTime int64) int64 {
  111. t1 := time.Unix(startTime, 0)
  112. t2 := time.Unix(endTime, 0)
  113. return CalcDaysBetweenTime(t1, t2)
  114. }
  115. // 计算从start日0点到endTime日0点,中间有几天,只计算日差值
  116. func CalcDaysByYmd(ymd1 int32, ymd2 int32) int32 {
  117. t1 := GetTimeByDateInt(ymd1)
  118. t2 := GetTimeByDateInt(ymd2)
  119. return int32(CalcDaysBetweenTime(t1, t2))
  120. }
  121. func GetTimeByDateInt(date int32) time.Time {
  122. year := int(date / 10000)
  123. month := int(date / 100 % 100)
  124. day := int(date % 100)
  125. t := time.Date(year, time.Month(month), day, 0, 0, 0, 0, time.Local)
  126. return t
  127. }
  128. // func GetDateIntTimeStamp(timeStamp int64) int32 {
  129. // _time := time.Unix(timeStamp, 0)
  130. // return int32(_time.Year()*10000 + int(_time.Month())*100 + _time.Day())
  131. // }
  132. // func SubDays(endTime, startTime time.Time) int64 {
  133. // endTime = time.Date(endTime.Year(), endTime.Month(), endTime.Day(), 0, 0, 0, 0, time.Local)
  134. // startTime = time.Date(startTime.Year(), startTime.Month(), startTime.Day(), 0, 0, 0, 0, time.Local)
  135. // return int64(endTime.Sub(startTime).Hours() / 24)
  136. // }
  137. // 获取这一天0点时的时间戳
  138. func GetNowDayTimestamp() int64 {
  139. now := time.Now()
  140. year, month, day := now.Date()
  141. midnight := time.Date(year, month, day, 0, 0, 0, 0, now.Location())
  142. timestamp := midnight.Unix()
  143. return timestamp
  144. }
  145. // 获取指定周0点时间
  146. func GetWeekZeroTime(timestamp int64) int64 {
  147. now := time.Unix(timestamp, 0)
  148. zeroTime := GetNowDayTimestamp()
  149. week := int64(now.Weekday())
  150. if week == 0 {
  151. week = 7
  152. }
  153. return zeroTime - ((week - 1) * 86400)
  154. }