Skip to content

Commit

Permalink
Implement DateRangeInterface:getTimestampInterval() method (#18)
Browse files Browse the repository at this point in the history
* Implement DateRangeInterface:getTimestampInterval() method

* Update CHANGELOG

* Update README
  • Loading branch information
slavcodev authored Feb 16, 2018
1 parent 1170065 commit 4bfd8bc
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 3 deletions.
11 changes: 8 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions src/DateRange.php
Original file line number Diff line number Diff line change
Expand Up @@ -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}
*/
Expand Down
7 changes: 7 additions & 0 deletions src/DateRangeInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
13 changes: 13 additions & 0 deletions tests/DateRangeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down

0 comments on commit 4bfd8bc

Please sign in to comment.