Skip to content

Date detection doesn't work for Dates from other realms #441

@michaelwallabi

Description

@michaelwallabi

Describe the bug

  1. Run a query that returns a date using duckdb in a node environment, say something like await db.all("SELECT DATE '2022-01-01' as dt")
  2. stringify the result

Expected:

1640995200000

Actual:

"""2022-01-01T00:00:00.000Z"""

To Reproduce

   import { Database } from 'duckdb-async';
 
    Database.create(":memory:")
    const t = await db.all("SELECT DATE '2022-01-01' as dt");

    // expect: 1640995200000
    console.warn(stringify(t));
    // actual: """2022-01-01T00:00:00.000Z"""

    // expect: 1640995200000
    console.warn(stringify(t, { cast: { date: (x) => x.getTime().toString() } }));
    // actual: """2022-01-01T00:00:00.000Z"""

    // expect: 1640995200000
    console.warn(stringify(t, { cast: { object: (x) => x.getTime().toString() } }));
    // actual: 1640995200000

Additional context

Some discussion in this stack overflow answer: https://stackoverflow.com/questions/643782/how-to-check-whether-an-object-is-a-date

Looks like the issue is that instanceof Date only works within the same frame/realm.

date-fns handles it this way.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions