time.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. endTime, _ := time.Parse("2006-01-02", endDate)
  25. for currTime := startTime; !currTime.After(endTime); currTime = currTime.AddDate(0, 0, 1) {
  26. // 设置时间为当天的开始时间
  27. midnight := time.Date(currTime.Year(), currTime.Month(), currTime.Day(), 0, 0, 0, 0, currTime.Location()).Unix()
  28. days = append(days, midnight)
  29. }
  30. return days
  31. }
  32. // 给出开始和结束日期 获取中间每一天的date
  33. func GetTimeDayDate(startDate string, endDate string) map[string][]int {
  34. days := make(map[string][]int)
  35. startTime, _ := time.Parse("2006-01-02", startDate)
  36. endTime, _ := time.Parse("2006-01-02", endDate)
  37. for currTime := startTime; !currTime.After(endTime); currTime = currTime.AddDate(0, 0, 1) {
  38. // 设置时间为当天的开始时间
  39. midnight := time.Date(currTime.Year(), currTime.Month(), currTime.Day(), 0, 0, 0, 0, currTime.Location()).Format("2006-01-02")
  40. days[midnight] = []int{}
  41. }
  42. return days
  43. }
  44. func GetTimeDayDateFormat(startDate string, endDate string) []string {
  45. days := make([]string, 0)
  46. startTime, _ := time.Parse("2006-01-02", startDate)
  47. endTime, _ := time.Parse("2006-01-02", endDate)
  48. for currTime := startTime; !currTime.After(endTime); currTime = currTime.AddDate(0, 0, 1) {
  49. // 设置时间为当天的开始时间
  50. midnight := time.Date(currTime.Year(), currTime.Month(), currTime.Day(), 0, 0, 0, 0, currTime.Location()).Format("2006-01-02")
  51. days = append(days, midnight)
  52. }
  53. return days
  54. }
  55. // 时间戳转化为 00:02:37 格式
  56. func TimeStampToMDS(timestamp int) string {
  57. hours := timestamp / (60 * 24)
  58. remainingMinutes := timestamp % (60 * 24)
  59. minutes := remainingMinutes / 60
  60. remainingSeconds := timestamp % 60
  61. return fmt.Sprintf("%02d:%02d:%02d", hours, minutes, remainingSeconds)
  62. }
  63. // 比较日期 ,
  64. func CompareDates(dateA, dateB string) (bool, string) {
  65. tA, errA := time.Parse("2006-01-02", dateA)
  66. tB, errB := time.Parse("2006-01-02", dateB)
  67. if errA != nil || errB != nil {
  68. return false, ""
  69. }
  70. diff := int(tA.Sub(tB).Hours() / 24)
  71. valuableDiff := []int{1, 2, 3, 4, 5, 6, 7, 14, 30}
  72. if InArray(diff, valuableDiff) {
  73. return true, "+" + strconv.Itoa(diff) + "day"
  74. } else {
  75. return false, ""
  76. }
  77. }