Restore highlighting when jumping to transactions using URI anchors
[phabricator/blender.git] / src / view / viewutils.php
blob7eee3e03fbf043b74bd9c3b7b5641e445438201f
1 <?php
3 function phabricator_date($epoch, PhabricatorUser $user) {
4 return phabricator_format_local_time(
5 $epoch,
6 $user,
7 phutil_date_format($epoch));
10 function phabricator_relative_date($epoch, $user, $on = false) {
11 static $today;
12 static $yesterday;
14 if (!$today || !$yesterday) {
15 $now = time();
16 $today = phabricator_date($now, $user);
17 $yesterday = phabricator_date($now - 86400, $user);
20 $date = phabricator_date($epoch, $user);
22 if ($date === $today) {
23 return 'today';
26 if ($date === $yesterday) {
27 return 'yesterday';
30 return (($on ? 'on ' : '').$date);
33 function phabricator_time($epoch, $user) {
34 $time_key = PhabricatorTimeFormatSetting::SETTINGKEY;
35 return phabricator_format_local_time(
36 $epoch,
37 $user,
38 $user->getUserSetting($time_key));
41 function phabricator_datetime($epoch, $user) {
42 $time_key = PhabricatorTimeFormatSetting::SETTINGKEY;
43 return phabricator_format_local_time(
44 $epoch,
45 $user,
46 pht('%s, %s',
47 phutil_date_format($epoch),
48 $user->getUserSetting($time_key)));
51 function phabricator_datetimezone($epoch, $user) {
52 $datetime = phabricator_datetime($epoch, $user);
53 $timezone = phabricator_format_local_time($epoch, $user, 'T');
55 // Some obscure timezones just render as "+03" or "-09". Make these render
56 // as "UTC+3" instead.
57 if (preg_match('/^[+-]/', $timezone)) {
58 $timezone = (int)trim($timezone, '+');
59 if ($timezone < 0) {
60 $timezone = pht('UTC-%s', $timezone);
61 } else {
62 $timezone = pht('UTC+%s', $timezone);
66 return pht('%s (%s)', $datetime, $timezone);
69 /**
70 * This function does not usually need to be called directly. Instead, call
71 * @{function:phabricator_date}, @{function:phabricator_time}, or
72 * @{function:phabricator_datetime}.
74 * @param int Unix epoch timestamp.
75 * @param PhabricatorUser User viewing the timestamp.
76 * @param string Date format, as per DateTime class.
77 * @return string Formatted, local date/time.
79 function phabricator_format_local_time($epoch, $user, $format) {
80 if (!$epoch) {
81 // If we're missing date information for something, the DateTime class will
82 // throw an exception when we try to construct an object. Since this is a
83 // display function, just return an empty string.
84 return '';
87 $user_zone = $user->getTimezoneIdentifier();
89 static $zones = array();
90 if (empty($zones[$user_zone])) {
91 $zones[$user_zone] = new DateTimeZone($user_zone);
93 $zone = $zones[$user_zone];
95 // NOTE: Although DateTime takes a second DateTimeZone parameter to its
96 // constructor, it ignores it if the date string includes timezone
97 // information. Further, it treats epoch timestamps ("@946684800") as having
98 // a UTC timezone. Set the timezone explicitly after constructing the object.
99 try {
100 $date = new DateTime('@'.$epoch);
101 } catch (Exception $ex) {
102 // NOTE: DateTime throws an empty exception if the format is invalid,
103 // just replace it with a useful one.
104 throw new Exception(
105 pht("Construction of a DateTime() with epoch '%s' ".
106 "raised an exception.", $epoch));
109 $date->setTimezone($zone);
111 return PhutilTranslator::getInstance()->translateDate($format, $date);