Skip to content

Support mapping date/time types to NodaTime #546

Closed

Description

@roji

See discussion in #347. As NodaTime is widely used it may be useful to support mapping date/time types to it directly. This could be achieved with an extra project/nuget that somehow replaces the date/time type handlers, etc.

If you are interested in this feature, please vote for it here.

Activity

  1. Brar commented on Jan 14, 2017

    @Brar
    Member

    @roji have you ever thought about making the type mapping system pluggable so that one could create custom mapping libraries to map any (standard or custom) PostgreSQL type to any (standard or custom) .Net type?

    That way one could create a Npgsql.NodaTime (or NodaTime.Npgsql) Nuget package that one could configure to replace the default date/time type handlers as well as some other mapping library that maps some other types.

    Factoring non standard mappings (like postgis mappings) out of core Npgsql could also reduce the codebase and the complexity of the Npgsql type mapping system (which seems overly complex to me anyways).

    What do you think?

  2. roji commented on Jan 17, 2017

    @roji
    MemberAuthor

    Hi @Brar!

    Yeah, this has been raised before: see conversation in #696.

    Here are some points, you're welcome to respond!

    • I did have pluggability in mind when I originally designed the type handler API for 3.0.
    • However, opening up the type handler API would mean we wouldn't be able to make changes to it as easily. As you yourself said (and I agree), the API is indeed overly complex (there are some good historical reasons for this). 3.2 is (partially) moving towards a simplification of async/sync I/O in general (single codepath with async flag instead of the async rewriter + manual state machines), which will bring a big simplication to how type handlers work as well (when completed, probably for 3.3).
    • The main point here is that the type handler API isn't really stable yet, so I don't think it's a good moment to open it up. Once we're happy with how the API looks and don't plan to change it anymore, it may make sense to do so.
    • There simply aren't that many PostgreSQL (extension) types out there, and the list doesn't seem to be evolving very quickly. In other words, there's hardly been any user demand for unimplemented type handlers up to now.
    • There's nothing really "nonstandard" about PostGIS - it's just a bunch of types that have a standard (chunking) type handler. I don't really see what we'd gain by moving them out of core Npgsql...
    • If someone wants to write support for some missing type, they can definitely do it today as a pull request to the core, this way everyone benefits from it. The only disadvantage is that you have to wait for a release (or use your private fork until the release)
  3. roji commented on May 23, 2017

    @roji
    MemberAuthor

    After #1475 is implemented, provide an Npgsql.Nodatime package which overrides the date/time handlers.

  4. self-assigned this
    on Jun 28, 2017
  5. added this to the milestone on Jun 28, 2017
  6. roji commented on Jun 29, 2017

    @roji
    MemberAuthor

    Am currently working on this feature and it's going well - I have an experimental plugin that makes Npgsql read/write Nodatime 2.0 types.

    I'd like to make sure I get the type mapping right between the PostgreSQL date/time types and the Nodatime types. I'd really appreciate feedback from anybody with experience in this domain and in Nodatime in particular (especially from you @jskeet!). The goal is to end up with a simple, sensible mapping table that doesn't resemble the current BCL mapping and forces users to do the right thing.

    PostgreSQL type Nodatime type Comments
    timestamp LocalDateTime Simple timestamp in PostgreSQL, no timezone/offset. There's a concept of infinity/negative infinity which could be represented by wrapping LocalDateTime in a LocalDateTimeWithFiniteness or something.
    timestamptz ZonedDateTime Despite the name, this fields holds only a timestamp, no timezone. But PostgreSQL treats it as UTC and performs timezone conversions based on the session-configured timezone when converting to text, etc. In binary transfer (which is what Npgsql uses) the timestamp is simply transferred as-is, and it seems correct to treat it as UTC. So this seems to make sense as a ZonedDateTime; when reading a timestamptz Npgsql would return either a ZonedDateTime with timezone UTC, or a ZonedDateTime with the session (or computer's) timezone.
    date LocalDate A simple date, no timezone/offset. There's a concept of infinity/negative infinity which could be represented by wrapping LocalDate in a LocalDateWithFiniteness or something.
    time LocalTime A simple time-of-day, no timezone/offset.
    timetz OffsetDateTime This is a PostgreSQL type that actually stores a time and an offset (odd and generally not recommended for use by PostgreSQL). The only option seems to be to map to OffsetDateTime and keep the date component zero'd out.
    interval Period The wire representation of this type is months, days and microseconds in the day, so it clearly corresponds to Period rather than Duration.
  7. jskeet commented on Jun 29, 2017

    @jskeet

    Without having looked at Postgres specifically, I'd expect both timestamp and timestamptz to be represented by Instant. Logically, a timestamp isn't in a particular calendar system but does represent an absolute instant in time. Given that a timestamp is usually "Event X happened at timestamp Y", I'd always suggest using an Instant for that - it's unambiguous about what information it does and doesn't include. Is there anything about Postgres that would make that hard to achieve?

    The rest look reasonable.

  8. roji commented on Jun 29, 2017

    @roji
    MemberAuthor

    @jskeet thanks for the super-quick review. I'm new to Nodatime, but I was under the impression an Instant is only an absolute, globally-agreed upon point in time (represented in number of ticks since epoch in UTC). Since there's no timezone information at all, is it possible/correct to actually treat timestamp as an Instant? It seems to make sense for timestamptz (in general it seems that Instant and ZonedDateTime are convertible with no additional information: everything is there in both), but isn't it problematic to simply assume that timestamp is UTC?

    Note that it's possible to allow mapping both LocalDateTime and Instant, but one of them would still have to be the default.

  9. roji commented on Jun 29, 2017

    @roji
    MemberAuthor

    The problem here is that I'm not an application - just a component translating types from one system to another. I have no idea as to the meaning or source of timestamps in a particular PostgreSQL database (i.e. the "what is the original data source" question in your docs). It seemed to make sense to me to provide LocalDateTime, which pretends to claim/guarantee the least, and then allow the user to say "it's OK, this is actually a UTC timestamp" via the Nodatime conversion methods etc.

  10. jskeet commented on Jun 29, 2017

    @jskeet

    Put it this way: if you're recording something as a timestamp in a database but you can't tell when it actually happened, that seems pretty broken to me. Using a local date/time as a timestamp definitely sounds like a bad idea as:

    • It can be ambiguous due to DST changes
    • It could change meaning if you change the time zone of the database

    I definitely see the predicament, and it's effectively a deficiency in SQL/Postgres... but I'm not sure of the best way to handle it in terms of library support. I think I'd rather reward users who are behaving sensibly (i.e. storing UTC) with a logical type, at the cost of users who are genuinely storing a local time, rather than the other way round.

    On the other hand, the Postgres docs do explicitly state that it's behaving badly (IMO):

    When a timestamp with time zone value is output, it is always converted from UTC to the current timezone zone, and displayed as local time in that zone. To see the time in another time zone, either change timezone or use the AT TIME ZONE construct (see Section 9.9.3).

    Conversions between timestamp without time zone and timestamp with time zone normally assume that the timestamp without time zone value should be taken or given as timezone local time. A different time zone can be specified for the conversion using AT TIME ZONE.

    Urgh.

    How feasible would it be to make this configurable?

  11. roji commented on Jun 29, 2017

    @roji
    MemberAuthor

    Don't forget there's timestamp and timestamptz, with precisely this difference - PostgreSQL assumes that timestamptz values - which again are just timestamps, no timezone/offset - are in UTC. So again, it seems OK to do the same on my side and map timestamptz to Instant or ZonedDateTime (or allow both). The problem is with timestamp, for which it doesn't seem to make sense to make assumptions (that's what timestamptz is about) - at the very least the two should be mapped differently.

    How feasible would it be to make this configurable?

    It's definitely possible, although it would make the mapping even more complex/unpredictable when moving around applications. I guess we could have a knob or two when setting up the Nodatime plugin. But in that case what would you set the default behavior to?

  12. jskeet commented on Jun 29, 2017

    @jskeet

    Not sure about the default behaviour, to be honest. I tend to err on the side of aggressively pursuing logical goodness, which might ignore practicality...

    I'd definitely make timestamptz map to Instant, and encourage applications to use that. It's not a ZonedDateTime because it doesn't really store a time zone, and shouldn't be viewed that way. It's logically just an instant in time, IMO.

  13. roji commented on Jun 29, 2017

    @roji
    MemberAuthor

    Hmm, OK.

    One possible reason to put a local date/time in a timestamp column, is that you're storing its timezone/offset in another column. The lack of a single datatype containing both a timestamp and timezone pretty much forces you to do it. In that scenario you're pretty much forced to go for a two-column solution, and then mapping timestamp to LocalDateTime does make sense. That's at least a case where a local date/time in timestamp isn't the wrong thing to do.

    But I admit that seems to be the rare case, and that 90% of the use cases are storing UTC in timestamp, so LocalDateTime would just make things more difficult to work with? So maybe defaulting to Instant, but allowing LocalDateTime is the right way to go.

    This does mean that Npgsql treats timestamp and timestamptz in the exact same way, which may be a bit odd (maybe the LocalDateTime option shouldn't be available for timestamptz, where it makes no real sense).

  14. jskeet commented on Jun 29, 2017

    @jskeet

    Do we have any idea how common timestamp is vs timestamptz?

    Maybe use LocalDateTime as the default for timestamp but have it configurable to Instant, but always use Instant for timestamptz, which I think is what you were suggesting... and then document a recommendation to use timestamptz.

    Database date/time handling feels even more broken than language/library handling :(

  15. roji commented on Jun 29, 2017

    @roji
    MemberAuthor

    Oh yeah, database date/time is really awful, and mapping that to language/library types is yet another level of awful - especially when those types happen to be .NET BCL.

    To be completely honest, I (unscientifically) think a lot more people use timestamp than timestamptz. If you just want to store UTC timestamps, and also to work on them in UTC in your app - the UTC everywhere pattern - you're better off just using timestamp and thereby avoiding the conversions PostgreSQL does on timestamptz. That's my general way of working, and I'd guess that's what most people do.

    So Instant for both timestamp and timestamptz seems to make sense, with the option to get LocalDateTime from timestamp (note that this isn't a config option - it's an ADO.NET API option).

    So here's a final proposal:

    PostgreSQL type Nodatime type Comments
    timestamp Instant, also allow LocalDateTime Simple timestamp in PostgreSQL, no timezone/offset. Since the vast (and correct) usage is to store UTC timestamps in this type, it makes sense to map to Instant. For the few cases where the value actually represents a local date time (e.g. another column holds the timezone/offset), allow reading/writing LocalDateTime as well. In addition, there's a concept of infinity/negative infinity which could be represented by wrapping LocalDateTime in a LocalDateTimeWithFiniteness or something.
    timestamptz Instant, also allow ZonedDateTime Despite the name, this fields holds only a timestamp, no timezone. But PostgreSQL treats it as UTC and performs timezone conversions based on the session-configured timezone when converting to text, etc. In binary transfer (which is what Npgsql uses) the timestamp is simply transferred as-is, and it seems correct to treat it as UTC. So this seems to make sense as a ZonedDateTime; when reading a timestamptz Npgsql would return either a ZonedDateTime with timezone UTC, or a ZonedDateTime with the session (or computer's) timezone.
    date LocalDate A simple date, no timezone/offset. There's a concept of infinity/negative infinity which could be represented by wrapping LocalDate in a LocalDateWithFiniteness or something.
    time LocalTime A simple time-of-day, no timezone/offset.
    timetz OffsetDateTime This is a PostgreSQL type that actually stores a time and an offset (odd and generally not recommended for use by PostgreSQL). The only option seems to be to map to OffsetDateTime and keep the date component zero'd out.
    interval Period The wire representation of this type is months, days and microseconds in the day, so it clearly corresponds to Period rather than Duration.
  16. roji commented on Jul 2, 2017

    @roji
    MemberAuthor

    Note that there would be one difference between timestamp and timestamptz handling - if a ZonedDateTime is written as a timestamptz, Npgsql would perform conversion to UTC before sending it out.

    An additional thing: when reading a ZonedDateTime, the type handler could return a non-UTC value based on the PostgreSQL session's time zone, mimicking timestamptz behavior. The problem here is for the type handler to be aware of the current session's timezone, things don't work that way at the moment.

  17. pawepaw commented on Nov 27, 2018

    @pawepaw

    @roji How to change default map to read timestamp as LocalDateTime? I got exception that it's unable to cast instant to localDateTime.

    Message: System.InvalidCastException : Unable to cast object of type 'NodaTime.Instant' to type 'NodaTime.LocalDateTime'.

  18. roji commented on Nov 27, 2018

    @roji
    MemberAuthor

    @pawepaw can you please open a new issue and post some code to show exactly what you're trying to do?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions