Skip to content

Commit d4bff8d

Browse files
authored
fix date time rounding issue for range and multirange (palicao#4)
1 parent f2fec55 commit d4bff8d

File tree

2 files changed

+67
-4
lines changed

2 files changed

+67
-4
lines changed

src/TimeSeries.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -225,8 +225,8 @@ public function range(
225225
?AggregationRule $rule = null
226226
): array
227227
{
228-
$fromTs = $from ? (int)$from->format('Uu') / 1000 : '-';
229-
$toTs = $to ? (int)$to->format('Uu') / 1000 : '+';
228+
$fromTs = $from ? DateTimeUtils::timestampWithMsFromDateTime($from) : '-';
229+
$toTs = $to ? DateTimeUtils::timestampWithMsFromDateTime($to) : '+';
230230

231231
$params = ['TS.RANGE', $key, $fromTs, $toTs];
232232
if ($count !== null) {
@@ -292,8 +292,8 @@ public function multiRangeRaw(
292292
?AggregationRule $rule = null
293293
): array
294294
{
295-
$fromTs = $from ? (int)$from->format('Uu') / 1000 : '-';
296-
$toTs = $to ? (int)$to->format('Uu') / 1000 : '+';
295+
$fromTs = $from ? DateTimeUtils::timestampWithMsFromDateTime($from) : '-';
296+
$toTs = $to ? DateTimeUtils::timestampWithMsFromDateTime($to) : '+';
297297

298298
$params = ['TS.MRANGE', $fromTs, $toTs];
299299
if ($count !== null) {

tests/Integration/IntegrationTest.php

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,11 @@
55

66
namespace Palicao\PhpRedisTimeSeries\Tests\Integration;
77

8+
use DateInterval;
9+
use DateTime;
810
use DateTimeImmutable;
911
use Palicao\PhpRedisTimeSeries\AggregationRule;
12+
use Palicao\PhpRedisTimeSeries\DateTimeUtils;
1013
use Palicao\PhpRedisTimeSeries\Filter;
1114
use Palicao\PhpRedisTimeSeries\Label;
1215
use Palicao\PhpRedisTimeSeries\RedisClient;
@@ -150,4 +153,64 @@ public function testAddAndRetrieveKeysWithMultipleFilters(): void
150153
$this->assertEquals($expectedResult, $range);
151154
}
152155

156+
public function testAddAndRetrieveWithDateTimeObjectAsMultiRangeWithMultipleFilters(): void
157+
{
158+
$currentDate = new DateTime();
159+
$from = $currentDate->sub(new DateInterval('P1D'));
160+
$to = $currentDate;
161+
162+
$this->sut->create(
163+
'temperature:3:11',
164+
6000,
165+
[new Label('sensor_id', '2'), new Label('area_id', '32')]
166+
);
167+
$this->sut->add(new Sample('temperature:3:11', 30, $from));
168+
$this->sut->add(new Sample('temperature:3:11', 42, $to));
169+
170+
$filter = new Filter('sensor_id', '2');
171+
$filter->add('area_id', Filter::OP_EQUALS, '32');
172+
173+
$range = $this->sut->multiRange($filter);
174+
175+
$expectedRange = [
176+
Sample::createFromTimestamp('temperature:3:11', (float)42, DateTimeUtils::timestampWithMsFromDateTime(new DateTimeImmutable($to->format('Y-m-d H:i:s.u'))))
177+
];
178+
179+
$this->assertEquals($expectedRange, $range);
180+
}
181+
182+
183+
public function testAddAndRetrieveWithDateTimeObjectAsRange(): void
184+
{
185+
$from = new DateTimeImmutable('2019-11-06 20:34:17.103000');
186+
$to = new DateTimeImmutable('2019-11-06 20:34:17.107000');
187+
188+
$this->sut->create(
189+
'temperature:3:11',
190+
null,
191+
[new Label('sensor_id', '2'), new Label('area_id', '32')]
192+
);
193+
194+
$this->sut->add(new Sample('temperature:3:11', 30, $from));
195+
$this->sut->add(new Sample('temperature:3:11', 42, $to));
196+
197+
$range = $this->sut->range(
198+
'temperature:3:11'
199+
);
200+
201+
$expectedRange = [
202+
Sample::createFromTimestamp(
203+
'temperature:3:11',
204+
(float)30,
205+
DateTimeUtils::timestampWithMsFromDateTime(new DateTimeImmutable($from->format('Y-m-d H:i:s.u')))
206+
),
207+
Sample::createFromTimestamp(
208+
'temperature:3:11',
209+
(float)42,
210+
DateTimeUtils::timestampWithMsFromDateTime(new DateTimeImmutable($to->format('Y-m-d H:i:s.u')))
211+
),
212+
];
213+
214+
$this->assertEquals($expectedRange, $range);
215+
}
153216
}

0 commit comments

Comments
 (0)