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 new feature addBusinessDays #1154

Merged
merged 9 commits into from Jun 10, 2019

Conversation

codinsonn
Copy link
Contributor

@codinsonn codinsonn commented Apr 26, 2019

Should partly solve #584. Allthough it does not take holidays into consideration, I recon it might still have some value for end users.

@codinsonn
Copy link
Contributor Author

codinsonn commented Apr 26, 2019

PS: Also planning on making a PR for a getWeekDays function somewhere in the near future.

This is how I've solved the lack thereof so far:

// Get a range or number of weekdays
export const weekDays = (start: DirtyDateType, end: DirtyDateType, asNumber: boolean = false) => {
    const businessDays = eachDay(toDate(start), toDate(end)).filter(d => !isWeekend(d));
    return asNumber ? businessDays.length : businessDays;
};

Is a function like this something that could be handy in your opinion?

Copy link
Member

@kossnocorp kossnocorp left a comment

Choose a reason for hiding this comment

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

I like the idea but weekday means any day, not just working days so I suggest naming it addWorkingDays.

@kossnocorp
Copy link
Member

Update: I've figured that "business day" is more common term, so please name it addBusinessDays.

@codinsonn codinsonn force-pushed the feature/addWeekDays branch 2 times, most recently from 6f23b5a to da817a6 Compare June 9, 2019 19:36
@codinsonn
Copy link
Contributor Author

Update: I've figured that "business day" is more common term, so please name it addBusinessDays.

Updated to addBusinessDays as requested 🙌

@kossnocorp
Copy link
Member

kossnocorp commented Jun 10, 2019

Thanks for the update! I see that the tests fail in America/Santiago time zone: https://travis-ci.org/date-fns/date-fns/jobs/543469770#L691

The problem that your tests operate on the DST edge (which is a lucky coincidence) and it causes an hour shift. Look how eachDayOfInterval solves this problem: https://github.com/date-fns/date-fns/blob/master/src/eachDayOfInterval/index.js#L94. However, in your case, instead of setting time to 00:00, you have to save hours from the original date and reapply in each iteration. I think this will work.

Since your function, is affected by DST, please add a DST test. See:

Also, please rebase your branch with the fresh master.

Please tell me if you need assistance!

@codinsonn codinsonn changed the title Add new feature addWeekDays Add new feature addBusinessDays Jun 10, 2019
@codinsonn
Copy link
Contributor Author

I'm not 100% sure I follow.
I get that the DST is giving problems in some regions, but I'm still unsure of how to fix it.

More specifically, I'm unsure of:

  • How to "save" hours in the function
  • If I should move the failing test from ./src/addBusinessDays/test.js to ./test/dst/addBusinessDays/basic.js

PS: I'll likely only have time to get back to this in a few days.
Any help would be appreciated though.

@kossnocorp
Copy link
Member

Alrighty, I'll proceed from here 👌

@kossnocorp
Copy link
Member

You can see the solution for the DST problem: 03bbf4d

kossnocorp
kossnocorp previously approved these changes Jun 10, 2019
Copy link
Member

@kossnocorp kossnocorp left a comment

Choose a reason for hiding this comment

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

It's complete.

@kossnocorp kossnocorp merged commit c00ef1d into date-fns:master Jun 10, 2019
@kossnocorp
Copy link
Member

This PR was released as v2.0.0-alpha.32: https://gist.github.com/kossnocorp/a307a464760b405bb78ef5020a4ab136#v200-alpha32

@codinsonn
Copy link
Contributor Author

Thanks a bunch for the help and finalisation! 🙌 I’ll try to take some time this weekend to check out how the final pieces actually connect with fixing the tests

@hugows
Copy link

hugows commented Jul 3, 2019

Its very unexpected that this function doesn't accept negative arguments.

elmomalmo pushed a commit to elmomalmo/date-fns that referenced this pull request Jul 12, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants