Skip to content

Commit 8ca4389

Browse files
committed
returns date-time as immutable object Nette\Database\DateTime (BC break) [Closes #270]
1 parent 57f0244 commit 8ca4389

10 files changed

Lines changed: 84 additions & 11 deletions

src/Database/DateTime.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
/**
4+
* This file is part of the Nette Framework (https://nette.org)
5+
* Copyright (c) 2004 David Grudl (https://davidgrudl.com)
6+
*/
7+
8+
declare(strict_types=1);
9+
10+
namespace Nette\Database;
11+
12+
use Nette;
13+
14+
15+
/**
16+
* Date Time.
17+
*/
18+
final class DateTime extends \DateTimeImmutable implements \JsonSerializable
19+
{
20+
use Nette\SmartObject;
21+
22+
public function __construct(string|int $time = 'now')
23+
{
24+
if (is_numeric($time)) {
25+
$time = (new self('@' . $time))
26+
->setTimezone(new \DateTimeZone(date_default_timezone_get()))
27+
->format('Y-m-d H:i:s.u');
28+
}
29+
parent::__construct($time);
30+
}
31+
32+
33+
/**
34+
* Returns JSON representation in ISO 8601 (used by JavaScript).
35+
*/
36+
public function jsonSerialize(): string
37+
{
38+
return $this->format('c');
39+
}
40+
41+
42+
/**
43+
* Returns the date and time in the format 'Y-m-d H:i:s'.
44+
*/
45+
public function __toString(): string
46+
{
47+
return $this->format('Y-m-d H:i:s');
48+
}
49+
}

src/Database/ResultSet.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ public function normalizeRow(array $row): array
147147
|| $type === IStructure::FIELD_DATE
148148
|| $type === IStructure::FIELD_TIME
149149
) {
150-
$row[$key] = new Nette\Utils\DateTime($value);
150+
$row[$key] = new DateTime($value);
151151

152152
} elseif ($type === IStructure::FIELD_TIME_INTERVAL) {
153153
preg_match('#^(-?)(\d+)\D(\d+)\D(\d+)(\.\d+)?$#D', $value, $m);
@@ -156,7 +156,7 @@ public function normalizeRow(array $row): array
156156
$row[$key]->invert = (int) (bool) $m[1];
157157

158158
} elseif ($type === IStructure::FIELD_UNIX_TIMESTAMP) {
159-
$row[$key] = Nette\Utils\DateTime::from($value);
159+
$row[$key] = new DateTime($value);
160160
}
161161
}
162162

src/Database/Table/ActiveRow.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public function toArray(): array
6565

6666
/**
6767
* Returns primary key value.
68-
* @return mixed possible int, string, array, object (Nette\Utils\DateTime)
68+
* @return mixed possible int, string, array, object (Nette\Database\DateTime)
6969
*/
7070
public function getPrimary(bool $throw = true): mixed
7171
{

tests/Database/DateTime.phpt

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Nette\Database\DateTime;
6+
use Tester\Assert;
7+
8+
require __DIR__ . '/../bootstrap.php';
9+
10+
11+
date_default_timezone_set('Europe/Prague');
12+
13+
// timestamp
14+
Assert::same('1978-01-23 11:40:00', (string) new DateTime(254_400_000));
15+
Assert::same(254_400_000, (new DateTime(254_400_000))->getTimestamp());
16+
17+
Assert::same('2050-08-13 11:40:00', (string) new DateTime(254_400_0000));
18+
Assert::same(is_int(2_544_000_000) ? 2_544_000_000 : '2544000000', (new DateTime(2_544_000_000))->getTimestamp()); // 64 bit
19+
20+
// to string
21+
Assert::same('1978-01-23 11:40:00', (string) new DateTime('1978-01-23 11:40'));
22+
23+
// JSON
24+
Assert::same('"1978-01-23T11:40:00+01:00"', json_encode(new DateTime('1978-01-23 11:40')));

tests/Database/ResultSet.normalizeRow.mysql.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
declare(strict_types=1);
99

10-
use Nette\Utils\DateTime;
10+
use Nette\Database\DateTime;
1111
use Tester\Assert;
1212

1313
require __DIR__ . '/connect.inc.php'; // create $connection

tests/Database/ResultSet.normalizeRow.postgre.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
declare(strict_types=1);
99

10-
use Nette\Utils\DateTime;
10+
use Nette\Database\DateTime;
1111
use Tester\Assert;
1212

1313
require __DIR__ . '/connect.inc.php'; // create $connection

tests/Database/ResultSet.normalizeRow.sqlite.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
declare(strict_types=1);
99

10-
use Nette\Utils\DateTime;
10+
use Nette\Database\DateTime;
1111
use Tester\Assert;
1212

1313
require __DIR__ . '/connect.inc.php'; // create $connection

tests/Database/ResultSet.normalizeRow.sqlsrv.phpt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
declare(strict_types=1);
99

10-
use Nette\Utils\DateTime;
10+
use Nette\Database\DateTime;
1111
use Tester\Assert;
1212

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

132132

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

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

tests/Database/Table/Selection.insert().phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ $book = $explorer->table('author')->insert([
2222
// id = 14
2323

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

2727

2828
$books = $explorer->table('book');

tests/Database/Table/bugs/bug216.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,4 @@ $book = $explorer->table('author')->insert([
4343

4444
Assert::type(Nette\Database\Table\ActiveRow::class, $book);
4545
Assert::equal('eddard stark', $book->name);
46-
Assert::equal(new Nette\Utils\DateTime('2011-11-11'), $book->born);
46+
Assert::equal(new Nette\Database\DateTime('2011-11-11'), $book->born);

0 commit comments

Comments
 (0)