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
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.
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.
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")>]typeThing={[<Column("id")>]
Id :int[<Column("start")>]
Start :DateTime[<Column("end")>]
End :DateTime}
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)?
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.
Activity
roji commentedon Apr 21, 2017
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.
mohd-akram commentedon Apr 21, 2017
I think that's the problem with using the binary encoding.
Quoting the relevant part of the Postgres documentation:
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:roji commentedon Apr 21, 2017
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)?mohd-akram commentedon Apr 21, 2017
Ah, I assumed it was doing the same for binary. You were right, I added this:
and it fixed it! So it does seem like an issue with that layer.
roji commentedon Apr 23, 2017
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.
mohd-akram commentedon Apr 23, 2017
I switched to DateTimeOffset to avoid any ambiguity.