You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Does anybody have any insight here? Maybe we can remove the extended types that aren't really necessary and use the .NET ones? It would simplify life for everyone...
@franciscojunior, I have a weird issue with extended types, maybe you can help. While working on #342 I wanted to make the extended type NpgsqlDate is properly sent in the new binary encoding. I wrote a unit test to test NpgsqlDate which fails on the following line:
var parameter = new NpgsqlParameter("p1", NpgsqlDbType.Date) { Value = inVal };
Note that inVal is an NpgsqlDate. The exception comes from this line, called from this line.
Apparently even though I have UseExtendedType=true, Npgsql tries to cast my NpgsqlDate into a DateTime, which is impossible. Or have I misunderstood how to use NpgsqlDate? There aren't any unit tests that actually send this type to the database...
Looking at the Postgresql time type, it seems we are currently mapping it to DateTime, and optionally to the extended type NpgsqlTime.
.NET DateTime supports a resolution of 100 nanoseconds, which is more than the microsecond resolution supported by Postgresql - so I'm not sure why we have the NpgsqlTime extended type. It seems it can be removed.
@franciscojunior and @Emill, I think both of you worked on date/time stuff before, any comments?
One more point regarding Postgresql time: it seems it may be better to represent it with .NET TimeSpan rather than DateTime. It supports 100ns resolution just like DateTime but does not care at all about the date component. DateTime forces you to populate the date between year 1 and 9999, which is annoying at the very least.
DateTime even has a TimeOfDay property that returns a TimeSpan, and TimeSpan's documentation says it is suitable for representing a time of day detached from any specific date.
Apparently even though I have UseExtendedType=true, Npgsql tries to cast my NpgsqlDate into a DateTime, which is impossible. Or have I misunderstood how to use NpgsqlDate? There aren't any unit tests that actually send this type to the database...
This is strange because the line you pointed out is only called when extended types is false. :(
Some of the extended types weren't very used or exercised, that's why you don't see any unit tests for them. I think @jbcooley may shed us some light about those types. Josh, could you please help us in this issue? Could you give us some more background information about those types and the extended types? Thanks in advance.
One more point regarding Postgresql time: it seems it may be better to represent it with .NET TimeSpan rather than DateTime. It supports 100ns resolution just like DateTime but does not care at all about the date component. DateTime forces you to populate the date between year 1 and 9999, which is annoying at the very least.
+1 for this. At first, I thought we didn't use it because it wasn't supported on 2.0 and at the time we needed to support 2.0. But this isn't the case. Maybe @jbcooley may give us some information about that too? :)
While reimplementing the type system in read_refactor, I think I'll return the Postgresql time type as TimeSpan as described above, and get rid of our NpgsqlTime, which doesn't seem to add any value over TimeSpan.
Replace NpgsqlTime with either NpgsqlDate or TimeSpan
Possibly replace NpgsqlTimeTZ with DateTimeOffset, or if not, modify it to include whatever replace NpgsqlTime above (NpgsqlDate or TimeSpan)
We need NpgsqlTimeStamp (DateTime's date range is only 1-9999), but we probably don't need NpgsqlTimeStampTZ - PostgreSQL itself stores them as the same value internally, the difference is only in the conversions.
I'd like to wrap up date/time handling in v3.0, and there are some non-trivial questions. I had a short discussion with @Emill, and here is what we came up with for now. This is far from final and the subject is pretty confusing, any feedback is really welcome!
Sending values to the database
.NET Value
PG type
Action
DateTime(Kind=UTC)
timestamp
Send as-is
DateTime(Kind=Local)
timestamp
Send as-is
DateTime(Kind=Unspecified)
timestamp
Send as-is
DateTimeOffset
timestamp
Strip offset, send as-is.
DateTime(Kind=UTC)
timestamptz
Send as-is
DateTime(Kind=Local)
timestamptz
Convert to UTC locally before sending
DateTime(Kind=Unspecified)
timestamptz
Send as is.
DateTimeOffset
timestamptz
Convert to UTC locally
DateTime(Kind=UTC)
time
Send as-is
DateTime(Kind=Local)
time
Send as-is
DateTime(Kind=Unspecified)
time
Send as-is
DateTimeOffset
time
Strip offset, send as-is.
DateTime(Kind=UTC)
timetz
Send time and UTC timezone
DateTime(Kind=Local)
timetz
Send time and local system timezone
DateTime(Kind=Unspecified)
timetz
Assume UTC and send as is
DateTimeOffset
timetz
Send time and timezone
Reading values from the database
PG type
.NET value
Action
timestamp
DateTime
Kind=Unspecified
timestamp
DateTimeOffset
Should throw an exception?
timestamptz
DateTime
Kind=UTC (PG binary encoding sends UTC)
timestamptz
DateTimeOffset
Kind=UTC (PG binary encoding sends UTC) With offset=0 (UTC)
time
TimeSpan
As-is
time
DateTime
Exception
time
DateTimeOffset
Exception
timetz
TimeSpan
Strip offset, read as-is
timetz
DateTime
Exception
timetz
DateTimeOffset
Exception
In addition there's the question of what PG type to infer when the user sends a DateTime. It seems like when the DateTime has Kind=UTC we can simply send a timestamptz (which is expected to represent a UTC timestamp). When Kind=Local we can convert locally to UTC and again send a timestamptz. For Kind=Unspecified timestamp seems to be the right option.
I'm a bit confused by the chart above. DateTimeOffset doesn't have a Kind, and any mapping between timestamptz and DateTimeOffset should preserve the offset as-is. It shouldn't do any conversions at all.
@mj1856 the DateTimeToUniversalTime() behavior is a good argument, I'll implement it that way.
Two more notes:
When a user writes a timetz as a TimeSpan, I propose to also treat it as local, i.e. send the local timezone to the database along with it. This will make the behavior consistent with DateTime (with kind=local or unspecified). When reading, the TimeSpan would again be adjusted to represent, in the machine's timezone, the point in time stored in the database (so again an adjustment).
Continuing along the same line, when reading a timetz as a DateTime, the result should always be with Kind=Local, meaning that instead of throwing away the offset in the database we use it to convert to the machine's time zone.
Some points regarding timestamp/timestamptz (@Emill, continues our conversation)...
As discussed before, TimeStampTZ isn't really a different type (same storage, same logic...), so I removed it from Npgsql.
It makes sense to me to make NpgsqlTimestamp identical in every way to DateTime, after all it's only there because of the range/precision differences between the CLR type and the PG type. So I'm adding a Kind property on NpgsqlTimeStamp. Just like DateTime.Kind is significant when type-inferring from DateTime (Utc -> timestamp, Local/Unspecified -> timestamptz), it should be significant when type-inferring from NpgsqlDateTime.
Basically the idea is for NpgsqlTimeStamp to replicate DateTime in API and behavior as much as possible.
OK guys, I pushed a big commit that does most (all?) of what's discussed above.
As the subject is difficult I'd really appreciate a review on this, look especially at the tests (DateTimeTests.cs). I'm sure there are still a lot of end-cases and potential cleanup to be done but this work is a bit exhausting :)
Yes, ToLocalTime does the opposite with regard to Unspecified, but considering the directionality of the operation, I think the behavior from ToUniversalTime makes most sense.
Regarding your points about timetz <=> TimeSpan. Without a date, the offset is somewhat arbitrary. The best you could do is use the current offset, which of course can change due to DST. However, you have to put something there, since that's what the timetz type requires, so sure - I guess that's ok. I don't know how useful it will be though.
Regarding timetz <=> DateTime - your proposal sounds reasonable.
Let me go on record saying that I detest the whole idea of DateTimeKind.Local in general, including DateTime.Now (see my blog). IMHO, any time you implicitly use the machine's time zone you're introducing potential for error. It would be cleaner if it didn't exist. But since it does, you guys appear to be doing the right thing with it.
I'll have a look later today at the commit. Though I'm not super familiar with npgsql internals, I'd be happy to comment on any date/time issues. :)
@mj1856 thanks for the comments, your input on this subject is really appreciated. I'd suggest looking less at Npgsql internals, and more at the acceptance tests (DateTimeTests) which should define the proper behavior.
Yeah, I agree about the problematic nature of DateTimeKind.Local, although perhaps less violently than you... At the end of the day, as long as programmers understand what is going on and the implicit context of the machine's timezone, things seem ok. I'm a UTC purist myself on servers and in applications, and in any case we have no choice but to make Npgsql handle DateTimeKind.Local in the most sane way we can.
The possibility of reading/writing NodaTime entities is interesting. I'm sure it would be fairly easy to implement, the only issue would be the extra dependency on NodaTime, which is kind of annoying. Maybe we can find a way to have a separate Npgsql.NodaTime project/nuget that users can optionally install and which brings this feature. I've opened #546 to track this, if enough people show interest in this we can definitely look at it.
Activity
roji commentedon Oct 16, 2014
Does anybody have any insight here? Maybe we can remove the extended types that aren't really necessary and use the .NET ones? It would simplify life for everyone...
roji commentedon Oct 16, 2014
OK, .NET DateTime support years between 1 and 9999, Postgresql date supports 4713 BC to 5874897 AD.
roji commentedon Oct 18, 2014
@franciscojunior, I have a weird issue with extended types, maybe you can help. While working on #342 I wanted to make the extended type NpgsqlDate is properly sent in the new binary encoding. I wrote a unit test to test NpgsqlDate which fails on the following line:
var parameter = new NpgsqlParameter("p1", NpgsqlDbType.Date) { Value = inVal };Note that inVal is an NpgsqlDate. The exception comes from this line, called from this line.
Apparently even though I have UseExtendedType=true, Npgsql tries to cast my NpgsqlDate into a DateTime, which is impossible. Or have I misunderstood how to use NpgsqlDate? There aren't any unit tests that actually send this type to the database...
roji commentedon Oct 18, 2014
Looking at the Postgresql time type, it seems we are currently mapping it to DateTime, and optionally to the extended type NpgsqlTime.
.NET DateTime supports a resolution of 100 nanoseconds, which is more than the microsecond resolution supported by Postgresql - so I'm not sure why we have the NpgsqlTime extended type. It seems it can be removed.
@franciscojunior and @Emill, I think both of you worked on date/time stuff before, any comments?
roji commentedon Oct 18, 2014
One more point regarding Postgresql time: it seems it may be better to represent it with .NET TimeSpan rather than DateTime. It supports 100ns resolution just like DateTime but does not care at all about the date component. DateTime forces you to populate the date between year 1 and 9999, which is annoying at the very least.
DateTime even has a TimeOfDay property that returns a TimeSpan, and TimeSpan's documentation says it is suitable for representing a time of day detached from any specific date.
franciscojunior commentedon Oct 18, 2014
This is strange because the line you pointed out is only called when extended types is false. :(
Some of the extended types weren't very used or exercised, that's why you don't see any unit tests for them. I think @jbcooley may shed us some light about those types. Josh, could you please help us in this issue? Could you give us some more background information about those types and the extended types? Thanks in advance.
franciscojunior commentedon Oct 18, 2014
+1 for this. At first, I thought we didn't use it because it wasn't supported on 2.0 and at the time we needed to support 2.0. But this isn't the case. Maybe @jbcooley may give us some information about that too? :)
roji commentedon Nov 23, 2014
While reimplementing the type system in read_refactor, I think I'll return the Postgresql time type as TimeSpan as described above, and get rid of our NpgsqlTime, which doesn't seem to add any value over TimeSpan.
roji commentedon Dec 24, 2014
Tasks:
Any comments welcome of course.
roji commentedon Feb 17, 2015
Hi everyone.
I'd like to wrap up date/time handling in v3.0, and there are some non-trivial questions. I had a short discussion with @Emill, and here is what we came up with for now. This is far from final and the subject is pretty confusing, any feedback is really welcome!
Sending values to the database
Reading values from the database
Kind=UTC (PG binary encoding sends UTC)With offset=0 (UTC)In addition there's the question of what PG type to infer when the user sends a DateTime. It seems like when the DateTime has Kind=UTC we can simply send a timestamptz (which is expected to represent a UTC timestamp). When Kind=Local we can convert locally to UTC and again send a timestamptz. For Kind=Unspecified timestamp seems to be the right option.
roji commentedon Mar 13, 2015
Replace NpgsqlTimeZone with TimeSpan
mattjohnsonpint commentedon Mar 18, 2015
I'm a bit confused by the chart above.
DateTimeOffsetdoesn't have aKind, and any mapping betweentimestamptzandDateTimeOffsetshould preserve the offset as-is. It shouldn't do any conversions at all.15 remaining items
roji commentedon Mar 20, 2015
@mj1856 the DateTimeToUniversalTime() behavior is a good argument, I'll implement it that way.
Two more notes:
Are you OK with these?
roji commentedon Mar 20, 2015
Some points regarding timestamp/timestamptz (@Emill, continues our conversation)...
As discussed before, TimeStampTZ isn't really a different type (same storage, same logic...), so I removed it from Npgsql.
It makes sense to me to make NpgsqlTimestamp identical in every way to DateTime, after all it's only there because of the range/precision differences between the CLR type and the PG type. So I'm adding a Kind property on NpgsqlTimeStamp. Just like DateTime.Kind is significant when type-inferring from DateTime (Utc -> timestamp, Local/Unspecified -> timestamptz), it should be significant when type-inferring from NpgsqlDateTime.
Basically the idea is for NpgsqlTimeStamp to replicate DateTime in API and behavior as much as possible.
roji commentedon Mar 20, 2015
@mj1856, but see the opposite assumption in
DateTime.ToLocalTime()roji commentedon Mar 20, 2015
OK guys, I pushed a big commit that does most (all?) of what's discussed above.
As the subject is difficult I'd really appreciate a review on this, look especially at the tests (DateTimeTests.cs). I'm sure there are still a lot of end-cases and potential cleanup to be done but this work is a bit exhausting :)
Anyway, let me know what you think.
mattjohnsonpint commentedon Mar 20, 2015
Yes,
ToLocalTimedoes the opposite with regard toUnspecified, but considering the directionality of the operation, I think the behavior fromToUniversalTimemakes most sense.Regarding your points about
timetz<=>TimeSpan. Without a date, the offset is somewhat arbitrary. The best you could do is use the current offset, which of course can change due to DST. However, you have to put something there, since that's what thetimetztype requires, so sure - I guess that's ok. I don't know how useful it will be though.Regarding
timetz<=>DateTime- your proposal sounds reasonable.Let me go on record saying that I detest the whole idea of
DateTimeKind.Localin general, includingDateTime.Now(see my blog). IMHO, any time you implicitly use the machine's time zone you're introducing potential for error. It would be cleaner if it didn't exist. But since it does, you guys appear to be doing the right thing with it.I'll have a look later today at the commit. Though I'm not super familiar with npgsql internals, I'd be happy to comment on any date/time issues. :)
roji commentedon Mar 21, 2015
@mj1856 thanks for the comments, your input on this subject is really appreciated. I'd suggest looking less at Npgsql internals, and more at the acceptance tests (DateTimeTests) which should define the proper behavior.
Yeah, I agree about the problematic nature of
DateTimeKind.Local, although perhaps less violently than you... At the end of the day, as long as programmers understand what is going on and the implicit context of the machine's timezone, things seem ok. I'm a UTC purist myself on servers and in applications, and in any case we have no choice but to make Npgsql handleDateTimeKind.Localin the most sane way we can.The possibility of reading/writing NodaTime entities is interesting. I'm sure it would be fairly easy to implement, the only issue would be the extra dependency on NodaTime, which is kind of annoying. Maybe we can find a way to have a separate Npgsql.NodaTime project/nuget that users can optionally install and which brings this feature. I've opened #546 to track this, if enough people show interest in this we can definitely look at it.