Wrong timestamp!
var pg = require('pg'); var conString = "..."; var client = new pg.Client(conString); client.connect(function(err) { if(err) { return console.error('could not connect to postgres', err); } var sql = "SELECT NOW() as now1, timezone('UTC'::text, now()) as now2"; client.query(sql, function(err, result) { if(err) { return console.error('error running query', err); } console.log(result.rows); client.end(); }); });
Result is: [ { now1: Thu Aug 29 2013 11:37:14 GMT+0300 (FLE Summer Time), now2: Thu Aug 29 2013 08:37:14 GMT+0300 (FLE Summer Time) } ] And must be: [ { now1: Thu Aug 29 2013 11:37:14 GMT+0300 (FLE Summer Time), now2: Thu Aug 29 2013 08:37:14 GMT+0000 (UTC) } ] or equal dates This is tested with latest version node-postgres.
IMHO, I think it's a bug, here's why:
when I do new Date("Wed Feb 11 2015 13:41:48") in javascript, it returns this date in MY TIMEZONE.
The datetime coming from postgres is in UTC, so let's say the time in the database is "Wed Feb 11 2015 13:41:48", if we get it in the code and put it in a new Date(), it gets converted to a wrong timezone. To do it correctly, we need to append the correct timezone to it (UTC).
Here's how I made use of your code, @brianc
pg = require('pg')
types = require('pg').types
timestampOID = 1114
types.setTypeParser 1114, (stringValue) ->
return new Date(Date.parse(stringValue + "+0000"))
Now this returns all my times correctly (offset to the right my computer's timezone so its .getTime() is correct)
I'd love to hear back from you guys =)
I spent many hours banging my head against my keyboard trying to understand why the dates I was seeing in my app didn't correspond with the dates in the db. I can admit that it came down to my lack of understanding about JavaScript's date handling (applying the local time zone when no time zone is present in the timestamp) and I can understand the approach taken by node-postgres after reading through this thread. Still, the default date handling is unintuitive and confusing. I expect the timestamp in my app to match what's in the db (assumed UTC), not to have it converted to local time because that's JavaScript's weird way of dealing with it.
Using @zxiest's example above, I'm now getting proper UTC timestamps, but I was really scratching my head for a while.
No issues with node-postgres otherwise, though. Very fast and stable. Thanks for all your hard work.
:) My pleasure. We've gone back and forth and back and forth on date handling. So we ended up settling on the way postgres recommends & then providing an easy way to punch over for your own needs. Dates in general are painful and sad and make me cry. I cannot recommend this lib enough if you've not already used it: http://momentjs.com/
On Mon, Apr 20, 2015 at 2:31 PM, Jay Sylvester [email protected] wrote:
I spent many hours banging my head against my keyboard trying to understand why the dates I was seeing in my app didn't correspond with the dates in the db. I can admit that it came down to my lack of understanding about JavaScript's date handling (applying the local time zone when no time zone is present in the timestamp) and I can understand the approach taken by node-postgres after reading through this thread. Still, the default date handling is unintuitive and confusing. I expect the timestamp in my app to match what's in the db (assumed UTC), not to have it converted to local time because that's JavaScript's weird way of dealing with it.
Using @zxiest https://github.com/zxiest's example above, I'm now getting proper UTC timestamps, but I was really scratching my head for a while.
No issues with node-postgres otherwise, though. Very fast and stable. Thanks for all your hard work.
— Reply to this email directly or view it on GitHub https://github.com/brianc/node-postgres/issues/429#issuecomment-94531793 .
I'm actually using Moment.js, which is an amazing library, but it was causing additional confusion because my dates were being double-converted. They'd come out of the db as UTC, get converted by node-postgres to local, then converted again because Moment thought they were still in UTC. It wasn't until I removed Moment from the chain that I saw what was happening. The date output overwrite function above combined with Moment is doing everything correctly now.
Would you consider adding a specific example to the docs regarding date handling? It's great that you have some customization docs in the wiki, but I think this is a case that could use its own section (unless it's already there and I missed it).
I actually tried to document using moment.js specifically here:
https://github.com/brianc/node-pg-types
Cool, that's helpful, thanks. I see the link in the node-postgres readme that points to it, but completely missed it when I read the readme previously.
I'm running into a similar issues that @jaysylvester shared with my dates in the DB being different than what is being output in the app. I was under the impression that as long as I stored dates with a time zone (UTC in this case) that when I retrieve them they won't be converted to local time. Or am I misunderstanding and the conversion to local time is the default behavior?
Are you storing your dates with the datatype TIMESTAMPTZ? That should
respect the timezone.
On Wed, May 20, 2015 at 2:28 PM, Joel Taylor [email protected] wrote:
I'm running into a similar issues that @jaysylvester https://github.com/jaysylvester shared with my dates in the DB being different than what is being output in the app. I was under the impression that as long as I stored dates with a time zone (UTC in this case) that when I retrieve them they won't be converted to local time. Or am I misunderstanding and the conversion to local time is the default behavior?
— Reply to this email directly or view it on GitHub https://github.com/brianc/node-postgres/issues/429#issuecomment-103987006 .
Yup! I just wanted to make sure that the timezone should be respected. It must be a bug somewhere in my code. Thanks!
var types = require('pg').types; var timestampOID = 1114; types.setTypeParser(1114, function(stringValue) { return stringValue; })
I did that and I get segmentation fault.... feeling like C
pg.types.setTypeParser(1114, function(stringValue) {
console.log(stringValue);
return new Date(Date.parse(stringValue + "+0000"));
});
that worked perfectly for me.
Is 1114 a postgresql code or pg driver code? Is there any risk of this to change in future?
http://doxygen.postgresql.org/include_2catalog_2pg__type_8h.html
search for 1114 -- it the OID for timestamp in postgres itself
ok, thanks for the help given so far with this weird behavior. so I managed to read from DB the UTC times stored there. but now, I am sending UTC to DB and in DB I see local time not UTC (although the field in DB is type timestamp -- without timezone info and I do send UTC format, +0000 or GMT at the end of the string format: I send to DB something like this new Date('Wed, 23 Apr 2014 09:54:51 GMT') and when I check the DB value it is my local time. how can I tell pg module to not convert to local time when sending/saving to DB?
What is the output for SHOW TIMEZONE;?
By standard should be like this "2013-08-29T09:57:57.267863Z"
import pg, {types} from 'pg'; import moment from 'moment';
const TIMESTAMPTZ_OID = 1184; const TIMESTAMP_OID = 1114; const parseFn = (val) => { return val === null ? null : moment(new Date(Date.parse(val + "+0000"))); }; types.setTypeParser(TIMESTAMPTZ_OID, parseFn); types.setTypeParser(TIMESTAMP_OID, parseFn);
This is how it works for me + it return date as moment.js object
Bump, but this behavior caused me a few issues that were pretty hard to track down on a project. What we ended up doing here was to override the driver to return strings instead of creating dates with an assumed timezone. The reason was, we were accessing dates from a db that was being written by a different microservice, which did not use timestamptz column types, but just timestamp. Our servers are in UTC, but when accessing a remote db from our local machines to test, the driver assumed localtime, which was incorrect.
It's sort of an edge case, but definitely not an unthinkable one. It illustrates why I don't think either of the solutions above - assuming UTC or assuming local time - are the best ones (just in my opinion). Here, assuming a time zone created issues that were hard to track down (we were creating an API with hourly data, and all the hours were shifted 8 hours into the future - this was not immediately obvious but caused our data to be incorrect because the dates themselves, coming out of the db query were incorrect).
A timestamp column without a timezone does not have a time zone, so it seems counterintuitive to me to have the driver assume a timezone which may or may not be correct, although I respect the convenience of directly outputting date objects, I unfortunately don't think Javascript's built-in Date object is good enough to justify being the primary data type output from queries: it's just too dangerous with too many pitfalls.
I think for non-tz timestamp columns, defaulting to a string/struct that explicitly has no timezone, and providing a few (documented) simple helper functions to create an ISO string, create a date object etc. from it while assuming a call-site specified timezone would be a bit more resilient, if more verbose.
Just my 2c.
Dude come on, I have Date, why the hell would it try to convert to my timezone and get datetime with different date, how is this the correct behavior.
@OBorce It isn’t correct, it’s just backwards-compatible. Do this: https://github.com/brianc/node-postgres/issues/429#issuecomment-24870258