Repository navigation
Conversation
This specifies the DateTime Kind property instead of returning Unspecified when querying a postgresql server. Now it returns UTC so a programmer can KNOW it's UTC and not maybe local time.
|
Why would it be UTC? The "timestamp without time zone" is exactly that - no time zone specified so nobody knows (except the programmer). |
|
Agree with @Emill - timestamp without time zone means exactly that we know nothing about the time zone (unlike timestamp with time zone)... If you're interested in learning more about all this you can check out the discussions in #347 (beware, it's long and painful). Am closing this but feel free to continue the conversation here. |
|
Note that if the programmer happens to know that a timestamp without time zone is UTC, they can (and should) perform the conversion in C# by creating a new DateTime with Kind UTC... |
|
So my problem is the website says it returns a DateTime with Kind of UTC, and that is definitely NOT true. I based my code on what that said, and a review of the code finds something else. Very frustrating on my side. I'm also finding that if I SET TIMEZONE 'US/Denver', and update a timestamptz field with a DateTime where Kind is UTC it's still adding 6 hours, causing my times to slowly shift forward. I think it can be traced down to if a timestamptz is read, a DateTime of Kind Unspecified is returned by the code, but when that same variable is passed into an UPDATE or INSERT command, npgsql appears to be treating it as a local time and not UTC. Unspecified needs to be treated the same on both sides - either it's assumed local or it's assumed UTC, not both. I agree that this patch is ill-advised, and recall it, but I will make another patch that treats Unspecified the same as UTC instead of mixing them. |
|
@cdwiegand, can you please post some code illustrating the issues you're seeing? The DateTime situation is indeed complicated - read the Npgsql docs carefully, and also the PostgreSQL docs. I'd like to help you, but I can't do it without seeing any code. |
|
Sure, here ya go: I think this is caused by line 47 of src/Npgsql/TypeHandlers/DateTimeHandlers/TimeStampTzHandler.cs: Npgsql is converting to my box's local time, but Kind == Unspecified. Additionally I'm already specifying a time zone I want all of my dates in.. |
|
I've looked at your example, and there's some problematic stuff going on indeed, but not exactly where you'd think. Date/time is really confusing in Npgsql/PostgreSQL because .NET date/time concepts don't map well to PostgreSQL, and there's also some poor naming involved. I'll explain. First off, you're using a timestamptz column. In PostgreSQL, timestamptz means something like "we're going to assume this timestamp is UTC, please perform a conversion to my local timezone when I fetch it". Now, usually "my local timezone" is the TIME ZONE variable you set - this is why your pgAdmin returned Now, Npgsql uses binary encoding to talk to PostgreSQL, not text (which is what pgAdmin uses). When returning data in binary, PostgreSQL does not apply timezone conversions - it simply sends whatever it has in its table. So, to make things look and feel like standard "text mode" PostgreSQL, when retrieving a timestamptz Npgsql converts it to local time. However, it doesn't know anything about the SET TIME ZONE command you sent earlier, all it does is use the system time zone (this is what Up to now everything is the expected behavior. I've confirmed and when using NpgsqlDataReader directly you indeed get a local time (with Kind=Local). However, for some obscure reason, when DataAdapter is used the Kind changes to Unspecified (there's probably some bad copying going on in there). I've opened #1075 and will try to investigate. Another lesson from this is that when Npgsql converts timestamptz to local, it should be sensitive to what PostgreSQL considers to be your local timezone, not what your system timezone is - sending SET TIME ZONE should have an effect. I've updated #646 to track this, although it might be a bit tricky to implement. Finally, for your specific case... If what you want is to put a timestamp in your database and not have any conversions performed, I really suggest you use timestamp. timestamptz is confusing (as you can see) and not very useful, timestamp ensures that you always get out what you put in, and it's up to you to do any conversions later. If you really want to use timestamptz, I suggest dropping NpgsqlDataAdapter (which is considered somewhat obsolete and will not exist in CoreCLR). Hope this clarifies things... Don't hesitate to post back questions and comments. |
|
I've also fixed the documentation to specify that timestamptz is returned as a DateTime with Kind=Local, not UTC. |
|
Finally, note that PostgreSQL do not recommend using timestamptz - http://www.postgresql.org/docs/current/static/datatype-datetime.html#DATATYPE-TIMEZONES. |
|
If you read that document, it is timetz they don't recommend as I can see. |
|
Oops you're right. Although with all the confusion timestamptz creates I'd recommend against using it as well. |
This specifies the DateTime Kind property instead of returning Unspecified when querying a postgresql server. Now it returns UTC so a programmer can KNOW it's UTC and not maybe local time.