Skip to content

Commit 4bfd8bc

Browse files
authored
Implement DateRangeInterface:getTimestampInterval() method (#18)
* Implement DateRangeInterface:getTimestampInterval() method * Update CHANGELOG * Update README
1 parent 1170065 commit 4bfd8bc

File tree

5 files changed

+38
-3
lines changed

5 files changed

+38
-3
lines changed

CHANGELOG.md

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,19 @@ Security - in case of vulnerabilities.
1818

1919
_TBD_
2020

21+
## [0.4.0] 2018-02-16
22+
23+
### Added
24+
- Added new method `getTimestampInterval` to returns duration in seconds ([#18](../../pull/18)).
25+
2126
## [0.3.0] 2017-11-18
2227

2328
### Added
24-
- Added new methods `getInterval`, `getPeriod`, `split`
25-
- Added `DateRangeProvider` interface and basic provider `FiniteDateRangeProvider`
29+
- Added `DateRangeProvider` interface and basic provider `FiniteDateRangeProvider` ([#13](../../pull/13)).
30+
- Added new method `getDateInterval`, `getDatePeriod`, `split` ([#9](../../pull/9)).
2631

2732
### Changed
28-
- Rename methods from using `time` ot `date`.
33+
- Rename methods from using `time` ot `date` ([#11](../../pull/11)).
2934
- Refactored internals.
3035

3136
## [0.1.1] 2017-11-17

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ $range->isEnded();
4242
$range->isStartedOn(new DateTime());
4343
// Checking if range ended on specific date
4444
$range->isEndedOn(new DateTime());
45+
// Accessing range duration in seconds
46+
$range->getTimestampInterval();
4547
// Accessing range interval
4648
$range->getDateInterval()->format('%s');
4749
// Printing

src/DateRange.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,14 @@ public function isEnded(): bool
190190
return $this->hasEndDate() && $this->state->compareEndDate(new DateTimeImmutable()) < 0;
191191
}
192192

193+
/**
194+
* {@inheritdoc}
195+
*/
196+
public function getTimestampInterval(): int
197+
{
198+
return $this->getEndDate()->getTimestamp() - $this->getStartDate()->getTimestamp();
199+
}
200+
193201
/**
194202
* {@inheritdoc}
195203
*/

src/DateRangeInterface.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,13 @@ public function isStarted(): bool;
112112
*/
113113
public function isEnded(): bool;
114114

115+
/**
116+
* Returns the range duration in seconds.
117+
*
118+
* @return int
119+
*/
120+
public function getTimestampInterval(): int;
121+
115122
/**
116123
* Returns the range interval.
117124
*

tests/DateRangeTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,19 @@ public function dumpRange()
111111
self::assertNotContains('state', $dump);
112112
}
113113

114+
/**
115+
* @test
116+
*/
117+
public function getTimestampInterval()
118+
{
119+
$now = new DateTimeImmutable();
120+
$oneHourRange = new DateRange($now, $now->add(new DateInterval('PT1H')));
121+
$oneDayRange = new DateRange($now, $now->add(new DateInterval('P1D')));
122+
123+
self::assertSame(3600, $oneHourRange->getTimestampInterval());
124+
self::assertSame(86400, $oneDayRange->getTimestampInterval());
125+
}
126+
114127
/**
115128
* @test
116129
*/

0 commit comments

Comments
 (0)