Skip to content

Commit

Permalink
returns date-time as immutable object Nette\Database\DateTime (BC bre…
Browse files Browse the repository at this point in the history
…ak) [Closes #270]
  • Loading branch information
dg committed May 7, 2024
1 parent 1e897af commit b67e76d
Show file tree
Hide file tree
Showing 13 changed files with 72 additions and 21 deletions.
34 changes: 34 additions & 0 deletions src/Database/DateTime.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

/**
* This file is part of the Nette Framework (https://nette.org)
* Copyright (c) 2004 David Grudl (https://davidgrudl.com)
*/

declare(strict_types=1);

namespace Nette\Database;


/**
* Date Time.
*/
final class DateTime extends \DateTimeImmutable implements \JsonSerializable
{
/**
* Returns JSON representation in ISO 8601 (used by JavaScript).
*/
public function jsonSerialize(): string
{
return $this->format('c');
}


/**
* Returns the date and time in the format 'Y-m-d H:i:s.u'.
*/
public function __toString(): string
{
return $this->format('Y-m-d H:i:s.u');
}
}
8 changes: 4 additions & 4 deletions src/Database/Helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ public static function detectType(string $type): string


/** @internal */
public static function normalizeRow(array $row, ResultSet $resultSet): array
public static function normalizeRow(array $row, ResultSet $resultSet, $dateTimeClass = DateTime::class): array
{
foreach ($resultSet->getColumnTypes() as $key => $type) {
$value = $row[$key];
Expand All @@ -224,10 +224,10 @@ public static function normalizeRow(array $row, ResultSet $resultSet): array
} elseif ($type === IStructure::FIELD_DATETIME || $type === IStructure::FIELD_DATE) {
$row[$key] = str_starts_with($value, '0000-00')
? null
: new Nette\Utils\DateTime($value);
: new $dateTimeClass($value);

} elseif ($type === IStructure::FIELD_TIME) {
$row[$key] = (new Nette\Utils\DateTime($value))->setDate(1, 1, 1);
$row[$key] = (new $dateTimeClass($value))->setDate(1, 1, 1);

} elseif ($type === IStructure::FIELD_TIME_INTERVAL) {
preg_match('#^(-?)(\d+)\D(\d+)\D(\d+)(\.\d+)?$#D', $value, $m);
Expand All @@ -236,7 +236,7 @@ public static function normalizeRow(array $row, ResultSet $resultSet): array
$row[$key]->invert = (int) (bool) $m[1];

} elseif ($type === IStructure::FIELD_UNIX_TIMESTAMP) {
$row[$key] = Nette\Utils\DateTime::from($value);
$row[$key] = (new $dateTimeClass)->setTimestamp($value);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/Database/Table/ActiveRow.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public function toArray(): array

/**
* Returns primary key value.
* @return mixed possible int, string, array, object (Nette\Utils\DateTime)
* @return mixed possible int, string, array, object (Nette\Database\DateTime)
*/
public function getPrimary(bool $throw = true): mixed
{
Expand Down
17 changes: 17 additions & 0 deletions tests/Database/DateTime.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

use Nette\Database\DateTime;
use Tester\Assert;

require __DIR__ . '/../bootstrap.php';


date_default_timezone_set('Europe/Prague');

// to string
Assert::same('1978-01-23 11:40:00.000000', (string) new DateTime('1978-01-23 11:40'));

// JSON
Assert::same('"1978-01-23T11:40:00+01:00"', json_encode(new DateTime('1978-01-23 11:40')));
4 changes: 2 additions & 2 deletions tests/Database/Explorer/Selection.fetchPairs().phpt
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ test('', function () use ($explorer) {
$explorer->table('author')->get(12)->update(['born' => new DateTime('2002-02-02')]);
$list = $explorer->table('author')->where('born IS NOT NULL')->order('born')->fetchPairs('born', 'name');
Assert::same([
'2002-02-02 00:00:00' => 'David Grudl',
'2002-02-20 00:00:00' => 'Jakub Vrana',
'2002-02-02 00:00:00.000000' => 'David Grudl',
'2002-02-20 00:00:00.000000' => 'Jakub Vrana',
], $list);
});
2 changes: 1 addition & 1 deletion tests/Database/Explorer/Selection.insert().phpt
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ $book = $explorer->table('author')->insert([
// id = 14

Assert::equal('eddard stark', $book->name);
Assert::equal(new Nette\Utils\DateTime('2011-11-11'), $book->born);
Assert::equal(new Nette\Database\DateTime('2011-11-11'), $book->born);


$books = $explorer->table('book');
Expand Down
2 changes: 1 addition & 1 deletion tests/Database/Explorer/bugs/bug216.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@ $book = $explorer->table('author')->insert([

Assert::type(Nette\Database\Table\ActiveRow::class, $book);
Assert::equal('eddard stark', $book->name);
Assert::equal(new Nette\Utils\DateTime('2011-11-11'), $book->born);
Assert::equal(new Nette\Database\DateTime('2011-11-11'), $book->born);
4 changes: 2 additions & 2 deletions tests/Database/ResultSet.fetchAssoc().phpt
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ test('', function () use ($connection) {
$pairs = $connection->query('UPDATE author SET born = ? WHERE id = 12', new DateTime('2002-02-02'));
$pairs = $connection->query('SELECT * FROM author WHERE born IS NOT NULL ORDER BY born')->fetchAssoc('born=name');
Assert::same([
'2002-02-02 00:00:00' => 'David Grudl',
'2002-02-20 00:00:00' => 'Jakub Vrana',
'2002-02-02 00:00:00.000000' => 'David Grudl',
'2002-02-20 00:00:00.000000' => 'Jakub Vrana',
], $pairs);
});
8 changes: 4 additions & 4 deletions tests/Database/ResultSet.fetchPairs().phpt
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ test('', function () use ($connection) {
$pairs = $connection->query('UPDATE author SET born = ? WHERE id = 12', new DateTime('2002-02-02'));
$pairs = $connection->query('SELECT * FROM author WHERE born IS NOT NULL ORDER BY born')->fetchPairs('born', 'name');
Assert::same([
'2002-02-02 00:00:00' => 'David Grudl',
'2002-02-20 00:00:00' => 'Jakub Vrana',
'2002-02-02 00:00:00.000000' => 'David Grudl',
'2002-02-20 00:00:00.000000' => 'Jakub Vrana',
], $pairs);
});

Expand Down Expand Up @@ -125,8 +125,8 @@ $pairs = $connection->query('UPDATE author SET born = ? WHERE id = 11', new Date
$pairs = $connection->query('UPDATE author SET born = ? WHERE id = 12', new DateTime('2002-02-02'));
$pairs = $connection->query('SELECT * FROM author WHERE born IS NOT NULL ORDER BY born')->fetchPairs('born', 'name');
Assert::same([
'2002-02-02 00:00:00' => 'David Grudl',
'2002-02-20 00:00:00' => 'Jakub Vrana',
'2002-02-02 00:00:00.000000' => 'David Grudl',
'2002-02-20 00:00:00.000000' => 'Jakub Vrana',
], $pairs);


Expand Down
2 changes: 1 addition & 1 deletion tests/Database/ResultSet.normalizeRow.mysql.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

declare(strict_types=1);

use Nette\Utils\DateTime;
use Nette\Database\DateTime;
use Tester\Assert;

require __DIR__ . '/connect.inc.php'; // create $connection
Expand Down
2 changes: 1 addition & 1 deletion tests/Database/ResultSet.normalizeRow.postgre.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

declare(strict_types=1);

use Nette\Utils\DateTime;
use Nette\Database\DateTime;
use Tester\Assert;

require __DIR__ . '/connect.inc.php'; // create $connection
Expand Down
2 changes: 1 addition & 1 deletion tests/Database/ResultSet.normalizeRow.sqlite.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

declare(strict_types=1);

use Nette\Utils\DateTime;
use Nette\Database\DateTime;
use Tester\Assert;

require __DIR__ . '/connect.inc.php'; // create $connection
Expand Down
6 changes: 3 additions & 3 deletions tests/Database/ResultSet.normalizeRow.sqlsrv.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

declare(strict_types=1);

use Nette\Utils\DateTime;
use Nette\Database\DateTime;
use Tester\Assert;

require __DIR__ . '/connect.inc.php'; // create $connection
Expand Down Expand Up @@ -131,13 +131,13 @@ function isTimestamp($str)


$row = (array) $connection->query('SELECT [datetimeoffset], CAST([sql_variant] AS int) AS [sql_variant], [timestamp] FROM types2 WHERE id = 1')->fetch();
Assert::type('DateTime', $row['datetimeoffset']);
Assert::type(DateTime::class, $row['datetimeoffset']);
Assert::same($row['datetimeoffset']->format('Y-m-d H:i:s P'), '2012-10-13 10:10:10 +02:00');
Assert::same($row['sql_variant'], 123456);
Assert::true(isTimestamp($row['timestamp']));

$row = (array) $connection->query('SELECT [datetimeoffset], CAST([sql_variant] AS varchar) AS [sql_variant], [timestamp] FROM types2 WHERE id = 2')->fetch();
Assert::type('DateTime', $row['datetimeoffset']);
Assert::type(DateTime::class, $row['datetimeoffset']);
Assert::same($row['datetimeoffset']->format('Y-m-d H:i:s P'), '0001-01-01 00:00:00 +00:00');
Assert::same($row['sql_variant'], 'abcd');
Assert::true(isTimestamp($row['timestamp']));
Expand Down

0 comments on commit b67e76d

Please sign in to comment.