-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Client returns wrong date, when the date in Postgres is declared as date-type #818
Comments
The node-postgres team decided long ago to convert dates and datetimes The good news is its trivially easy to over-ride this behavior and return There's documentation on how to do this here: There's probably even a module somewhere that will convert dates from On Fri, Jul 24, 2015 at 6:36 AM, Valentin Heinitz notifications@github.com
|
I'm affraid, trying to convert date objects to timestamps doesn't work wll in all use-cases. There are dates like birth-dates or historical events, which have no time-relevance. If one client writes let's say 1973-09-11 and another reads 1973-09-10 it is bad. The date 1973-09-11 is a historical event. May be, that from Australian point of view it happened a day before or a day after, but even in their historical books the date is provided as 1973-09-11. |
Yeah you're totally right about that if you're storing them for that On Fri, Jul 24, 2015 at 12:19 PM, Valentin Heinitz <notifications@github.com
|
Thank you very much, Brian for you quick response and suggestions! I'll check the opportunities you've mentioned. |
This is workaround what i did: /* Node postgres date fix / |
…ateStr + 'T00:00:00Z') /* text of issue denodrivers#181 copied */ Currently I get my dates (defined with datatype DATE) back as f.e. "2021-03-02T23:00:00.000Z" while pgadmin gives 2021-03-03. So I get the wrong date back (and a timestamp was added). You can find the same issue in node-postgres brianc/node-postgres#818 where someone suggests a workaround in the end (that I just tested, it works). brianc/node-postgres#818 , see a paste of the snippet below. Can this be fixed in deno-postgres? My proposal would be to change decodeDate in decode.ts to simply function decodeDate(dateStr: string): Date { return new Date(dateStr + 'T00:00:00Z'); } of course that works for year 0001 too. ;-) For null values in the postgress date the function decodeDate is not executed. /* Node postgres date fix / var Moment = require('moment'); var parseDate = function parseDate(val) { return val === null ? null : Moment(val).format('YYYY-MM-DD') }; var types = pg.types; var DATATYPE_DATE = 1082; types.setTypeParser(DATATYPE_DATE, function(val) { return val === null ? null : parseDate(val) }); / Node postgres date fix - end */
This behaviour is crazy to me. In almost all cases you should be using Storing without timezone for future dates protects againsts changes to DST that can and do happen before the future date arrives. Since pg is parsing I think the main problem here is that pg coerces both of these types into a Date. It would be super weird if For anyone running into similar issues, I ended up bypassing the parser for import pg from 'pg'
pg.types.setTypeParser(1114, function (value) {
return value
}) |
@ashconnell thank you! You have to bypass the date parser also: pg.types.setTypeParser(1082, function(value) { //date
return value;
}); |
There are built-in constants for pg.types.setTypeParser(pg.types.builtins.DATE, value => value)
pg.types.setTypeParser(pg.types.builtins.TIMESTAMP, value => value)
pg.types.setTypeParser(pg.types.builtins.TIMESTAMPTZ, value => value) |
I have declared a date column in Postgres as date.
When I write the value with node's pg module, the Postgres Tool pgAdmin displays it correctly.
When I read the value back using pg, Instead of plain date, a date-time string comes with wrong day.
e.g.:
Date inserted: 1975-05-11
Date displayed by pgAdmin: 1975-05-11
Date returned by node's pg: 1975-05-10T23:00:00.000Z
The error seems to be due to the different handling of time-zone during parsing from string and converting the Date-object back to string.
I hope this post may help: SO
The text was updated successfully, but these errors were encountered: