diff --git a/packages/react-native/ReactCommon/react/timing/primitives.h b/packages/react-native/ReactCommon/react/timing/primitives.h index 1c39e303f5c918..91e5c33ed57830 100644 --- a/packages/react-native/ReactCommon/react/timing/primitives.h +++ b/packages/react-native/ReactCommon/react/timing/primitives.h @@ -238,6 +238,20 @@ class HighResTimeStamp { .toDOMHighResTimeStamp(); } + // This method is expected to be used only when converting time stamps from + // external systems. + static constexpr HighResTimeStamp fromChronoSteadyClockTimePoint( + std::chrono::steady_clock::time_point chronoTimePoint) { + return HighResTimeStamp(chronoTimePoint); + } + + // This method is provided for convenience, if you need to convert + // HighResTimeStamp to some common epoch with time stamps from other sources. + constexpr std::chrono::steady_clock::time_point toChronoSteadyClockTimePoint() + const { + return chronoTimePoint_; + } + constexpr bool operator==(const HighResTimeStamp& rhs) const { return chronoTimePoint_ == rhs.chronoTimePoint_; } diff --git a/packages/react-native/ReactCommon/react/timing/tests/PrimitivesTest.cpp b/packages/react-native/ReactCommon/react/timing/tests/PrimitivesTest.cpp index bde37c28551c45..be1751c2bff3d9 100644 --- a/packages/react-native/ReactCommon/react/timing/tests/PrimitivesTest.cpp +++ b/packages/react-native/ReactCommon/react/timing/tests/PrimitivesTest.cpp @@ -128,4 +128,11 @@ TEST(HighResTimeStamp, ComparisonOperators) { EXPECT_FALSE(now >= later); } +TEST(HighResTimeStamp, SteadyClockTimePointConversion) { + [[maybe_unused]] auto timestamp = + HighResTimeStamp::now().toChronoSteadyClockTimePoint(); + + EXPECT_TRUE(decltype(timestamp)::clock::is_steady); +} + } // namespace facebook::react