Skip to content

Commit aba5f8e

Browse files
committed
Add system time to Clock
1 parent 148f0da commit aba5f8e

File tree

7 files changed

+35
-13
lines changed

7 files changed

+35
-13
lines changed

src/engine/internal/clock.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,12 @@ std::shared_ptr<Clock> Clock::instance()
1515
return m_instance;
1616
}
1717

18-
std::chrono::steady_clock::time_point Clock::currentTime() const
18+
std::chrono::steady_clock::time_point Clock::currentSteadyTime() const
1919
{
2020
return std::chrono::steady_clock::now();
2121
}
22+
23+
std::chrono::system_clock::time_point Clock::currentSystemTime() const
24+
{
25+
return std::chrono::system_clock::now();
26+
}

src/engine/internal/clock.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ class Clock : public IClock
1616

1717
static std::shared_ptr<Clock> instance();
1818

19-
std::chrono::steady_clock::time_point currentTime() const override;
19+
std::chrono::steady_clock::time_point currentSteadyTime() const override;
20+
std::chrono::system_clock::time_point currentSystemTime() const override;
2021

2122
private:
2223
static std::shared_ptr<Clock> m_instance;

src/engine/internal/iclock.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ class IClock
1212
public:
1313
virtual ~IClock() { }
1414

15-
virtual std::chrono::steady_clock::time_point currentTime() const = 0;
15+
virtual std::chrono::steady_clock::time_point currentSteadyTime() const = 0;
16+
virtual std::chrono::system_clock::time_point currentSystemTime() const = 0;
1617
};
1718

1819
} // namespace libscratchcpp

src/engine/internal/timer.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ Timer::Timer(IClock *clock) :
2222

2323
double Timer::value() const
2424
{
25-
return std::chrono::duration_cast<std::chrono::milliseconds>(m_clock->currentTime() - m_startTime).count() / 1000.0;
25+
return std::chrono::duration_cast<std::chrono::milliseconds>(m_clock->currentSteadyTime() - m_startTime).count() / 1000.0;
2626
}
2727

2828
void Timer::reset()
@@ -33,5 +33,5 @@ void Timer::reset()
3333
// Required to avoid calling the virtual method from the constructors
3434
void Timer::resetTimer()
3535
{
36-
m_startTime = m_clock->currentTime();
36+
m_startTime = m_clock->currentSteadyTime();
3737
}

test/clock/clock_test.cpp

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,29 @@
55

66
using namespace libscratchcpp;
77

8-
TEST(ClockTest, CurrentTime)
8+
TEST(ClockTest, CurrentSteadyTime)
99
{
1010
auto clock = Clock::instance();
1111
ASSERT_TRUE(clock);
1212

1313
auto currentTime = std::chrono::time_point_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now());
1414
double currentTimeMs = std::round(currentTime.time_since_epoch().count() / 10) * 10;
1515

16-
auto reportedTime = std::chrono::time_point_cast<std::chrono::milliseconds>(clock->currentTime());
16+
auto reportedTime = std::chrono::time_point_cast<std::chrono::milliseconds>(clock->currentSteadyTime());
17+
double reportedTimeMs = std::round(reportedTime.time_since_epoch().count() / 10) * 10;
18+
19+
ASSERT_EQ(reportedTimeMs, currentTimeMs);
20+
}
21+
22+
TEST(ClockTest, CurrentSystemTime)
23+
{
24+
auto clock = Clock::instance();
25+
ASSERT_TRUE(clock);
26+
27+
auto currentTime = std::chrono::time_point_cast<std::chrono::milliseconds>(std::chrono::system_clock::now());
28+
double currentTimeMs = std::round(currentTime.time_since_epoch().count() / 10) * 10;
29+
30+
auto reportedTime = std::chrono::time_point_cast<std::chrono::milliseconds>(clock->currentSystemTime());
1731
double reportedTimeMs = std::round(reportedTime.time_since_epoch().count() / 10) * 10;
1832

1933
ASSERT_EQ(reportedTimeMs, currentTimeMs);

test/mocks/clockmock.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,6 @@ using namespace libscratchcpp;
88
class ClockMock : public IClock
99
{
1010
public:
11-
MOCK_METHOD(std::chrono::steady_clock::time_point, currentTime, (), (const override));
11+
MOCK_METHOD(std::chrono::steady_clock::time_point, currentSteadyTime, (), (const, override));
12+
MOCK_METHOD(std::chrono::system_clock::time_point, currentSystemTime, (), (const, override));
1213
};

test/timer/timer_test.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,22 @@ TEST(TimerTest, ValueAndReset)
1212
ClockMock clock;
1313

1414
std::chrono::steady_clock::time_point startTime(std::chrono::milliseconds(50));
15-
EXPECT_CALL(clock, currentTime()).WillOnce(Return(startTime));
15+
EXPECT_CALL(clock, currentSteadyTime()).WillOnce(Return(startTime));
1616
Timer timer(&clock);
1717

1818
std::chrono::steady_clock::time_point time1(std::chrono::milliseconds(73));
19-
EXPECT_CALL(clock, currentTime()).WillOnce(Return(time1));
19+
EXPECT_CALL(clock, currentSteadyTime()).WillOnce(Return(time1));
2020
ASSERT_EQ(timer.value(), 0.023);
2121

2222
std::chrono::steady_clock::time_point time2(std::chrono::milliseconds(15432));
23-
EXPECT_CALL(clock, currentTime()).WillOnce(Return(time2));
23+
EXPECT_CALL(clock, currentSteadyTime()).WillOnce(Return(time2));
2424
ASSERT_EQ(timer.value(), 15.382);
2525

2626
std::chrono::steady_clock::time_point time3(std::chrono::milliseconds(16025));
27-
EXPECT_CALL(clock, currentTime()).WillOnce(Return(time3));
27+
EXPECT_CALL(clock, currentSteadyTime()).WillOnce(Return(time3));
2828
timer.reset();
2929

3030
std::chrono::steady_clock::time_point time4(std::chrono::milliseconds(24632));
31-
EXPECT_CALL(clock, currentTime()).WillOnce(Return(time4));
31+
EXPECT_CALL(clock, currentSteadyTime()).WillOnce(Return(time4));
3232
ASSERT_EQ(timer.value(), 8.607);
3333
}

0 commit comments

Comments
 (0)