Skip to content

Review the extended Npgsql date types #347

Closed

Description

@roji

NpgsqlInterval, NpgsqlDate, NpgsqlTimeZone, NpgsqlTime, NpgsqlTimeTZ, NpgsqlTimeStamp, NpgsqlTimeStampTZ.

In some cases the .NET type doesn't seem to be fully adequate for the Postgresql type (for example see comment on NpgsqlInterval).

But in many (most?) other cases I'm personally not clear on the value added over the .NET types, can someone help here?

Activity

  1. added this to the milestone on Sep 15, 2014
  2. roji commented on Oct 16, 2014

    @roji
    MemberAuthor

    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...

  3. roji commented on Oct 16, 2014

    @roji
    MemberAuthor

    OK, .NET DateTime support years between 1 and 9999, Postgresql date supports 4713 BC to 5874897 AD.

  4. roji commented on Oct 18, 2014

    @roji
    MemberAuthor

    @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...

  5. roji commented on Oct 18, 2014

    @roji
    MemberAuthor

    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?

  6. roji commented on Oct 18, 2014

    @roji
    MemberAuthor

    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.

  7. franciscojunior commented on Oct 18, 2014

    @franciscojunior
    Member

    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.

  8. franciscojunior commented on Oct 18, 2014

    @franciscojunior
    Member

    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? :)

  9. roji commented on Nov 23, 2014

    @roji
    MemberAuthor

    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.

  10. roji commented on Dec 24, 2014

    @roji
    MemberAuthor

    Tasks:

    • 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.

    Any comments welcome of course.

  11. roji commented on Feb 17, 2015

    @roji
    MemberAuthor

    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
    .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.

  12. roji commented on Mar 13, 2015

    @roji
    MemberAuthor

    Replace NpgsqlTimeZone with TimeSpan

  13. mattjohnsonpint commented on Mar 18, 2015

    @mattjohnsonpint

    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.

  14. 15 remaining items

  15. roji commented on Mar 20, 2015

    @roji
    MemberAuthor

    @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.

    Are you OK with these?

  16. roji commented on Mar 20, 2015

    @roji
    MemberAuthor

    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.

  17. roji commented on Mar 20, 2015

    @roji
    MemberAuthor

    @mj1856, but see the opposite assumption in DateTime.ToLocalTime()

    This instance of DateTime is assumed to be a UTC time, and the conversion is performed as if Kind were Utc.
    
  18. roji commented on Mar 20, 2015

    @roji
    MemberAuthor

    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.

  19. mattjohnsonpint commented on Mar 20, 2015

    @mattjohnsonpint

    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. :)

  20. roji commented on Mar 21, 2015

    @roji
    MemberAuthor

    @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.

  21. modified the milestones: , 3.0-beta1 on Mar 30, 2015
  22. modified the milestones: 3.0-beta1, on Aug 7, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions