[ Index ]

PHP Cross Reference of Phabricator

title

Body

[close]

/src/view/ -> viewutils.php (source)

   1  <?php
   2  
   3  function phabricator_date($epoch, PhabricatorUser $user) {
   4    return phabricator_format_local_time(
   5      $epoch,
   6      $user,
   7      _phutil_date_format($epoch));
   8  }
   9  
  10  function phabricator_on_relative_date($epoch, $user) {
  11    return phabricator_relative_date($epoch, $user, true);
  12  }
  13  
  14  function phabricator_relative_date($epoch, $user, $on = false) {
  15    static $today;
  16    static $yesterday;
  17  
  18    if (!$today || !$yesterday) {
  19      $now = time();
  20      $today = phabricator_date($now, $user);
  21      $yesterday = phabricator_date($now - 86400, $user);
  22    }
  23  
  24    $date = phabricator_date($epoch, $user);
  25  
  26    if ($date === $today) {
  27      return 'today';
  28    }
  29  
  30    if ($date === $yesterday) {
  31      return 'yesterday';
  32    }
  33  
  34    return (($on ? 'on ' : '').$date);
  35  }
  36  
  37  function phabricator_time($epoch, $user) {
  38    return phabricator_format_local_time(
  39      $epoch,
  40      $user,
  41      _phabricator_time_format($user));
  42  }
  43  
  44  function phabricator_datetime($epoch, $user) {
  45    return phabricator_format_local_time(
  46      $epoch,
  47      $user,
  48      pht('%s, %s',
  49        _phutil_date_format($epoch),
  50        _phabricator_time_format($user)));
  51  }
  52  
  53  function _phabricator_time_format($user) {
  54    $prefs = $user->loadPreferences();
  55  
  56    $pref = $prefs->getPreference(
  57      PhabricatorUserPreferences::PREFERENCE_TIME_FORMAT);
  58  
  59    if (strlen($pref)) {
  60      return $pref;
  61    }
  62  
  63    return pht('g:i A');
  64  }
  65  
  66  /**
  67   * This function does not usually need to be called directly. Instead, call
  68   * @{function:phabricator_date}, @{function:phabricator_time}, or
  69   * @{function:phabricator_datetime}.
  70   *
  71   * @param int Unix epoch timestamp.
  72   * @param PhabricatorUser User viewing the timestamp.
  73   * @param string Date format, as per DateTime class.
  74   * @return string Formatted, local date/time.
  75   */
  76  function phabricator_format_local_time($epoch, $user, $format) {
  77    if (!$epoch) {
  78      // If we're missing date information for something, the DateTime class will
  79      // throw an exception when we try to construct an object. Since this is a
  80      // display function, just return an empty string.
  81      return '';
  82    }
  83  
  84    $user_zone = $user->getTimezoneIdentifier();
  85  
  86    static $zones = array();
  87    if (empty($zones[$user_zone])) {
  88      $zones[$user_zone] = new DateTimeZone($user_zone);
  89    }
  90    $zone = $zones[$user_zone];
  91  
  92    // NOTE: Although DateTime takes a second DateTimeZone parameter to its
  93    // constructor, it ignores it if the date string includes timezone
  94    // information. Further, it treats epoch timestamps ("@946684800") as having
  95    // a UTC timezone. Set the timezone explicitly after constructing the object.
  96    try {
  97      $date = new DateTime('@'.$epoch);
  98    } catch (Exception $ex) {
  99      // NOTE: DateTime throws an empty exception if the format is invalid,
 100      // just replace it with a useful one.
 101      throw new Exception(
 102        pht("Construction of a DateTime() with epoch '%s' ".
 103        "raised an exception.", $epoch));
 104    }
 105  
 106    $date->setTimeZone($zone);
 107  
 108    return PhutilTranslator::getInstance()->translateDate($format, $date);
 109  }


Generated: Sun Nov 30 09:20:46 2014 Cross-referenced by PHPXref 0.7.1