Skip to content

Commit 74898a0

Browse files
hoxyqfacebook-github-bot
authored andcommitted
Add toChronoSteadyClockTimePoint and fromChronoSteadyClockTimePoint converters (facebook#51455)
Summary: Pull Request resolved: facebook#51455 # Changelog: [Internal] The main idea is that subsystems who might use a different time origin (the starting point of the whole timeline of events), can use `toChronoSteadyClockTimePoint` method to get raw `std::chrono::steady_clock::time_point` and then offset it by some arbitrary epoch: be it unix time origin or `std::chrono::steady_clock::epoch`. `fromChronoSteadyClockTimePoint` can be used to convert time stamps from external systems, like Hermes. Differential Revision: D74892329
1 parent 36df97f commit 74898a0

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

packages/react-native/ReactCommon/react/timing/primitives.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,20 @@ class HighResTimeStamp {
238238
.toDOMHighResTimeStamp();
239239
}
240240

241+
// This method is expected to be used only when converting time stamps from
242+
// external systems.
243+
static constexpr HighResTimeStamp fromChronoSteadyClockTimePoint(
244+
std::chrono::steady_clock::time_point chronoTimePoint) {
245+
return HighResTimeStamp(chronoTimePoint);
246+
}
247+
248+
// This method is provided for convenience, if you need to convert
249+
// HighResTimeStamp to some common epoch with time stamps from other sources.
250+
constexpr std::chrono::steady_clock::time_point toChronoSteadyClockTimePoint()
251+
const {
252+
return chronoTimePoint_;
253+
}
254+
241255
constexpr bool operator==(const HighResTimeStamp& rhs) const {
242256
return chronoTimePoint_ == rhs.chronoTimePoint_;
243257
}

packages/react-native/ReactCommon/react/timing/tests/PrimitivesTest.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,4 +128,11 @@ TEST(HighResTimeStamp, ComparisonOperators) {
128128
EXPECT_FALSE(now >= later);
129129
}
130130

131+
TEST(HighResTimeStamp, SteadyClockTimePointConversion) {
132+
[[maybe_unused]] auto timestamp =
133+
HighResTimeStamp::now().toChronoSteadyClockTimePoint();
134+
135+
EXPECT_TRUE(decltype(timestamp)::clock::is_steady);
136+
}
137+
131138
} // namespace facebook::react

0 commit comments

Comments
 (0)