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
{{ message }}
Repository navigation
timestamptz should be read as DateTimeOffset with local timezone, not UTC #1076
When reading a timestamptz as a DateTime, Npgsql returns it with Kind=Local. However, when reading it as a DateTimeOffset, it's returned as UTC - this is inconsistent.
Don't forget to update datetime and migration docs.
@roji I'm not seeing DateTimeOffset being returned in UTC representation with the recently released 3.1.0.
I'm using all default settings (no tz variables or changes from default PostgreSQL 9.5 windows install) and doing a LINQ select with entity framework on a column that is of type timestamptz is returning me a DateTimeOffset having the offset of my local machine's timezone (local postgresql server).
@rwasef1830 Unfortunately Entity Framework adds its own layer of complexity on top of Npgsql, it's better to clearly separate between do and ensure good behavior on each layer...
I'd be interested in hearing especially about issues in Npgsql itself (e.g. without EF).
Ok, I debugged this in TimeStampTzHandler and I see the reason for my behavior is that Entity Framework just calls NpgsqlDataReader.GetValue(int) when materializing result types and then TimeStampTzHandler defaults to returning a DateTime with Local kind which EF then converts to DateTimeOffset, so I end up with a DateTimeOffset having offset set to that of my machine's local timezone.
(I guess this is because Entity Framework assumes that all drivers work like Sql Server and that there will be a clean mapping between the data type and DB column type).
However, when you explicitly request a DateTimeOffset using GetFieldValue<T>, it returns a DateTimeOffset with TimeSpan.Zero, but Entity Framework didn't call that API in my tests.
When this bug is done, could we have a connection string parameter that lets us force all DateTime and DateTimeOffset to be returned as Utc ? I believe many people want this and it would remove a whole layer of confusion from the API.
Although this issue has been discussed to death I would like to add my 2 cents.
I have interesting case when representing timestamptz as DateTime with Kind=Local causes a problem.
I have following workflow:
Create command based on select from a table including at least one column of timestamptz datatype.
Execute data reader and pipe into DataTable.Load
Make some data modifications in the table
Call NpgsqlDataAdapter.Update passing the table.
It fails with Concurrency violation: the UpdateCommand affected 0 of the expected 1 records. ...
Root cause: NpgsqlDataReader reports timestamptz columns as of DataType = typeof(DateTime). After data is loaded into the data table all originally timestamptz columns get DataColumn.DateTimeMode = DataSetDateTime.UnspecifiedLocal not DataSetDateTime.Local. When generated by NpgsqlCommandBuilder update command gets parameter populated they send-as-is because information that it has Kind=Local is lost at this point. As result nothing gets updated because generated by ConflictOption.CompareAllSearchableValues policy update statements have where clause that doesn't match single record. All those originally timestamptz values never got converted to UTC. :(
It took me a long time to debug/figure out. Nothing if this would happen if timestamptz column were mapped to DateTimeOffset (matters little local of UTC offset).
I cannot help but remind advice from Microsoft on datetime with offset representation: "Use DateTimeOffset, not DateTime"
Workaround (very tedious) : Manually set DataColumn.DateTimeMode to DataSetDateTime.Local for all columns of type timestamptz.
I'm largely in favor of @rwasef1830suggestion to add parameter to connection string to force timestamptz to DateTimeOffset mapping.
Sorry to resurrect. Just to clarify, because the red scary text from the 4.0 release notes frightened me:
When reading timestamptz as a DateTimeOffset, the machine local offset will be used. Previously a DateTimeOffset in UTC was returned.
So I know DateTimeOffset has a time zone component, and timestamptz is stored as UTC no matter what. So did you change the point in time described by timestamptz, or just the time zone? In other words, if I applied the offset, would it match the database value?
Not sure why the user of DateTimeOffset would make assumptions about the raw value before the offset. We usually represent time that requires the original time zone using two columns.
IIRC the change was only to the timezone - definitely not to the actual point in time. This matches the PostgreSQL behavior of returning textual representations of timetstamptz in the user's local timezone (as configured in the PG Timezone parameter).
Activity
roji commentedon May 9, 2016
Actually, until #646 is fully done it's probably a better idea to leave this as it is. At least with UTC there are no conversions and no errors.
rwasef1830 commentedon May 17, 2016
@roji I'm not seeing DateTimeOffset being returned in UTC representation with the recently released 3.1.0.
I'm using all default settings (no tz variables or changes from default PostgreSQL 9.5 windows install) and doing a LINQ select with entity framework on a column that is of type timestamptz is returning me a DateTimeOffset having the offset of my local machine's timezone (local postgresql server).
roji commentedon May 17, 2016
@rwasef1830 Unfortunately Entity Framework adds its own layer of complexity on top of Npgsql, it's better to clearly separate between do and ensure good behavior on each layer...
I'd be interested in hearing especially about issues in Npgsql itself (e.g. without EF).
rwasef1830 commentedon May 21, 2016
Ok, I debugged this in
TimeStampTzHandlerand I see the reason for my behavior is that Entity Framework just callsNpgsqlDataReader.GetValue(int)when materializing result types and thenTimeStampTzHandlerdefaults to returning aDateTimewithLocalkind which EF then converts toDateTimeOffset, so I end up with aDateTimeOffsethaving offset set to that of my machine's local timezone.(I guess this is because Entity Framework assumes that all drivers work like Sql Server and that there will be a clean mapping between the data type and DB column type).
However, when you explicitly request a
DateTimeOffsetusingGetFieldValue<T>, it returns aDateTimeOffsetwithTimeSpan.Zero, but Entity Framework didn't call that API in my tests.When this bug is done, could we have a connection string parameter that lets us force all DateTime and DateTimeOffset to be returned as Utc ? I believe many people want this and it would remove a whole layer of confusion from the API.
dmitry-a-morozov commentedon Jan 4, 2018
Although this issue has been discussed to death I would like to add my 2 cents.
I have interesting case when representing
timestamptzasDateTimewithKind=Localcauses a problem.I have following workflow:
timestamptzdatatype.DataTable.LoadIt fails with
Concurrency violation: the UpdateCommand affected 0 of the expected 1 records. ...Root cause:
NpgsqlDataReaderreportstimestamptzcolumns as ofDataType = typeof(DateTime). After data is loaded into the data table all originallytimestamptzcolumns getDataColumn.DateTimeMode = DataSetDateTime.UnspecifiedLocalnotDataSetDateTime.Local. When generated byNpgsqlCommandBuilderupdate command gets parameter populated they send-as-is because information that it hasKind=Localis lost at this point. As result nothing gets updated because generated byConflictOption.CompareAllSearchableValuespolicy update statements have where clause that doesn't match single record. All those originallytimestamptzvalues never got converted to UTC. :(It took me a long time to debug/figure out. Nothing if this would happen if
timestamptzcolumn were mapped toDateTimeOffset(matters little local of UTC offset).I cannot help but remind advice from Microsoft on datetime with offset representation: "Use DateTimeOffset, not DateTime"
Workaround (very tedious) : Manually set
DataColumn.DateTimeModetoDataSetDateTime.Localfor all columns of typetimestamptz.I'm largely in favor of @rwasef1830 suggestion to add parameter to connection string to force
timestamptztoDateTimeOffsetmapping.roji commentedon Jun 8, 2018
This was done in #1940 and released in 4.0.0.
mattheworres commentedon Sep 12, 2019
I know this is quite old, but is there any way to disable the default action of converting a value being read out of Postgres into local time?
YohDeadfall commentedon Sep 12, 2019
There is no option, so you should implement (copy-paste except one line) a handler and register it to change the default behavior.
russellfoster commentedon Oct 22, 2020
Sorry to resurrect. Just to clarify, because the red scary text from the 4.0 release notes frightened me:
When reading timestamptz as a DateTimeOffset, the machine local offset will be used. Previously a DateTimeOffset in UTC was returned.So I know DateTimeOffset has a time zone component, and timestamptz is stored as UTC no matter what. So did you change the point in time described by timestamptz, or just the time zone? In other words, if I applied the offset, would it match the database value?
Not sure why the user of DateTimeOffset would make assumptions about the raw value before the offset. We usually represent time that requires the original time zone using two columns.
roji commentedon Oct 22, 2020
IIRC the change was only to the timezone - definitely not to the actual point in time. This matches the PostgreSQL behavior of returning textual representations of timetstamptz in the user's local timezone (as configured in the PG Timezone parameter).
YohDeadfall commentedon Oct 22, 2020
The driver reads a value of the
timestamptzas is (in UTC) and uses the current time zone like:Therefore, the date time part is read unchanged. The behavior is aligned with
psqland PostgreSQL.