Skip to content

DateTime(Kind=UTC) is sent without Z offset #1538

Closed

Description

@mohd-akram

As per the documentation, a DateTime(Kind=UTC) uses timestamptz by default. However, the value sent to Postgres does not have a timezone specifier (either +00:00 or z). This means that if a database has its timezone set to something else, it will be stored as that timezone and not as UTC, as per the Postgres documentation.

Activity

  1. roji commented on Apr 21, 2017

    @roji
    Member

    The mapping of CLR date/time types to PostgreSQL is confusing, but this is the expected behavior...

    The timestamptz data type does not contain a timezone - PostgreSQL simply stores a timestamp in the database; timestamptz (as opposed to simple timestamp) simply means that the timestamp is assumed to be UTC. When Npgsql sends and receives timestamptz (or timestamp) values, it does in a binary encoding and does not transmit any timezone to PostgreSQL, so it makes no sense to say that "the value sent does not have a timezone specifier". If you send a DateTime(Kind=UTC) as a timestamptz (which is the default), the UTC value simply gets sent and inserted as-is into the database.

    The same happens when reading a timestamptz column as binary - the value will be read as-is and you will receive the exact timestamp as a DateTime(Kind=UTC). However, if you read the value from PostgreSQL as text, PostgreSQL will convert the timestamptz's assumed UTC value to the local timezone (which is determined either by PostgreSQL or by your connection settings). This is probably the source of your confusion.

    If you're still unclear on this please submit a minimal code sample.

  2. mohd-akram commented on Apr 21, 2017

    @mohd-akram
    Author

    When Npgsql sends and receives timestamptz (or timestamp) values, it does in a binary encoding and does not transmit any timezone to PostgreSQL

    I think that's the problem with using the binary encoding.

    Quoting the relevant part of the Postgres documentation:

    If no time zone is stated in the input string, then it is assumed to be in the time zone indicated by the system's TimeZone parameter, and is converted to UTC using the offset for the timezone zone.

    It does not get inserted as-is. In my example, my postgresql.conf has a timezone = 'Asia/Dubai' value. Inserting a DateTime(Kind=UTC) with 12:00, it gets inserted as 12:00+4 (i.e. 08:00 is effectively stored in the database). I'm using Npgsql through Entity Framework Core in F# with a simple model like this:

    [<CLIMutable>]
    [<ReferenceEquality>]
    [<Table("things")>]
    type Thing= {
        [<Column("id")>]
        Id : int
        [<Column("start")>]
        Start : DateTime
        [<Column("end")>]
        End : DateTime
    }
  3. roji commented on Apr 21, 2017

    @roji
    Member

    I think that's the problem with using the binary encoding.

    Quoting the relevant part of the Postgres documentation:

    If no time zone is stated in the input string, then it is assumed to be in the time zone indicated by the system's TimeZone parameter, and is converted to UTC using the offset for the timezone zone.

    Read carefully: the documentation refers to input strings - when using PostgreSQL binary encoding conversion does not occur. You may dislike this (or binary encoding in general), but sending/receiving values in text means constructing string representations of timestamps - and worse, parsing them - which is bad for performance and very problematic across locales.

    You didn't mention before that you're using EF Core - this may be an issue at that layer. Can you please try to explicitly specify the column type as timestamptz (via the EF Core fluent API or attribute)?

  4. mohd-akram commented on Apr 21, 2017

    @mohd-akram
    Author

    Ah, I assumed it was doing the same for binary. You were right, I added this:

    builder.Entity<Thing>().Property(t => t.Start).ForNpgsqlHasColumnType("timestamptz");
    builder.Entity<Thing>().Property(t => t.End).ForNpgsqlHasColumnType("timestamptz");

    and it fixed it! So it does seem like an issue with that layer.

  5. roji commented on Apr 23, 2017

    @roji
    Member

    If I recall correctly, the Npgsql EF Core provider will by default map DateTime to timestamp, not timestamptz. This is because the kind of the DateTime (UTC, local...) isn't known when deciding which data type to map to. This is probably the cause for the problematic behavior you saw, and the way to solve it is to explicitly specify the data type.

  6. mohd-akram commented on Apr 23, 2017

    @mohd-akram
    Author

    I switched to DateTimeOffset to avoid any ambiguity.

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

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions