Skip to content

Read DateTimeOffset with the PostgreSQL TIMEZONE #3594

Closed

Description

@roji

When reading a NodaTime ZonedDateTime from a PostgreSQL timestamp with time zone, we look at the PostgreSQL connection's Timezone parameter and return a ZonedDateTime in that timezone - this mimics the general PostgreSQL behavior when not using Npgsql.

We did not implement this for non-NodaTime DateTimeOffset, because PostgreSQL timezones are in IANA format, but .NET did not support those up to now - but that's been done: dotnet/runtime#49412. This means we can now start returning DateTimeOffset in the appropriate timezone, instead of in UTC the machine's configured timezone.

On the downside, this would be a breaking change; even more tricky, the IANA support is only going to be available in .NET 6.0, so this would possibly mean a behavioral change depending on which .NET is being used (or we throw NotSupported or something. We can discuss.

Activity

  1. vonzshik commented on Mar 12, 2021

    @vonzshik
    Contributor

    Dropping the support for the DateTimeOffset for .NET versions below 6.0 is a pretty huge breaking change IMO.

  2. roji commented on Mar 13, 2021

    @roji
    MemberAuthor

    @vonzshik I agree. Though I'm also uncomfortable with having Npgsql behave differently based on the .NET version being used - pre-6.0 it would be UTC, 6.0 it would be based on the PG TIMEZONE parameter...

    Since this also doesn't seem to be that important, we can always just backlog it and revisit in a couple years.

  3. vonzshik commented on Mar 13, 2021

    @vonzshik
    Contributor

    I'm adding this to the backlog for now, let's think about it for 7.0 (at the very least, by that time we will have multiple TFMs supporting this API, so it should be less risky).

  4. added this to the Backlog milestone on Mar 13, 2021
  5. Emill commented on Apr 12, 2021

    @Emill
    Contributor

    Can't we make this configurable? In my opinion, I can see use cases where users actually want to retrieve the timestamp in UTC, rather than local time. To me that actually makes more sense for many applications that just want to record a time (for example created_at) and maybe compare times and so on. With local time we have issues like different time zones, daylight saving time and so on. If the timestamp is actually to be displayed for an end user, that can be done client side in Javascript or whatever with the user's time zone info. I think this is generally the common approach nowadays, to use UTC everywhere for everything, except when you display the time for an end user according to his/her time zone. Can you come up with one single use case where it makes sense for a modern application to let Npgsql convert the timestamptz to a local time (whether it's the db server's local time or the application server's local time?), rather than just giving the user the raw UTC timestamp? And yes I think the timestamptz type is pretty stupid, how it tricks people to think that it actually stores a timezone but that's another topic.

  6. roji commented on Apr 12, 2021

    @roji
    MemberAuthor

    @Emill these issues around DateTime/DateTimeOffset/timestamp with/without timezone have been discussed so many times in the past :) I'll try to summarize...

    • The reason we'd return a local DateTimeOffset for PG timestamptz, is that this is what PG does for timestamptz - if you select a timestamptz in psql, you get back a local timestamp in your PG session's configured TimeZone. In fact, that's the only thing that timestamptz does, when compared to regular timestamp. So this is about making Npgsql behave consistently with PG.
    • I completely agree with the recommendation to UTC everywhere, but then... why would a user ever use timestamptz, as opposed to timestamp? Remember timestamptz doesn't store the time zone, it just performs tz conversions on input/output - so why would you use it if you want UTC everywhere? See this thing I wrote some time ago: https://dba.stackexchange.com/questions/59006/what-is-a-valid-use-case-for-using-timestamp-without-time-zone/215563#215563
    • timestamptz is even more useless then you think - it typically can't be used to display a timestamp in the user's time zone, since the timezone of the PG session is used, rather than something coming from the user. Maybe there are applications out there constantly setting the PG time zone based on the user's value from the browser, but I'd really avoid it.
    • To summarize, most of the confusion I've seen is people thinking that they need timestamptz to be UTC everywhere, rather than just storing in timestamp and treating values as UTC by convention.

    Note that I don't really care much about this issue :) I mainly opened it because .NET finally added support for IANA/Olson timezones...

  7. Emill commented on Apr 12, 2021

    @Emill
    Contributor

    Not to make this discussion longer than necessary, but one benefit of using timestamptz is that PostgreSQL then knows that this represents an exact point in time. This is important when using date/time functions in PostgreSQL. PostgreSQL assumes a "timestamp without time zone" is generally in local time, so if you want to compare a "timestamp without time zone" column containing a utc value with CURRENT_TIMESTAMP (which returns a timestamptz), that won't give the correct results since it assumes the column is in local time. So I believe that timestamptz is the type you should generally use if you want to store an "exact point in time". If PostgreSQL then displays it using the db's local time zone when converted to text, that's not big of a deal, since the text i/o is only used when interacting with the db in a maintenence tool (or are there still pg drivers using the text protocol rather than the binary? In any case it contains a zone offset so you still know what time point it represents). It makes sense that the binary protocol is UTC if you disregard the name of the type. What should be changed is the name "timestamp with time zone". It should be called "point in time" or something; then everything would make sense.

    If we look at https://www.npgsql.org/doc/types/datetime.html for reading values from the db, there is no way to receive the value as a DateTime with Kind=UTC. This is something at least I would like to have a configurable option for, and would like to use myself in my own projects, as it makes much sense if you use the approach that "everything is utc except when displaying".

  8. roji commented on Apr 13, 2021

    @roji
    MemberAuthor

    Note that I got confused in the OP, and wrote that we currently return a DateTimeOffset with Offset=0 (UTC) for timestamptz. We in fact return a DateTimeOffset with a local offset, based on the machine's configured timezone. This issue would be about changing this to use the PostgreSQL TimeZone instead.

  9. roji commented on Apr 13, 2021

    @roji
    MemberAuthor

    PostgreSQL assumes a "timestamp without time zone" is generally in local time, so if you want to compare a "timestamp without time zone" column containing a utc value with CURRENT_TIMESTAMP (which returns a timestamptz), that won't give the correct results since it assumes the column is in local time.

    That's true, though you can simply use LOCALTIMESTAMP to get timestamp without time zone.

    So I believe that timestamptz is the type you should generally use if you want to store an "exact point in time".

    It's true that the naming points that way (CURRENT_TIMESTAMP vs. LOCALTIMESTAMP), but I don't see much else...

    If PostgreSQL then displays it using the db's local time zone when converted to text, that's not big of a deal, since the text i/o is only used when interacting with the db in a maintenence tool (or are there still pg drivers using the text protocol rather than the binary?

    Last time I looked, Npgsql is the only driver which exclusively uses the extended protocol and therefore binary :) And AFAIK this is still the case. So if you use pgJDBC, for example, I think you'd see the same behavior of a getting back timestamptz converted to your local timezone. We basically just try to be consistent with the native PG behavior here...

    You do make good points, and it's true reading a timestamp as DateTime with Kind=UTC is missing. However, the question is whether we should make such a non-trivial breaking change, and thereby change Npgsql's behavior to be less consistent with PostgreSQL...

    My general view on this, is that the .NET date/time types are themselves broken in many ways, and the mapping to PostgreSQL is even more broken... Users looking for a good, robust mapping should look at mapping with NodaTime instead, where things are much better...

  10. Emill commented on Apr 13, 2021

    @Emill
    Contributor

    You can't use LOCALTIMESTAMP to compare with a "timestamp without time zone" containing a value in utc either, since then you compare values with different time zones, without taking the time zone in mind.

    I don't think we can make DateTime.Kind be UTC the default, since that would be a breaking change. That's why I thought it should be a configurable option.

  11. roji commented on Apr 13, 2021

    @roji
    MemberAuthor

    You can't use LOCALTIMESTAMP to compare with a "timestamp without time zone" containing a value in utc either, since then you compare values with different time zones, without taking the time zone in mind.

    You're right - you can use CURRENT_TIMESTAMP AT TIME ZONE 'UTC', which produces a timestamp without time zone.

    I don't think we can make DateTime.Kind be UTC the default, since that would be a breaking change. That's why I thought it should be a configurable option.

    We could, though I generally try to avoid adding configuration switches for every need that arises. If we did that everyone someone needed something the driver would now be unmanageable.

  12. roji commented on Aug 28, 2021

    @roji
    MemberAuthor

    Closing as we've decided to remove the convert-to-local behavior for timestamptz for 6.0 (#3947). We'll instead always hand back a DateTimeOffset with offset=0.

  13. removed this from the Backlog milestone on Aug 28, 2021
  14. added theissue type on Jun 18, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions