From 4bfd8bcebd3e3ac897cda759a21482f6fd775d4e Mon Sep 17 00:00:00 2001 From: Veaceslav Medvedev Date: Fri, 16 Feb 2018 16:19:38 +0100 Subject: [PATCH] Implement DateRangeInterface:getTimestampInterval() method (#18) * Implement DateRangeInterface:getTimestampInterval() method * Update CHANGELOG * Update README --- CHANGELOG.md | 11 ++++++++--- README.md | 2 ++ src/DateRange.php | 8 ++++++++ src/DateRangeInterface.php | 7 +++++++ tests/DateRangeTest.php | 13 +++++++++++++ 5 files changed, 38 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 46b1273..d9270e1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,14 +18,19 @@ Security - in case of vulnerabilities. _TBD_ +## [0.4.0] 2018-02-16 + +### Added +- Added new method `getTimestampInterval` to returns duration in seconds ([#18](../../pull/18)). + ## [0.3.0] 2017-11-18 ### Added -- Added new methods `getInterval`, `getPeriod`, `split` -- Added `DateRangeProvider` interface and basic provider `FiniteDateRangeProvider` +- Added `DateRangeProvider` interface and basic provider `FiniteDateRangeProvider` ([#13](../../pull/13)). +- Added new method `getDateInterval`, `getDatePeriod`, `split` ([#9](../../pull/9)). ### Changed -- Rename methods from using `time` ot `date`. +- Rename methods from using `time` ot `date` ([#11](../../pull/11)). - Refactored internals. ## [0.1.1] 2017-11-17 diff --git a/README.md b/README.md index d4ac9b8..b2652fb 100644 --- a/README.md +++ b/README.md @@ -42,6 +42,8 @@ $range->isEnded(); $range->isStartedOn(new DateTime()); // Checking if range ended on specific date $range->isEndedOn(new DateTime()); +// Accessing range duration in seconds +$range->getTimestampInterval(); // Accessing range interval $range->getDateInterval()->format('%s'); // Printing diff --git a/src/DateRange.php b/src/DateRange.php index 2652587..d7250c0 100644 --- a/src/DateRange.php +++ b/src/DateRange.php @@ -190,6 +190,14 @@ public function isEnded(): bool return $this->hasEndDate() && $this->state->compareEndDate(new DateTimeImmutable()) < 0; } + /** + * {@inheritdoc} + */ + public function getTimestampInterval(): int + { + return $this->getEndDate()->getTimestamp() - $this->getStartDate()->getTimestamp(); + } + /** * {@inheritdoc} */ diff --git a/src/DateRangeInterface.php b/src/DateRangeInterface.php index fcc5ecf..61925c7 100644 --- a/src/DateRangeInterface.php +++ b/src/DateRangeInterface.php @@ -112,6 +112,13 @@ public function isStarted(): bool; */ public function isEnded(): bool; + /** + * Returns the range duration in seconds. + * + * @return int + */ + public function getTimestampInterval(): int; + /** * Returns the range interval. * diff --git a/tests/DateRangeTest.php b/tests/DateRangeTest.php index e009e5b..f07f834 100644 --- a/tests/DateRangeTest.php +++ b/tests/DateRangeTest.php @@ -111,6 +111,19 @@ public function dumpRange() self::assertNotContains('state', $dump); } + /** + * @test + */ + public function getTimestampInterval() + { + $now = new DateTimeImmutable(); + $oneHourRange = new DateRange($now, $now->add(new DateInterval('PT1H'))); + $oneDayRange = new DateRange($now, $now->add(new DateInterval('P1D'))); + + self::assertSame(3600, $oneHourRange->getTimestampInterval()); + self::assertSame(86400, $oneDayRange->getTimestampInterval()); + } + /** * @test */