[ Index ]

PHP Cross Reference of Phabricator

title

Body

[close]

/src/infrastructure/time/ -> PhabricatorTime.php (source)

   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  }


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