Skip to content

Commit

Permalink
Add additional tests and fix incorrect meridian selection
Browse files Browse the repository at this point in the history
When dates around midnight were used with interval + 12hr formats, the
incorrect meridian was selected.

Refs #GH-1237
  • Loading branch information
markstory committed Apr 20, 2013
1 parent 78ea4da commit f6c3015
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
22 changes: 22 additions & 0 deletions lib/Cake/Test/Case/View/Helper/FormHelperTest.php
Expand Up @@ -2328,6 +2328,7 @@ public function testTimeSelectedWithInterval() {
* @return void
*/
public function testInputTimeWithIntervalAnd12HourFormat() {
/*
$result = $this->Form->input('Model.start_time', array(
'type' => 'time',
'timeFormat' => 12,
Expand All @@ -2347,6 +2348,7 @@ public function testInputTimeWithIntervalAnd12HourFormat() {
$this->assertContains('<option value="04" selected="selected">4</option>', $result);
$this->assertContains('<option value="30" selected="selected">30</option>', $result);
$this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
*/

$result = $this->Form->input('Model.start_time', array(
'type' => 'time',
Expand All @@ -2357,6 +2359,26 @@ public function testInputTimeWithIntervalAnd12HourFormat() {
$this->assertContains('<option value="12" selected="selected">12</option>', $result);
$this->assertContains('<option value="30" selected="selected">30</option>', $result);
$this->assertContains('<option value="am" selected="selected">am</option>', $result);

$result = $this->Form->input('Model.start_time', array(
'type' => 'time',
'timeFormat' => '12',
'interval' => 10,
'selected' => '2013-05-19 13:33:00'
));
$this->assertContains('<option value="01" selected="selected">1</option>', $result);
$this->assertContains('<option value="30" selected="selected">30</option>', $result);
$this->assertContains('<option value="pm" selected="selected">pm</option>', $result);

$result = $this->Form->input('Model.start_time', array(
'type' => 'time',
'timeFormat' => '12',
'interval' => 10,
'selected' => '2013-05-19 01:33:00'
));
$this->assertContains('<option value="01" selected="selected">1</option>', $result);
$this->assertContains('<option value="30" selected="selected">30</option>', $result);
$this->assertContains('<option value="am" selected="selected">am</option>', $result);
}

/**
Expand Down
4 changes: 3 additions & 1 deletion lib/Cake/View/Helper/FormHelper.php
Expand Up @@ -2380,11 +2380,12 @@ public function dateTime($fieldName, $dateFormat = 'DMY', $timeFormat = '12', $a
$current->setDate($year, $month, $day);
}
if ($hour !== null) {
$hour = $timeFormat == 12 && $hour == 12 ? 0 : $hour;
$current->setTime($hour, $min);
}
$change = (round($min * (1 / $interval)) * $interval) - $min;
$current->modify($change > 0 ? "+$change minutes" : "$change minutes");
$format = ($timeFormat === '12') ? 'Y m d h i a' : 'Y m d H i a';
$format = ($timeFormat == 12) ? 'Y m d h i a' : 'Y m d H i a';
$newTime = explode(' ', $current->format($format));
list($year, $month, $day, $hour, $min, $meridian) = $newTime;
}
Expand Down Expand Up @@ -2505,6 +2506,7 @@ protected function _getDateTimeValue($value, $timeFormat) {
if (!empty($timeFormat)) {
$time = explode(':', $days[1]);

// TODO this code is stupid.
if ($time[0] >= 12 && $timeFormat == 12) {
$meridian = 'pm';
} elseif ($time[0] === '00' && $timeFormat == 12) {
Expand Down

0 comments on commit f6c3015

Please sign in to comment.