time.go 6.0 KB

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