浏览代码

Allow Time to accept arrays.

Mark Scherer 11 年之前
父节点
当前提交
ed7247314b
共有 2 个文件被更改,包括 24 次插入2 次删除
  1. 1 1
      src/Model/Table/Table.php
  2. 23 1
      src/Utility/Time.php

+ 1 - 1
src/Model/Table/Table.php

@@ -10,7 +10,7 @@ use Cake\Validation\Validator;
 use Tools\Utility\Utility;
 use Cake\ORM\Query;
 use Cake\Event\Event;
-use Cake\I18n\Time;
+use Tools\Utility\Time;
 
 class Table extends CakeTable {
 

+ 23 - 1
src/Utility/Time.php

@@ -16,8 +16,30 @@ class Time extends CakeTime {
  */
 	public function __construct($time = null, $tz = null) {
 		if (is_array($time)) {
-			return;
+			$value = $time + ['hour' => 0, 'minute' => 0, 'second' => 0];
+
+			$format = '';
+			if (
+				isset($value['year'], $value['month'], $value['day']) &&
+				(is_numeric($value['year']) && is_numeric($value['month']) && is_numeric($value['day']))
+			) {
+				$format .= sprintf('%d-%02d-%02d', $value['year'], $value['month'], $value['day']);
+			}
+
+			if (isset($value['meridian'])) {
+				$value['hour'] = strtolower($value['meridian']) === 'am' ? $value['hour'] : $value['hour'] + 12;
+			}
+			$format .= sprintf(
+				'%s%02d:%02d:%02d',
+				empty($format) ? '' : ' ',
+				$value['hour'],
+				$value['minute'],
+				$value['second']
+			);
+
+			$time = $format;
 		}
+
 		parent::__construct($time, $tz);
 	}