time_helper.go 1.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. package utils
  2. import (
  3. "time"
  4. )
  5. const ntpEpochOffset = 2208988800
  6. type packet struct {
  7. Settings uint8
  8. Stratum uint8
  9. Poll int8
  10. Precision int8
  11. RootDelay uint32
  12. RootDispersion uint32
  13. ReferenceID uint32
  14. RefTimeSec uint32
  15. RefTimeFrac uint32
  16. OrigTimeSec uint32
  17. OrigTimeFrac uint32
  18. RxTimeSec uint32
  19. RxTimeFrac uint32
  20. TxTimeSec uint32
  21. TxTimeFrac uint32
  22. }
  23. // day 当天凌晨0点
  24. func ZeroHourTimeOfDay(day time.Time) time.Time {
  25. dayStr := day.Format("2006-01-02")
  26. zeroHourTime, _ := ParseTimeStringToTime("2006-01-02", dayStr)
  27. return *zeroHourTime
  28. }
  29. // day 当月一号凌晨0点
  30. func BeginningOfMonth(day time.Time) time.Time {
  31. dayStr := day.Format("2006-01")
  32. monthTime, _ := ParseTimeStringToTime("2006-01", dayStr)
  33. return *monthTime
  34. }
  35. // 指定年月的月份的月初凌晨0点和月末23点59分59秒
  36. func MonthBeginningToEnd(year int, month int) (time.Time, time.Time) {
  37. beginningOfMonth := time.Date(year, time.Month(month), 1, 0, 0, 0, 0, time.Local)
  38. beginningOfNextMonth := beginningOfMonth.AddDate(0, 1, 0)
  39. endOfMonth := beginningOfNextMonth.Add(time.Duration(-1))
  40. return beginningOfMonth, endOfMonth
  41. }