time.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. package utils
  2. import (
  3. "fmt"
  4. "strconv"
  5. "time"
  6. )
  7. // 给出日期 获取其中每小时的开始时间戳
  8. func GetDayHour(date time.Time) []int64 {
  9. date = time.Date(date.Year(), date.Month(), date.Day(), 0, 0, 0, 0, time.Local)
  10. var hours []int64
  11. // 遍历这一天中的每一小时
  12. for hour := 0; hour < 24; hour++ {
  13. // 设置小时并保留分钟和秒为零
  14. t := date.Add(time.Duration(hour) * time.Hour)
  15. //fmt.Println(t.Format("2006-01-02 15:04:05"))
  16. hours = append(hours, t.Unix())
  17. }
  18. return hours
  19. }
  20. // 给出开始和结束日期 获取其中每一天的开始时间戳
  21. func GetTimeDay(startDate string, endDate string) []int64 {
  22. var days []int64
  23. startTime, _ := time.Parse("2006-01-02", startDate)
  24. var endTime time.Time
  25. if len(endDate) > 10 {
  26. endTime, _ = time.Parse("2006-01-02 15:04:05", endDate)
  27. } else {
  28. endTime, _ = time.Parse("2006-01-02", endDate)
  29. }
  30. for currTime := startTime; !currTime.After(endTime); currTime = currTime.AddDate(0, 0, 1) {
  31. // 设置时间为当天的开始时间
  32. localLocation, _ := time.LoadLocation("Local") //设置时区
  33. midnight := time.Date(currTime.Year(), currTime.Month(), currTime.Day(), 0, 0, 0, 0, localLocation).Unix()
  34. days = append(days, midnight)
  35. }
  36. return days
  37. }
  38. // 给出开始和结束日期 获取中间每一天的date
  39. func GetTimeDayDate(startDate string, endDate string) map[string][]int {
  40. days := make(map[string][]int)
  41. startTime, _ := time.Parse("2006-01-02", startDate)
  42. endTime, _ := time.Parse("2006-01-02", endDate)
  43. for currTime := startTime; !currTime.After(endTime); currTime = currTime.AddDate(0, 0, 1) {
  44. // 设置时间为当天的开始时间
  45. midnight := time.Date(currTime.Year(), currTime.Month(), currTime.Day(), 0, 0, 0, 0, currTime.Location()).Format("2006-01-02")
  46. days[midnight] = []int{}
  47. }
  48. return days
  49. }
  50. func GetTimeDayDateFormat(startDate string, endDate string) []string {
  51. days := make([]string, 0)
  52. startTime, _ := time.Parse("2006-01-02", startDate)
  53. var endTime time.Time
  54. if len(endDate) > 10 {
  55. endTime, _ = time.Parse("2006-01-02 15:04:05", endDate)
  56. } else {
  57. endTime, _ = time.Parse("2006-01-02", endDate)
  58. }
  59. for currTime := startTime; !currTime.After(endTime); currTime = currTime.AddDate(0, 0, 1) {
  60. // 设置时间为当天的开始时间
  61. midnight := time.Date(currTime.Year(), currTime.Month(), currTime.Day(), 0, 0, 0, 0, currTime.Location()).Format("2006-01-02")
  62. days = append(days, midnight)
  63. }
  64. return days
  65. }
  66. // 时间戳转化为 00:02:37 格式
  67. func TimeStampToMDS(timestamp int) string {
  68. hours := timestamp / (60 * 24)
  69. remainingMinutes := timestamp % (60 * 24)
  70. minutes := remainingMinutes / 60
  71. remainingSeconds := timestamp % 60
  72. return fmt.Sprintf("%02d:%02d:%02d", hours, minutes, remainingSeconds)
  73. }
  74. // 比较日期 ,
  75. func CompareDates(dateA, dateB string) (bool, string) {
  76. tA, errA := time.Parse("2006-01-02", dateA)
  77. tB, errB := time.Parse("2006-01-02", dateB)
  78. if errA != nil || errB != nil {
  79. return false, ""
  80. }
  81. diff := int(tA.Sub(tB).Hours() / 24)
  82. valuableDiff := []int{1, 2, 3, 4, 5, 6, 7, 14, 30}
  83. if InArray(diff, valuableDiff) {
  84. return true, "+" + strconv.Itoa(diff) + "day"
  85. } else {
  86. return false, ""
  87. }
  88. }