Path: blob/master/src/infrastructure/time/PhabricatorTime.php
12241 views
<?php12final class PhabricatorTime extends Phobject {34private static $stack = array();5private static $originalZone;67public static function pushTime($epoch, $timezone) {8if (empty(self::$stack)) {9self::$originalZone = date_default_timezone_get();10}1112$ok = date_default_timezone_set($timezone);13if (!$ok) {14throw new Exception(pht("Invalid timezone '%s'!", $timezone));15}1617self::$stack[] = array(18'epoch' => $epoch,19'timezone' => $timezone,20);2122return new PhabricatorTimeGuard(last_key(self::$stack));23}2425public static function popTime($key) {26if ($key !== last_key(self::$stack)) {27throw new Exception(28pht(29'%s with bad key.',30__METHOD__));31}32array_pop(self::$stack);3334if (empty(self::$stack)) {35date_default_timezone_set(self::$originalZone);36} else {37$frame = end(self::$stack);38date_default_timezone_set($frame['timezone']);39}40}4142public static function getNow() {43if (self::$stack) {44$frame = end(self::$stack);45return $frame['epoch'];46}47return time();48}4950public static function parseLocalTime($time, PhabricatorUser $user) {51$old_zone = date_default_timezone_get();5253date_default_timezone_set($user->getTimezoneIdentifier());54$timestamp = (int)strtotime($time, self::getNow());55if ($timestamp <= 0) {56$timestamp = null;57}58date_default_timezone_set($old_zone);5960return $timestamp;61}6263public static function getTodayMidnightDateTime($viewer) {64$timezone = new DateTimeZone($viewer->getTimezoneIdentifier());65$today = new DateTime('@'.time());66$today->setTimezone($timezone);67$year = $today->format('Y');68$month = $today->format('m');69$day = $today->format('d');70$today = new DateTime("{$year}-{$month}-{$day}", $timezone);71return $today;72}7374public static function getDateTimeFromEpoch($epoch, PhabricatorUser $viewer) {75$datetime = new DateTime('@'.$epoch);76$datetime->setTimezone($viewer->getTimeZone());77return $datetime;78}7980public static function getTimezoneDisplayName($raw_identifier) {8182// Internal identifiers have names like "America/Los_Angeles", but this is83// just an implementation detail and we can render them in a more human84// readable format with spaces.85$name = str_replace('_', ' ', $raw_identifier);8687return $name;88}8990}919293