Skip to content

Commit

Permalink
refactor: convert dates to timestamps
Browse files Browse the repository at this point in the history
  • Loading branch information
simPod committed Jan 29, 2025
1 parent 9a90f8e commit f1fab3f
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 7 deletions.
3 changes: 2 additions & 1 deletion src/Param/ParamValueConverterRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -267,14 +267,15 @@ private static function decimalConverter(): Closure
private static function dateConverter(): Closure
{
return static fn (DateTimeInterface|string|int|float $value) => $value instanceof DateTimeInterface
// We cannot convert to timestamp yet https://github.com/ClickHouse/ClickHouse/issues/75217
? $value->format('Y-m-d')
: $value;
}

private static function dateTimeConverter(): Closure
{
return static fn (DateTimeInterface|string|int|float $value) => $value instanceof DateTimeInterface
? $value->format('Y-m-d H:i:s')
? $value->getTimestamp()
: $value;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Sql/ValueFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public function format(mixed $value, string|null $paramName = null, string|null
$value = $value->setTimezone($this->dateTimeZone);
}

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

if ($value instanceof Expression) {
Expand Down
5 changes: 2 additions & 3 deletions tests/Client/Http/RequestFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,9 @@ public function testParamParsed(): void
);

$now = new DateTimeImmutable();

Check failure on line 81 in tests/Client/Http/RequestFactoryTest.php

View workflow job for this annotation

GitHub Actions / Coding Standards (8.2)

Equals sign not aligned correctly; expected 1 space but found 5 spaces
$nowDate = $now->format('Y-m-d');

$request = $requestFactory->prepareSqlRequest(
'SELECT {p1:String}, {p_2:Date}',
'SELECT {p1:String}, {p_2:DateTime}',
new RequestSettings(
[],
[],
Expand All @@ -104,7 +103,7 @@ public function testParamParsed(): void
'Content-Disposition: form-data; name="param_p_2"',
'Content-Length: 10',
'',
$nowDate,
$now->getTimestamp(),
],
),
$body,
Expand Down
4 changes: 2 additions & 2 deletions tests/Sql/ValueFormatterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,9 @@ public static function providerFormat(): iterable
'SELECT * FROM table WHERE (a,b) IN (:tuples)',
];

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

Expand Down

0 comments on commit f1fab3f

Please sign in to comment.