Permalink
Browse files

Make minutes/hours wrap around correctly.

Also account for times around midnight that roll over days/months/years.

Fixes #3242
  • Loading branch information...
1 parent 853d866 commit 80c355baa5c0ceb93bbd879b345ad49c3bca8ab6 @markstory markstory committed Sep 29, 2012
Showing with 51 additions and 3 deletions.
  1. +37 −0 lib/Cake/Test/Case/View/Helper/FormHelperTest.php
  2. +14 −3 lib/Cake/View/Helper/FormHelper.php
@@ -2229,6 +2229,43 @@ public function testInputTime() {
$this->assertRegExp('#<option value="pm"[^>]*>pm</option>#', $result);
}
+/**
+ * Test interval + selected near the hour roll over.
+ *
+ * @return void
+ */
+ public function testTimeSelectedWithInterval() {
+ $result = $this->Form->input('Model.start_time', array(
+ 'timeFormat' => 24,
+ 'type' => 'time',
+ 'interval' => 15,
+ 'selected' => '15:57'
+ ));
+ $this->assertContains('<option value="16" selected="selected">16</option>', $result);
+ $this->assertContains('<option value="00" selected="selected">00</option>', $result);
+
+ $result = $this->Form->input('Model.start_time', array(
+ 'timeFormat' => 24,
+ 'type' => 'time',
+ 'interval' => 15,
+ 'selected' => '23:57'
+ ));
+ $this->assertContains('<option value="00" selected="selected">0</option>', $result);
+ $this->assertContains('<option value="00" selected="selected">00</option>', $result);
+
+ $result = $this->Form->input('Model.created', array(
+ 'timeFormat' => 24,
+ 'type' => 'datetime',
+ 'interval' => 15,
+ 'selected' => '2012-09-30 23:56'
+ ));
+ $this->assertContains('<option value="2012" selected="selected">2012</option>', $result);
+ $this->assertContains('<option value="10" selected="selected">October</option>', $result);
+ $this->assertContains('<option value="01" selected="selected">1</option>', $result);
+ $this->assertContains('<option value="00" selected="selected">0</option>', $result);
+ $this->assertContains('<option value="00" selected="selected">00</option>', $result);
+ }
+
/**
* test form->input() with datetime, date and time types
*
@@ -2244,6 +2244,20 @@ public function dateTime($fieldName, $dateFormat = 'DMY', $timeFormat = '12', $a
$monthNames = $attributes['monthNames'];
$attributes = array_diff_key($attributes, $defaults);
+ if (!empty($interval) && $interval > 1 && !empty($min)) {
+ $current = new DateTime();
+ if ($year !== null) {
+ $current->setDate($year, $month, $day);
+ }
+ if ($hour !== null) {
+ $current->setTime($hour, $min);
+ }
+ $change = (round($min * (1 / $interval)) * $interval) - $min;
+ $current->modify($change > 0 ? "+$change minutes" : "$change minutes");
+ $newTime = explode(' ', $current->format('Y m d H i a'));
+ list($year, $month, $day, $hour, $min, $meridian) = $newTime;
+ }
+
if (isset($attributes['id'])) {
if (is_string($attributes['id'])) {
// build out an array version
@@ -2305,9 +2319,6 @@ public function dateTime($fieldName, $dateFormat = 'DMY', $timeFormat = '12', $a
}
$opt = implode($separator, $selects);
- if (!empty($interval) && $interval > 1 && !empty($min)) {
- $min = round($min * (1 / $interval)) * $interval;
- }
$selectMinuteAttr['interval'] = $interval;
switch ($timeFormat) {
case '24':

0 comments on commit 80c355b

Please sign in to comment.