diff --git a/core/Base/Date.class.php b/core/Base/Date.class.php index 57ce906b7b..4d6cd33b9a 100644 --- a/core/Base/Date.class.php +++ b/core/Base/Date.class.php @@ -27,20 +27,18 @@ class Date implements Stringable, DialectString const WEEKDAY_FRIDAY = 5; const WEEKDAY_SATURDAY = 6; const WEEKDAY_SUNDAY = 0; // because strftime('%w') is 0 on Sunday - - protected $string = null; - protected $int = null; - - protected $year = null; - protected $month = null; - protected $day = null; - + + /** + * @var DateTime + */ + protected $dateTime = null; + /** * @return Date **/ public static function create($date) { - return new self($date); + return new static($date); } public static function today($delimiter = '-') @@ -53,7 +51,7 @@ public static function today($delimiter = '-') **/ public static function makeToday() { - return new self(self::today()); + return new static(static::today()); } /** @@ -68,13 +66,13 @@ public static function makeFromWeek($weekNumber, $year = null) Assert::isTrue( ($weekNumber > 0) - && ($weekNumber <= self::getWeekCountInYear($year)) + && ($weekNumber <= static::getWeekCountInYear($year)) ); $date = - new self( + new static( date( - self::getFormat(), + static::getFormat(), mktime( 0, 0, 0, 1, 1, $year ) @@ -85,7 +83,7 @@ public static function makeFromWeek($weekNumber, $year = null) ( ( $weekNumber - 1 - + (self::getWeekCountInYear($year - 1) == 53 ? 1 : 0) + + (static::getWeekCountInYear($year - 1) == 53 ? 1 : 0) ) * 7 ) + 1 - $date->getWeekDay(); @@ -110,10 +108,10 @@ public static function dayDifference(Date $left, Date $right) public static function compare(Date $left, Date $right) { - if ($left->int == $right->int) + if ($left->toStamp() == $right->toStamp()) return 0; else - return ($left->int > $right->int ? 1 : -1); + return ($left->toStamp() > $right->toStamp() ? 1 : -1); } public static function getWeekCountInYear($year) @@ -129,69 +127,57 @@ public static function getWeekCountInYear($year) public function __construct($date) { - if (is_int($date) || is_numeric($date)) { // unix timestamp - $this->string = date($this->getFormat(), $date); - } elseif ($date && is_string($date)) - $this->stringImport($date); - - if ($this->string === null) { - throw new WrongArgumentException( - "strange input given - '{$date}'" - ); - } - - $this->import($this->string); - $this->buildInteger(); + $this->import($date); } - public function __sleep() + public function __clone() { - return array('int'); + $this->dateTime = clone $this->dateTime; } - - public function __wakeup() + + public function __sleep() { - $this->import(date($this->getFormat(), $this->int)); + return array('dateTime'); } - + public function toStamp() { - return $this->int; + return $this->getDateTime()->getTimestamp(); } public function toDate($delimiter = '-') { return - $this->year + $this->getYear() .$delimiter - .$this->month + .$this->getMonth() .$delimiter - .$this->day; + .$this->getDay(); } public function getYear() { - return $this->year; + return $this->dateTime->format('Y'); } public function getMonth() { - return $this->month; + return $this->dateTime->format('m'); } public function getDay() { - return $this->day; + return $this->dateTime->format('d'); } public function getWeek() { - return date('W', $this->int); + return date('W', $this->dateTime->getTimestamp()); } public function getWeekDay() { - return strftime('%w', $this->int); + return strftime('%w', $this->dateTime->getTimestamp()); } /** @@ -199,7 +185,8 @@ public function getWeekDay() **/ public function spawn($modification = null) { - $child = new $this($this->string); + + $child = new static($this->toString()); if ($modification) return $child->modify($modification); @@ -214,17 +201,8 @@ public function spawn($modification = null) public function modify($string) { try { - $time = strtotime($string, $this->int); - - if ($time === false) - throw new WrongArgumentException( - "modification yielded false '{$string}'" - ); - - $this->int = $time; - $this->string = date($this->getFormat(), $time); - $this->import($this->string); - } catch (BaseException $e) { + $this->dateTime->modify($string); + } catch (Exception $e) { throw new WrongArgumentException( "wrong time string '{$string}'" ); @@ -238,9 +216,9 @@ public function getDayStartStamp() return mktime( 0, 0, 0, - $this->month, - $this->day, - $this->year + $this->getMonth(), + $this->getDay(), + $this->getYear() ); } @@ -249,9 +227,9 @@ public function getDayEndStamp() return mktime( 23, 59, 59, - $this->month, - $this->day, - $this->year + $this->getMonth(), + $this->getDay(), + $this->getYear() ); } @@ -277,12 +255,12 @@ public function getLastDayOfWeek($weekStart = Date::WEEKDAY_MONDAY) public function toString() { - return $this->string; + return $this->dateTime->format(static::getFormat()); } public function toFormatString($format) { - return date($format, $this->toStamp()); + return $this->dateTime->format($format); } public function toDialectString(Dialect $dialect) @@ -306,57 +284,53 @@ public function toTimestamp() { return Timestamp::create($this->toStamp()); } + + /** + * @return DateTime|null + */ + public function getDateTime() + { + return $this->dateTime; + } protected static function getFormat() { return 'Y-m-d'; } - - /* void */ protected function import($string) + + + protected function import($date) { - list($this->year, $this->month, $this->day) = - explode('-', $string, 3); - - if (!$this->month || !$this->day) + try{ + if (is_int($date) || is_numeric($date)) { // unix timestamp + $this->dateTime = new DateTime(date(static::getFormat(), $date)); + + } elseif ($date && is_string($date)) { + + if ( + preg_match('/^(\d{1,4})[-\.](\d{1,2})[-\.](\d{1,2})/', $date, $matches) + ) { + Assert::isTrue( + checkdate($matches[2], $matches[3], $matches[1]) + ); + } elseif ( + preg_match('/^(\d{1,2})[-\.](\d{1,2})[-\.](\d{1,4})/', $date, $matches) + ) { + Assert::isTrue( + checkdate($matches[2], $matches[2], $matches[3]) + ); + } + + $this->dateTime = new DateTime($date); + } + + + } catch(Exception $e) { throw new WrongArgumentException( - 'month and day must not be zero' - ); - - $this->string = - sprintf( - '%04d-%02d-%02d', - $this->year, - $this->month, - $this->day - ); - - list($this->year, $this->month, $this->day) = - explode('-', $this->string, 3); - } - - /* void */ protected function stringImport($string) - { - $matches = array(); - - if ( - preg_match('/^(\d{1,4})-(\d{1,2})-(\d{1,2})$/', $string, $matches) - ) { - if (checkdate($matches[2], $matches[3], $matches[1])) - $this->string = $string; - - } elseif (($stamp = strtotime($string)) !== false) - $this->string = date($this->getFormat(), $stamp); - } - - /* void */ protected function buildInteger() - { - $this->int = - mktime( - 0, 0, 0, - $this->month, - $this->day, - $this->year + "strange input given - '{$date}'" ); + } + } } ?> diff --git a/core/Base/Timestamp.class.php b/core/Base/Timestamp.class.php index f097d3db2a..85f3f2c1a7 100644 --- a/core/Base/Timestamp.class.php +++ b/core/Base/Timestamp.class.php @@ -20,21 +20,17 @@ **/ class Timestamp extends Date { - private $hour = null; - private $minute = null; - private $second = null; - /** * @return Timestamp **/ - public static function create($timestamp) + public static function create($timestamp, DateTimeZone $zone=null) { - return new self($timestamp); + return new static($timestamp, $zone); } public static function now() { - return date(self::getFormat()); + return date(static::getFormat()); } /** @@ -42,7 +38,7 @@ public static function now() **/ public static function makeNow() { - return new self(time()); + return new static(time()); } /** @@ -50,17 +46,40 @@ public static function makeNow() **/ public static function makeToday() { - return new self(self::today()); + return new static(static::today()); + } + + public function __construct($dateTime, DateTimeZone $zone=null) + { + parent::__construct($dateTime); + + if($zone) { + $this->dateTime->setTimezone($zone); + } + + } + + private function getDefaultTimeZone() + { + try { + $defaultTimeZoneName = date_default_timezone_get(); + return new DateTimeZone($defaultTimeZoneName); + } catch(Exception $e) { + throw new WrongStateException( + "strange default time zone given - '{$defaultTimeZoneName}'!". + 'Use date_default_timezone_set() for set valid default time zone.' + ); + } } public function toTime($timeDelimiter = ':', $secondDelimiter = '.') { return - $this->hour + $this->getHour() .$timeDelimiter - .$this->minute + .$this->getMinute() .$secondDelimiter - .$this->second; + .$this->getSecond(); } public function toDateTime( @@ -76,17 +95,17 @@ public function toDateTime( public function getHour() { - return $this->hour; + return $this->dateTime->format('H'); } public function getMinute() { - return $this->minute; + return $this->dateTime->format('i'); } public function getSecond() { - return $this->second; + return $this->dateTime->format('s'); } public function equals(Timestamp $timestamp) @@ -96,25 +115,25 @@ public function equals(Timestamp $timestamp) public function getDayStartStamp() { - if (!$this->hour && !$this->minute && !$this->second) - return $this->int; + if (!$this->getHour() && !$this->getMinute() && !$this->getSecond()) + return $this->dateTime->getTimestamp(); else return parent::getDayStartStamp(); } public function getHourStartStamp() { - if (!$this->minute && !$this->second) - return $this->int; + if (!$this->getMinute() && !$this->getSecond()) + return $this->dateTime->getTimestamp(); return mktime( - $this->hour, + $this->getHour(), 0, 0, - $this->month, - $this->day, - $this->year + $this->getMonth(), + $this->getDay(), + $this->getYear() ); } @@ -124,9 +143,9 @@ public function getHourStartStamp() public function toIsoString($convertToUtc = true) { if ($convertToUtc) - return date('Y-m-d\TH:i:s\Z', $this->int - date('Z', $this->int)); + return date('Y-m-d\TH:i:s\Z', $this->dateTime->getTimestamp() - date('Z', $this->dateTime->getTimestamp())); else - return date('Y-m-d\TH:i:sO', $this->int); + return date('Y-m-d\TH:i:sO', $this->dateTime->getTimestamp()); } /** @@ -134,74 +153,12 @@ public function toIsoString($convertToUtc = true) **/ public function toTimestamp() { - return $this; + return $this->spawn(); } protected static function getFormat() { return 'Y-m-d H:i:s'; } - - /* void */ protected function import($string) - { - list($date, $time) = explode(' ', $string, 2); - - list($this->hour, $this->minute, $this->second) = - explode(':', $time, 3); - - $time = - sprintf( - '%02d:%02d:%02d', - $this->hour, - $this->minute, - $this->second - ); - - list($this->hour, $this->minute, $this->second) = - explode(':', $time, 3); - - parent::import($date); - - $this->string .= ' '.$time; - } - - /* void */ protected function stringImport($string) - { - $matches = array(); - - if ( - preg_match( - '/^(\d{1,4})-(\d{1,2})-(\d{1,2})\s\d{1,2}:\d{1,2}:\d{1,2}$/', - $string, - $matches - ) - ) { - if (checkdate($matches[2], $matches[3], $matches[1])) - $this->string = $string; - } elseif ( - preg_match( - '/^(\d{1,4})-(\d{1,2})-(\d{1,2})$/', - $string, - $matches - ) - ) { - if (checkdate($matches[2], $matches[3], $matches[1])) - $this->string = $string . ' 00:00:00'; - } elseif (($stamp = strtotime($string)) !== false) - $this->string = date($this->getFormat(), $stamp); - } - - /* void */ protected function buildInteger() - { - $this->int = - mktime( - $this->hour, - $this->minute, - $this->second, - $this->month, - $this->day, - $this->year - ); - } } ?> \ No newline at end of file diff --git a/core/Base/TimestampTZ.class.php b/core/Base/TimestampTZ.class.php new file mode 100644 index 0000000000..47234e984c --- /dev/null +++ b/core/Base/TimestampTZ.class.php @@ -0,0 +1,57 @@ +toStamp(), $zone); + } + + return parent::toTimestamp(); + } + + public static function compare(Date $left, Date $right) + { + Assert::isTrue( + ( + $left instanceof TimestampTZ + && $right instanceof TimestampTZ + ) + ); + + return parent::compare($left, $right); + } + } +?> \ No newline at end of file diff --git a/core/DB/PgSQL.class.php b/core/DB/PgSQL.class.php index cacec8acad..646131303e 100644 --- a/core/DB/PgSQL.class.php +++ b/core/DB/PgSQL.class.php @@ -187,7 +187,10 @@ public function getTableInfo($table) static $types = array( 'time' => DataType::TIME, 'date' => DataType::DATE, - 'timestamp' => DataType::TIMESTAMP, + + 'timestamp' => DataType::TIMESTAMP, + 'timestamptz' => DataType::TIMESTAMPTZ, + 'timestamp with time zone' => DataType::TIMESTAMPTZ, 'bool' => DataType::BOOLEAN, diff --git a/core/Form/Primitive.class.php b/core/Form/Primitive.class.php index 0d94b18a1d..41f6fab878 100644 --- a/core/Form/Primitive.class.php +++ b/core/Form/Primitive.class.php @@ -146,6 +146,14 @@ public static function timestamp($name) { return new PrimitiveTimestamp($name); } + + /** + * @return PrimitiveTimestampTZ + **/ + public static function timestampTZ($name) + { + return new PrimitiveTimestampTZ($name); + } /** * @return PrimitiveTime diff --git a/core/Form/Primitives/ComplexPrimitive.class.php b/core/Form/Primitives/ComplexPrimitive.class.php index f5dfdaa3fb..aa739efb39 100644 --- a/core/Form/Primitives/ComplexPrimitive.class.php +++ b/core/Form/Primitives/ComplexPrimitive.class.php @@ -74,8 +74,8 @@ public function setAnyState() } // implement me, child :-) - abstract protected function importSingle($scope); - abstract protected function importMarried($scope); + abstract public function importSingle($scope); + abstract public function importMarried($scope); public function import($scope) { diff --git a/core/Form/Primitives/PrimitiveTimestamp.class.php b/core/Form/Primitives/PrimitiveTimestamp.class.php index d3fb56dab7..23413801db 100644 --- a/core/Form/Primitives/PrimitiveTimestamp.class.php +++ b/core/Form/Primitives/PrimitiveTimestamp.class.php @@ -12,7 +12,7 @@ /** * @ingroup Primitives **/ - final class PrimitiveTimestamp extends PrimitiveDate + class PrimitiveTimestamp extends PrimitiveDate { const HOURS = 'hrs'; const MINUTES = 'min'; @@ -73,5 +73,27 @@ protected function getObjectName() { return 'Timestamp'; } + + public function exportValue() + { + $parent = parent::exportValue(); + + if(is_array($parent)) { + + if($this->value) { + $parent[static::HOURS] = $this->value->getHour(); + $parent[static::MINUTES] = $this->value->getMinute(); + $parent[static::SECONDS] = $this->value->getSecond(); + + } else { + + $parent[static::HOURS] = null; + $parent[static::MINUTES] = null; + $parent[static::SECONDS] = null; + } + } + + return $parent; + } } ?> \ No newline at end of file diff --git a/core/Form/Primitives/PrimitiveTimestampTZ.class.php b/core/Form/Primitives/PrimitiveTimestampTZ.class.php new file mode 100644 index 0000000000..f5ade79792 --- /dev/null +++ b/core/Form/Primitives/PrimitiveTimestampTZ.class.php @@ -0,0 +1,91 @@ +name][self::DAY], + $scope[$this->name][self::MONTH], + $scope[$this->name][self::YEAR], + $scope[$this->name][self::HOURS], + $scope[$this->name][self::MINUTES], + $scope[$this->name][self::SECONDS], + $scope[$this->name][self::ZONE] + ) + && is_array($scope[$this->name]) + ) { + if ($this->isEmpty($scope)) + return !$this->isRequired(); + + $zone = $scope[$this->name][self::ZONE]; + + $hours = (int) $scope[$this->name][self::HOURS]; + $minutes = (int) $scope[$this->name][self::MINUTES]; + $seconds = (int) $scope[$this->name][self::SECONDS]; + + $year = (int) $scope[$this->name][self::YEAR]; + $month = (int) $scope[$this->name][self::MONTH]; + $day = (int) $scope[$this->name][self::DAY]; + + if (!checkdate($month, $day, $year)) + return false; + + try { + $stamp = new TimestampTZ( + $year.'-'.$month.'-'.$day.' ' + .$hours.':'.$minutes.':'.$seconds + .' '.$zone + ); + } catch (WrongArgumentException $e) { + return false; + } + + if ($this->checkRanges($stamp)) { + $this->value = $stamp; + return true; + } + } + + return false; + } + + protected function getObjectName() + { + return 'TimestampTZ'; + } + + public function exportValue() + { + $parent = parent::exportValue(); + + if(is_array($parent)) { + if($this->value) { + $parent[static::ZONE] = $this->value->getDateTime()->getTimezone()->getName(); + + } else { + $parent[static::ZONE] = null; + } + + } + + return $parent; + } + } +?> \ No newline at end of file diff --git a/core/OSQL/DataType.class.php b/core/OSQL/DataType.class.php index b299cb0990..87e57dc832 100644 --- a/core/OSQL/DataType.class.php +++ b/core/OSQL/DataType.class.php @@ -33,6 +33,8 @@ final class DataType extends Enumeration implements DialectString const DATE = 0x00000B; const TIME = 0x000A0C; const TIMESTAMP = 0x000A0D; + const TIMESTAMPTZ = 0x000A0E; + const INTERVAL = 0x00000F; const BINARY = 0x00000E; @@ -72,6 +74,7 @@ final class DataType extends Enumeration implements DialectString self::DATE => 'DATE', self::TIME => 'TIME', self::TIMESTAMP => 'TIMESTAMP', + self::TIMESTAMPTZ => 'TIMESTAMP', self::INTERVAL => 'INTERVAL', self::BINARY => 'BINARY', diff --git a/main/Base/CalendarDay.class.php b/main/Base/CalendarDay.class.php index b3a718951d..6923975ab3 100644 --- a/main/Base/CalendarDay.class.php +++ b/main/Base/CalendarDay.class.php @@ -29,7 +29,10 @@ public static function create($timestamp) public function __sleep() { - return array('int', 'selected', 'outside'); + $sleep = parent::__sleep(); + $sleep[] = 'selected'; + $sleep[] = 'outside'; + return $sleep; } public function isSelected() diff --git a/meta/types/TimeType.class.php b/meta/types/TimeType.class.php index a0cfbf40a3..246f56c222 100644 --- a/meta/types/TimeType.class.php +++ b/meta/types/TimeType.class.php @@ -18,12 +18,12 @@ public function getPrimitiveName() { return 'time'; } - + public function isGeneric() { return true; } - + public function toColumnType() { return 'DataType::create(DataType::TIME)'; diff --git a/meta/types/TimestampTZType.class.php b/meta/types/TimestampTZType.class.php new file mode 100644 index 0000000000..6327e7dfb0 --- /dev/null +++ b/meta/types/TimestampTZType.class.php @@ -0,0 +1,27 @@ +setTimezoned(true)'; + } + } +?> \ No newline at end of file diff --git a/test/config.inc.php.tpl b/test/config.inc.php.tpl index 847ed05472..36184d8f65 100644 --- a/test/config.inc.php.tpl +++ b/test/config.inc.php.tpl @@ -1,4 +1,5 @@ assertFalse( + $date1->getDateTime() === $date2->getDateTime() + ); + } + /** * @test **/ diff --git a/test/core/PrimitiveTimestampTZTest.class.php b/test/core/PrimitiveTimestampTZTest.class.php new file mode 100644 index 0000000000..1e850ebdfb --- /dev/null +++ b/test/core/PrimitiveTimestampTZTest.class.php @@ -0,0 +1,73 @@ +format('O'); + + $prm = Primitive::timestampTZ('test')->setComplex(); + + $array = array( + 'test' => array( + PrimitiveDate::DAY => '1', + PrimitiveDate::MONTH => '2', + PrimitiveDate::YEAR => '', + PrimitiveTimestamp::HOURS => '17', + PrimitiveTimestamp::MINUTES => '38', + PrimitiveTimestamp::SECONDS => '59', + PrimitiveTimestampTZ::ZONE => $currentTimeZone->getName(), + ) + ); + + $this->assertFalse($prm->import($array)); + $this->assertEquals($array['test'], $prm->getRawValue()); + + $this->assertEmpty( + array_filter($prm->exportValue()) + ); + + //not supported other epochs + $array['test'][PrimitiveDate::YEAR] = '3456'; + $this->assertTrue($prm->import($array)); + $this->assertEquals(3456, $prm->getValue()->getYear()); + $this->assertEquals(17, $prm->getValue()->getHour()); + + $this->assertEquals( + $array['test'], + $prm->exportValue() + ); + + $array['test'][PrimitiveDate::YEAR] = '2012'; + + $this->assertTrue($prm->import($array)); + $this->assertEquals( + '2012-02-01 17:38:59'.$zone, + $prm->getValue()->toString() + ); + } + + public function testSingle() + { + $prm = Primitive::timestampTZ('test')->setSingle(); + + $array = array('test' => '1234-01-02 17:38:59'); + + $this->assertTrue($prm->import($array)); + $this->assertEquals(1234, $prm->getValue()->getYear()); + + $array = array('test' => '1975-01-02 17:38:59'); + + $this->assertTrue($prm->import($array)); + + $this->assertEquals( + '1975-01-02 17:38.59', + $prm->getValue()->toDateTime() + ); + } + } +?> \ No newline at end of file diff --git a/test/core/PrimitiveTimestampTest.class.php b/test/core/PrimitiveTimestampTest.class.php index 2dfe06d9f9..60cc6b3217 100644 --- a/test/core/PrimitiveTimestampTest.class.php +++ b/test/core/PrimitiveTimestampTest.class.php @@ -20,11 +20,20 @@ public function testMarried() $this->assertFalse($prm->import($array)); $this->assertEquals($prm->getRawValue(), $array['test']); + + $this->assertEmpty( + array_filter($prm->exportValue()) + ); $array['test'][PrimitiveDate::YEAR] = '3456'; $this->assertTrue($prm->import($array)); $this->assertTrue($prm->getValue()->getYear() == 3456); + + $this->assertEquals( + $array['test'], + $prm->exportValue() + ); $array['test'][PrimitiveDate::YEAR] = '2006'; diff --git a/test/core/TimestampTZTest.class.php b/test/core/TimestampTZTest.class.php new file mode 100644 index 0000000000..46e204d642 --- /dev/null +++ b/test/core/TimestampTZTest.class.php @@ -0,0 +1,66 @@ +assertEquals( + '2011-01-01 12:10:10+0300', + $someDate->toTimestamp('Europe/Moscow')->toString() + ); + + $this->assertEquals( + $someDate->toTimestamp('Europe/London')->toString(), + '2011-01-01 09:10:10+0000' + ); + + $moscowTime = TimestampTZ::create('2011-01-01 00:00:00 Europe/Moscow'); + $londonTime = TimestampTZ::create('2010-12-31 21:00:00 Europe/London'); + + $this->assertEquals(0, TimestampTZ::compare($moscowTime, $londonTime)); + + $moscowTime->modify('+ 1 second'); + $this->assertEquals(TimestampTZ::compare($moscowTime, $londonTime), 1); + $moscowTime->modify('- 2 second'); + $this->assertEquals(TimestampTZ::compare($moscowTime, $londonTime), -1); + + + $this->assertEquals( + $moscowTime->toTimestamp('Europe/Moscow')->toString(), + '2010-12-31 23:59:59+0300' + ); + } + + /** + * @group ff + */ + public function testDialect() + { + //setup + $someDate = TimestampTZ::create('2012-02-23 12:12:12 GMT'); + //expectation + $expectation = $someDate->toTimestamp()->toString(); + + //check + $this->assertEquals( + $someDate->toDialectString(ImaginaryDialect::me()), + $expectation + ); + } + + /** + * @group ff + */ + public function testSleeping() { + $time = TimestampTZ::create('2011-03-08 12:12:12 PST'); + $sleepedTime = unserialize(serialize($time)); + + $this->assertEquals(TimestampTZ::compare($time, $sleepedTime), 0); + } + } +?> \ No newline at end of file diff --git a/test/core/TimestampTest.class.php b/test/core/TimestampTest.class.php index 32b6e27e8d..5dcb3a7632 100644 --- a/test/core/TimestampTest.class.php +++ b/test/core/TimestampTest.class.php @@ -8,16 +8,16 @@ public function testNonEpoch() $future = '4683-03-04'; $after = new Timestamp($future); - $this->assertEquals($after->getDay(), '04'); - $this->assertEquals($after->getMonth(), '03'); - $this->assertEquals($after->getYear(), '4683'); + $this->assertEquals('04', $after->getDay()); + $this->assertEquals('03', $after->getMonth()); + $this->assertEquals('4683', $after->getYear()); $past = '1234-04-03'; $before = new Timestamp($past); - $this->assertEquals($before->getDay(), '03'); - $this->assertEquals($before->getMonth(), '04'); - $this->assertEquals($before->getYear(), '1234'); + $this->assertEquals('03', $before->getDay()); + $this->assertEquals('04', $before->getMonth()); + $this->assertEquals('1234', $before->getYear()); $this->assertFalse($after->equals($before)); @@ -27,6 +27,36 @@ public function testNonEpoch() $time = ' 00:00.00'; $this->assertEquals($future.$time, $after->toDateTime()); $this->assertEquals($past.$time, $before->toDateTime()); + + $past = '1-04-03'; + $before = new Timestamp($past); + + $this->assertEquals('03', $before->getDay()); + $this->assertEquals('04', $before->getMonth()); + $this->assertEquals( + substr(date('Y', time()), 0, 2).'01', + $before->getYear() + ); + + $past = '14-01-02'; + $before = new Timestamp($past); + + $this->assertEquals('02', $before->getDay()); + $this->assertEquals('01', $before->getMonth()); + $this->assertEquals( + substr(date('Y', time()), 0, 2).'14', + $before->getYear() + ); + + $past = '113-01-02'; + $before = new Timestamp($past); + + $this->assertEquals('02', $before->getDay()); + $this->assertEquals('01', $before->getMonth()); + $this->assertEquals( + '0113', + $before->getYear() + ); } public function testInvalidTimestamp() @@ -123,6 +153,32 @@ public function testSleeping() $this->assertEquals($stamp->getMinute(), $unserializedStamp->getMinute()); $this->assertEquals($stamp->getSecond(), $unserializedStamp->getSecond()); + + $stamp = Timestamp::create('2039-01-05 12:14:05 Europe/Moscow'); + + $serializedStamp = serialize($stamp); + + $unserializedStamp = unserialize($serializedStamp); + + $this->assertEquals($stamp->getDay(), $unserializedStamp->getDay()); + $this->assertEquals($stamp->getMonth(), $unserializedStamp->getMonth()); + $this->assertEquals($stamp->getYear(), $unserializedStamp->getYear()); + + $this->assertEquals($stamp->getMinute(), $unserializedStamp->getMinute()); + $this->assertEquals($stamp->getSecond(), $unserializedStamp->getSecond()); + + $stamp = Timestamp::create('1899-12-31 16:07:45 Europe/London'); + + $serializedStamp = serialize($stamp); + + $unserializedStamp = unserialize($serializedStamp); + + $this->assertEquals($stamp->getDay(), $unserializedStamp->getDay()); + $this->assertEquals($stamp->getMonth(), $unserializedStamp->getMonth()); + $this->assertEquals($stamp->getYear(), $unserializedStamp->getYear()); + + $this->assertEquals($stamp->getMinute(), $unserializedStamp->getMinute()); + $this->assertEquals($stamp->getSecond(), $unserializedStamp->getSecond()); } } ?> \ No newline at end of file