From 7318658017f46b15a60533177232851d63ce8918 Mon Sep 17 00:00:00 2001 From: Etienne CHAMPETIER Date: Fri, 4 Oct 2013 01:19:20 +0200 Subject: [PATCH] Use DateTime instead of date() On 32bits systems, date() is affected bug the 2038 bug DateTime always uses 64bits representation [Closes #110] Signed-off-by: Etienne CHAMPETIER --- dibi/drivers/DibiFirebirdDriver.php | 7 ++++--- dibi/drivers/DibiMsSql2005Driver.php | 7 ++++--- dibi/drivers/DibiMsSqlDriver.php | 7 ++++--- dibi/drivers/DibiMySqlDriver.php | 7 ++++--- dibi/drivers/DibiMySqliDriver.php | 7 ++++--- dibi/drivers/DibiOdbcDriver.php | 7 ++++--- dibi/drivers/DibiOracleDriver.php | 7 ++++--- dibi/drivers/DibiPdoDriver.php | 7 ++++--- dibi/drivers/DibiPostgreDriver.php | 7 ++++--- dibi/drivers/DibiSqlite3Driver.php | 7 ++++--- dibi/drivers/DibiSqliteDriver.php | 7 ++++--- dibi/libs/DibiResult.php | 15 ++------------- dibi/libs/DibiRow.php | 9 +++++---- 13 files changed, 51 insertions(+), 50 deletions(-) diff --git a/dibi/drivers/DibiFirebirdDriver.php b/dibi/drivers/DibiFirebirdDriver.php index 9ddd0cc6..5647f8dd 100644 --- a/dibi/drivers/DibiFirebirdDriver.php +++ b/dibi/drivers/DibiFirebirdDriver.php @@ -281,10 +281,11 @@ class DibiFirebirdDriver extends DibiObject implements IDibiDriver, IDibiResultD return $value ? 1 : 0; case dibi::DATE: - return $value instanceof DateTime ? $value->format("'Y-m-d'") : date("'Y-m-d'", $value); - case dibi::DATETIME: - return $value instanceof DateTime ? $value->format("'Y-m-d H:i:s'") : date("'Y-m-d H:i:s'", $value); + if (!$value instanceof DateTime) { + $value = new DibiDateTime($value); + } + return $value->format($type === dibi::DATETIME ? "'Y-m-d H:i:s'" : "Y-m-d"); default: throw new InvalidArgumentException('Unsupported type.'); diff --git a/dibi/drivers/DibiMsSql2005Driver.php b/dibi/drivers/DibiMsSql2005Driver.php index a6903bf5..f6719937 100644 --- a/dibi/drivers/DibiMsSql2005Driver.php +++ b/dibi/drivers/DibiMsSql2005Driver.php @@ -237,10 +237,11 @@ class DibiMsSql2005Driver extends DibiObject implements IDibiDriver, IDibiResult return $value ? 1 : 0; case dibi::DATE: - return $value instanceof DateTime ? $value->format("'Y-m-d'") : date("'Y-m-d'", $value); - case dibi::DATETIME: - return $value instanceof DateTime ? $value->format("'Y-m-d H:i:s'") : date("'Y-m-d H:i:s'", $value); + if (!$value instanceof DateTime) { + $value = new DibiDateTime($value); + } + return $value->format($type === dibi::DATETIME ? "'Y-m-d H:i:s'" : "Y-m-d"); default: throw new InvalidArgumentException('Unsupported type.'); diff --git a/dibi/drivers/DibiMsSqlDriver.php b/dibi/drivers/DibiMsSqlDriver.php index 1a5c90b9..733d355c 100644 --- a/dibi/drivers/DibiMsSqlDriver.php +++ b/dibi/drivers/DibiMsSqlDriver.php @@ -222,10 +222,11 @@ class DibiMsSqlDriver extends DibiObject implements IDibiDriver, IDibiResultDriv return $value ? 1 : 0; case dibi::DATE: - return $value instanceof DateTime ? $value->format("'Y-m-d'") : date("'Y-m-d'", $value); - case dibi::DATETIME: - return $value instanceof DateTime ? $value->format("'Y-m-d H:i:s'") : date("'Y-m-d H:i:s'", $value); + if (!$value instanceof DateTime) { + $value = new DibiDateTime($value); + } + return $value->format($type === dibi::DATETIME ? "'Y-m-d H:i:s'" : "Y-m-d"); default: throw new InvalidArgumentException('Unsupported type.'); diff --git a/dibi/drivers/DibiMySqlDriver.php b/dibi/drivers/DibiMySqlDriver.php index 1cd933ec..56f7f8e0 100644 --- a/dibi/drivers/DibiMySqlDriver.php +++ b/dibi/drivers/DibiMySqlDriver.php @@ -316,10 +316,11 @@ class DibiMySqlDriver extends DibiObject implements IDibiDriver, IDibiResultDriv return $value ? 1 : 0; case dibi::DATE: - return $value instanceof DateTime ? $value->format("'Y-m-d'") : date("'Y-m-d'", $value); - case dibi::DATETIME: - return $value instanceof DateTime ? $value->format("'Y-m-d H:i:s'") : date("'Y-m-d H:i:s'", $value); + if (!$value instanceof DateTime) { + $value = new DibiDateTime($value); + } + return $value->format($type === dibi::DATETIME ? "'Y-m-d H:i:s'" : "Y-m-d"); default: throw new InvalidArgumentException('Unsupported type.'); diff --git a/dibi/drivers/DibiMySqliDriver.php b/dibi/drivers/DibiMySqliDriver.php index f6698649..3d0d752e 100644 --- a/dibi/drivers/DibiMySqliDriver.php +++ b/dibi/drivers/DibiMySqliDriver.php @@ -304,10 +304,11 @@ class DibiMySqliDriver extends DibiObject implements IDibiDriver, IDibiResultDri return $value ? 1 : 0; case dibi::DATE: - return $value instanceof DateTime ? $value->format("'Y-m-d'") : date("'Y-m-d'", $value); - case dibi::DATETIME: - return $value instanceof DateTime ? $value->format("'Y-m-d H:i:s'") : date("'Y-m-d H:i:s'", $value); + if (!$value instanceof DateTime) { + $value = new DibiDateTime($value); + } + return $value->format($type === dibi::DATETIME ? "'Y-m-d H:i:s'" : "Y-m-d"); default: throw new InvalidArgumentException('Unsupported type.'); diff --git a/dibi/drivers/DibiOdbcDriver.php b/dibi/drivers/DibiOdbcDriver.php index 70d7cb7c..10e32832 100644 --- a/dibi/drivers/DibiOdbcDriver.php +++ b/dibi/drivers/DibiOdbcDriver.php @@ -245,10 +245,11 @@ class DibiOdbcDriver extends DibiObject implements IDibiDriver, IDibiResultDrive return $value ? 1 : 0; case dibi::DATE: - return $value instanceof DateTime ? $value->format("#m/d/Y#") : date("#m/d/Y#", $value); - case dibi::DATETIME: - return $value instanceof DateTime ? $value->format("#m/d/Y H:i:s#") : date("#m/d/Y H:i:s#", $value); + if (!$value instanceof DateTime) { + $value = new DibiDateTime($value); + } + return $value->format($type === dibi::DATETIME ? "#m/d/Y H:i:s#" : "#m/d/Y#"); default: throw new InvalidArgumentException('Unsupported type.'); diff --git a/dibi/drivers/DibiOracleDriver.php b/dibi/drivers/DibiOracleDriver.php index 41e9069e..837b0e44 100644 --- a/dibi/drivers/DibiOracleDriver.php +++ b/dibi/drivers/DibiOracleDriver.php @@ -239,10 +239,11 @@ class DibiOracleDriver extends DibiObject implements IDibiDriver, IDibiResultDri return $value ? 1 : 0; case dibi::DATE: - return $value instanceof DateTime ? $value->format($this->fmtDate) : date($this->fmtDate, $value); - case dibi::DATETIME: - return $value instanceof DateTime ? $value->format($this->fmtDateTime) : date($this->fmtDateTime, $value); + if (!$value instanceof DateTime) { + $value = new DibiDateTime($value); + } + return $value->format($type === dibi::DATETIME ? $this->fmtDateTime : $this->fmtDate); default: throw new InvalidArgumentException('Unsupported type.'); diff --git a/dibi/drivers/DibiPdoDriver.php b/dibi/drivers/DibiPdoDriver.php index be2dc3ab..0ae7cdc9 100644 --- a/dibi/drivers/DibiPdoDriver.php +++ b/dibi/drivers/DibiPdoDriver.php @@ -282,10 +282,11 @@ class DibiPdoDriver extends DibiObject implements IDibiDriver, IDibiResultDriver return $this->connection->quote($value, PDO::PARAM_BOOL); case dibi::DATE: - return $value instanceof DateTime ? $value->format("'Y-m-d'") : date("'Y-m-d'", $value); - case dibi::DATETIME: - return $value instanceof DateTime ? $value->format("'Y-m-d H:i:s'") : date("'Y-m-d H:i:s'", $value); + if (!$value instanceof DateTime) { + $value = new DibiDateTime($value); + } + return $value->format($type === dibi::DATETIME ? "'Y-m-d H:i:s'" : "Y-m-d"); default: throw new InvalidArgumentException('Unsupported type.'); diff --git a/dibi/drivers/DibiPostgreDriver.php b/dibi/drivers/DibiPostgreDriver.php index 50e2b733..f8388ce4 100644 --- a/dibi/drivers/DibiPostgreDriver.php +++ b/dibi/drivers/DibiPostgreDriver.php @@ -297,10 +297,11 @@ class DibiPostgreDriver extends DibiObject implements IDibiDriver, IDibiResultDr return $value ? 'TRUE' : 'FALSE'; case dibi::DATE: - return $value instanceof DateTime ? $value->format("'Y-m-d'") : date("'Y-m-d'", $value); - case dibi::DATETIME: - return $value instanceof DateTime ? $value->format("'Y-m-d H:i:s'") : date("'Y-m-d H:i:s'", $value); + if (!$value instanceof DateTime) { + $value = new DibiDateTime($value); + } + return $value->format($type === dibi::DATETIME ? "'Y-m-d H:i:s'" : "Y-m-d"); default: throw new InvalidArgumentException('Unsupported type.'); diff --git a/dibi/drivers/DibiSqlite3Driver.php b/dibi/drivers/DibiSqlite3Driver.php index dcde878b..091286a4 100644 --- a/dibi/drivers/DibiSqlite3Driver.php +++ b/dibi/drivers/DibiSqlite3Driver.php @@ -238,10 +238,11 @@ class DibiSqlite3Driver extends DibiObject implements IDibiDriver, IDibiResultDr return $value ? 1 : 0; case dibi::DATE: - return $value instanceof DateTime ? $value->format($this->fmtDate) : date($this->fmtDate, $value); - case dibi::DATETIME: - return $value instanceof DateTime ? $value->format($this->fmtDateTime) : date($this->fmtDateTime, $value); + if (!$value instanceof DateTime) { + $value = new DibiDateTime($value); + } + return $value->format($type === dibi::DATETIME ? $this->fmtDateTime : $this->fmtDate); default: throw new InvalidArgumentException('Unsupported type.'); diff --git a/dibi/drivers/DibiSqliteDriver.php b/dibi/drivers/DibiSqliteDriver.php index b2306d3b..86491799 100644 --- a/dibi/drivers/DibiSqliteDriver.php +++ b/dibi/drivers/DibiSqliteDriver.php @@ -243,10 +243,11 @@ class DibiSqliteDriver extends DibiObject implements IDibiDriver, IDibiResultDri return $value ? 1 : 0; case dibi::DATE: - return $value instanceof DateTime ? $value->format($this->fmtDate) : date($this->fmtDate, $value); - case dibi::DATETIME: - return $value instanceof DateTime ? $value->format($this->fmtDateTime) : date($this->fmtDateTime, $value); + if (!$value instanceof DateTime) { + $value = new DibiDateTime($value); + } + return $value->format($type === dibi::DATETIME ? $this->fmtDateTime : $this->fmtDate); default: throw new InvalidArgumentException('Unsupported type.'); diff --git a/dibi/libs/DibiResult.php b/dibi/libs/DibiResult.php index 3e4ee362..ff82319b 100644 --- a/dibi/libs/DibiResult.php +++ b/dibi/libs/DibiResult.php @@ -524,20 +524,9 @@ class DibiResult extends DibiObject implements IDataSource $row[$key] = ((bool) $value) && $value !== 'f' && $value !== 'F'; } elseif ($type === dibi::DATE || $type === dibi::DATETIME) { - if ((int) $value === 0 && substr((string) $value, 0, 3) !== '00:') { // '', NULL, FALSE, '0000-00-00', ... - - } elseif (empty($this->formats[$type])) { // return DateTime object (default) - $row[$key] = new DibiDateTime(is_numeric($value) ? date('Y-m-d H:i:s', $value) : $value); - - } elseif ($this->formats[$type] === 'U') { // return timestamp - $row[$key] = is_numeric($value) ? (int) $value : strtotime($value); - - } elseif (is_numeric($value)) { // formatted date - $row[$key] = date($this->formats[$type], $value); - - } else { + if ((int) $value !== 0 || substr((string) $value, 0, 3) === '00:') { // '', NULL, FALSE, '0000-00-00', ... $value = new DibiDateTime($value); - $row[$key] = $value->format($this->formats[$type]); + $row[$key] = empty($this->formats[$type]) ? $value : $value->format($this->formats[$type]); } } elseif ($type === dibi::BINARY) { diff --git a/dibi/libs/DibiRow.php b/dibi/libs/DibiRow.php index 19a8bc6d..476f2bd7 100644 --- a/dibi/libs/DibiRow.php +++ b/dibi/libs/DibiRow.php @@ -46,7 +46,7 @@ class DibiRow implements ArrayAccess, IteratorAggregate, Countable if ((int) $time === 0) { // '', NULL, FALSE, '0000-00-00', ... return NULL; } - $time = new DibiDateTime(is_numeric($time) ? date('Y-m-d H:i:s', $time) : $time); + $time = new DibiDateTime($time); } return $format === NULL ? $time : $time->format($format); } @@ -61,9 +61,10 @@ class DibiRow implements ArrayAccess, IteratorAggregate, Countable { trigger_error(__METHOD__ . '() is deprecated.', E_USER_WARNING); $time = $this[$key]; - return (int) $time === 0 // '', NULL, FALSE, '0000-00-00', ... - ? NULL - : (is_numeric($time) ? (int) $time : strtotime($time)); + if ((int) $time !== 0) { // '', NULL, FALSE, '0000-00-00', ... + $time = new DibiDateTime($time); + return $time->getTimestamp(); + } }