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
Ability to configure whether DateTimeOffset is deserialized as UTC (without offset) #2985
Reading "timestamp with tz" from database into a DateTimeOffset causes dates to be converted into a local date time. I'm wondering if it would be possible to add configuration to allow keeping DateTimeOffset in UTC.
Specifically in in TimeStampTzHandler.cs:
DateTimeOffset INpgsqlSimpleTypeHandler<DateTimeOffset>.Read(NpgsqlReadBuffer buf, int len, FieldDescription? fieldDescription)
{
// TODO: Convert directly to DateTime without passing through NpgsqlTimeStamp?
var ts = ReadTimeStamp(buf, len, fieldDescription);
if (ts.IsFinite)
return ts.ToDateTime().ToLocalTime();
allowing someway to configure whether the ToLocalTime() is called there or not. This has impact when serializing the DateTimeOffset since when calling ToLocalTime() will cause the offset to be added to the ISO8601 string which I'm trying to avoid in this case.
Further technical details
Npgsql version: 4.1.3.1
PostgreSQL version: 9.6
Operating system: OSX Catalina
Other details about my project setup: .NET Core 3.1
@giuliano-barberi-tf there is a long history around what to do with timestamptz, with a lot of back and forths...
Npgsql is here mimicking the PostgreSQL native behavior for timestamptz: when you select a timestamptz value, it will be converted to your local time, based on the PG session's timezone. Note you can always call ToUTC() to convert it back to a UTC DateTimeOffset; however, at that point one must ask why use a DateTimeOffset at all.
I'm generally averse to providing config options and flags for every behavior users want, since in the end you get an unpredictable and unwieldy driver. Since PG implements this behavior, I think it makes sense for Npgsql to do so as well.
Activity
roji commentedon May 27, 2020
@giuliano-barberi-tf there is a long history around what to do with timestamptz, with a lot of back and forths...
Npgsql is here mimicking the PostgreSQL native behavior for timestamptz: when you select a timestamptz value, it will be converted to your local time, based on the PG session's timezone. Note you can always call ToUTC() to convert it back to a UTC DateTimeOffset; however, at that point one must ask why use a DateTimeOffset at all.
I'm generally averse to providing config options and flags for every behavior users want, since in the end you get an unpredictable and unwieldy driver. Since PG implements this behavior, I think it makes sense for Npgsql to do so as well.
giuliano-barberi-tf commentedon May 27, 2020
Alright thanks for the quick reply.