@recaptime-dev's working patches + fork for Phorge, a community fork of Phabricator. (Upstream dev and stable branches are at upstream/main and upstream/stable respectively.) hq.recaptime.dev/wiki/Phorge
phorge phabricator
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

Consolidate some datetime code and add unit tests

Summary: See D6115.

Test Plan:
- Ran unit tests.
- Viewed reports.
- Created a countdown.
- Searched chatlog.
- Searched pastes by created date.

Reviewers: btrahan, chad

Reviewed By: btrahan

CC: aran

Differential Revision: https://secure.phabricator.com/D6116

+182 -44
+5 -1
src/__phutil_library_map__.php
··· 1481 1481 'PhabricatorTestDataGenerator' => 'applications/lipsum/generator/PhabricatorTestDataGenerator.php', 1482 1482 'PhabricatorTestStorageEngine' => 'applications/files/engine/PhabricatorTestStorageEngine.php', 1483 1483 'PhabricatorTestWorker' => 'infrastructure/daemon/workers/__tests__/PhabricatorTestWorker.php', 1484 + 'PhabricatorTime' => 'infrastructure/time/PhabricatorTime.php', 1485 + 'PhabricatorTimeGuard' => 'infrastructure/time/PhabricatorTimeGuard.php', 1486 + 'PhabricatorTimeTestCase' => 'infrastructure/time/__tests__/PhabricatorTimeTestCase.php', 1484 1487 'PhabricatorTimelineCursor' => 'infrastructure/daemon/timeline/storage/PhabricatorTimelineCursor.php', 1485 1488 'PhabricatorTimelineDAO' => 'infrastructure/daemon/timeline/storage/PhabricatorTimelineDAO.php', 1486 1489 'PhabricatorTimelineEvent' => 'infrastructure/daemon/timeline/storage/PhabricatorTimelineEvent.php', ··· 3028 3031 'PhabricatorOAuthUnlinkController' => 'PhabricatorAuthController', 3029 3032 'PhabricatorObjectHandleStatus' => 'PhabricatorObjectHandleConstants', 3030 3033 'PhabricatorObjectItemListExample' => 'PhabricatorUIExample', 3031 - 'PhabricatorObjectItemListView' => 'AphrontView', 3034 + 'PhabricatorObjectItemListView' => 'AphrontTagView', 3032 3035 'PhabricatorObjectItemView' => 'AphrontTagView', 3033 3036 'PhabricatorObjectListView' => 'AphrontView', 3034 3037 'PhabricatorObjectMailReceiver' => 'PhabricatorMailReceiver', ··· 3291 3294 'PhabricatorTestCase' => 'ArcanistPhutilTestCase', 3292 3295 'PhabricatorTestStorageEngine' => 'PhabricatorFileStorageEngine', 3293 3296 'PhabricatorTestWorker' => 'PhabricatorWorker', 3297 + 'PhabricatorTimeTestCase' => 'PhabricatorTestCase', 3294 3298 'PhabricatorTimelineCursor' => 'PhabricatorTimelineDAO', 3295 3299 'PhabricatorTimelineDAO' => 'PhabricatorLiskDAO', 3296 3300 'PhabricatorTimelineEvent' => 'PhabricatorTimelineDAO',
+1 -7
src/applications/chatlog/controller/PhabricatorChatLogChannelLogController.php
··· 232 232 ); 233 233 234 234 } else if ($at_date) { 235 - $timezone = new DateTimeZone($user->getTimezoneIdentifier()); 236 - try { 237 - $date = new DateTime($at_date, $timezone); 238 - $timestamp = $date->format('U'); 239 - } catch (Exception $e) { 240 - $timestamp = null; 241 - } 235 + $timestamp = PhabricatorTime::parseLocalTime($at_date, $user); 242 236 243 237 if ($timestamp) { 244 238 $context_logs = $query
+15 -19
src/applications/countdown/controller/PhabricatorCountdownEditController.php
··· 50 50 $e_text = null; 51 51 if (!strlen($title)) { 52 52 $e_text = pht('Required'); 53 - $errors[] = pht('You must give it a name.'); 53 + $errors[] = pht('You must give the countdown a name.'); 54 54 } 55 55 56 - // If the user types something like "5 PM", convert it to a timestamp 57 - // using their local time, not the server time. 58 - $timezone = new DateTimeZone($user->getTimezoneIdentifier()); 59 - 60 - try { 61 - $date = new DateTime($epoch, $timezone); 62 - $timestamp = $date->format('U'); 63 - } catch (Exception $e) { 64 - $errors[] = pht('You entered an incorrect date. You can enter date'. 65 - ' like \'2011-06-26 13:33:37\' to create an event at'. 66 - ' 13:33:37 on the 26th of June 2011.'); 67 - $timestamp = null; 56 + if (strlen($epoch)) { 57 + $timestamp = PhabricatorTime::parseLocalTime($epoch, $user); 58 + if (!$timestamp) { 59 + $errors[] = pht( 60 + 'You entered an incorrect date. You can enter date '. 61 + 'like \'2011-06-26 13:33:37\' to create an event at '. 62 + '13:33:37 on the 26th of June 2011.'); 63 + } 64 + } else { 65 + $e_epoch = pht('Required'); 66 + $errors[] = pht('You must specify the end date for a countdown.'); 68 67 } 69 68 70 - $countdown->setTitle($title); 71 - $countdown->setEpoch($timestamp); 72 - 73 69 if (!count($errors)) { 70 + $countdown->setTitle($title); 71 + $countdown->setEpoch($timestamp); 74 72 $countdown->setAuthorPHID($user->getPHID()); 75 73 $countdown->save(); 76 74 return id(new AphrontRedirectResponse()) ··· 84 82 } 85 83 86 84 if ($countdown->getEpoch()) { 87 - $display_epoch = phabricator_datetime( 88 - $countdown->getEpoch(), 89 - $user); 85 + $display_epoch = phabricator_datetime($countdown->getEpoch(), $user); 90 86 } else { 91 87 $display_epoch = $request->getStr('epoch'); 92 88 }
+2 -5
src/applications/maniphest/controller/ManiphestReportController.php
··· 709 709 // Do locale-aware parsing so that the user's timezone is assumed for 710 710 // time windows like "3 PM", rather than assuming the server timezone. 711 711 712 - $timezone = new DateTimeZone($user->getTimezoneIdentifier()); 713 - try { 714 - $date = new DateTime($window_str, $timezone); 715 - $window_epoch = $date->format('U'); 716 - } catch (Exception $e) { 712 + $window_epoch = PhabricatorTime::parseLocalTime($window_str, $user); 713 + if (!$window_epoch) { 717 714 $error = 'Invalid'; 718 715 $window_epoch = time() - (60 * 60 * 24 * 7); 719 716 }
+1 -12
src/applications/search/engine/PhabricatorApplicationSearchEngine.php
··· 207 207 return null; 208 208 } 209 209 210 - $viewer = $this->requireViewer(); 211 - 212 - $timezone = new DateTimeZone($viewer->getTimezoneIdentifier()); 213 - 214 - try { 215 - $date = new DateTime($date_time, $timezone); 216 - $timestamp = $date->format('U'); 217 - } catch (Exception $e) { 218 - $timestamp = null; 219 - } 220 - 221 - return $timestamp; 210 + return PhabricatorTime::parseLocalTime($date_time, $this->requireViewer()); 222 211 } 223 212 224 213
+61
src/infrastructure/time/PhabricatorTime.php
··· 1 + <?php 2 + 3 + final class PhabricatorTime { 4 + 5 + private static $stack = array(); 6 + private static $originalZone; 7 + 8 + public static function pushTime($epoch, $timezone) { 9 + if (empty(self::$stack)) { 10 + self::$originalZone = date_default_timezone_get(); 11 + } 12 + 13 + $ok = date_default_timezone_set($timezone); 14 + if (!$ok) { 15 + throw new Exception("Invalid timezone '{$timezone}'!"); 16 + } 17 + 18 + self::$stack[] = array( 19 + 'epoch' => $epoch, 20 + 'timezone' => $timezone, 21 + ); 22 + 23 + return new PhabricatorTimeGuard(last_key(self::$stack)); 24 + } 25 + 26 + public static function popTime($key) { 27 + if ($key !== last_key(self::$stack)) { 28 + throw new Exception("PhabricatorTime::popTime with bad key."); 29 + } 30 + array_pop(self::$stack); 31 + 32 + if (empty(self::$stack)) { 33 + date_default_timezone_set(self::$originalZone); 34 + } else { 35 + $frame = end(self::$stack); 36 + date_default_timezone_set($frame['timezone']); 37 + } 38 + } 39 + 40 + public static function getNow() { 41 + if (self::$stack) { 42 + $frame = end(self::$stack); 43 + return $frame['epoch']; 44 + } 45 + return time(); 46 + } 47 + 48 + public static function parseLocalTime($time, PhabricatorUser $user) { 49 + $old_zone = date_default_timezone_get(); 50 + 51 + date_default_timezone_set($user->getTimezoneIdentifier()); 52 + $timestamp = (int)strtotime($time, PhabricatorTime::getNow()); 53 + if ($timestamp <= 0) { 54 + $timestamp = null; 55 + } 56 + date_default_timezone_set($old_zone); 57 + 58 + return $timestamp; 59 + } 60 + 61 + }
+15
src/infrastructure/time/PhabricatorTimeGuard.php
··· 1 + <?php 2 + 3 + final class PhabricatorTimeGuard { 4 + 5 + private $frameKey; 6 + 7 + public function __construct($frame_key) { 8 + $this->frameKey = $frame_key; 9 + } 10 + 11 + public function __destruct() { 12 + PhabricatorTime::popTime($this->frameKey); 13 + } 14 + 15 + }
+82
src/infrastructure/time/__tests__/PhabricatorTimeTestCase.php
··· 1 + <?php 2 + 3 + final class PhabricatorTimeTestCase extends PhabricatorTestCase { 4 + 5 + public function testPhabricatorTimeStack() { 6 + $t = 1370202281; 7 + $time = PhabricatorTime::pushTime($t, 'UTC'); 8 + 9 + $this->assertEqual( 10 + true, 11 + (PhabricatorTime::getNow() === $t)); 12 + 13 + unset($time); 14 + 15 + $this->assertEqual( 16 + false, 17 + (PhabricatorTime::getNow() === $t)); 18 + } 19 + 20 + public function testParseLocalTime() { 21 + $u = new PhabricatorUser(); 22 + $u->setTimezoneIdentifier('UTC'); 23 + 24 + $v = new PhabricatorUser(); 25 + $v->setTimezoneIdentifier('America/Los_Angeles'); 26 + 27 + $t = 1370202281; // 2013-06-02 12:44:41 -0700 28 + $time = PhabricatorTime::pushTime($t, 'America/Los_Angeles'); 29 + 30 + $this->assertEqual( 31 + $t, 32 + PhabricatorTime::parseLocalTime('now', $u)); 33 + $this->assertEqual( 34 + $t, 35 + PhabricatorTime::parseLocalTime('now', $v)); 36 + 37 + $this->assertEqual( 38 + $t, 39 + PhabricatorTime::parseLocalTime('2013-06-02 12:44:41 -0700', $u)); 40 + $this->assertEqual( 41 + $t, 42 + PhabricatorTime::parseLocalTime('2013-06-02 12:44:41 -0700', $v)); 43 + 44 + $this->assertEqual( 45 + $t, 46 + PhabricatorTime::parseLocalTime('2013-06-02 12:44:41 PDT', $u)); 47 + $this->assertEqual( 48 + $t, 49 + PhabricatorTime::parseLocalTime('2013-06-02 12:44:41 PDT', $v)); 50 + 51 + $this->assertEqual( 52 + $t, 53 + PhabricatorTime::parseLocalTime('2013-06-02 19:44:41', $u)); 54 + $this->assertEqual( 55 + $t, 56 + PhabricatorTime::parseLocalTime('2013-06-02 12:44:41', $v)); 57 + 58 + $this->assertEqual( 59 + $t + 3600, 60 + PhabricatorTime::parseLocalTime('+1 hour', $u)); 61 + $this->assertEqual( 62 + $t + 3600, 63 + PhabricatorTime::parseLocalTime('+1 hour', $v)); 64 + 65 + unset($time); 66 + 67 + $t = 1370239200; // 2013-06-02 23:00:00 -0700 68 + $time = PhabricatorTime::pushTime($t, 'America/Los_Angeles'); 69 + 70 + // For the UTC user, midnight was 6 hours ago because it's early in the 71 + // morning for htem. For the PDT user, midnight was 23 hours ago. 72 + $this->assertEqual( 73 + $t + (-6 * 3600) + 60, 74 + PhabricatorTime::parseLocalTime('12:01:00 AM', $u)); 75 + $this->assertEqual( 76 + $t + (-23 * 3600) + 60, 77 + PhabricatorTime::parseLocalTime('12:01:00 AM', $v)); 78 + 79 + unset($time); 80 + } 81 + 82 + }