public function testFormatReturnsCorrectDateWithTimezoneApplied() { DateTimeFactory::setConfig(array('timezone' => 'Europe/Berlin')); $helper = new Zend_View_Helper_DateFormat($this->getRequestMock()); $this->assertEquals('12:05', $helper->format(1397729100, 'H:i'), 'Zend_View_Helper_DateFormat::format does not return a valid' . ' formatted time or does not apply the user\'s timezone'); }
/** * Setup user timezone if set and valid, otherwise global default timezone * * @return self * @see ApplicationBootstrap::setupTimezone */ protected function setupTimezone() { if ($this->user !== null && $this->user->getPreferences() !== null) { $userTimezone = $this->user->getPreferences()->get('app.timezone'); } else { $userTimezone = null; } try { DateTimeFactory::setConfig(array('timezone' => $userTimezone)); date_default_timezone_set($userTimezone); } catch (ConfigurationError $e) { return parent::setupTimezone(); } return $this; }
/** * Calculate the labels for this dataset * * @param integer $unit The unit to use as the basis for calculation */ private function calculateLabels($unit) { $fac = DateTimeFactory::create(); $duration = $this->getMax() - $this->getMin(); // Calculate number of ticks, but not more than 30 $tickCount = $duration / $unit * 10; if ($tickCount > 30) { $tickCount = 30; } $step = $duration / $tickCount; $format = $this->timeFormat; if ($unit === self::DAY) { $format = $this->dateFormat; } elseif ($unit === self::MONTH) { $format = $this->dateFormat; } for ($i = 0; $i <= $duration; $i += $step) { $this->labels[] = $fac->setTimestamp($this->getMin() + $i)->format($format); } }
/** * Setup the default timezone and pass it to DateTimeFactory::setConfig */ public static function setupTimezone() { date_default_timezone_set('UTC'); DateTimeFactory::setConfig(array('timezone' => 'UTC')); }
/** * Validate the input value and set the value of @see validPattern if the input machtes a pattern * * @param string $value The format string to validate * @param null $context The form context (ignored) * * @return bool True when the input is valid, otherwise false * * @see Zend_Validate_Abstract::isValid() */ public function isValid($value, $context = null) { $this->validPattern = false; if (!is_string($value) && !is_int($value)) { $this->error('INVALID_TYPE'); return false; } if ($this->isUnixTimestamp($value)) { $dt = DateTimeFactory::create(); $dt->setTimestamp($value); } else { if (!isset($this->patterns)) { throw new ProgrammingError('There are no allowed timeformats configured'); } $match_found = false; foreach ($this->patterns as $pattern) { $dt = DateTimeFactory::parse($value, $pattern); if ($dt !== false && $dt->format($pattern) === $value) { $match_found = true; $this->validPattern = $pattern; break; } } if (!$match_found) { $this->_error('NO_MATCHING_PATTERN'); return false; } } return true; }
/** * @param $weekday * * @return string */ private function weekdayName($weekday) { $sun = DateTimeFactory::create('last Sunday'); $interval = new DateInterval('P' . $weekday . 'D'); $sun->add($interval); return $sun->format('D'); }
/** * Validate filtered date/time strings * * Expects one or more valid formats being set in $this->patterns. Sets element value as Unix timestamp * if the input is considered valid. Utilizes DateTimeFactory to ensure time zone awareness. * * @param string $value * @param mixed $context * @return bool */ public function isValid($value, $context = null) { // Overwrite the internal validator to use if (!parent::isValid($value, $context)) { return false; } $pattern = $this->dateValidator->getValidPattern(); if (!$pattern) { $this->setValue($value); return true; } $this->setValue(DateTimeFactory::parse($value, $pattern)->getTimestamp()); return true; }
/** * Setup default timezone * * @return self * @throws ConfigurationError if the timezone in config.ini isn't valid */ protected function setupTimezone() { $timeZoneString = $this->config->global !== null ? $this->config->global->get('timezone', 'UTC') : 'UTC'; date_default_timezone_set($timeZoneString); DateTimeFactory::setConfig(array('timezone' => $timeZoneString)); return $this; }