-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
FIX Update date.lib.php Function num_between_day #31434
Conversation
When debugging Asset Module, I dsicovered that function num_between_day is returning nothing when $timestampStart equals (==) $timestampEnd Normally it has to return 0 if $lastday = 0 1 if $lastday = 1
htdocs/core/lib/date.lib.php
Outdated
@@ -852,7 +852,7 @@ function num_public_holiday($timestampStart, $timestampEnd, $country_code = '', | |||
*/ | |||
function num_between_day($timestampStart, $timestampEnd, $lastday = 0) | |||
{ | |||
if ($timestampStart < $timestampEnd) | |||
if ($timestampStart <= $timestampEnd) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When fixing a bug in old version, it is better to backport the fix from a recent version.
Can you instead suggest a PR that take the code of develop branch that seems to already have this fixed ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @eldy
Sorry but I did not find the fix in the latest develop version but maybe I did not look in the right version. (See my screenshot)
This is why then I parsed all the versions and I stop at 12.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @eldy
The correction in develop seems to be wrong as we need to get :
0 if $lastday = 0
1 if $lastday = 1
and not always 0
So I am suggesting to add the else part on my solution but we need to add <= to get the value 1 'mandatory in asset module.
Thoughts ?
Hi @eldy So I am suggesting to add the else part on my PR but we need to add <= to get the value 1 mandatory in asset module in dev code. I can do another PR to fix dev first? |
I don't see what you should get 1 when the 2 date are the same, because we should never call the method with the 2 timestamp that are same (that contains hours). So 0 is a default value when param are wrong. |
Hi @eldy |
Hi @eldy |
Hi @eldy The issue is coming from the use of function floor() instead of ceil() Here is the explanation :
Both dates are precisely at midnight (00:00:00), so there's no partial-day issue to account for here. The difference between the two timestamps is calculated as follows: Now, we convert the difference in seconds to days: Without including the last day, this would result in 277 days. Since we passed LastDay = 1, we want to include the last day in the count. To do this, we add 1 to the total number of days:
Final Result: The total number of days between 2017-12-27 (inclusive) and 2018-09-30 (inclusive) is 278 days. Let's calculate the number of days between December 27, 2017 and September 30, 2018, using a calendar-based method by considering the days in each month: We'll break it down month by month.
Total Days Calculation: Now, let's sum up all the days: Final Result: Could I move forward and change the code to use ceil() and also the <= operand as already requested in dev and then backport this on version 12 or any version you may suggest? |
Hi @eldy After another round of test, I discovered other issues using ceil() function.
} Let me know your thoughts. |
Situation is more complex than that: This is why there is the comment of the function say: "date must be UTC date !" Whatever is the use of this function, results will always be unpredictable if parameters are not UTC dates. In your example, you are not using UTC dates. So for So trouble seems not into the function, but into the caller of function that does not use GMT date. All phpunit tests pass with success the function, in all different use case, so i doubt trouble is inside this function.
|
Well I will take a Doliprane before it is done by USA ;-) |
The problem is surely here: i added a param to solve the trouble around the function getCurrentPeriodOfFiscalYear() so it can return a value in UTC. |
Hi @eldy Many thanks for all the explanation. This is now crystal clear.
Waiting your directions |
It's difficult to may to say if you can use gmt everywhere. it depends only what is the date, what it represent and how we plan to use it and for what. For the fix of the <= instead of <, you can try this, push the fix on the develop branch (this is a critical change that can have side effect), and we will see if CI says every thing is ok. If yes, i will validate it on develop. |
@josett225 @eldy i don't understand this problem !
why would you |
@josett225 create another function or specific module if you would your specific function ! |
@josett225 This function was done for a short time! here you give examples over a year! adds a function that calculates over a year or more! |
@josett225 and have a good day ! ;-) |
@josett225 and don't forget leap years (bisextile), that's probably the problem! ;-) Good luck ! |
Hi @eldy and @hregis
2012-01-01 between 2012-01-01 Therefore this is a bug in this function and we need to avoid to add extra code to compensate this issue. Let me know if you need more information and enjoy your day. |
@josett225 the function being made to calculate the number of days between two dates, when is it if the date is exactly the same? This is a philosophical question... can there be a last day? :-) |
As this may change behaviour, if we change something, it must be in develop. A PR was merged in develop so we can test for v21 if everything is ok and we can cancel this one. |
FIX - When debugging Asset Module, I discovered that function num_between_day is returning nothing when $timestampStart equals (==) $timestampEnd
Normally it has to return
0 if $lastday = 0
1 if $lastday = 1