Skip to content

Fix NpgsqlDateTime.Add #3019

Closed
Closed

Description

@GreenYun

Before submitting

This repo is for Npgsql ADO.NET issues only. Entity Framework issues belong in Npgsql.EntityFrameworkCore.PostgreSQL for EF Core, or EntityFramework6.Npgsql for EF 6.x.

Steps to reproduce

            var interval = new NpgsqlTimeSpan(1, 0, 0); // 1 mon 0 days 0 milliseconds
            var timestart = new NpgsqlDateTime(2020, 2, 1, 0, 0, 0); // 2020-02-01 00:00:00
            var timeend = timestart.AddMonths(1); // 2020-03-01 00:00:00 as expected
            var nexttimeend = timeend.Add(interval); // Expected 2020-04-01 00:00:00 but actually 2020-03-01 00:00:00

The issue

It is common to think that we should add Months and Days to sum up the new NpgsqlDateTime but we found the source code writes:

public NpgsqlDateTime Add(in NpgsqlTimeSpan value) { return AddTicks(value.Ticks); }

which means Months and Days not added. I would wonder know if it is a feature, and how to do if I want to add the exact TimeSpan to a particular DataTime.

Further technical details

Npgsql version: 3.1.4
PostgreSQL version: 12.3 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 10.1.0, 64-bit
Operating system: x86_64 PC linux

Other details about my project setup:

Activity

  1. roji commented on Jun 19, 2020

    @roji
    Member

    That certainly seems like a bug, would you like to submit a PR to fix this?

    Note that the Npgsql-specific date/time types (NpgsqlDateTime, NpgsqlTimeSpan...) are considered somewhat deprecated and are not generally recommended for use (see #2009). This is especially true since Npgsql supports NodaTime.

    Test:

    [Test]
    public void NpgsqlDateTimeMath()
    {
        Assert.That(new NpgsqlDateTime(2020, 1, 1, 0, 0, 0).Add(new NpgsqlTimeSpan(1, 1, 0)),
            Is.EqualTo(new NpgsqlDateTime(2020, 2, 2, 0, 0, 0)));
    }
  2. added this to the 4.1.4 milestone on Jun 19, 2020
  3. warcha commented on Jun 26, 2020

    @warcha
    Contributor

    I've pushed a fix for this. Note though that the final expected value in @GreenYun's repro example is incorrect as adding NpgsqlTimeSpan(1, 0, 0) will only add 30 days not 31 days (it does not take into account the month of the date being added to) as the implementation of the NpgsqlTimeSpan sets DaysPerMonth = 30.

    For the repro example provided, adding 1 month to the start date will set timeend = 2020-03-01 as expected, then adding NpgsqlTimeSpan(1, 0, 0) to this will set nexttimeend = 2020-03-31 (as 30 days will be added) not 31 as expected in the example provided.

    Kind Regards,
    Warren Chan
    Fujitsu Australia

  4. added a commit that references this issue on Jun 27, 2020
    21bdc52
  5. roji commented on Jun 27, 2020

    @roji
    Member

    Thanks @warcha as always for a good contribution.

    Seeing your tests and the incorrect behavior we have around 1 month = 30 days, I'm even more convinced that these types shouldn't really exist as they are (#2009). When this comment says that this behavior is "for consistency with PostgreSQL", I don't really know what that means, since SELECT '2020-1-1'::timestamp + '1 month 2 days'::interval; gives back 2020-02-03, and not the erroneous 2020-02-02 as your tests show.

    At the end of the day, date and time calculations are a really tough business, and should be done by dedicated libraries/types which do them correctly (e.g. Nodatime). While it's a nice goal for the driver to provide access to all values supported by PostgreSQL, I don't think that justifies us having our own date/time implementation (which is quite buggy and definitely inefficient). We may want to consider a middle ground, which is to only provide types for reading/writing the information, but not perform calculations on it. I'm not sure how useful that would actually be, but then again buggy calculations also aren't that useful. We can continue this conversation in #2009.

    One super small nitpick: would it be possible to include "Closes #" or "Fixes #" as appropriate in your commit messages? This automatically closes the issue and links it with the PR as appropriate, and allows to easily go back from the git history to the relevant issue etc.

  6. warcha commented on Jun 28, 2020

    @warcha
    Contributor

    Thanks @roji and yes I was a bit surprised by the comment about "consistency with PostgreSQL" but took the comment for what it was and didn't even think to check the functionality at the PostgreSQL level. I will definitely be following the conversation in #2009

    Apologies for the nitpick, I'll commit future changes with the appropriate message tags as mentioned.

    Kind Regards,
    Warren Chan
    Fujitsu Australia

  7. roji commented on Jun 28, 2020

    @roji
    Member

    Definitely no need to apologize for anything, your contributions are of a very high quality and are much appreciated!

  8. changed the title [-]Is NpgsqlDateTime.Add Method a feature?[/-] [+]Fix NpgsqlDateTime.Add[/+] on Jul 19, 2020
  9. added theissue type on Jun 18, 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

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions