Skip to content

Commit

Permalink
Bug fix Period::createFromDay #36
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesdb authored and nyamsprod committed Apr 11, 2016
1 parent 5728444 commit ca0e6ab
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 9 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Period

[![Author](http://img.shields.io/badge/author-@nyamsprod-blue.svg?style=flat-square)](https://twitter.com/nyamsprod)
[![Latest Version](https://img.shields.io/github/release/thephpleague/period.svg?style=flat-square)](https://github.com/thephpleague/period/releases)
[![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](LICENSE.md)
[![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](LICENSE)
[![Build Status](https://img.shields.io/travis/thephpleague/period/master.svg?style=flat-square)](https://travis-ci.org/thephpleague/period)
[![HHVM Status](https://img.shields.io/hhvm/league/period.svg?style=flat-square)](http://hhvm.h4cc.de/package/league/period)
[![Coverage Status](https://img.shields.io/scrutinizer/coverage/g/thephpleague/period.svg?style=flat-square)](https://scrutinizer-ci.com/g/thephpleague/period/code-structure)
Expand Down Expand Up @@ -71,4 +71,4 @@ Credits
License
-------

The MIT License (MIT). Please see [LICENSE](LICENSE) for more information.
The MIT License (MIT). Please see [LICENSE](LICENSE) for more information.
26 changes: 19 additions & 7 deletions src/Period.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,12 @@ public static function __set_state(array $period)
*/
public static function createFromDay($day)
{
$startDate = static::filterDatePoint($day)->setTime(0, 0, 0);
$startDate = static::filterDatePoint($day);
$startDate = $startDate->createFromFormat(
static::DATE_LOCALE,
$startDate->format('Y-m-d').' 00:00:00.000000',
$startDate->getTimeZone()
);

return new static($startDate, $startDate->add(new DateInterval('P1D')));
}
Expand Down Expand Up @@ -821,14 +826,21 @@ public function split($interval)
*/
public function intersect(Period $period)
{
if ($this->abuts($period)) {
throw new LogicException('Both object should not abuts');
if (! $this->overlaps($period)) {
throw new LogicException('Both Period objects must overlaps');
}

return new static(
($period->getStartDate() > $this->startDate) ? $period->getStartDate() : $this->startDate,
($period->getEndDate() < $this->endDate) ? $period->getEndDate() : $this->endDate
);
$startDate = $period->getStartDate();
if ($startDate < $this->startDate) {
$startDate = $this->startDate;
}

$endDate = $period->getEndDate();
if ($endDate > $this->endDate) {
$endDate = $this->endDate;
}

return new static($startDate, $endDate);
}

/**
Expand Down
29 changes: 29 additions & 0 deletions test/PeriodTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,20 @@
use League\Period\Period;
use PHPUnit_Framework_TestCase as TestCase;

class ExtendedDate extends DateTimeImmutable
{
public static function createFromFormat($format, $time, $timezone = null)
{
if (!is_object($timezone) || !$timezone instanceof DateTimeZone) {
$timezone = date_default_timezone_get();
}

$datetime = parent::createFromFormat($format, $time, $timezone);

return new self($datetime->format('Y-m-d H:i:s.u'), $timezone);
}
}

class PeriodTest extends TestCase
{
private $timezone;
Expand Down Expand Up @@ -394,6 +408,21 @@ public function testCreateFromDay()
$this->assertEquals($period->getEndDate(), new DateTimeImmutable('2015-01-04'));
}

public function testCreateFromDayPreserveTimezone()
{
$period = Period::createFromDay('2008-07-01T22:35:17+08:00');
$this->assertEquals('+08:00', $period->getStartDate()->format('P'));
$this->assertEquals('+08:00', $period->getEndDate()->format('P'));
}

public function testCreateFromDayPreserveInstance()
{
$today = new ExtendedDate('NOW');
$period = Period::createFromDay($today);
$this->assertInstanceof(ExtendedDate::class, $period->getStartDate());
$this->assertInstanceof(ExtendedDate::class, $period->getEndDate());
}

public function testIsBeforeDatetime()
{
$orig = Period::createFromDuration('2012-01-01', '1 MONTH');
Expand Down

0 comments on commit ca0e6ab

Please sign in to comment.