You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
varinterval=newNpgsqlTimeSpan(1,0,0);// 1 mon 0 days 0 millisecondsvartimestart=newNpgsqlDateTime(2020,2,1,0,0,0);// 2020-02-01 00:00:00vartimeend=timestart.AddMonths(1);// 2020-03-01 00:00:00 as expectedvarnexttimeend=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:
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
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.
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.
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.
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.
Activity
roji commentedon Jun 19, 2020
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:
warcha commentedon Jun 26, 2020
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-01as expected, then addingNpgsqlTimeSpan(1, 0, 0)to this will setnexttimeend = 2020-03-31(as 30 days will be added) not 31 as expected in the example provided.Kind Regards,
Warren Chan
Fujitsu Australia
Fix NpgsqlDateTime.Add when adding NpgsqlTimeSpan
roji commentedon Jun 27, 2020
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 back2020-02-03, and not the erroneous2020-02-02as 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.
warcha commentedon Jun 28, 2020
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
roji commentedon Jun 28, 2020
Definitely no need to apologize for anything, your contributions are of a very high quality and are much appreciated!
[-]Is NpgsqlDateTime.Add Method a feature?[/-][+]Fix NpgsqlDateTime.Add[/+]