question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Mismatch in Date precision causes odd behavior

See original GitHub issue

The PostgreSQL TIMESTAMP type has a maximum precision of 1 microsecond, but the JavaScript Date type only has a maximum precision of 1 millisecond. This causes information to be silently discarded, leading to bizarre behavior:

client.query("SELECT '2017-01-01 00:00:00.00001'::timestamp as d", (err, res) => {
let date = res.rows[0].d;

client.query("SELECT '2017-01-01 00:00:00.00001'::timestamp = $1 as eq,\
'2017-01-01 00:00:00.00001'::timestamp > $1 as gt", [date], (err, res) => {
console.log(res.rows[0].eq); //false
console.log(res.rows[0].gt); //true
});
});

I’m not sure this a bug that could be fixed without using a non-standard Date object, but even if it isn’t fixed it should be at least noted in the documentation.

Issue Analytics

  • State:open
  • Created 7 years ago
  • Reactions:1
  • Comments:9 (3 by maintainers)

github_iconTop GitHub Comments

43reactions
williamsteincommented, Feb 7, 2017

For what it’s worth, I probably wasted about 60 hours last week, and cause a lot of pain to users, because there wasn’t a sentence about this in the main README.md page. I did systematically read through the fine README before starting to use the driver, and if there was a warning about timestamps silently rounding by “textually deleting the last 3 digits” instead of rounding, it would have helped. (And other than this, I really like this driver!)

I think the behavior of the driver is perhaps more surprising than your examples above suggest. What happens with the driver is completely different than what happens using postgresql’s ::timestamp(3) explicit cast, which rounds to the nearest timestamp, rather than just textually removing the last 3 digits.

8reactions
vitaly-tcommented, Jan 21, 2017

JavaScript doesn’t support microseconds, so wouldn’t this be self-implying for a JavaScript library?

No point documenting the obvious, imo.

And there is nothing bizarre in your example, because:

let date = res.rows[0].d;
//=> 2017-01-01T00:00:00.000Z

which makes the rest logically apparent:

2017-01-01 00:00:00.00001 = 2017-01-01T00:00:00.000Z => false
2017-01-01 00:00:00.00001 > 2017-01-01T00:00:00.000Z => true
Read more comments on GitHub >

github_iconTop Results From Across the Web

The Model Performance Mismatch Problem (and what to do ...
After reading this post, you will know: The problem of model performance mismatch that may occur when evaluating machine learning algorithms.
Read more >
Why does Date.parse give incorrect results? - Stack Overflow
The value will be different in different time zones. This behaviour is specified in ECMA-262 so all browsers do it the same way....
Read more >
Is FindRoot wrong about its WorkingPrecision?
I noticed an odd mismatch between the behaviors of NDSolve and FindRoot. If you give NDSolve an equation of a certain precision, say...
Read more >
MANAGING MISMATCH BETWEEN BELIEF AND BEHAVIOR
Her statements about racial equality do not express beliefs of hers, and we need some other story to explain why she makes those...
Read more >
WEIRD bodies: mismatch, medicine and missing diversity
Evolution and Human Behavior ... WEIRD bodies: mismatch, medicine and missing diversity ... Next we discuss evolutionary mismatch, reaction norms and ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found