Skip to content

Translate DateTimeOffset members #473

Closed

Description

@SamHuo213

Hi,

I am using EF Core 2.1 and PostgreSQL. I have some entities that have a column in DateTimeOffset. Whenever I use these columns in a query the execution is happening on Client side. I would like to be able to do diffs on two DateTimeOffset on client side; I have also looked into DbFunctions but there seems to be no support for that either.

The following code is what I am trying to accomplish:

context.Entity
.Where(x => EF.Functions.DateDiffSeconds(x.ExpirationTime, DateTimeOffset.UtcNow))
.ToList();

Thanks
Sam

Activity

  1. roji commented on Jun 18, 2018

    @roji
    Member

    DateDiffSeconds is defined in SqlServerDbFunctionsExtensions, which means it's an SqlServer-specific method - there's no reason to expect it to work server-side on PostgreSQL.

    The Npgsql provider does translate operations on DateTime, so you should be able to write .Where(x => x.ExpirationTime - DateTime.UtcNow > y). However, DateTimeOffset members aren't translated yet - will keep this issue open to track that.

    In that meantime, consider using DateTime instead of DateTimeOffset (especially since PostgreSQL doesn't actually save the offset in the database).

  2. added this to the 2.2.0 milestone on Jun 18, 2018
  3. changed the title [-]DateTimeOffset diff is executed on client side[/-] [+]Translate DateTimeOffset members[/+] on Jun 18, 2018
  4. roji commented on Jun 18, 2018

    @roji
    Member

    Actually, thinking about this again, you should be able to subtract one DateTimeOffset from another, yielding a TimeSpan (just like you can subtract one DateTime from another). Give that a try.

  5. SamHuo213 commented on Jun 18, 2018

    @SamHuo213
    Author

    Thank you for the quick response. I followed your recommendation and tried doing the following which did not work the query is still being executed on client side:

    context.Entity
    .Where(x => (x.ExpirationTime - DateTimeOffset.UtcNow) <= TimeSpan.Zero)
    .ToList();

    However, I did do this and it seems to be working on server side now:

    var now = DateTimeOffset.UtcNow;
    context.Entity
    .Where(x => x => x.ExpirationTime <= now)
    .ToList();

  6. roji commented on Jun 20, 2018

    @roji
    Member

    @SamHuo213 great, your working solution is actually the best one.

    Regarding the one that isn't working, I'll try to dig deeper but it will probably take me some time to get around to it. Will keep this issue to track that.

  7. added
    good first issue
    on Jul 4, 2018
  8. sguryev commented on Jul 4, 2018

    @sguryev

    @SamHuo213 thank you for your post. So the trick is to save the static property value to the local variable?

  9. roji commented on Jul 4, 2018

    @roji
    Member

    Looking at this again, any query which has a DateTimeOffset member (such as UtcNow) will be evaluated client-side because the Npgsql provider currently doesn't translate any DateTimeOffset members. This issue tracks that, and once it's done you should be able to have expressions such as x.ExpirationTime - DateTimeOffset.UtcNow in your query and have that evaluated by PostgreSQL.

    The workaround of having a local variable assigned to DateTimeOffset.UtcNow is ugly and not ideal, but it's probably the best temporary solution until translation is implemented.

    Please note that DateTime's members are translated, so if you switch from DateTimeOffset to DateTime everything will work as expected. As a general rule DateTime is recommended over DateTimeOffset when using PostgreSQL, since the database doesn't actually save the offset component. Long story short, unless you're very clear on why you need DateTimeOffset (and these cases should be quite rare), it's really recommended you consider using DateTime instead. If you need any more info on this feel free to ask.

  10. SamHuo213 commented on Jul 4, 2018

    @SamHuo213
    Author

    @sguryev , that is correct. Avoiding DateTimeOffset members is my work around. DateTime members such as Now & UtcNow are working. Looks like it is being explicitly translated here: https://github.com/npgsql/Npgsql.EntityFrameworkCore.PostgreSQL/blob/dev/src/EFCore.PG/Query/ExpressionTranslators/Internal/NpgsqlDateTimeMemberTranslator.cs.

    @roji , thanks for your reply. Yes I know that DateTime members are working. However, the decision to switch types is not up to me. I will try to get management to reconsider switching back to DateTime.

  11. sguryev commented on Jul 13, 2018

    @sguryev

    Hi guys. It's again me.
    What about the case when I have to compare the Date part of the DateTime (DateTimeOffset).
    Something like:
    .Where(x => x.Date.Date <= nowDateTimeOffset)
    It looks like the issue is in the Date.Date request.
    I have tried the DbFunctions:
    .Where(x => EF.Functions.DateDiffDay(x.Date, nowDateTimeOffset) <= 0)
    But it doesn't work as well.

    Does it mean that all DateTime value mutations before filtering are not supported?
    Thank you!
    @roji

  12. roji commented on Jul 13, 2018

    @roji
    Member

    @sguryev, DateTime.Date is properly translated to SQL, producing the following:

    SELECT x."Id", x."Date", x."Name"
            FROM blogs AS x
            WHERE DATE_TRUNC('day', x."Date") <= NOW()

    However, as written above, no members of DateTimeOffset are currently translated - this is what this issue is about.

    Finally, DateDiffDay() is an SQL Server-specific extension and doesn't work when using Npgsql.

  13. sguryev commented on Jul 13, 2018

    @sguryev

    @roji Oh, thank you! So the problem not with the .Date but with the DateTimeOffset.Date. So DateTime.Date should work. Thank you! I didn't check the DateTime usecase yet.

    DateDiffDay() returns the same error about the client side evaluation (as I remember). So nothing noted me that this will not work with NPG. And yes - I had to add the SqlServer Nuget package in order to get this function available. I thought that NPG support all functions from SQL + some own from PostgreExtensions

  14. roji commented on Jul 13, 2018

    @roji
    Member

    It really is discouraged to use timestamp with time zone unless you have a very good reason to do so - consider regular timestamp with DateTime on the. NET side. timestamp with time zone is a quirky data type that is better avoided in most scenarios (this is also why translation of members on DateTimeOffset haven't been implemented yet).

    SQL Server is a different database product with it's own provider, anything coming from that package will only work with that provider, not with Npgsql. Many generic components and translations do exist, but they're in the core of EF Core and in not in the SQL Server package.

    Apparently DateDiffDay() contains a C# implementation, which means you can do client evaluation. This is why it worked (but triggered client evaluation).

  15. 33 remaining items

  16. added 11 commits that reference this issue on Sep 8, 2021
    11e12aa
    c3d8cf6
    6580c8f
    556ea64
    e536be5
    6bbafa5
    9aff107
    64f36a3
    bb36860
    be01865
    7062d6c
  17. added a commit that references this issue on Sep 23, 2021
    aac1a9e
  18. added theissue type on Jun 19, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions