Created
April 24, 2019 09:12
-
-
Save Ciantic/f11395395c748ce2a7a10e1147a02aac to your computer and use it in GitHub Desktop.
node sqlite3 uses getTime (unixepoch as milliseconds) for default date to integer conversion
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sqlite3 from "sqlite3"; | |
let db: any; | |
beforeEach(done => { | |
db = new sqlite3.Database(":memory:", () => { | |
db.run("CREATE TABLE foo (num INT)", done); | |
}); | |
}); | |
test("Should insert datetime as dateObject.getTime() and retrieve it as so", done => { | |
db.run( | |
"INSERT INTO foo VALUES($date)", | |
{ | |
$date: new Date("2000-01-01T02:00Z") | |
}, | |
(err: any) => { | |
db.all("SELECT * from foo", (err: any, rows: any) => { | |
expect(new Date("2000-01-01T02:00Z").getTime()).toEqual(946692000000); | |
expect(rows[0].num).toEqual(946692000000); | |
expect(new Date(rows[0].num)).toEqual(new Date("2000-01-01T02:00Z")); | |
done(); | |
}); | |
} | |
); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Note to myself, this is probably a side effect from Date to integer conversion in JavaScript engines, especially Chrome's. Putting this to console:
Results in an integer
946692000000
.