schedule.go 984 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. package mhayaTimeWheel
  2. import (
  3. "time"
  4. )
  5. // Scheduler determines the execution plan of a task.
  6. type Scheduler interface {
  7. // Next returns the next execution time after the given (previous) time.
  8. // It will return a zero time if no next time is scheduled.
  9. //
  10. // All times must be UTC.
  11. Next(time.Time) time.Time
  12. }
  13. type EverySchedule struct {
  14. Interval time.Duration
  15. }
  16. func (s *EverySchedule) Next(prev time.Time) time.Time {
  17. return prev.Add(s.Interval)
  18. }
  19. type FixedDateSchedule struct {
  20. Hour, Minute, Second int
  21. }
  22. func (s *FixedDateSchedule) Next(prev time.Time) time.Time {
  23. hour := prev.Hour()
  24. if s.Hour >= 0 {
  25. hour = s.Hour
  26. }
  27. fixedTime := time.Date(
  28. prev.Year(),
  29. prev.Month(),
  30. prev.Day(),
  31. hour,
  32. s.Minute,
  33. s.Second,
  34. 0,
  35. prev.Location(),
  36. )
  37. remain := fixedTime.UnixNano() - prev.UnixNano()
  38. if remain > 0 {
  39. return prev.Add(time.Duration(remain))
  40. }
  41. if s.Hour >= 0 {
  42. return fixedTime.Add(24 * time.Hour)
  43. }
  44. return fixedTime.Add(time.Hour)
  45. }