From 3729ac1f2f83f9c47de3cf08661891add0f0dbac Mon Sep 17 00:00:00 2001 From: mark_story Date: Tue, 23 Oct 2012 12:47:12 -0400 Subject: [PATCH] Fix issue where the incorrect meridian would be selected. When combining 12 hour format, interval and afternoon times, the incorrect meridian would be selected. Moving the hour math into FormHelper::hour() makes that method generally more correct and lenient on its input. Fixes #3299 --- .../Test/Case/View/Helper/FormHelperTest.php | 40 ++++++++----------- lib/Cake/View/Helper/FormHelper.php | 10 +++-- 2 files changed, 23 insertions(+), 27 deletions(-) diff --git a/lib/Cake/Test/Case/View/Helper/FormHelperTest.php b/lib/Cake/Test/Case/View/Helper/FormHelperTest.php index 9da345458d1..d4ce34deb76 100644 --- a/lib/Cake/Test/Case/View/Helper/FormHelperTest.php +++ b/lib/Cake/Test/Case/View/Helper/FormHelperTest.php @@ -2224,9 +2224,9 @@ public function testInputTime() { 'type' => 'time', 'selected' => '18:15' )); - $this->assertRegExp('##', $result); - $this->assertRegExp('##', $result); - $this->assertRegExp('##', $result); + $this->assertContains('', $result); + $this->assertContains('', $result); + $this->assertContains('', $result); } /** @@ -2235,6 +2235,15 @@ public function testInputTime() { * @return void */ public function testTimeSelectedWithInterval() { + $result = $this->Form->input('Model.start_time', array( + 'type' => 'time', + 'interval' => 15, + 'selected' => '2012-10-23 15:57:00' + )); + $this->assertContains('', $result); + $this->assertContains('', $result); + $this->assertContains('', $result); + $result = $this->Form->input('Model.start_time', array( 'timeFormat' => 24, 'type' => 'time', @@ -5621,26 +5630,11 @@ public function testHour() { $this->Form->request->data['Model']['field'] = ''; $result = $this->Form->hour('Model.field', true, array('value' => '23')); - $expected = array( - array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')), - array('option' => array('value' => '')), - '/option', - array('option' => array('value' => '00')), - '0', - '/option', - array('option' => array('value' => '01')), - '1', - '/option', - array('option' => array('value' => '02')), - '2', - '/option', - $hoursRegex, - array('option' => array('value' => '23', 'selected' => 'selected')), - '23', - '/option', - '/select', - ); - $this->assertTags($result, $expected); + $this->assertContains('', $result); + + $result = $this->Form->hour('Model.field', false, array('value' => '23')); + $this->assertContains('', $result); + $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32'; $result = $this->Form->hour('Model.field', true); diff --git a/lib/Cake/View/Helper/FormHelper.php b/lib/Cake/View/Helper/FormHelper.php index 301acaaec06..ea843e4ae38 100644 --- a/lib/Cake/View/Helper/FormHelper.php +++ b/lib/Cake/View/Helper/FormHelper.php @@ -2042,6 +2042,11 @@ public function hour($fieldName, $format24Hours = false, $attributes = array()) } elseif ($attributes['value'] === false) { $attributes['value'] = null; } + + if ($attributes['value'] > 12 && !$format24Hours) { + $attributes['value'] -= 12; + } + return $this->select( $fieldName . ".hour", $this->_generateOptions($format24Hours ? 'hour24' : 'hour'), @@ -2206,10 +2211,7 @@ public function dateTime($fieldName, $dateFormat = 'DMY', $timeFormat = '12', $a if (!empty($timeFormat)) { $time = explode(':', $days[1]); - if (($time[0] > 12) && $timeFormat == '12') { - $time[0] = $time[0] - 12; - $meridian = 'pm'; - } elseif ($time[0] == '12' && $timeFormat == '12') { + if ($time[0] >= '12' && $timeFormat == '12') { $meridian = 'pm'; } elseif ($time[0] == '00' && $timeFormat == '12') { $time[0] = 12;