Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions napi-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
#endif // NAPI_HAS_THREADS
#include <type_traits>
#include <utility>
#if __cplusplus >= 201103L
#include <chrono>
#endif

#if defined(__clang__) || defined(__GNUC__)
#define NAPI_NO_SANITIZE_VPTR __attribute__((no_sanitize("vptr")))
Expand Down Expand Up @@ -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<double>(
duration_cast<milliseconds>(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");

Expand Down
9 changes: 9 additions & 0 deletions napi.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
#endif // NAPI_HAS_THREADS
#include <string>
#include <vector>
#if __cplusplus >= 201103L
#include <chrono>
#endif

// VS2015 RTM has bugs with constexpr, so require min of VS2015 Update 3 (known
// good version)
Expand Down Expand Up @@ -685,6 +688,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.
Expand Down
12 changes: 12 additions & 0 deletions test/date.cc
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
#if __cplusplus >= 201103L
#include <chrono>
#endif

#include "napi.h"

using namespace Napi;
Expand All @@ -11,6 +15,13 @@ Value CreateDate(const CallbackInfo& info) {
return Date::New(info.Env(), input);
}

#if __cplusplus >= 201103L
Value CreateDateFromTimePoint(const CallbackInfo& info) {
auto input = std::chrono::system_clock::time_point{};
return Date::New(info.Env(), input);
}
#endif

Value IsDate(const CallbackInfo& info) {
Date input = info[0].As<Date>();

Expand All @@ -35,6 +46,7 @@ Value OperatorValue(const CallbackInfo& info) {
Object InitDate(Env env) {
Object exports = Object::New(env);
exports["CreateDate"] = Function::New(env, CreateDate);
exports["CreateDateFromTimePoint"] = Function::New(env, CreateDate);
exports["IsDate"] = Function::New(env, IsDate);
exports["ValueOf"] = Function::New(env, ValueOf);
exports["OperatorValue"] = Function::New(env, OperatorValue);
Expand Down
1 change: 1 addition & 0 deletions test/date.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ function test (binding) {
OperatorValue
} = binding.date;
assert.deepStrictEqual(CreateDate(0), new Date(0));
assert.deepStrictEqual(CreateDateFromTimePoint(), new Date(0));
assert.strictEqual(IsDate(new Date(0)), true);
assert.strictEqual(ValueOf(new Date(42)), 42);
assert.strictEqual(OperatorValue(new Date(42)), true);
Expand Down