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

Created extensions adding business day functions #706

Closed
wants to merge 1 commit into from
Closed

Created extensions adding business day functions #706

wants to merge 1 commit into from

Conversation

rmblstrp
Copy link

@rmblstrp rmblstrp commented Jul 6, 2016

I created a set of extension methods which provides a flexible set of
methods that allow the determination if a date is a business day as
well as determining the next or previous business day. Custom holiday
and business schedules can be supplies to the methods to allow an
end-user to determine what holidays, if any, are to be observed and the
days that fit operating schedule of the business being modeled.

NOTE: I know that this functionality has been asked for before but it seemed that you didn't want to include the functionality into the Carbon class. I felt that extending Carbon would be a bad idea because it would lock implementations to a certain subclass and created a hierarchical mess if there were multiple subclasses with some users wanting functionality that was contained in 2 or more subclasses. Because of those reasons I opted to create an extension class where other contributors could add in functionality to extend Carbon while keeping the Carbon class pure. If that functionality is deemed worthy enough to be promoted to the Carbon class it can be done easily without breaking backwards compatibility and without having duplication of code.

This was referenced Jul 6, 2016
I created a set of extension methods which provides a flexible set of
methods that allow the determination if a date is a business day as
well as determining the next or previous business day. Custom holiday
and business schedules can be supplies to the methods to allow an
end-user to determine what holidays, if any, are to be observed and the
days that fit operating schedule of the business being modeled.
// Check to see if a date is a business day defaulting to a Monday
// through Friday work week
$today = Carbon::now();
CarbonExtensions::isBusinessDay($today);
Copy link
Collaborator

Choose a reason for hiding this comment

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

\Carbon\Carbon::isWeekday already exists for that

Copy link
Author

@rmblstrp rmblstrp Jul 14, 2016

Choose a reason for hiding this comment

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

isWeekday is not the same as isBusinessDay. Weekdays are Monday-Friday whereas a business might only be open Friday-Sunday. Additionally, isBusinessDay checks to see if the day is a holiday if a holiday schedule is passed in as a parameter.

For example: A business operates on a Monday - Friday schedule. The current date is on a Friday and the following Monday is a holiday. You need to know what the date is going to be for 3 business days from now. Since the following Monday is a holiday the date would be for the following Thursday.

While the basic functionality of isBusinessDay does the same as isWeekday, the difference is that it allows for but does not require custom business and holiday schedules.

@lucasff
Copy link

lucasff commented Nov 10, 2016

+1. Using setWeekendDays and afterward using addWeekdays seems to not have any effect at all.

@lucasmichot
Copy link
Collaborator

@briannesbitt whats your opinion on that?
I feel this should probably target 2.x

@lucasmichot
Copy link
Collaborator

@rmblstrp It seems the test are failing...
Can you squash and rebase ?

Copy link
Collaborator

@lucasmichot lucasmichot left a comment

Choose a reason for hiding this comment

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

Many docblocks missing or to fix

*
* @author Christopher McGinnis <[email protected]>
*
* @param Carbon $date
Copy link
Collaborator

Choose a reason for hiding this comment

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

for param, return and throw docblocks we use FQN class name

{
do {
$date->addDay();
} while (!self::isBusinessDay($date, $schedule));
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can you call with static:: instead ?

*/
public static function addBusinessDays(Carbon $date, $days, $schedule = null)
{
while ($days != 0) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Any reason not to use a stricter comparison !== ?


interface HolidaySchedule
{
public function isHoliday(Carbon $date);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Docblock?

use Carbon\HolidaySchedule;

/**
* Class FederalHolidays
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can you remove the class name declaration in this docblck ?

*
* @property-read int $year;
* @property int $observedHolidays;
* @property-read Carbon $newYearsDay;
Copy link
Collaborator

Choose a reason for hiding this comment

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

\Carbon\Carbon instead

*
* @returns bool
*/
public function isHoliday(Carbon $date)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Carbon $carbon instead

*/
class FederalHolidays implements HolidaySchedule
{
const NEW_YEARS_DAY = 0x0001;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Missing docblocks for const and properties

// created a copy of the datetime object to we can remove the time values without affecting
// the passed in instance

return ($this->newYearsDay->isSameDay($date) && $this->isObserved(self::NEW_YEARS_DAY))
Copy link
Collaborator

Choose a reason for hiding this comment

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

I would separate this huge conditions in smaller conditions

*/
protected function getNewYearsDay()
{
return Carbon::create($this->year, 1, 1, 0, 0, 0);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why not calling Carbon::now()->startOfYear()?

@briannesbitt
Copy link
Owner

Seems possible, but waiting to v2 might be a good idea.

Not sure why "CarbonExtensions" is so generic where the interface is specific. Seems like a more specific class name makes more sense ?

@rmblstrp
Copy link
Author

@briannesbitt Agreed. I put this together because we needed this functionality in a work projects. I'll retool it a bit so it makes more sense and get it ready for the 2.x release.

@lucasff
Copy link

lucasff commented Jan 9, 2018

up?

@Glavic Glavic added the macro candidate Rather than new methods, this could be a macro/mixin label Feb 14, 2018
@kylekatarnls kylekatarnls added this to the 2.0 milestone Feb 28, 2018
Repository owner deleted a comment from lucasmichot Apr 14, 2018
Repository owner deleted a comment from lucasmichot Apr 14, 2018
@kylekatarnls
Copy link
Collaborator

Hi, I started this library (compatible with the coming soon 1.26 version) based on @rmblstrp work:
https://github.com/kylekatarnls/business-day

You're all invited to test, contribute, add holidays lists, etc.

@kylekatarnls kylekatarnls added documentation Rely to documentations or gh-pages branch and removed macro candidate Rather than new methods, this could be a macro/mixin labels Apr 15, 2018
@kylekatarnls kylekatarnls reopened this Apr 15, 2018
@kylekatarnls
Copy link
Collaborator

I close this issue as kylekatarnls/business-day should fulfill the need. If something is missing please open an issue here: https://github.com/kylekatarnls/business-day/issues

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Rely to documentations or gh-pages branch
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants