diff --git a/src/__phutil_library_map__.php b/src/__phutil_library_map__.php --- a/src/__phutil_library_map__.php +++ b/src/__phutil_library_map__.php @@ -3290,7 +3290,6 @@ 'phabricator_format_local_time' => 'view/viewutils.php', 'phabricator_relative_date' => 'view/viewutils.php', 'phabricator_time' => 'view/viewutils.php', - 'phabricator_time_format' => 'view/viewutils.php', 'phid_get_subtype' => 'applications/phid/utils.php', 'phid_get_type' => 'applications/phid/utils.php', 'phid_group_by_type' => 'applications/phid/utils.php', diff --git a/src/applications/people/storage/PhabricatorUser.php b/src/applications/people/storage/PhabricatorUser.php --- a/src/applications/people/storage/PhabricatorUser.php +++ b/src/applications/people/storage/PhabricatorUser.php @@ -738,6 +738,41 @@ return new DateTimeZone($this->getTimezoneIdentifier()); } + public function getPreference($key) { + $preferences = $this->loadPreferences(); + + // TODO: After T4103 and T7707 this should eventually be pushed down the + // stack into modular preference definitions and role profiles. This is + // just fixing T8601 and mildly anticipating those changes. + $value = $preferences->getPreference($key); + + $allowed_values = null; + switch ($key) { + case PhabricatorUserPreferences::PREFERENCE_TIME_FORMAT: + $allowed_values = array( + 'g:i A', + 'H:i', + ); + break; + case PhabricatorUserPreferences::PREFERENCE_DATE_FORMAT: + $allowed_values = array( + 'Y-m-d', + 'n/j/Y', + 'd-m-Y', + ); + break; + } + + if ($allowed_values !== null) { + $allowed_values = array_fuse($allowed_values); + if (empty($allowed_values[$value])) { + $value = head($allowed_values); + } + } + + return $value; + } + public function __toString() { return $this->getUsername(); } diff --git a/src/view/form/control/AphrontFormDateControl.php b/src/view/form/control/AphrontFormDateControl.php --- a/src/view/form/control/AphrontFormDateControl.php +++ b/src/view/form/control/AphrontFormDateControl.php @@ -137,19 +137,13 @@ } private function getTimeFormat() { - $viewer = $this->getUser(); - $preferences = $viewer->loadPreferences(); - $pref_time_format = PhabricatorUserPreferences::PREFERENCE_TIME_FORMAT; - - return $preferences->getPreference($pref_time_format, 'g:i A'); + return $this->getUser() + ->getPreference(PhabricatorUserPreferences::PREFERENCE_TIME_FORMAT); } private function getDateFormat() { - $viewer = $this->getUser(); - $preferences = $viewer->loadPreferences(); - $pref_date_format = PhabricatorUserPreferences::PREFERENCE_DATE_FORMAT; - - return $preferences->getPreference($pref_date_format, 'Y-m-d'); + return $this->getUser() + ->getPreference(PhabricatorUserPreferences::PREFERENCE_DATE_FORMAT); } private function getTimeInputValue() { diff --git a/src/view/form/control/AphrontFormDateControlValue.php b/src/view/form/control/AphrontFormDateControlValue.php --- a/src/view/form/control/AphrontFormDateControlValue.php +++ b/src/view/form/control/AphrontFormDateControlValue.php @@ -10,7 +10,6 @@ private $zone; private $optional; - public function getValueDate() { return $this->valueDate; } @@ -56,6 +55,10 @@ return $this->optional; } + public function getViewer() { + return $this->viewer; + } + public static function newFromParts( PhabricatorUser $viewer, $year, @@ -71,8 +74,7 @@ $year, $month, $day, - coalesce($time, '12:00 AM'), - $value); + coalesce($time, '12:00 AM')); $value->valueEnabled = $enabled; return $value; @@ -85,8 +87,7 @@ list($value->valueDate, $value->valueTime) = $value->getFormattedDateFromDate( $request->getStr($key.'_d'), - $request->getStr($key.'_t'), - $value); + $request->getStr($key.'_t')); $value->valueEnabled = $request->getStr($key.'_e'); return $value; @@ -108,8 +109,7 @@ $year, $month, $day, - $time, - $value); + $time); return $value; } @@ -123,8 +123,7 @@ list($value->valueDate, $value->valueTime) = $value->getFormattedDateFromDate( idx($dictionary, 'd'), - idx($dictionary, 't'), - $value); + idx($dictionary, 't')); $value->valueEnabled = idx($dictionary, 'e'); @@ -205,29 +204,25 @@ } private function getTimeFormat() { - $preferences = $this->viewer->loadPreferences(); - $pref_time_format = PhabricatorUserPreferences::PREFERENCE_TIME_FORMAT; - - return $preferences->getPreference($pref_time_format, 'g:i A'); + return $this->getViewer() + ->getPreference(PhabricatorUserPreferences::PREFERENCE_TIME_FORMAT); } private function getDateFormat() { - $preferences = $this->viewer->loadPreferences(); - $pref_date_format = PhabricatorUserPreferences::PREFERENCE_DATE_FORMAT; - - return $preferences->getPreference($pref_date_format, 'Y-m-d'); + return $this->getViewer() + ->getPreference(PhabricatorUserPreferences::PREFERENCE_DATE_FORMAT); } - private function getFormattedDateFromDate($date, $time, $value) { + private function getFormattedDateFromDate($date, $time) { $original_input = $date; - $zone = $value->getTimezone(); - $separator = $value->getFormatSeparator(); + $zone = $this->getTimezone(); + $separator = $this->getFormatSeparator(); $parts = preg_split('@[,./:-]@', $date); $date = implode($separator, $parts); $date = id(new DateTime($date, $zone)); if ($date) { - $date = $date->format($value->getDateFormat()); + $date = $date->format($this->getDateFormat()); } else { $date = $original_input; } @@ -235,8 +230,8 @@ $date = id(new DateTime("{$date} {$time}", $zone)); return array( - $date->format($value->getDateFormat()), - $date->format($value->getTimeFormat()), + $date->format($this->getDateFormat()), + $date->format($this->getTimeFormat()), ); } @@ -244,14 +239,14 @@ $year, $month, $day, - $time, - $value) { - $zone = $value->getTimezone(); + $time) { + + $zone = $this->getTimezone(); $date_time = id(new DateTime("{$year}-{$month}-{$day} {$time}", $zone)); return array( - $date_time->format($value->getDateFormat()), - $date_time->format($value->getTimeFormat()), + $date_time->format($this->getDateFormat()), + $date_time->format($this->getTimeFormat()), ); } diff --git a/src/view/phui/calendar/PHUICalendarListView.php b/src/view/phui/calendar/PHUICalendarListView.php --- a/src/view/phui/calendar/PHUICalendarListView.php +++ b/src/view/phui/calendar/PHUICalendarListView.php @@ -141,11 +141,8 @@ } private function getEventTooltip(AphrontCalendarEventView $event) { - $viewer = $this->getUser(); - $preferences = $viewer->loadPreferences(); - $time_pref = $preferences->getPreference( - PhabricatorUserPreferences::PREFERENCE_TIME_FORMAT, - 'g:i A'); + $time_pref = $this->getUser() + ->getPreference(PhabricatorUserPreferences::PREFERENCE_TIME_FORMAT); Javelin::initBehavior('phabricator-tooltips'); diff --git a/src/view/viewutils.php b/src/view/viewutils.php --- a/src/view/viewutils.php +++ b/src/view/viewutils.php @@ -31,32 +31,21 @@ } function phabricator_time($epoch, $user) { + $time_key = PhabricatorUserPreferences::PREFERENCE_TIME_FORMAT; return phabricator_format_local_time( $epoch, $user, - phabricator_time_format($user)); + $user->getPreference($time_key)); } function phabricator_datetime($epoch, $user) { + $time_key = PhabricatorUserPreferences::PREFERENCE_TIME_FORMAT; return phabricator_format_local_time( $epoch, $user, pht('%s, %s', phutil_date_format($epoch), - phabricator_time_format($user))); -} - -function phabricator_time_format($user) { - $prefs = $user->loadPreferences(); - - $pref = $prefs->getPreference( - PhabricatorUserPreferences::PREFERENCE_TIME_FORMAT); - - if (strlen($pref)) { - return $pref; - } - - return pht('g:i A'); + $user->getPreference($time_key))); } /**