Skip to content

Ability to configure whether DateTimeOffset is deserialized as UTC (without offset) #2985

Closed

Description

@giuliano-barberi-tf

The issue

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

Activity

  1. roji commented on May 27, 2020

    @roji
    Member

    @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.

  2. giuliano-barberi-tf commented on May 27, 2020

    @giuliano-barberi-tf
    Author

    Alright thanks for the quick reply.

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