Skip to content

Commit

Permalink
Merge pull request #395 from cakephp/3.x-readd-setisodate
Browse files Browse the repository at this point in the history
3.x: re-add setISODate
  • Loading branch information
othercorey authored Apr 26, 2023
2 parents 7dadeeb + e562bb6 commit d26e6bb
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/Chronos.php
Original file line number Diff line number Diff line change
Expand Up @@ -879,6 +879,22 @@ public function setDate(int $year, int $month, int $day): static
return $new;
}

/**
* Sets the date according to the ISO 8601 standard
*
* @param int $year Year of the date.
* @param int $week Week of the date.
* @param int $dayOfWeek Offset from the first day of the week.
* @return static
*/
public function setISODate(int $year, int $week, int $dayOfWeek = 1): static
{
$new = clone $this;
$new->native = $new->native->setISODate($year, $week, $dayOfWeek);

return $new;
}

/**
* Sets the time.
*
Expand Down
16 changes: 16 additions & 0 deletions src/ChronosDate.php
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,22 @@ public function setDate(int $year, int $month, int $day): static
return $new;
}

/**
* Sets the date according to the ISO 8601 standard
*
* @param int $year Year of the date.
* @param int $week Week of the date.
* @param int $dayOfWeek Offset from the first day of the week.
* @return static
*/
public function setISODate(int $year, int $week, int $dayOfWeek = 1): static
{
$new = clone $this;
$new->native = $new->native->setISODate($year, $week, $dayOfWeek);

return $new;
}

/**
* Returns the difference between this instance and target.
*
Expand Down
17 changes: 17 additions & 0 deletions tests/TestCase/DateTime/FluidSettersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
namespace Cake\Chronos\Test\TestCase\DateTime;

use Cake\Chronos\Chronos;
use Cake\Chronos\ChronosDate;
use Cake\Chronos\Test\TestCase\TestCase;

class FluidSettersTest extends TestCase
Expand Down Expand Up @@ -68,6 +69,22 @@ public function testFluidSetDate()
$this->assertDateTime($d, 1996, 2, 1);
}

public function testFluidChronosSetISODate()
{
$d = Chronos::createFromDate(2000, 1, 1);
$d = $d->setISODate(2023, 17, 3);
$this->assertTrue($d instanceof Chronos);
$this->assertDateTime($d, 2023, 04, 26);
}

public function testFluidChronosDateSetISODate()
{
$d = ChronosDate::create(2000, 1, 1);
$d = $d->setISODate(2023, 17, 3);
$this->assertTrue($d instanceof ChronosDate);
$this->assertDateTime($d, 2023, 04, 26);
}

public function testFluidHourSetter()
{
$d = Chronos::now();
Expand Down

0 comments on commit d26e6bb

Please sign in to comment.