aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/time_provider/time_provider.cpp
blob: e1045dae05ef48583215259a4c55fdad7ea37d15 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include "time_provider.h"

class TDefaultTimeProvider: public ITimeProvider {
public:
    TInstant Now() override {
        return TInstant::Now();
    }
};

class TDeterministicTimeProvider: public ITimeProvider {
public:
    TDeterministicTimeProvider(ui64 seed) {
        Value = TInstant::Seconds(seed);
    }

    TInstant Now() override {
        return Value;
    }

private:
    TInstant Value;
};

TIntrusivePtr<ITimeProvider> CreateDefaultTimeProvider() {
    return TIntrusivePtr<ITimeProvider>(new TDefaultTimeProvider());
}

TIntrusivePtr<ITimeProvider> CreateDeterministicTimeProvider(ui64 seed) {
    return TIntrusivePtr<ITimeProvider>(new TDeterministicTimeProvider(seed));
}

namespace {
    TIntrusivePtr<ITimeProvider> GlobalTimeProvider;
}

void TInstantOperator::RegisterProvider(TIntrusivePtr<ITimeProvider> provider) {
    GlobalTimeProvider = provider;
}

TInstant TInstantOperator::Now() {
    if (!GlobalTimeProvider) {
        return TInstant::Now();
    } else {
        return GlobalTimeProvider->Now();
    }
}