clock.go 867 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. package sentio
  2. import "time"
  3. type Clock interface {
  4. Now() time.Time
  5. After(d time.Duration) <-chan time.Time
  6. }
  7. type ClockUTC struct {
  8. }
  9. func (t ClockUTC) Now() time.Time {
  10. return time.Now().In(time.UTC)
  11. }
  12. func (t ClockUTC) After(d time.Duration) <-chan time.Time {
  13. return time.After(d)
  14. }
  15. type ClockNY struct {
  16. location *time.Location
  17. }
  18. func NewClockNY() (Clock, error) {
  19. l, err := time.LoadLocation("America/New_York")
  20. if err != nil {
  21. return nil, err
  22. }
  23. return ClockNY{
  24. location: l,
  25. }, nil
  26. }
  27. func (t ClockNY) Now() time.Time {
  28. return time.Now().In(t.location)
  29. }
  30. func (t ClockNY) After(d time.Duration) <-chan time.Time {
  31. return time.After(d)
  32. }
  33. type StubClock struct {
  34. Clock time.Time
  35. }
  36. func (t StubClock) Now() time.Time {
  37. return t.Clock.In(time.UTC)
  38. }
  39. func (t StubClock) After(d time.Duration) <-chan time.Time {
  40. return time.After(d)
  41. }