Skip to content

Commit f1fab3f

Browse files
committed
refactor: convert dates to timestamps
1 parent 9a90f8e commit f1fab3f

File tree

4 files changed

+7
-7
lines changed

4 files changed

+7
-7
lines changed

src/Param/ParamValueConverterRegistry.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,14 +267,15 @@ private static function decimalConverter(): Closure
267267
private static function dateConverter(): Closure
268268
{
269269
return static fn (DateTimeInterface|string|int|float $value) => $value instanceof DateTimeInterface
270+
// We cannot convert to timestamp yet https://github.com/ClickHouse/ClickHouse/issues/75217
270271
? $value->format('Y-m-d')
271272
: $value;
272273
}
273274

274275
private static function dateTimeConverter(): Closure
275276
{
276277
return static fn (DateTimeInterface|string|int|float $value) => $value instanceof DateTimeInterface
277-
? $value->format('Y-m-d H:i:s')
278+
? $value->getTimestamp()
278279
: $value;
279280
}
280281

src/Sql/ValueFormatter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public function format(mixed $value, string|null $paramName = null, string|null
7171
$value = $value->setTimezone($this->dateTimeZone);
7272
}
7373

74-
return "'" . $value->format('Y-m-d H:i:s') . "'";
74+
return (string) $value->getTimestamp();
7575
}
7676

7777
if ($value instanceof Expression) {

tests/Client/Http/RequestFactoryTest.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,9 @@ public function testParamParsed(): void
7979
);
8080

8181
$now = new DateTimeImmutable();
82-
$nowDate = $now->format('Y-m-d');
8382

8483
$request = $requestFactory->prepareSqlRequest(
85-
'SELECT {p1:String}, {p_2:Date}',
84+
'SELECT {p1:String}, {p_2:DateTime}',
8685
new RequestSettings(
8786
[],
8887
[],
@@ -104,7 +103,7 @@ public function testParamParsed(): void
104103
'Content-Disposition: form-data; name="param_p_2"',
105104
'Content-Length: 10',
106105
'',
107-
$nowDate,
106+
$now->getTimestamp(),
108107
],
109108
),
110109
$body,

tests/Sql/ValueFormatterTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,9 @@ public static function providerFormat(): iterable
6868
'SELECT * FROM table WHERE (a,b) IN (:tuples)',
6969
];
7070

71-
yield 'DateTimeImmutable' => ["'2020-01-31 01:23:45'", new DateTimeImmutable('2020-01-31 01:23:45')];
71+
yield 'DateTimeImmutable' => ['1580433825', new DateTimeImmutable('2020-01-31 01:23:45')];
7272
yield 'DateTimeImmutable different PHP and ClickHouse timezones' => [
73-
"'2020-01-31 01:23:45'",
73+
'1580433825',
7474
new DateTimeImmutable('2020-01-31 02:23:45', new DateTimeZone('Europe/Prague')),
7575
];
7676

0 commit comments

Comments
 (0)