Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add LocalDateRange::toDuration, YearMonthRange::toDuration methods #48

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/LocalDateRange.php
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,16 @@ public function count() : int
return $this->end->toEpochDay() - $this->start->toEpochDay() + 1;
}

/**
* Returns Duration of this range
*
* @return Duration
*/
public function toDuration(): Duration
{
return Duration::ofDays($this->count());
Copy link
Member

@BenMorel BenMorel Jun 5, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this be Duration::ofDays($this->count() - 1)?

As it stands, toPeriod() returns a 0-day Period if the start date is equal to the end date (this is in line with its Java counterpart).

So wouldn't it be weird if toDuration() returned a 1-day Duration for, say, 2022-06-05/2022-06-05?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This object is iterable and countable: the iterator returns all the LocalDate objects contained
in the range, while count() returns the total number of dates contained in the range.

The end date, inclusive.

I think duration of one day range is One day, not zero)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As it stands, toPeriod() returns a 0-day Period if the start date is equal to the end date (this is in line with its Java counterpart).

Quite weird. Is LocalDateRange in java inclusive too?

Copy link
Member

@BenMorel BenMorel Jun 6, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it makes sense in terms of Period: there is 1 day between 2022-06-06 an 2022-06-07.

That is: LocalDate::parse('2022-06-06')->plusPeriod(Period::ofDays(1)) is 2022-06-07, so this approach is consistent.

In terms of Duration, maybe the approach is different. But this can be confusing.

}

/**
* Serializes as a string using {@see LocalDateRange::__toString()}.
*/
Expand Down
10 changes: 10 additions & 0 deletions src/YearMonthRange.php
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,16 @@ public function toLocalDateRange(): LocalDateRange
);
}

/**
* Returns Duration of this range
*
* @return Duration
*/
public function toDuration(): Duration
{
return $this->toLocalDateRange()->toDuration();
}

/**
* Serializes as a string using {@see YearMonthRange::__toString()}.
*/
Expand Down
11 changes: 11 additions & 0 deletions tests/LocalDateRangeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Brick\DateTime\Tests;

use Brick\DateTime\DateTimeException;
use Brick\DateTime\Duration;
use Brick\DateTime\LocalDate;
use Brick\DateTime\LocalDateRange;
use Brick\DateTime\Parser\DateTimeParseException;
Expand Down Expand Up @@ -172,6 +173,16 @@ public function testCount(string $range, int $count): void
$this->assertCount($count, LocalDateRange::parse($range));
}

/**
* @dataProvider providerCount
*/
public function testToDuration(string $rangeString, int $expectedDaysCount): void
{
$expectedSeconds = $expectedDaysCount * 60 * 60 * 24;
$rangeObject = LocalDateRange::parse($rangeString);
$this->assertDurationIs($expectedSeconds, 0, $rangeObject->toDuration());
}

public function providerCount() : array
{
return [
Expand Down
23 changes: 23 additions & 0 deletions tests/YearMonthRangeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,29 @@ public function providerToLocalDateRange(): array
];
}

/**
* @dataProvider providerToDuration
*/
public function testToDuration(string $range, int $expectedDaysCount): void
{
$actualDuration = YearMonthRange::parse($range)->toDuration();
$expectedSeconds = $expectedDaysCount * 60 * 60 * 24;
$this->assertDurationIs($expectedSeconds, 0, $actualDuration);
}

public function providerToDuration(): array
{
return [
['1900-01/1900-12', 365],
['1900-05/1901-04', 365],
['1904-01/1904-12', 366],
['1900-02/1900-02', 28],
['2000-01/2000-02', 31 + 29],
['2001-01/2001-02', 31 + 28],
['1901-01/3000-12', 401767],
];
}

public function testJsonSerialize(): void
{
$this->assertSame(json_encode('2008-12/2011-01'), json_encode(YearMonthRange::of(
Expand Down