Skip to content

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

@giuliano-barberi-tf

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

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