Skip to content

Commit 22885cc

Browse files
committed
date conversion
1 parent 88d140a commit 22885cc

File tree

2 files changed

+62
-3
lines changed

2 files changed

+62
-3
lines changed

bindings/src/duckdb_node_bindings.cpp

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1014,21 +1014,41 @@ class DuckDBNodeAddon : public Napi::Addon<DuckDBNodeAddon> {
10141014
// function from_date(date: Date_): DateParts
10151015
Napi::Value from_date(const Napi::CallbackInfo& info) {
10161016
auto env = info.Env();
1017-
throw Napi::Error::New(env, "Not implemented yet");
1017+
auto date_obj = info[0].As<Napi::Object>();
1018+
auto days = date_obj.Get("days").As<Napi::Number>().Int32Value();
1019+
duckdb_date date = { days };
1020+
auto date_parts = duckdb_from_date(date);
1021+
auto result = Napi::Object::New(env);
1022+
result.Set("year", Napi::Number::New(env, date_parts.year));
1023+
result.Set("month", Napi::Number::New(env, date_parts.month));
1024+
result.Set("day", Napi::Number::New(env, date_parts.day));
1025+
return result;
10181026
}
10191027

10201028
// DUCKDB_API duckdb_date duckdb_to_date(duckdb_date_struct date);
10211029
// function to_date(parts: DateParts): Date_
10221030
Napi::Value to_date(const Napi::CallbackInfo& info) {
10231031
auto env = info.Env();
1024-
throw Napi::Error::New(env, "Not implemented yet");
1032+
auto date_parts_obj = info[0].As<Napi::Object>();
1033+
int32_t year = date_parts_obj.Get("year").As<Napi::Number>().Int32Value();
1034+
int8_t month = date_parts_obj.Get("month").As<Napi::Number>().Int32Value();
1035+
int8_t day = date_parts_obj.Get("day").As<Napi::Number>().Int32Value();
1036+
duckdb_date_struct date_parts { year, month, day };
1037+
auto date = duckdb_to_date(date_parts);
1038+
auto result = Napi::Object::New(env);
1039+
result.Set("days", Napi::Number::New(env, date.days));
1040+
return result;
10251041
}
10261042

10271043
// DUCKDB_API bool duckdb_is_finite_date(duckdb_date date);
10281044
// function is_finite_date(date: Date_): boolean
10291045
Napi::Value is_finite_date(const Napi::CallbackInfo& info) {
10301046
auto env = info.Env();
1031-
throw Napi::Error::New(env, "Not implemented yet");
1047+
auto date_obj = info[0].As<Napi::Object>();
1048+
auto days = date_obj.Get("days").As<Napi::Number>().Int32Value();
1049+
duckdb_date date = { days };
1050+
auto is_finite = duckdb_is_finite_date(date);
1051+
return Napi::Boolean::New(env, is_finite);
10321052
}
10331053

10341054
// DUCKDB_API duckdb_time_struct duckdb_from_time(duckdb_time time);

bindings/test/conversion.test.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
2+
import duckdb from '@duckdb/node-bindings';
3+
import { expect, suite, test } from 'vitest';
4+
5+
suite('conversion', () => {
6+
suite('from_date', () => {
7+
test('mid-range', () => {
8+
expect(duckdb.from_date({ days: 19877 })).toStrictEqual({ year: 2024, month: 6, day: 3 });
9+
});
10+
test('max', () => {
11+
expect(duckdb.from_date({ days: 0x7FFFFFFE })).toStrictEqual({ year: 5881580, month: 7, day: 10 });
12+
});
13+
test('min', () => {
14+
expect(duckdb.from_date({ days: -0x7FFFFFFE })).toStrictEqual({ year: -5877641, month: 6, day: 25 });
15+
});
16+
});
17+
suite('to_date', () => {
18+
test('mid-range', () => {
19+
expect(duckdb.to_date({ year: 2024, month: 6, day: 3 })).toStrictEqual({ days: 19877 });
20+
});
21+
test('max', () => {
22+
expect(duckdb.to_date({ year: 5881580, month: 7, day: 10 })).toStrictEqual({ days: 0x7FFFFFFE });
23+
});
24+
test('min', () => {
25+
expect(duckdb.to_date({ year: -5877641, month: 6, day: 25 })).toStrictEqual({ days: -0x7FFFFFFE });
26+
});
27+
});
28+
suite('is_finite_date', () => {
29+
test('finite', () => {
30+
expect(duckdb.is_finite_date({ days: 19877 })).toBe(true);
31+
});
32+
test('infinity', () => {
33+
expect(duckdb.is_finite_date({ days: 0x7FFFFFFF })).toBe(false);
34+
});
35+
test('-infinity', () => {
36+
expect(duckdb.is_finite_date({ days: -0x7FFFFFFF })).toBe(false);
37+
});
38+
});
39+
});

0 commit comments

Comments
 (0)