diff --git a/src/__phutil_library_map__.php b/src/__phutil_library_map__.php index 828ec80ad2..8ee103557f 100644 --- a/src/__phutil_library_map__.php +++ b/src/__phutil_library_map__.php @@ -531,6 +531,7 @@ phutil_register_library_map(array( 'PhabricatorCalendarController' => 'applications/calendar/controller/base', 'PhabricatorCalendarDAO' => 'applications/calendar/storage/base', 'PhabricatorCalendarHoliday' => 'applications/calendar/storage/holiday', + 'PhabricatorCalendarHolidayTestCase' => 'applications/calendar/storage/holiday/__tests__', 'PhabricatorChangesetResponse' => 'infrastructure/diff/response', 'PhabricatorChatLogChannelListController' => 'applications/chatlog/controller/channellist', 'PhabricatorChatLogChannelLogController' => 'applications/chatlog/controller/channellog', @@ -1465,6 +1466,7 @@ phutil_register_library_map(array( 'PhabricatorCalendarController' => 'PhabricatorController', 'PhabricatorCalendarDAO' => 'PhabricatorLiskDAO', 'PhabricatorCalendarHoliday' => 'PhabricatorCalendarDAO', + 'PhabricatorCalendarHolidayTestCase' => 'PhabricatorTestCase', 'PhabricatorChangesetResponse' => 'AphrontProxyResponse', 'PhabricatorChatLogChannelListController' => 'PhabricatorChatLogController', 'PhabricatorChatLogChannelLogController' => 'PhabricatorChatLogController', diff --git a/src/applications/calendar/storage/holiday/PhabricatorCalendarHoliday.php b/src/applications/calendar/storage/holiday/PhabricatorCalendarHoliday.php index 7d0d8d671e..242fff57f9 100644 --- a/src/applications/calendar/storage/holiday/PhabricatorCalendarHoliday.php +++ b/src/applications/calendar/storage/holiday/PhabricatorCalendarHoliday.php @@ -27,4 +27,22 @@ final class PhabricatorCalendarHoliday extends PhabricatorCalendarDAO { ) + parent::getConfiguration(); } + public static function getNthBusinessDay($epoch, $n) { + // Sadly, there are not many holidays. So we can load all of them. + $holidays = id(new PhabricatorCalendarHoliday())->loadAll(); + $holidays = mpull($holidays, null, 'getDay'); + $interval = ($n > 0 ? 1 : -1) * 24 * 60 * 60; + + $return = $epoch; + for ($i = abs($n); $i > 0; ) { + $return += $interval; + $weekday = date('w', $return); + if ($weekday != 0 && $weekday != 6 && // Sunday and Saturday + !isset($holidays[date('Y-m-d', $return)])) { + $i--; + } + } + return $return; + } + } diff --git a/src/applications/calendar/storage/holiday/__init__.php b/src/applications/calendar/storage/holiday/__init__.php index c5c4a005c2..f761714e78 100644 --- a/src/applications/calendar/storage/holiday/__init__.php +++ b/src/applications/calendar/storage/holiday/__init__.php @@ -8,5 +8,7 @@ phutil_require_module('phabricator', 'applications/calendar/storage/base'); +phutil_require_module('phutil', 'utils'); + phutil_require_source('PhabricatorCalendarHoliday.php'); diff --git a/src/applications/calendar/storage/holiday/__tests__/PhabricatorCalendarHolidayTestCase.php b/src/applications/calendar/storage/holiday/__tests__/PhabricatorCalendarHolidayTestCase.php new file mode 100644 index 0000000000..be0a51b71a --- /dev/null +++ b/src/applications/calendar/storage/holiday/__tests__/PhabricatorCalendarHolidayTestCase.php @@ -0,0 +1,55 @@ + true, + ); + } + + protected function willRunTests() { + parent::willRunTests(); + id(new PhabricatorCalendarHoliday()) + ->setDay('2012-01-02') + ->setName('International Testing Day') + ->save(); + } + + public function testNthBusinessDay() { + $map = array( + array('2011-12-30', 1, '2012-01-03'), + array('2012-01-01', 1, '2012-01-03'), + array('2012-01-01', 0, '2012-01-01'), + array('2012-01-01', -1, '2011-12-30'), + array('2012-01-04', -1, '2012-01-03'), + ); + foreach ($map as $val) { + list($date, $n, $expect) = $val; + $actual = PhabricatorCalendarHoliday::getNthBusinessDay( + strtotime($date), + $n); + $this->assertEqual( + $expect, + date('Y-m-d', $actual), + "{$n} business days since '{$date}'"); + } + } + +} diff --git a/src/applications/calendar/storage/holiday/__tests__/__init__.php b/src/applications/calendar/storage/holiday/__tests__/__init__.php new file mode 100644 index 0000000000..aa8a9d30b4 --- /dev/null +++ b/src/applications/calendar/storage/holiday/__tests__/__init__.php @@ -0,0 +1,15 @@ +