Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UTC DateTime #2669

Closed
btecu opened this issue Oct 4, 2019 · 1 comment

Comments

@btecu
Copy link

commented Oct 4, 2019

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.

@roji

This comment has been minimized.

Copy link
Member

commented Oct 14, 2019

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

@roji roji closed this Oct 14, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants
You can’t perform that action at this time.