Skip to content

Commit 32b5f78

Browse files
committed
Added method setIntervalForQueryingUsingExclusiveTimes and unit tests.
1 parent ff301c0 commit 32b5f78

File tree

2 files changed

+53
-2
lines changed

2 files changed

+53
-2
lines changed

src/DruidFamiliar/QueryParameters/SimpleGroupByQueryParameters.php

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use DruidFamiliar\Abstracts\AbstractTaskParameters;
66
use DruidFamiliar\Exception\MissingParametersException;
77
use DruidFamiliar\Interfaces\IDruidQueryParameters;
8+
use DruidFamiliar\DruidTime;
89
use DruidFamiliar\Interval;
910

1011
/**
@@ -227,11 +228,34 @@ public function setIntervals(Interval $intervals)
227228
}
228229

229230
/**
230-
* @param Interval $intervals
231+
* @param string|\DateTime|DruidTime $intervalStart
232+
* @param string|\DateTime|DruidTime $intervalEnd
231233
*/
232234
public function setIntervalByStartAndEnd($intervalStart, $intervalEnd)
233235
{
234-
$this->intervals = new Interval($intervalStart, $intervalEnd);
236+
$this->setIntervals(new Interval($intervalStart, $intervalEnd));
235237
}
236238

239+
/**
240+
* Adjusts the datetime to make the interval "exclusive" of the datetime.
241+
* e.g., given
242+
* startDateTime=2014-06-18T12:30:01Z and
243+
* endDateTime=2014-06-18T01:00:00Z
244+
* return and Interval containing
245+
* startDateTime=2014-06-18T12:30:00Z and
246+
* endDateTime=2014-06-18T01:00:01Z
247+
*
248+
* @param $startDateTime
249+
* @param $endDateTime
250+
* @return void
251+
*/
252+
public function setIntervalForQueryingUsingExclusiveTimes($startDateTime, $endDateTime)
253+
{
254+
$adjustedStartDateTime = new \DateTime($startDateTime);
255+
$adjustedStartDateTime->sub(new \DateInterval('PT1S'));
256+
$adjustedEndDateTime = new \DateTime($endDateTime);
257+
$adjustedEndDateTime->add(new \DateInterval('PT1S'));
258+
259+
$this->setIntervals(new Interval($adjustedStartDateTime, $adjustedEndDateTime));
260+
}
237261
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace DruidFamiliar\Test\QueryParameters;
4+
5+
use DruidFamiliar\Interval;
6+
use PHPUnit_Framework_TestCase;
7+
8+
class SimpleGroupByQueryParametersTest extends PHPUnit_Framework_TestCase
9+
{
10+
11+
public function testAdjustTimeIntervalForQueryingSuccessfullyBuildsExclusiveTimes()
12+
{
13+
$query = $this->getMockBuilder('DruidFamiliar\QueryParameters\SimpleGroupByQueryParameters')
14+
->setMethods(null)
15+
->getMock();
16+
17+
$origStartDateTime = '2014-06-18T12:30:01Z';
18+
$origEndDateTime = '2014-06-18T01:00:00Z';
19+
20+
$expectedResults = new Interval('2014-06-18T12:30:00Z', '2014-06-18T01:00:01Z');
21+
22+
$query->setIntervalForQueryingUsingExclusiveTimes($origStartDateTime, $origEndDateTime);
23+
$results = $query->getIntervals();
24+
$this->assertEquals($expectedResults, $results);
25+
}
26+
27+
}

0 commit comments

Comments
 (0)