Skip to content

UTC DateTime #2669

Closed as not planned
Closed as not planned

Description

@btecu

After updating to the latest EF Core 3 and therefore Npgsql 4.0 I've started to have some issues - which might have been pre-existing or not.

Say I have a model:

public class Model {
  public int Id { get; set; }
  public string Name { get; set; }
  public DateTime Created { get; set; }
}

After updating to version 4, I noticed the following in the ContextModelSnapshot.cs:

b.Property<DateTime>("Created")
                        .HasColumnType("timestamp without time zone");

I've also noticed that although I save someDate.ToUniversaltTime(), it comes back with an unspecified kind.

What's the easiest solution for saving and retrieving UTC date times? What I need is to have it as Kind.UTC when retrieving it from the database.

I've been reading here http://www.npgsql.org/doc/types/datetime.html but there's no code example and I don't really understand where I'm supposed to set NpgsqlDbType.TimestampTz, to what I'm supposed to set it or even if this is the correct way.
I use Npgsql.EntityFrameworkCore.PostgreSQL exclusively, no SQL queries.

Activity

  1. roji commented on Oct 14, 2019

    @roji
    Member

    As you can see in the type mapping docs, DateTime is by default mapped to timestamp without time zone. When reading that back from the database, you get instances with Kind=Unspecified.

    You can change your database column's type to timestamp with time zone by specifying that (see the general EF Core docs). This will give you back DateTime instances with Kind=Local.

    If you know you're always storing UTC values and want to have DateTime instances with Kind=UTC, you can set up value conversion to set the Kind manually. Another option is simply to ignore the type and treat DateTime with Kind=Unspecified as UTC.

    Am closing as I don't see anything actionable on Npgsql's side, but we can continue discussing this (and eventually reopen if we decide that any change is appropriate).

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