Skip to content

Commit

Permalink
Fixing date to time conversion (#94)
Browse files Browse the repository at this point in the history
* Fixing date to time conversion

* Update src/conversion.rs

Co-authored-by: Cory Grinstead <universalmind.candy@gmail.com>

---------

Co-authored-by: Darek <dchrostowski@medallia.com>
Co-authored-by: Cory Grinstead <universalmind.candy@gmail.com>
  • Loading branch information
3 people committed Aug 20, 2023
1 parent e6c1edb commit 534f19b
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
17 changes: 17 additions & 0 deletions __tests__/dataframe.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1575,6 +1575,23 @@ describe("io", () => {
const actual = df.toRecords();
expect(actual).toEqual(expected);
});
test("toRecords:date", () => {
const df = pl
.DataFrame({
date: [new Date()],
})
.withColumn(pl.col("date").cast(pl.Date).alias("date"));
const dt = new Date();
const expected = [
{
date: new Date(
Date.UTC(dt.getFullYear(), dt.getMonth(), dt.getDate(), 0, 0, 0, 0),
),
},
];
const actual = df.toRecords();
expect(JSON.stringify(actual)).toEqual(JSON.stringify(expected));
});
test("toObject", () => {
const expected = {
foo: [1],
Expand Down
5 changes: 4 additions & 1 deletion src/conversion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,11 @@ impl<'a> ToNapiValue for Wrap<AnyValue<'a>> {
AnyValue::Date(v) => {
let mut ptr = std::ptr::null_mut();

// Multiple days to get to Epoch time
let epoch_time: f64 = (v as f64) * 86400000.0;

check_status!(
napi::sys::napi_create_date(env, v as f64, &mut ptr),
napi::sys::napi_create_date(env, epoch_time, &mut ptr),
"Failed to convert rust type `AnyValue::Date` into napi value",
)?;

Expand Down

0 comments on commit 534f19b

Please sign in to comment.