time_provider.cpp 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. #include "time_provider.h"
  2. class TDefaultTimeProvider: public ITimeProvider {
  3. public:
  4. TInstant Now() override {
  5. return TInstant::Now();
  6. }
  7. };
  8. class TDeterministicTimeProvider: public ITimeProvider {
  9. public:
  10. TDeterministicTimeProvider(ui64 seed) {
  11. Value = TInstant::Seconds(seed);
  12. }
  13. TInstant Now() override {
  14. return Value;
  15. }
  16. private:
  17. TInstant Value;
  18. };
  19. TIntrusivePtr<ITimeProvider> CreateDefaultTimeProvider() {
  20. return TIntrusivePtr<ITimeProvider>(new TDefaultTimeProvider());
  21. }
  22. TIntrusivePtr<ITimeProvider> CreateDeterministicTimeProvider(ui64 seed) {
  23. return TIntrusivePtr<ITimeProvider>(new TDeterministicTimeProvider(seed));
  24. }
  25. namespace {
  26. TIntrusivePtr<ITimeProvider> GlobalTimeProvider;
  27. }
  28. void TInstantOperator::RegisterProvider(TIntrusivePtr<ITimeProvider> provider) {
  29. GlobalTimeProvider = provider;
  30. }
  31. TInstant TInstantOperator::Now() {
  32. if (!GlobalTimeProvider) {
  33. return TInstant::Now();
  34. } else {
  35. return GlobalTimeProvider->Now();
  36. }
  37. }