From 03b1ecbcdd0138dad98ad953e606b0285130d83d Mon Sep 17 00:00:00 2001 From: Bob Trahan Date: Mon, 3 Jun 2013 15:40:00 -0700 Subject: [PATCH] Conpherence - implement epoch creation slightly differently Summary: D6114 fixed some bugs but on production it shows up as a new bug where Saturday is the first day? stop messing with the DateTime object so much and do some old school epoch manipulation. This works correctly on my laptop and my still fail in production, but it will rule out DateTime suckage. Test Plan: still works on laptop Reviewers: epriestley, chad Reviewed By: epriestley CC: aran, Korvin Differential Revision: https://secure.phabricator.com/D6115 --- src/__phutil_library_map__.php | 3 + .../__tests__/ConpherenceTimeUtilTestCase.php | 59 ++++++++++++++++++ .../ConpherenceWidgetController.php | 27 +-------- .../query/ConpherenceThreadQuery.php | 16 ++--- .../conpherence/util/ConpherenceTimeUtil.php | 60 +++++++++++++++++++ 5 files changed, 128 insertions(+), 37 deletions(-) create mode 100644 src/applications/conpherence/__tests__/ConpherenceTimeUtilTestCase.php create mode 100644 src/applications/conpherence/util/ConpherenceTimeUtil.php diff --git a/src/__phutil_library_map__.php b/src/__phutil_library_map__.php index f28359d79f..dfe17fa8ae 100644 --- a/src/__phutil_library_map__.php +++ b/src/__phutil_library_map__.php @@ -272,6 +272,8 @@ phutil_register_library_map(array( 'ConpherenceThreadListView' => 'applications/conpherence/view/ConpherenceThreadListView.php', 'ConpherenceThreadMailReceiver' => 'applications/conpherence/mail/ConpherenceThreadMailReceiver.php', 'ConpherenceThreadQuery' => 'applications/conpherence/query/ConpherenceThreadQuery.php', + 'ConpherenceTimeUtil' => 'applications/conpherence/util/ConpherenceTimeUtil.php', + 'ConpherenceTimeUtilTestCase' => 'applications/conpherence/__tests__/ConpherenceTimeUtilTestCase.php', 'ConpherenceTransaction' => 'applications/conpherence/storage/ConpherenceTransaction.php', 'ConpherenceTransactionComment' => 'applications/conpherence/storage/ConpherenceTransactionComment.php', 'ConpherenceTransactionQuery' => 'applications/conpherence/query/ConpherenceTransactionQuery.php', @@ -2121,6 +2123,7 @@ phutil_register_library_map(array( 'ConpherenceThreadListView' => 'AphrontView', 'ConpherenceThreadMailReceiver' => 'PhabricatorObjectMailReceiver', 'ConpherenceThreadQuery' => 'PhabricatorCursorPagedPolicyAwareQuery', + 'ConpherenceTimeUtilTestCase' => 'PhabricatorTestCase', 'ConpherenceTransaction' => 'PhabricatorApplicationTransaction', 'ConpherenceTransactionComment' => 'PhabricatorApplicationTransactionComment', 'ConpherenceTransactionQuery' => 'PhabricatorApplicationTransactionQuery', diff --git a/src/applications/conpherence/__tests__/ConpherenceTimeUtilTestCase.php b/src/applications/conpherence/__tests__/ConpherenceTimeUtilTestCase.php new file mode 100644 index 0000000000..e8567e61ee --- /dev/null +++ b/src/applications/conpherence/__tests__/ConpherenceTimeUtilTestCase.php @@ -0,0 +1,59 @@ +setTimezoneIdentifier('America/Los_Angeles'); + $days = $this->getAllDays(); + foreach ($days as $day) { + $data = ConpherenceTimeUtil::getCalendarWidgetTimestamps( + $u, + $day); + + $this->assertEqual( + '000000', + $data['epoch_stamps'][0]->format('His')); + } + } + + public function testWidgetTimestampsStartDay() { + $u = new PhabricatorUser(); + $u->setTimezoneIdentifier('America/Los_Angeles'); + $days = $this->getAllDays(); + foreach ($days as $day) { + $data = ConpherenceTimeUtil::getCalendarWidgetTimestamps( + $u, + $day); + + $this->assertEqual( + $day, + $data['epoch_stamps'][0]->format('l')); + } + + $t = 1370202281; // 2013-06-02 12:44:41 -0700 -- a Sunday + $time = PhabricatorTime::pushTime($t, 'America/Los_Angeles'); + foreach ($days as $day) { + $data = ConpherenceTimeUtil::getCalendarWidgetTimestamps( + $u, + $day); + + $this->assertEqual( + $day, + $data['epoch_stamps'][0]->format('l')); + } + unset($time); + } + + private function getAllDays() { + return array( + 'Sunday', + 'Monday', + 'Tuesday', + 'Wednesday', + 'Thursday', + 'Friday', + 'Saturday'); + } + +} diff --git a/src/applications/conpherence/controller/ConpherenceWidgetController.php b/src/applications/conpherence/controller/ConpherenceWidgetController.php index 673a3b3425..d940c4a4a7 100644 --- a/src/applications/conpherence/controller/ConpherenceWidgetController.php +++ b/src/applications/conpherence/controller/ConpherenceWidgetController.php @@ -200,7 +200,7 @@ final class ConpherenceWidgetController extends $content = array(); $layout = id(new AphrontMultiColumnView()) ->setFluidLayout(true); - $timestamps = $this->getCalendarWidgetTimestamps(); + $timestamps = ConpherenceTimeUtil::getCalendarWidgetTimestamps($user); $today = $timestamps['today']; $epoch_stamps = $timestamps['epoch_stamps']; $one_day = 24 * 60 * 60; @@ -378,32 +378,9 @@ final class ConpherenceWidgetController extends ); } - private function getCalendarWidgetTimestamps() { - $user = $this->getRequest()->getUser(); - $timezone = new DateTimeZone($user->getTimezoneIdentifier()); - - $start_epoch = phabricator_format_local_time( - strtotime('last sunday', strtotime('tomorrow')), - $user, - 'U'); - $first_day = new DateTime('@'.$start_epoch, $timezone); - $timestamps = array(); - for ($day = 0; $day < 9; $day++) { - $timestamp = clone $first_day; - $timestamp->modify(sprintf('+%d days', $day)); - // set the timezone again 'cuz DateTime is weird - $timestamp->setTimeZone($timezone); - $timestamps[] = $timestamp; - } - return array( - 'today' => new DateTime('today', $timezone), - 'epoch_stamps' => $timestamps - ); - } - private function getWidgetURI() { $conpherence = $this->getConpherence(); return $this->getApplicationURI('update/'.$conpherence->getID().'/'); } - } +} diff --git a/src/applications/conpherence/query/ConpherenceThreadQuery.php b/src/applications/conpherence/query/ConpherenceThreadQuery.php index 0df5a770df..3c2b518d5a 100644 --- a/src/applications/conpherence/query/ConpherenceThreadQuery.php +++ b/src/applications/conpherence/query/ConpherenceThreadQuery.php @@ -216,18 +216,10 @@ final class ConpherenceThreadQuery $participant_phids = array_mergev($participant_phids); $file_phids = array_mergev($file_phids); - // statuses of everyone currently in the conpherence - // we show sunday -> saturday in a list *AND* a window - // of today -> +2 days. Ergo, if its saturday we need - // +2 days, for +9 days total. - $start_epoch = phabricator_format_local_time( - strtotime('last sunday', strtotime('tomorrow')), - $this->getViewer(), - 'U'); - $end_epoch = phabricator_format_local_time( - strtotime('last sunday +9 days', strtotime('tomorrow')), - $this->getViewer(), - 'U'); + $epochs = ConpherenceTimeUtil::getCalendarEventEpochs( + $this->getViewer()); + $start_epoch = $epochs['start_epoch']; + $end_epoch = $epochs['end_epoch']; $statuses = id(new PhabricatorUserStatus()) ->loadAllWhere( 'userPHID in (%Ls) AND dateTo >= %d AND dateFrom <= %d', diff --git a/src/applications/conpherence/util/ConpherenceTimeUtil.php b/src/applications/conpherence/util/ConpherenceTimeUtil.php new file mode 100644 index 0000000000..e5ef521183 --- /dev/null +++ b/src/applications/conpherence/util/ConpherenceTimeUtil.php @@ -0,0 +1,60 @@ +modify('+9 days'); + + return array( + 'start_epoch' => $start_day->format('U'), + 'end_epoch' => $end_day->format('U')); + } + + public static function getCalendarWidgetTimestamps( + PhabricatorUser $user, + $start_day_str = 'Sunday') { + + $objects = self::getStartDateTimeObjects($user, $start_day_str); + $start_day = $objects['start_day']; + $timestamps = array(); + for ($day = 0; $day < 9; $day++) { + $timestamp = clone $start_day; + $timestamp->modify(sprintf('+%d days', $day)); + $timestamps[] = $timestamp; + } + return array( + 'today' => $objects['today'], + 'epoch_stamps' => $timestamps + ); + } + + private static function getStartDateTimeObjects( + PhabricatorUser $user, + $start_day_str) { + $timezone = new DateTimeZone($user->getTimezoneIdentifier()); + + $today_epoch = PhabricatorTime::parseLocalTime('today', $user); + $today = new DateTime('@'.$today_epoch); + $today->setTimeZone($timezone); + + if ($today->format('l') == $start_day_str) { + $start_day = clone $today; + } else { + $start_epoch = PhabricatorTime::parseLocalTime( + 'last '.$start_day_str, + $user); + $start_day = new DateTime('@'.$start_epoch); + $start_day->setTimeZone($timezone); + } + return array( + 'today' => $today, + 'start_day' => $start_day); + } + +}