From 5ecb61657014714df256da1f3140aa868791cf36 Mon Sep 17 00:00:00 2001 From: Charles Cabergs Date: Fri, 30 Jan 2026 18:16:36 +0100 Subject: [PATCH] Add Date::New overload for a std::chrono::system_clock::time_point --- napi-inl.h | 12 ++++++++++++ napi.h | 6 ++++++ 2 files changed, 18 insertions(+) diff --git a/napi-inl.h b/napi-inl.h index 0f1717ecd..e9fcf82bc 100644 --- a/napi-inl.h +++ b/napi-inl.h @@ -20,6 +20,9 @@ #endif // NAPI_HAS_THREADS #include #include +#if __cplusplus >= 201103L +#include +#endif #if defined(__clang__) || defined(__GNUC__) #define NAPI_NO_SANITIZE_VPTR __attribute__((no_sanitize("vptr"))) @@ -1199,6 +1202,15 @@ inline Date Date::New(napi_env env, double val) { return Date(env, value); } +#if __cplusplus >= 201103L +inline Date Date::New(napi_env env, std::chrono::system_clock::time_point tp) { + using namespace std::chrono; + auto ms = static_cast( + duration_cast(tp.time_since_epoch()).count()); + return Date::New(env, ms); +} +#endif + inline void Date::CheckCast(napi_env env, napi_value value) { NAPI_CHECK(value != nullptr, "Date::CheckCast", "empty value"); diff --git a/napi.h b/napi.h index 013a9114d..50dc65bb2 100644 --- a/napi.h +++ b/napi.h @@ -685,6 +685,12 @@ class Date : public Value { double value ///< Number value ); + /// Creates a new Date value from a std::chrono::system_clock::time_point. + static Date New( + napi_env env, ///< Node-API environment + std::chrono::system_clock::time_point tp ///< Time point value + ); + static void CheckCast(napi_env env, napi_value value); Date(); ///< Creates a new _empty_ Date instance.