File tree Expand file tree Collapse file tree 5 files changed +38
-1
lines changed
Expand file tree Collapse file tree 5 files changed +38
-1
lines changed Original file line number Diff line number Diff line change @@ -37,6 +37,20 @@ static Napi::Date Napi::Date::New(Napi::Env env, double value);
3737
3838Returns a new instance of ` Napi::Date ` object.
3939
40+ ### New
41+
42+ Creates a new instance of a ` Napi::Date ` object.
43+
44+ ``` cpp
45+ static Napi::Date Napi::Date::New (napi_env env, std::chrono::system_clock::time_point time_point);
46+ ```
47+
48+ - `[in] env`: The environment in which to construct the `Napi::Date` object.
49+ - `[in] value`: The point in time, represented by an
50+ `std::chrono::system_clock::time_point`.
51+
52+ Returns a new instance of `Napi::Date` object.
53+
4054### ValueOf
4155
4256```cpp
Original file line number Diff line number Diff line change @@ -1199,6 +1199,13 @@ inline Date Date::New(napi_env env, double val) {
11991199 return Date (env, value);
12001200}
12011201
1202+ inline Date Date::New (napi_env env, std::chrono::system_clock::time_point tp) {
1203+ using namespace std ::chrono;
1204+ auto ms = static_cast <double >(
1205+ duration_cast<milliseconds>(tp.time_since_epoch ()).count ());
1206+ return Date::New (env, ms);
1207+ }
1208+
12021209inline void Date::CheckCast (napi_env env, napi_value value) {
12031210 NAPI_CHECK (value != nullptr , " Date::CheckCast" , " empty value" );
12041211
Original file line number Diff line number Diff line change 1717#if NAPI_HAS_THREADS
1818#include < mutex>
1919#endif // NAPI_HAS_THREADS
20+ #include < chrono>
2021#include < string>
2122#include < vector>
2223
@@ -685,6 +686,12 @@ class Date : public Value {
685686 double value // /< Number value
686687 );
687688
689+ // / Creates a new Date value from a std::chrono::system_clock::time_point.
690+ static Date New (
691+ napi_env env, // /< Node-API environment
692+ std::chrono::system_clock::time_point time_point // /< Time point value
693+ );
694+
688695 static void CheckCast (napi_env env, napi_value value);
689696
690697 Date (); // /< Creates a new _empty_ Date instance.
Original file line number Diff line number Diff line change @@ -11,6 +11,11 @@ Value CreateDate(const CallbackInfo& info) {
1111 return Date::New (info.Env (), input);
1212}
1313
14+ Value CreateDateFromTimePoint (const CallbackInfo& info) {
15+ auto input = std::chrono::system_clock::time_point{};
16+ return Date::New (info.Env (), input);
17+ }
18+
1419Value IsDate (const CallbackInfo& info) {
1520 Date input = info[0 ].As <Date>();
1621
@@ -35,6 +40,8 @@ Value OperatorValue(const CallbackInfo& info) {
3540Object InitDate (Env env) {
3641 Object exports = Object::New (env);
3742 exports[" CreateDate" ] = Function::New (env, CreateDate);
43+ exports[" CreateDateFromTimePoint" ] =
44+ Function::New (env, CreateDateFromTimePoint);
3845 exports[" IsDate" ] = Function::New (env, IsDate);
3946 exports[" ValueOf" ] = Function::New (env, ValueOf);
4047 exports[" OperatorValue" ] = Function::New (env, OperatorValue);
Original file line number Diff line number Diff line change @@ -9,9 +9,11 @@ function test (binding) {
99 CreateDate,
1010 IsDate,
1111 ValueOf,
12- OperatorValue
12+ OperatorValue,
13+ CreateDateFromTimePoint
1314 } = binding . date ;
1415 assert . deepStrictEqual ( CreateDate ( 0 ) , new Date ( 0 ) ) ;
16+ assert . deepStrictEqual ( CreateDateFromTimePoint ( ) , new Date ( 0 ) ) ;
1517 assert . strictEqual ( IsDate ( new Date ( 0 ) ) , true ) ;
1618 assert . strictEqual ( ValueOf ( new Date ( 42 ) ) , 42 ) ;
1719 assert . strictEqual ( OperatorValue ( new Date ( 42 ) ) , true ) ;
You can’t perform that action at this time.
0 commit comments