1
0
Fork 0
mirror of https://we.phorge.it/source/phorge.git synced 2024-12-20 04:20:55 +01:00

Implement getNthBusinessDay()

Summary: I will need it for nagging tool.

Test Plan:
None yet.
Please suggest me how to create a testing database (I need to insert some data in the table). I guess that it is now possible?
There is also probably some bug in `arc unit` - `setEnvConfig()` is not called before `getEnvConfig()` resulting in fatal error.

Reviewers: epriestley

Reviewed By: epriestley

CC: aran, Koolvin

Differential Revision: https://secure.phabricator.com/D2376
This commit is contained in:
vrana 2012-05-03 12:16:09 -07:00
parent 45c662e4f7
commit 631718f99e
5 changed files with 92 additions and 0 deletions

View file

@ -531,6 +531,7 @@ phutil_register_library_map(array(
'PhabricatorCalendarController' => 'applications/calendar/controller/base', 'PhabricatorCalendarController' => 'applications/calendar/controller/base',
'PhabricatorCalendarDAO' => 'applications/calendar/storage/base', 'PhabricatorCalendarDAO' => 'applications/calendar/storage/base',
'PhabricatorCalendarHoliday' => 'applications/calendar/storage/holiday', 'PhabricatorCalendarHoliday' => 'applications/calendar/storage/holiday',
'PhabricatorCalendarHolidayTestCase' => 'applications/calendar/storage/holiday/__tests__',
'PhabricatorChangesetResponse' => 'infrastructure/diff/response', 'PhabricatorChangesetResponse' => 'infrastructure/diff/response',
'PhabricatorChatLogChannelListController' => 'applications/chatlog/controller/channellist', 'PhabricatorChatLogChannelListController' => 'applications/chatlog/controller/channellist',
'PhabricatorChatLogChannelLogController' => 'applications/chatlog/controller/channellog', 'PhabricatorChatLogChannelLogController' => 'applications/chatlog/controller/channellog',
@ -1465,6 +1466,7 @@ phutil_register_library_map(array(
'PhabricatorCalendarController' => 'PhabricatorController', 'PhabricatorCalendarController' => 'PhabricatorController',
'PhabricatorCalendarDAO' => 'PhabricatorLiskDAO', 'PhabricatorCalendarDAO' => 'PhabricatorLiskDAO',
'PhabricatorCalendarHoliday' => 'PhabricatorCalendarDAO', 'PhabricatorCalendarHoliday' => 'PhabricatorCalendarDAO',
'PhabricatorCalendarHolidayTestCase' => 'PhabricatorTestCase',
'PhabricatorChangesetResponse' => 'AphrontProxyResponse', 'PhabricatorChangesetResponse' => 'AphrontProxyResponse',
'PhabricatorChatLogChannelListController' => 'PhabricatorChatLogController', 'PhabricatorChatLogChannelListController' => 'PhabricatorChatLogController',
'PhabricatorChatLogChannelLogController' => 'PhabricatorChatLogController', 'PhabricatorChatLogChannelLogController' => 'PhabricatorChatLogController',

View file

@ -27,4 +27,22 @@ final class PhabricatorCalendarHoliday extends PhabricatorCalendarDAO {
) + parent::getConfiguration(); ) + 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;
}
} }

View file

@ -8,5 +8,7 @@
phutil_require_module('phabricator', 'applications/calendar/storage/base'); phutil_require_module('phabricator', 'applications/calendar/storage/base');
phutil_require_module('phutil', 'utils');
phutil_require_source('PhabricatorCalendarHoliday.php'); phutil_require_source('PhabricatorCalendarHoliday.php');

View file

@ -0,0 +1,55 @@
<?php
/*
* Copyright 2012 Facebook, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
final class PhabricatorCalendarHolidayTestCase extends PhabricatorTestCase {
protected function getPhabricatorTestCaseConfiguration() {
return array(
self::PHABRICATOR_TESTCONFIG_BUILD_STORAGE_FIXTURES => 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}'");
}
}
}

View file

@ -0,0 +1,15 @@
<?php
/**
* This file is automatically generated. Lint this module to rebuild it.
* @generated
*/
phutil_require_module('phabricator', 'applications/calendar/storage/holiday');
phutil_require_module('phabricator', 'infrastructure/testing/testcase');
phutil_require_module('phutil', 'utils');
phutil_require_source('PhabricatorCalendarHolidayTestCase.php');