1
0
mirror of https://github.com/dg/dibi.git synced 2025-08-05 05:37:39 +02:00

added DateTime53, fix for crappy DateTime in PHP 5.2

This commit is contained in:
David Grudl
2010-01-14 23:41:37 +01:00
parent 9d4a887e7a
commit e177436e38
4 changed files with 65 additions and 7 deletions

54
dibi/Nette/DateTime53.php Normal file
View File

@@ -0,0 +1,54 @@
<?php
/**
* Nette Framework
*
* @copyright Copyright (c) 2004, 2010 David Grudl
* @license http://nettephp.com/license Nette license
* @link http://nettephp.com
* @category Nette
* @package Nette
*/
// no namespace
/**
* DateTime with serialization and timestamp support for PHP 5.2.
*
* @copyright Copyright (c) 2004, 2010 David Grudl
* @package Nette
*/
class DateTime53 extends DateTime
{
public function __sleep()
{
$this->fix = array($this->format('Y-m-d H:i:s'), $this->getTimezone()->getName());
return array('fix');
}
public function __wakeup()
{
$this->__construct($this->fix[0], new DateTimeZone($this->fix[1]));
unset($this->fix);
}
public function getTimestamp()
{
return (int) $this->format('U');
}
public function setTimestamp($timestamp)
{
return $this->__construct(date('Y-m-d H:i:s', $timestamp), new DateTimeZone($this->getTimezone()->getName())); // getTimeZone() crashes in PHP 5.2.6
}
}

View File

@@ -60,12 +60,16 @@ if (!interface_exists(/*Nette\*/'IDebuggable', FALSE)) {
require_once dirname(__FILE__) . '/Nette/IDebuggable.php';
}
if (!class_exists('DateTime53', FALSE)) {
require_once dirname(__FILE__) . '/Nette/DateTime53.php';
}
/**
* Back-compatibility
*/
class DibiVariable extends DateTime
class DibiVariable extends DateTime53
{
function __construct($val)
{
@@ -587,7 +591,7 @@ class dibi
*/
public static function datetime($time = NULL)
{
return new DateTime(is_numeric($time) ? date('Y-m-d H:i:s', $time) : $time);
return new DateTime53(is_numeric($time) ? date('Y-m-d H:i:s', $time) : $time);
}
@@ -597,7 +601,7 @@ class dibi
*/
public static function date($date = NULL)
{
return new DateTime(is_numeric($date) ? date('Y-m-d', $date) : $date);
return new DateTime53(is_numeric($date) ? date('Y-m-d', $date) : $date);
}

View File

@@ -623,13 +623,13 @@ class DibiResult extends DibiObject implements IDataSource
return is_numeric($value) ? (int) $value : strtotime($value);
} elseif ($format === TRUE) { // return DateTime object
return new DateTime(is_numeric($value) ? date('Y-m-d H:i:s', $value) : $value);
return new DateTime53(is_numeric($value) ? date('Y-m-d H:i:s', $value) : $value);
} elseif (is_numeric($value)) { // single timestamp
return date($format, $value);
} else {
$value = new DateTime($value);
$value = new DateTime53($value);
return $value->format($format);
}

View File

@@ -47,13 +47,13 @@ class DibiRow extends ArrayObject
return is_numeric($time) ? (int) $time : strtotime($time);
} elseif ($format === TRUE) { // return DateTime object
return new DateTime(is_numeric($time) ? date('Y-m-d H:i:s', $time) : $time);
return new DateTime53(is_numeric($time) ? date('Y-m-d H:i:s', $time) : $time);
} elseif (is_numeric($time)) { // single timestamp
return date($format, $time);
} else {
$time = new DateTime($time);
$time = new DateTime53($time);
return $time->format($format);
}
}