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

Add businessDiff method #45

Open
wants to merge 16 commits into
base: master
Choose a base branch
from

Conversation

kristremblay
Copy link

@kristremblay kristremblay commented Feb 17, 2021

Added businessDiff method to get number of business days between DateTimes.

After completing the switch from Moment to Luxon, I realized this functionality was missing in this library. The solution was to borrow from https://github.com/kalmecak/moment-business-days/blob/master/index.js.

Usage:

const targetDate = DateTime.local().plusBusiness({ days: 10 });

const businessDaysDiff = DateTime.local().businessDiff(targetDate);

// businessDaysDiff = 10

@codecov-io
Copy link

codecov-io commented Feb 20, 2021

Codecov Report

Merging #45 (6a81a34) into master (3ca6291) will not change coverage.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff            @@
##            master       #45   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files            5         5           
  Lines          116       133   +17     
  Branches        23        33   +10     
=========================================
+ Hits           116       133   +17     
Impacted Files Coverage Δ
src/index.js 100.00% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 3ca6291...6a81a34. Read the comment docs.

@kristremblay
Copy link
Author

@amaidah oddly, the tests were passing when this was submitted but when I opened vscode today they were failing. I've refactored to clear out some of the spider webs left over from a day of working :)

@amaidah
Copy link
Owner

amaidah commented Feb 27, 2021

Hey, thanks for opening a PR -- I will take a look (can't promise this weekend). I remember thinking about this method originally and had thought about changing the interface a bit.

@kristremblay
Copy link
Author

Sounds good, and you're welcome 😄

@kristremblay
Copy link
Author

Swapped out the relative param for a config object.

@kristremblay
Copy link
Author

@amaidah sorry to bother, but have you had a chance to take a peek?

@amaidah
Copy link
Owner

amaidah commented Apr 26, 2021

@amaidah sorry to bother, but have you had a chance to take a peek?

Thanks for being super patient, not yet. Will try for this week

src/index.test.js Outdated Show resolved Hide resolved
src/index.js Outdated Show resolved Hide resolved
/**
* @typedef BusinessDiffConfig
* @property {boolean} [includeEndDate=false] - include the end date in the calculation
* @property {boolean} [relative=false] - signs the return value as negative if end date is in the past
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I think about how I use a calendar, I say things like 2 days ago (-2) so it doesn't make total sense to me that a difference would be absolute by default. Is there a reason for this? I remember being confused with the moment version of this plugin when I first saw that too.

Both diff and diffNow are relative and I'd like to to keep this plugin similar to Luxon core concepts

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the rationale is that you already have methods for determining whether a date is in the past and it is probably a less common scenario to need a negative value for a business day calculation.

That being said, consistency is a really strong argument so if you think it would be better defaulting relative to true I will support that.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok cool, let's go with relative as default

src/index.js Show resolved Hide resolved

it('knows there are 3 business days between two dates that are 3 business days apart', () => {
const myCompanyTakesNoHolidays = [];
const startDate = DateTime.local().startOf('day');
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is .startOf necessary for the tests?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In all honestly, it's been a while and it's possible that startOf only seemed necessary as a symptom of another resolved problem.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removing them on local seems to not affect the tests, perhaps we can refactor them out.

@codecov-commenter
Copy link

codecov-commenter commented Apr 29, 2021

Codecov Report

Merging #45 (944ebb5) into master (3ca6291) will not change coverage.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff            @@
##            master       #45   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files            5         5           
  Lines          116       136   +20     
  Branches        23        34   +11     
=========================================
+ Hits           116       136   +20     
Impacted Files Coverage Δ
src/index.js 100.00% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 3ca6291...944ebb5. Read the comment docs.

src/index.test.js Outdated Show resolved Hide resolved
src/index.test.js Outdated Show resolved Hide resolved
Put startOf call back in.

Co-authored-by: Andrew Maidah <[email protected]>
@kristremblay
Copy link
Author

kristremblay commented May 1, 2021

Hmm...running into some failing tests when the current date is not a valid business day. Going to investigate.

How should calculations work if performed on a non-business day?

@kristremblay
Copy link
Author

Ok upon further investigation, it was already working correctly, it was just a matter of a bad test.

I am interested in your input on this, but maybe we should have a separate test for starting on a non-business day.

@amaidah
Copy link
Owner

amaidah commented May 1, 2021

How should calculations work if performed on a non-business day?

Should just return the number of days that are business days between the two compared dates right? So if starting on Sat, and the next day is Sun and both are nonbusiness running a diffBusiness on that range should return 0. I'm curious what would happen though if includeEndDate were true in this scenario

I am interested in your input on this, but maybe we should have a separate test for starting on a non-business day.

Yes, now that you bring it up, sounds smart to test for a few more things:

  • Starts on a non-business day (in general)
  • Ends on non-business day (in general)
  • Starts on business day but ends on non-business day and includes end date
  • Start on non-business day and ends on non-business day with no business days in the range and includes end date

I also include error handling conditional for the other methods:

  if (!dt.isValid) {
    return dt;
  }

Maybe we should think about returning an error if trying to call diffBusiness on something that isn't a DateTime

@kristremblay
Copy link
Author

I'm curious what would happen though if includeEndDate were true in this scenario

That's actually a good point that isn't currently accounted for. I just added the following:

let daysDiff = Number(
  includeEndDate && end.isBusinessDay() && !end.isHoliday()
);

Testing for starting on a non-business day should be next on the list. I will work on that over the weekend. In the meantime, I've updated the branch to use a static start date of Monday May 3 2021.

@amaidah
Copy link
Owner

amaidah commented May 10, 2021

I'm curious what would happen though if includeEndDate were true in this scenario

That's actually a good point that isn't currently accounted for. I just added the following:

let daysDiff = Number(
  includeEndDate && end.isBusinessDay() && !end.isHoliday()
);

Testing for starting on a non-business day should be next on the list. I will work on that over the weekend. In the meantime, I've updated the branch to use a static start date of Monday May 3 2021.

FYI, I merged in a bunch of dependabot PRs so you may need to pull from master

@kristremblay
Copy link
Author

Thanks for letting me know!

@hdoan-codaio
Copy link

I'm interested in using this method. Can you merge the PR?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants