-
-
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
Question about the date
type
#510
Comments
@jrf0110 I think you might be right about this. According to the postgres docs (Section 8.5.3): http://www.postgresql.org/docs/9.1/static/datatype-datetime.html
I think the last sentence there is probably the one to focus on. Something else to think about SELECT date_part('day', '2014-01-24'::date); -- returns 24 I would assume that the behavior should be the same when calling |
I think the problem is that javascript is inconsistent about parsing dates (this is from GMT-5):
So its not a pg problem, but a js problem. Here is one workaround:
If this is acceptable I can do a PR later to implement it. |
@benighted I agree it's a js issue not a postgres one. I had no idea there was that discrepancy with parsing dates. Crazy! Good to know. |
Right. Not a PG problem but a node-pg problem. My fix: pg.types.setTypeParser( 1082, 'text', function( val ){
return new Date( val + ' 00:00:00' );
}); |
I was curious so I looked into this some more, and apparently the problem is that ISO-8601 dates are parsed as UTC by default while non-ISO dates are parsed using local time zone, which results in the discrepancy we are seeing. Example:
So, the question is, do we want to assume local time or UTC when the time zone is not specified? |
Just some more clarification on this matter as to why this happens:
I'm not sure replacing the dashes with slashes is the best idea because It's not a supported format:
I think we should pass in the date parts via |
Changing dashes to slashes or explicitly adding the " 00:00:00" time portion (with no "T") has the same effect, to break out of the ISO-8601 format, which causes js to assume local time input instead of UTC. Passing in the date parts also assumes local time, but would require parsing the pieces out of the postgres date string (which might be as simple as |
I'm cool w/going with best performance. In that case: http://jsperf.com/fastest-way-to-parse-date-only-part-in-local-timezone |
Very cool @lalitkapoor 😄 Let's see what @brianc thinks about this issue, since it is a breaking change for anyone counting on the date to be interpreted as UTC. |
Yeah it's a breaking change, but I'm definitely all in favor of it to bring node-postgres more in line with expected & documented behavior -- honestly it wouldn't be the first time we've broken backwards compatibility for timezone stuff. Timezones & string encodings are hard! Also, we can merge this pull request as well & group everything up into a single major version bump -- ship all the breaking changes at once. 👍 Thanks for the good discussion in here too! 😄 |
This issue appears to be resolved and able to be closed. |
The Postgres
date
data type has no information about timezone. That wouldn't make any sense. However, the dateParser in node-postgres defaults to Greenwich timezone due to the way it's instantiating the object.According to this MDN article, when creating a new Date, the constructor uses the
Date.parse
which, if passed a string without a timezone, will default to the systems timezone. This is not the behavior I'm seeing in the node.Consider the following on my system in GMT-0600
It's my feeling that when the data type is
date
then we should use one of the latter two methods so that it actually defaults to system timezone. The first method is being employed by node-postgres right now:https://github.com/brianc/node-postgres/blob/master/lib/types/textParsers.js#L17
Thoughts?
The text was updated successfully, but these errors were encountered: