Skip to content

Commit

Permalink
Merge pull request #1165 from kylekatarnls/now-with-tz-and-resolve
Browse files Browse the repository at this point in the history
Keep resolveCarbon protected
  • Loading branch information
kylekatarnls authored Mar 9, 2018
2 parents 980fd4c + d8bb5dd commit 3584051
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 25 deletions.
24 changes: 12 additions & 12 deletions src/Carbon/Carbon.php
Original file line number Diff line number Diff line change
Expand Up @@ -456,16 +456,6 @@ public static function now($tz = null)
return new static(null, $tz);
}

/**
* Returns a present instance in the same timezone.
*
* @return static
*/
public function nowWithSameTz()
{
return static::now($this->getTimezone());
}

/**
* Create a Carbon instance for today.
*
Expand Down Expand Up @@ -807,14 +797,24 @@ public function copy()
return clone $this;
}

/**
* Returns a present instance in the same timezone.
*
* @return static
*/
public function nowWithSameTz()
{
return static::now($this->getTimezone());
}

/**
* Return the Carbon instance passed through or a copy in the same timezone.
*
* @param \Carbon\Carbon|null $date
*
* @return \Carbon\Carbon
* @return static
*/
public function resolveCarbon(self $date = null)
protected function resolveCarbon(self $date = null)
{
return $date ?: $this->nowWithSameTz();
}
Expand Down
21 changes: 8 additions & 13 deletions tests/Carbon/NowDerivativesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,17 @@ public function testNowWithSameTimezone()
{
$dt = Carbon::now('Europe/London');
$dt2 = $dt->nowWithSameTz();

$this->assertSame($dt2->toDateTimeString(), $dt->toDateTimeString());
$this->assertSame($dt2->tzName, $dt->tzName);
}

public function testResolveCarbon()
{
$dt = Carbon::now();
$dt2 = Carbon::yesterday();
$this->assertInstanceOfCarbon($dt->resolveCarbon($dt2));
$this->assertSame($dt2, $dt->resolveCarbon($dt2));
}
Carbon::setTestNow(new Carbon('2017-07-29T07:57:27.123456Z'));
$dt = Carbon::createFromTime(13, 40, 00, 'Africa/Asmara');
$dt2 = $dt->nowWithSameTz();
Carbon::setTestNow();

public function testResolveCarbonNull()
{
$dt = Carbon::now();
$this->assertInstanceOfCarbon($dt->resolveCarbon(null));
$this->assertSame($dt->toDateTimeString(), $dt->resolveCarbon(null)->toDateTimeString());
$this->assertSame($dt->format('H:i'), '13:40');
$this->assertSame($dt2->format('H:i'), '10:57');
$this->assertSame($dt2->tzName, $dt->tzName);
}
}

0 comments on commit 3584051

Please sign in to comment.