Skip to content

Commit 7fb063d

Browse files
cacharleKevinEady
andauthored
feat: add Date::New overload for a std::chrono::system_clock::time_point (#1705)
Co-authored-by: Kevin Eady <8634912+KevinEady@users.noreply.github.com>
1 parent 7b8d69e commit 7fb063d

File tree

5 files changed

+38
-1
lines changed

5 files changed

+38
-1
lines changed

doc/date.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,20 @@ static Napi::Date Napi::Date::New(Napi::Env env, double value);
3737

3838
Returns 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

napi-inl.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff 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+
12021209
inline void Date::CheckCast(napi_env env, napi_value value) {
12031210
NAPI_CHECK(value != nullptr, "Date::CheckCast", "empty value");
12041211

napi.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
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.

test/date.cc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff 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+
1419
Value IsDate(const CallbackInfo& info) {
1520
Date input = info[0].As<Date>();
1621

@@ -35,6 +40,8 @@ Value OperatorValue(const CallbackInfo& info) {
3540
Object 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);

test/date.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff 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);

0 commit comments

Comments
 (0)