time.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. // Copyright 2021 EMQ Technologies Co., Ltd.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package conf
  15. import (
  16. "github.com/benbjohnson/clock"
  17. "github.com/lf-edge/ekuiper/pkg/cast"
  18. "time"
  19. )
  20. var Clock clock.Clock
  21. func InitClock() {
  22. if IsTesting {
  23. Log.Debugf("running in testing mode")
  24. Clock = clock.NewMock()
  25. } else {
  26. Clock = clock.New()
  27. }
  28. }
  29. //Time related. For Mock
  30. func GetTicker(duration int) *clock.Ticker {
  31. return Clock.Ticker(time.Duration(duration) * time.Millisecond)
  32. }
  33. func GetTimer(duration int) *clock.Timer {
  34. return Clock.Timer(time.Duration(duration) * time.Millisecond)
  35. }
  36. func GetNowInMilli() int64 {
  37. return cast.TimeToUnixMilli(Clock.Now())
  38. }
  39. func GetNow() time.Time {
  40. return Clock.Now()
  41. }