Commit 2068e0ba authored by catch's avatar catch

Issue #2502571 by pjonckiere, mpdonadio, catch, jhodgdon: Date format...

Issue #2502571 by pjonckiere, mpdonadio, catch, jhodgdon: Date format granularity should only render adjacent units
parent a47cadae
......@@ -204,6 +204,11 @@ public function formatInterval($interval, $granularity = 2, $langcode = NULL) {
$interval %= $value;
$granularity--;
}
elseif ($output) {
// Break if there was previous output but not any output at this level,
// to avoid skipping levels and getting output like "1 year 1 second".
break;
}
if ($granularity == 0) {
break;
......@@ -373,15 +378,21 @@ public function formatDiff($from, $to, $options = array()) {
// ourselves.
$interval_output = '';
$days = $interval->d;
if ($days >= 7) {
$weeks = floor($days / 7);
$weeks = floor($days / 7);
if ($weeks) {
$interval_output .= $this->formatPlural($weeks, '1 week', '@count weeks', array(), array('langcode' => $options['langcode']));
$days -= $weeks * 7;
$granularity--;
}
if ($granularity > 0 && $days > 0) {
if ((!$output || $weeks > 0) && $granularity > 0 && $days > 0) {
$interval_output .= ($interval_output ? ' ' : '') . $this->formatPlural($days, '1 day', '@count days', array(), array('langcode' => $options['langcode']));
}
else {
// If we did not output days, set the granularity to 0 so that we
// will not output hours and get things like "1 week 1 hour".
$granularity = 0;
}
break;
case 'h':
......@@ -397,9 +408,14 @@ public function formatDiff($from, $to, $options = array()) {
break;
}
$output .= ($output ? ' ' : '') . $interval_output;
$output .= ($output && $interval_output ? ' ' : '') . $interval_output;
$granularity--;
}
elseif ($output) {
// Break if there was previous output but not any output at this level,
// to avoid skipping levels and getting output like "1 year 1 second".
break;
}
if ($granularity <= 0) {
break;
......
......@@ -131,7 +131,7 @@ public function providerTestFormatInterval() {
array(121, 2, '2 min 1 sec'),
// Check for hours with minutes and seconds.
array(3601, 1, '1 hour'),
array(3601, 2, '1 hour 1 sec'),
array(3601, 2, '1 hour'),
// Check for higher units.
array(86401, 1, '1 day'),
array(604800, 1, '1 week'),
......@@ -310,8 +310,8 @@ public function providerTestFormatDiff() {
// Checks for hours and possibly minutes or seconds.
array('1 hour', $this->createTimestamp('2013-12-11 09:09:08'), $request_time),
array('1 hour', $this->createTimestamp('2013-12-11 09:09:08'), $request_time),
array('1 hour 1 second', $this->createTimestamp('2013-12-11 09:09:07'), $request_time),
array('1 hour 2 seconds', $this->createTimestamp('2013-12-11 09:09:06'), $request_time),
array('1 hour', $this->createTimestamp('2013-12-11 09:09:07'), $request_time),
array('1 hour', $this->createTimestamp('2013-12-11 09:09:06'), $request_time),
array('1 hour 1 minute', $this->createTimestamp('2013-12-11 09:08:08'), $request_time),
array('1 hour 1 minute 1 second', $this->createTimestamp('2013-12-11 09:08:07'), $request_time, $granularity_3),
array('1 hour 1 minute 2 seconds', $this->createTimestamp('2013-12-11 09:08:06'), $request_time, $granularity_4),
......@@ -321,13 +321,13 @@ public function providerTestFormatDiff() {
// Checks for days and possibly hours, minutes or seconds.
array('1 day', $this->createTimestamp('2013-12-10 10:09:08'), $request_time),
array('1 day 1 second', $this->createTimestamp('2013-12-10 10:09:07'), $request_time),
array('1 day', $this->createTimestamp('2013-12-10 10:09:07'), $request_time),
array('1 day 1 hour', $this->createTimestamp('2013-12-10 09:09:08'), $request_time),
array('1 day 1 hour 1 minute', $this->createTimestamp('2013-12-10 09:08:07'), $request_time, $granularity_3 + $langcode_en),
array('1 day 1 hour 1 minute 1 second', $this->createTimestamp('2013-12-10 09:08:07'), $request_time, $granularity_4 + $langcode_lolspeak),
array('1 day 2 hours 2 minutes 2 seconds', $this->createTimestamp('2013-12-10 08:07:06'), $request_time, $granularity_4),
array('2 days', $this->createTimestamp('2013-12-09 10:09:08'), $request_time),
array('2 days 2 minutes', $this->createTimestamp('2013-12-09 10:07:08'), $request_time),
array('2 days', $this->createTimestamp('2013-12-09 10:07:08'), $request_time),
array('2 days 2 hours', $this->createTimestamp('2013-12-09 08:09:08'), $request_time),
array('2 days 2 hours 2 minutes', $this->createTimestamp('2013-12-09 08:07:06'), $request_time, $granularity_3 + $langcode_en),
array('2 days 2 hours 2 minutes 2 seconds', $this->createTimestamp('2013-12-09 08:07:06'), $request_time, $granularity_4 + $langcode_lolspeak),
......@@ -343,33 +343,33 @@ public function providerTestFormatDiff() {
// Checks for months and possibly days, hours, minutes or seconds.
array('1 month', $this->createTimestamp('2013-11-11 10:09:08'), $request_time),
array('1 month 1 second', $this->createTimestamp('2013-11-11 10:09:07'), $request_time),
array('1 month 1 hour', $this->createTimestamp('2013-11-11 09:09:08'), $request_time),
array('1 month 1 hour 1 minute', $this->createTimestamp('2013-11-11 09:08:07'), $request_time, $granularity_3),
array('1 month 1 hour 1 minute 1 second', $this->createTimestamp('2013-11-11 09:08:07'), $request_time, $granularity_4),
array('1 month', $this->createTimestamp('2013-11-11 10:09:07'), $request_time),
array('1 month', $this->createTimestamp('2013-11-11 09:09:08'), $request_time),
array('1 month', $this->createTimestamp('2013-11-11 09:08:07'), $request_time, $granularity_3),
array('1 month', $this->createTimestamp('2013-11-11 09:08:07'), $request_time, $granularity_4),
array('1 month 4 weeks', $this->createTimestamp('2013-10-13 10:09:08'), $request_time),
array('1 month 4 weeks 1 day', $this->createTimestamp('2013-10-13 10:09:08'), $request_time, $granularity_3),
array('1 month 4 weeks', $this->createTimestamp('2013-10-12 10:09:08'), $request_time),
array('1 month 4 weeks 2 days', $this->createTimestamp('2013-10-12 10:09:08'), $request_time, $granularity_3),
array('2 months', $this->createTimestamp('2013-10-11 10:09:08'), $request_time),
array('2 months 1 day', $this->createTimestamp('2013-10-10 10:09:08'), $request_time),
array('2 months 2 days', $this->createTimestamp('2013-10-09 08:07:06'), $request_time),
array('2 months 2 days 2 hours', $this->createTimestamp('2013-10-09 08:07:06'), $request_time, $granularity_3),
array('2 months 2 days 2 hours 2 minutes', $this->createTimestamp('2013-10-09 08:07:06'), $request_time, $granularity_4),
array('6 months 2 days', $this->createTimestamp('2013-06-09 10:09:08'), $request_time),
array('11 months 3 hours', $this->createTimestamp('2013-01-11 07:09:08'), $request_time),
array('2 months', $this->createTimestamp('2013-10-10 10:09:08'), $request_time),
array('2 months', $this->createTimestamp('2013-10-09 08:07:06'), $request_time),
array('2 months', $this->createTimestamp('2013-10-09 08:07:06'), $request_time, $granularity_3),
array('2 months', $this->createTimestamp('2013-10-09 08:07:06'), $request_time, $granularity_4),
array('6 months', $this->createTimestamp('2013-06-09 10:09:08'), $request_time),
array('11 months', $this->createTimestamp('2013-01-11 07:09:08'), $request_time),
array('11 months 4 weeks', $this->createTimestamp('2012-12-12 10:09:08'), $request_time),
array('11 months 4 weeks 2 days', $this->createTimestamp('2012-12-12 10:09:08'), $request_time, $granularity_3),
// Checks for years and possibly months, days, hours, minutes or seconds.
array('1 year', $this->createTimestamp('2012-12-11 10:09:08'), $request_time),
array('1 year 1 minute', $this->createTimestamp('2012-12-11 10:08:08'), $request_time),
array('1 year 1 day', $this->createTimestamp('2012-12-10 10:09:08'), $request_time),
array('1 year', $this->createTimestamp('2012-12-11 10:08:08'), $request_time),
array('1 year', $this->createTimestamp('2012-12-10 10:09:08'), $request_time),
array('2 years', $this->createTimestamp('2011-12-11 10:09:08'), $request_time),
array('2 years 2 minutes', $this->createTimestamp('2011-12-11 10:07:08'), $request_time),
array('2 years 2 days', $this->createTimestamp('2011-12-09 10:09:08'), $request_time),
array('2 years 2 months 2 days', $this->createTimestamp('2011-10-09 08:07:06'), $request_time, $granularity_3),
array('2 years 2 months 2 days 2 hours', $this->createTimestamp('2011-10-09 08:07:06'), $request_time, $granularity_4),
array('2 years', $this->createTimestamp('2011-12-11 10:07:08'), $request_time),
array('2 years', $this->createTimestamp('2011-12-09 10:09:08'), $request_time),
array('2 years 2 months', $this->createTimestamp('2011-10-09 08:07:06'), $request_time, $granularity_3),
array('2 years 2 months', $this->createTimestamp('2011-10-09 08:07:06'), $request_time, $granularity_4),
array('10 years', $this->createTimestamp('2003-12-11 10:09:08'), $request_time),
array('100 years', $this->createTimestamp('1913-12-11 10:09:08'), $request_time),
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment