public function testLocaleTimezones() { $localeDetector = $this->getMock('Sonata\\IntlBundle\\Locale\\LocaleDetectorInterface'); $localeDetector->expects($this->any())->method('getLocale')->will($this->returnValue('fr')); $timezoneDetector = $this->getMock('Sonata\\IntlBundle\\Timezone\\TimezoneDetectorInterface'); $timezoneDetector->expects($this->any())->method('getTimezone')->will($this->returnValue('Europe/London')); $timezoneDetectorWithMapping = $this->getMock('Sonata\\IntlBundle\\Timezone\\TimezoneDetectorInterface'); $timezoneDetectorWithMapping->expects($this->any())->method('getTimezone')->will($this->returnValue('Europe/Paris')); $this->assertEquals('Europe/London', $timezoneDetector->getTimezone()); $this->assertEquals('Europe/Paris', $timezoneDetectorWithMapping->getTimezone()); // One helper without a locale mapping (current default) $helper = new DateTimeHelper($timezoneDetector, 'UTF-8', $localeDetector); // Helper with a mapping for the detected locale $helperWithMapping = new DateTimeHelper($timezoneDetectorWithMapping, 'UTF-8', $localeDetector); $dateLondon = new \DateTime('13:37', new \DateTimeZone('Europe/London')); // Test if the time is correctly corrected for the 'detected' timezone $this->assertEquals('13:37', $helper->format($dateLondon, 'HH:mm'), 'A date in the Europe/London timezone, should not be corrected when formatted with timezone Europe/London.'); $this->assertEquals('14:37', $helperWithMapping->format($dateLondon, 'HH:mm'), 'A date in the Europe/London timezone, should be corrected when formatted with timezone Europe/Paris.'); // Test if the time is correctly correct if the timezone is given as function parameter $this->assertEquals('15:37', $helper->format($dateLondon, 'HH:mm', 'fr', 'Europe/Helsinki'), 'A date in the Europe/London timezone, should be corrected when formatted with timezone Europe/Helsinki.'); $this->assertEquals('15:37', $helperWithMapping->format($dateLondon, 'HH:mm', 'fr', 'Europe/Helsinki'), 'A date in the Europe/London timezone, should be corrected when formatted with timezone Europe/Helsinki.'); // Test if the time is correctly corrected for the 'detected' timezone $dateParis = new \DateTime('13:37', new \DateTimeZone('Europe/Paris')); $this->assertEquals('12:37', $helper->format($dateParis, 'HH:mm'), 'A date in the Europe/Paris timezone, should be corrected when formatted with timezone Europe/London.'); $this->assertEquals('13:37', $helperWithMapping->format($dateParis, 'HH:mm'), 'A date in the Europe/Paris timezone, should be corrected when formatted with timezone Europe/Paris.'); }
/** * @param \Datetime|string|int $time * @param string|null $pattern * @param string|null $locale * @param string|null $timezone * @param string|null $dateType * @param string|null $timeType * * @return string */ public function formatDatetime($time, $pattern = null, $locale = null, $timezone = null, $dateType = null, $timeType = null) { if ($pattern) { return $this->helper->format($time, $pattern, $locale, $timezone); } return $this->helper->formatDateTime($time, $locale, $timezone, $dateType, $timeType); }
protected function prepareMessage(Distance $distance, $include_seconds, $include_months) { $distance_in_minutes = $distance->getMinutes(); $distance_in_seconds = $distance->getSeconds(); if ($distance_in_seconds <= 60) { if ($include_seconds) { if ($distance_in_seconds < 20) { return $this->translator->transchoice('less than %seconds seconds ago', $distance_in_seconds, array('%seconds' => $distance_in_seconds)); } elseif ($distance_in_seconds < 40) { return $this->translator->trans('half a minute ago'); } elseif ($distance_in_seconds < 60) { return $this->translator->trans('less than a minute ago'); } else { return $this->translator->trans('1 minute ago'); } } return $distance_in_minutes === 0 ? $this->translator->trans('less than a minute ago', array()) : $this->translator->trans('1 minute ago', array()); } elseif ($distance_in_minutes <= 45) { return $this->translator->transchoice('%minutes minutes ago', $distance_in_minutes, array('%minutes' => $distance_in_minutes)); } elseif ($distance_in_minutes <= 90) { return $this->translator->trans('about 1 hour ago'); } elseif ($distance_in_minutes <= 240) { return $this->translator->transchoice('about %hours hours ago', $distance->getHours(), array('%hours' => $distance->getHours())); } elseif ($distance->isToday()) { return $this->translator->trans('today at %time', array('%time' => $this->datetimeformater->formatTime($distance->getFrom(), null, null, IntlDateFormatter::SHORT))); } elseif ($distance->isYesterday()) { return $this->translator->trans('yersterday at %time', array('%time' => $this->datetimeformater->formatTime($distance->getFrom(), null, null, IntlDateFormatter::SHORT))); } elseif ($distance->isThisYear()) { return $this->translator->trans('%date at %time', array('%time' => $this->datetimeformater->formatTime($distance->getFrom(), null, null, IntlDateFormatter::SHORT), '%date' => $this->datetimeformater->format($distance->getFrom(), "d LLL"))); } else { return $this->translator->trans('%date', array('%date' => $this->datetimeformater->format($distance->getFrom(), "d LLL yyyy"))); } }
<?php if (file_exists($file = __DIR__ . '/autoload.php')) { require_once $file; } elseif (file_exists($file = __DIR__ . '/autoload.php.dist')) { require_once $file; } use Sonata\IntlBundle\Templating\Helper\DateTimeHelper; echo sprintf("ICU Version : %s\n", DateTimeHelper::getICUDataVersion()); // try to get Symfony's PHPunit Bridge $files = array_filter(array(__DIR__ . '/../../vendor/symfony/symfony/src/Symfony/Bridge/PhpUnit/bootstrap.php', __DIR__ . '/../../vendor/symfony/phpunit-bridge/bootstrap.php', __DIR__ . '/../../../../../vendor/symfony/symfony/src/Symfony/Bridge/PhpUnit/bootstrap.php', __DIR__ . '/../../../../../vendor/symfony/phpunit-bridge/bootstrap.php'), 'file_exists'); if ($files) { require_once current($files); }