time.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. package timext
  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. // IsSameMonthByTs checks if two timestamps belong to the same month
  58. func IsSameMonthByTs(a int64, b int64) bool {
  59. if a == b {
  60. return true
  61. }
  62. a1 := time.Unix(a, 0)
  63. b1 := time.Unix(b, 0)
  64. return GetYearMonth(&a1) == GetYearMonth(&b1)
  65. }
  66. // DiffNatureDays 判断两个时间戳相差几天
  67. func DiffNatureDays(t1, t2 int64) int {
  68. var secondsOfDay int64 = 86400
  69. if t1 == t2 {
  70. return 0
  71. }
  72. if t1 > t2 {
  73. t1, t2 = t2, t1
  74. }
  75. diffDays := 0
  76. secDiff := t2 - t1
  77. if secDiff > secondsOfDay {
  78. tmpDays := int(secDiff / secondsOfDay)
  79. t1 += int64(tmpDays) * secondsOfDay
  80. diffDays += tmpDays
  81. }
  82. st := time.Unix(t1, 0)
  83. et := time.Unix(t2, 0)
  84. dateFormatTpl := "20060102"
  85. if st.Format(dateFormatTpl) != et.Format(dateFormatTpl) {
  86. diffDays += 1
  87. }
  88. return diffDays
  89. }
  90. // GetWeekDay 时间是周几,范围1~7
  91. func GetWeekDay(t time.Time) int {
  92. dayOfWeek := t.Weekday()
  93. // 将周日转换为7,其余依次加1
  94. dayNumber := int(dayOfWeek)
  95. if dayNumber == 0 { // 周日
  96. dayNumber = 7
  97. }
  98. return dayNumber
  99. }
  100. // GetWhichDayOfPeriod 活动开始后第几天,包括结束日
  101. func CalcWhichDayOfPeriod(startTime int64, endTime int64) int64 {
  102. n := CalcDaysBetweenTs(startTime, endTime)
  103. return n + 1
  104. }
  105. // 计算从start日0点到endTime日0点,中间有几天,只计算日差值
  106. func CalcDaysBetweenTime(startTime time.Time, endTime time.Time) int64 {
  107. endTime = time.Date(endTime.Year(), endTime.Month(), endTime.Day(), 0, 0, 0, 0, time.Local)
  108. startTime = time.Date(startTime.Year(), startTime.Month(), startTime.Day(), 0, 0, 0, 0, time.Local)
  109. return int64(endTime.Sub(startTime).Hours() / 24)
  110. }
  111. // 计算从start日0点到endTime日0点,中间有几天,只计算日差值
  112. func CalcDaysBetweenTs(startTime int64, endTime int64) int64 {
  113. t1 := time.Unix(startTime, 0)
  114. t2 := time.Unix(endTime, 0)
  115. return CalcDaysBetweenTime(t1, t2)
  116. }
  117. // 计算从start日0点到endTime日0点,中间有几天,只计算日差值
  118. func CalcDaysByYmd(ymd1 int32, ymd2 int32) int32 {
  119. t1 := GetTimeByDateInt(ymd1)
  120. t2 := GetTimeByDateInt(ymd2)
  121. return int32(CalcDaysBetweenTime(t1, t2))
  122. }
  123. func GetTimeByDateInt(date int32) time.Time {
  124. year := int(date / 10000)
  125. month := int(date / 100 % 100)
  126. day := int(date % 100)
  127. t := time.Date(year, time.Month(month), day, 0, 0, 0, 0, time.Local)
  128. return t
  129. }
  130. // func GetDateIntTimeStamp(timeStamp int64) int32 {
  131. // _time := time.Unix(timeStamp, 0)
  132. // return int32(_time.Year()*10000 + int(_time.Month())*100 + _time.Day())
  133. // }
  134. // func SubDays(endTime, startTime time.Time) int64 {
  135. // endTime = time.Date(endTime.Year(), endTime.Month(), endTime.Day(), 0, 0, 0, 0, time.Local)
  136. // startTime = time.Date(startTime.Year(), startTime.Month(), startTime.Day(), 0, 0, 0, 0, time.Local)
  137. // return int64(endTime.Sub(startTime).Hours() / 24)
  138. // }
  139. // 获取这一天0点时的时间戳
  140. func GetNowDayTimestamp() int64 {
  141. now := time.Now()
  142. year, month, day := now.Date()
  143. midnight := time.Date(year, month, day, 0, 0, 0, 0, now.Location())
  144. timestamp := midnight.Unix()
  145. return timestamp
  146. }
  147. // 获取指定周0点时间
  148. func GetWeekZeroTime(timestamp int64) int64 {
  149. now := time.Unix(timestamp, 0)
  150. zeroTime := GetNowDayTimestamp()
  151. week := int64(now.Weekday())
  152. if week == 0 {
  153. week = 7
  154. }
  155. return zeroTime - ((week - 1) * 86400)
  156. }
  157. // GetStartTimeOfDay 获取指定时间的0时
  158. func GetStartTimeOfDay(timestamp int64) int64 {
  159. t := time.Unix(timestamp, 0)
  160. y, m, d := t.Date()
  161. zeroTime := time.Date(y, m, d, 0, 0, 0, 0, t.Location())
  162. return zeroTime.Unix()
  163. }
  164. func GetEndTimeOfDay(timestamp int64) int64 {
  165. // 将时间戳转换为 time.Time 类型
  166. t := time.Unix(timestamp, 0)
  167. // 获取当日的年、月、日
  168. year, month, day := t.Date()
  169. // 创建当天23点59分59秒的时间对象
  170. endOfDay := time.Date(year, month, day, 23, 59, 59, 0, t.Location())
  171. // 返回23点59分59秒对应的Unix时间戳
  172. return endOfDay.Unix()
  173. }
  174. // func GetEndTimeOfDayByTs(timestamp int64) int64 {
  175. // // 将时间戳转换为 time.Time 类型
  176. // t := time.Unix(timestamp, 0)
  177. // // 获取当日的年、月、日
  178. // year, month, day := t.Date()
  179. // // 创建当天23点59分59秒的时间对象
  180. // endOfDay := time.Date(year, month, day, 23, 59, 59, 0, t.Location())
  181. // // 返回23点59分59秒对应的Unix时间戳
  182. // return endOfDay.Unix()
  183. // }
  184. func GetEndTimeOfWeek(timestamp int64) int64 {
  185. // 将时间戳转换为 time.Time 类型
  186. t := time.Unix(timestamp, 0)
  187. // 获取当前日期的星期几
  188. weekday := t.Weekday()
  189. // 计算距离星期日的天数
  190. daysToSunday := (7 - int(weekday)) % 7
  191. // 计算星期日的日期
  192. sunday := t.AddDate(0, 0, daysToSunday)
  193. // 计算星期日的23点59分59秒
  194. endOfWeek := time.Date(sunday.Year(), sunday.Month(), sunday.Day(), 23, 59, 59, 0, t.Location())
  195. // 返回星期日的23点59分59秒对应的Unix时间戳
  196. return endOfWeek.Unix()
  197. }
  198. func GetEndTimeOfMonth(timestamp int64) int64 {
  199. // 将时间戳转换为time.Time
  200. t := time.Unix(timestamp, 0)
  201. nextMonth := t.Month() + 1
  202. year := t.Year()
  203. if nextMonth > 12 {
  204. nextMonth = 1
  205. year++ // 显式跨年处理
  206. }
  207. firstOfNextMonth := time.Date(year, nextMonth, 1, 0, 0, 0, 0, t.Location())
  208. // 减去1秒得到本月最后一天23:59:59
  209. endOfMonth := firstOfNextMonth.Add(-time.Second)
  210. return endOfMonth.Unix()
  211. }