Skip to content

Commit 92cdb01

Browse files
committed
feat: timezone_tz map to Date in nodejs binding.
1 parent 6cbd8e5 commit 92cdb01

File tree

2 files changed

+24
-18
lines changed

2 files changed

+24
-18
lines changed

bindings/nodejs/src/lib.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@ use std::sync::Arc;
2424
use std::{collections::HashMap, path::Path};
2525
use tokio_stream::StreamExt;
2626

27-
const TIMESTAMP_TIMEZONE_FORMAT: &str = "%Y-%m-%d %H:%M:%S%.6f %z";
28-
2927
static VERSION: Lazy<String> = Lazy::new(|| {
3028
let version = option_env!("CARGO_PKG_VERSION").unwrap_or("unknown");
3129
version.to_string()
@@ -326,8 +324,13 @@ impl ToNapiValue for Value<'_> {
326324
Ok(js_date)
327325
}
328326
databend_driver::Value::TimestampTz(dt) => {
329-
let formatted = dt.strftime(TIMESTAMP_TIMEZONE_FORMAT);
330-
String::to_napi_value(env, formatted.to_string())
327+
let mut js_date = std::ptr::null_mut();
328+
let millis = dt.timestamp().as_millisecond() as f64;
329+
check_status!(
330+
unsafe { sys::napi_create_date(env, millis, &mut js_date) },
331+
"Failed to convert jiff timestamp into napi value",
332+
)?;
333+
Ok(js_date)
331334
}
332335
databend_driver::Value::Date(_) => {
333336
let inner = val.inner.clone();

bindings/nodejs/tests/binding.js

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ Given("A new Databend Driver Client", async function () {
5252
assert.fail("No connection returned");
5353
}
5454
this.conn = conn;
55+
conn.exec("set timezone='Asia/Shanghai'")
5556
});
5657

5758
Then("Select string {string} should be equal to {string}", async function (input, output) {
@@ -143,7 +144,7 @@ Then("Select types should be expected native types", async function () {
143144
const row = await this.conn.queryRow(
144145
"SELECT to_datetime('2020-01-01 12:34:56.789'), to_datetime('2020-01-02 12:34:56.789')",
145146
);
146-
assert.deepEqual(row.values(), [new Date("2020-01-01T12:34:56.789Z"), new Date("2020-01-02T12:34:56.789Z")]);
147+
assert.deepEqual(row.values(), [new Date("2020-01-01T04:34:56.789Z"), new Date("2020-01-02T04:34:56.789Z")]);
147148
}
148149

149150
// VARCHAR
@@ -166,8 +167,10 @@ Then("Select types should be expected native types", async function () {
166167

167168
// TUPLE
168169
{
169-
const row = await this.conn.queryRow(`SELECT (10, '20', to_datetime('2024-04-16 12:34:56.789'))`);
170-
assert.deepEqual(row.values(), [[10, "20", new Date("2024-04-16T12:34:56.789Z")]]);
170+
const row = await this.conn.queryRow(
171+
`SELECT (10, '20', to_datetime('2024-04-16 12:34:56.789'))`,
172+
);
173+
assert.deepEqual(row.values(), [[10, "20", new Date("2024-04-16T04:34:56.789Z")]]);
171174
}
172175

173176
// MAP
@@ -220,7 +223,7 @@ Then("Select types should be expected native types", async function () {
220223
// TimestampTz
221224
if (!(DRIVER_VERSION > [0, 30, 3] && DB_VERSION >= [1, 2, 836])) {
222225
const row = await this.conn.queryRow(`SELECT to_datetime_tz('2024-04-16 12:34:56.789 +0800'))`);
223-
assert.deepEqual(row.values(), ["2024-04-16T12:34:56.789 +0800"]);
226+
assert.deepEqual(row.values(), [new Date("2024-04-16 04:34:56.789Z")]);
224227
}
225228
});
226229

@@ -310,9 +313,9 @@ Then("Insert and Select should be equal", async function () {
310313
ret.push(row.values());
311314
}
312315
const expected = [
313-
[-1, 1, 1.0, "'", null, new Date("2011-03-06"), new Date("2011-03-06T06:20:00Z")],
314-
[-2, 2, 2.0, '"', "", new Date("2012-05-31"), new Date("2012-05-31T11:20:00Z")],
315-
[-3, 3, 3.0, "\\", "NULL", new Date("2016-04-04"), new Date("2016-04-04T11:30:00Z")],
316+
[-1, 1, 1.0, "'", null, new Date("2011-03-06"), new Date("2011-03-05T22:20:00Z")],
317+
[-2, 2, 2.0, '"', "", new Date("2012-05-31"), new Date("2012-05-31T03:20:00Z")],
318+
[-3, 3, 3.0, "\\", "NULL", new Date("2016-04-04"), new Date("2016-04-04T03:30:00Z")],
316319
];
317320
assert.deepEqual(ret, expected);
318321
});
@@ -353,16 +356,16 @@ Then("Load file with Stage and Select should be equal", async function () {
353356
ret.push(row.values());
354357
}
355358
const expected = [
356-
[-1, 1, 1.0, "'", null, new Date("2011-03-06"), new Date("2011-03-06T06:20:00Z")],
357-
[-2, 2, 2.0, '"', null, new Date("2012-05-31"), new Date("2012-05-31T11:20:00Z")],
358-
[-3, 3, 3.0, "\\", "NULL", new Date("2016-04-04"), new Date("2016-04-04T11:30:00Z")],
359+
[-1, 1, 1.0, "'", null, new Date("2011-03-06"), new Date("2011-03-05T22:20:00Z")],
360+
[-2, 2, 2.0, '"', null, new Date("2012-05-31"), new Date("2012-05-31T03:20:00Z")],
361+
[-3, 3, 3.0, "\\", "NULL", new Date("2016-04-04"), new Date("2016-04-04T03:30:00Z")],
359362
];
360363
assert.deepEqual(ret, expected);
361364
});
362365

363366
Then("Load file with Streaming and Select should be equal", async function () {
364367
const progress = await this.conn.loadFile(
365-
`INSERT INTO test VALUES from @_databend_load file_format=(type=csv)`,
368+
`INSERT /*+SET_VAR(timezone='Asia/Shanghai')*/ INTO test VALUES from @_databend_load file_format=(type=csv)`,
366369
"tests/data/test.csv",
367370
"streaming",
368371
);
@@ -374,9 +377,9 @@ Then("Load file with Streaming and Select should be equal", async function () {
374377
ret.push(row.values());
375378
}
376379
const expected = [
377-
[-1, 1, 1.0, "'", null, new Date("2011-03-06"), new Date("2011-03-06T06:20:00Z")],
378-
[-2, 2, 2.0, '"', null, new Date("2012-05-31"), new Date("2012-05-31T11:20:00Z")],
379-
[-3, 3, 3.0, "\\", "NULL", new Date("2016-04-04"), new Date("2016-04-04T11:30:00Z")],
380+
[-1, 1, 1.0, "'", null, new Date("2011-03-06"), new Date("2011-03-05T22:20:00Z")],
381+
[-2, 2, 2.0, '"', null, new Date("2012-05-31"), new Date("2012-05-31T03:20:00Z")],
382+
[-3, 3, 3.0, "\\", "NULL", new Date("2016-04-04"), new Date("2016-04-04T03:30:00Z")],
380383
];
381384
assert.deepEqual(ret, expected);
382385
});

0 commit comments

Comments
 (0)