node-postgres icon indicating copy to clipboard operation
node-postgres copied to clipboard

parseInputDatesAsUTC is not documented

Open djmitche opened this issue 5 years ago • 7 comments

Per #783, #1746, and a few other TZ-related issues, it seems that node-postgres interprets JS Date objects as in the local timezone, unless pg.defaults.parseInputDatesAsUTC is true.

This behavior is, I think, surprising for those of us building server software, where using UTC everywhere is a general best practice. It's even more surprising since the behavior depends on the local system timezone, so only appears when tests are run on a non-UTC system, meaning tests will pass in CI and fail on development systems.

It would be great if this behavior, and the default, was documented.

djmitche avatar Mar 20 '20 17:03 djmitche

I've been struggling with something related in my app code lately and this would be helpful. I've got some TIMESTAMP fields (WITHOUT TIME ZONE) and I store UTC values (using moment.utc()) but was getting back different offsets in my node app code. A co-worker found this workaround:

https://60devs.com/working-with-postgresql-timestamp-without-timezone-in-node.html

Which I'd like to avoid so it's nice to see this is configurable within node-postgres itself but it's hard to know that if it's not documented.

mriedem avatar May 15 '20 14:05 mriedem

This would probably be a good place to mention this:

https://node-postgres.com/features/types

In the date / timestamp / timestamptz section because it does mention:

node-postgres converts DATE and TIMESTAMP columns into the local time of the node process set at process.env.TZ.

Looks like this issue should be moved to the node-postgres-docs repo though because that's where the docs live:

https://github.com/brianc/node-postgres-docs/blob/master/content/features/5-types.mdx

mriedem avatar May 15 '20 16:05 mriedem

Why library is trying to parse the date to local time of the server, when the column type is just Date, no time.

bbroniewski avatar Oct 11 '21 21:10 bbroniewski

Hi @bbroniewski -- I suspect it was unintentional, but language like "why the hell' can read as aggressive and insulting to open-source developers. We can fix bugs while still being kind to each other, and without such language.

And on that topic, it appears that @lolopinto has addressed this bug, although perhaps only for the timestamp type. Hopefully they can provide some detail here: is there more to fix in this issue?

djmitche avatar Oct 11 '21 21:10 djmitche

Hi @djmitche sorry for that. I think it is mentioned as to be done in version 4 of types for pg, but it is like this since few years I feel. I see the same issues in 2016.

I did a custom parser like below:

import { types } from 'pg';

types.setTypeParser(types.builtins.DATE, function(val) {
    try{
        return new Date(Date.UTC(parseInt(val.substring(0, 4), 10), parseInt(val.substring(5, 7)), parseInt(val.substring(8, 10)) ));
    } catch {
        return null;
    }
});

bbroniewski avatar Oct 11 '21 23:10 bbroniewski

holy, didn't expect to find a 3yo issue after an hour of stepping through code

ilibar-zpt avatar Oct 11 '23 11:10 ilibar-zpt