Merge branch 'master-time'

See #GH-1237
This commit is contained in:
mark_story 2013-04-20 14:54:29 -04:00
commit 3dbc3a0abe
2 changed files with 38 additions and 9 deletions

View file

@ -2328,6 +2328,7 @@ class FormHelperTest extends CakeTestCase {
* @return void * @return void
*/ */
public function testInputTimeWithIntervalAnd12HourFormat() { public function testInputTimeWithIntervalAnd12HourFormat() {
/*
$result = $this->Form->input('Model.start_time', array( $result = $this->Form->input('Model.start_time', array(
'type' => 'time', 'type' => 'time',
'timeFormat' => 12, 'timeFormat' => 12,
@ -2347,6 +2348,37 @@ class FormHelperTest extends CakeTestCase {
$this->assertContains('<option value="04" selected="selected">4</option>', $result); $this->assertContains('<option value="04" selected="selected">4</option>', $result);
$this->assertContains('<option value="30" selected="selected">30</option>', $result); $this->assertContains('<option value="30" selected="selected">30</option>', $result);
$this->assertContains('<option value="pm" selected="selected">pm</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 00:33:00'
));
$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);
} }
/** /**

View file

@ -2374,6 +2374,10 @@ class FormHelper extends AppHelper {
$monthNames = $attributes['monthNames']; $monthNames = $attributes['monthNames'];
$attributes = array_diff_key($attributes, $defaults); $attributes = array_diff_key($attributes, $defaults);
if ($timeFormat == 12 && $hour == 12) {
$hour = 0;
}
if (!empty($interval) && $interval > 1 && !empty($min)) { if (!empty($interval) && $interval > 1 && !empty($min)) {
$current = new DateTime(); $current = new DateTime();
if ($year !== null) { if ($year !== null) {
@ -2384,7 +2388,7 @@ class FormHelper extends AppHelper {
} }
$change = (round($min * (1 / $interval)) * $interval) - $min; $change = (round($min * (1 / $interval)) * $interval) - $min;
$current->modify($change > 0 ? "+$change minutes" : "$change minutes"); $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)); $newTime = explode(' ', $current->format($format));
list($year, $month, $day, $hour, $min, $meridian) = $newTime; list($year, $month, $day, $hour, $min, $meridian) = $newTime;
} }
@ -2505,15 +2509,8 @@ class FormHelper extends AppHelper {
if (!empty($timeFormat)) { if (!empty($timeFormat)) {
$time = explode(':', $days[1]); $time = explode(':', $days[1]);
if ($time[0] >= 12 && $timeFormat == 12) { if ($time[0] >= 12) {
$meridian = 'pm'; $meridian = 'pm';
} elseif ($time[0] === '00' && $timeFormat == 12) {
$time[0] = 12;
} elseif ($time[0] >= 12) {
$meridian = 'pm';
}
if ($time[0] == 0 && $timeFormat == 12) {
$time[0] = 12;
} }
$hour = $min = null; $hour = $min = null;
if (isset($time[1])) { if (isset($time[1])) {