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

[5.5] New Carbon helper #21660

Closed
wants to merge 1 commit 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
26 changes: 26 additions & 0 deletions src/Illuminate/Foundation/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,32 @@ function cache()
}
}

if (! function_exists('carbon')) {
/**
* Create a new Carbon instance for the given datetime or return a new instance.
*
* @param \DateTime|string|null $datetime
* @param \DateTimeZone|string|null $tz
* @return \Illuminate\Support\Carbon
*/
function carbon($datetime = null, $tz = null)
{
if (is_null($datetime)) {
return new Carbon(null, $tz);
}

if ($datetime instanceof \DateTime) {
if (is_null($tz)) {
return Carbon::instance($datetime);
}

return Carbon::instance($datetime)->setTimezone($tz);
}

return new Carbon($datetime, $tz);
}
}

if (! function_exists('config')) {
/**
* Get / set the specified configuration value.
Expand Down
40 changes: 40 additions & 0 deletions tests/Foundation/FoundationHelpersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Illuminate\Tests\Foundation;

use Mockery as m;
use Illuminate\Support\Carbon;
use PHPUnit\Framework\TestCase;
use Illuminate\Foundation\Application;

Expand Down Expand Up @@ -43,6 +44,45 @@ public function testCacheThrowsAnExceptionIfAnExpirationIsNotProvided()
cache(['foo' => 'bar']);
}

public function testCarbon()
{
// carbon()
$this->assertInstanceOf('Illuminate\Support\Carbon', carbon());

// carbon('string', 'timezone')
$this->assertEquals(13, carbon('2017-10-13 22:00:00')->day);
$this->assertEquals('2017', carbon('first day of october 2017')->year);
$this->assertFalse(carbon('today', 'America/Sao_Paulo')->utc);
$this->assertFalse(carbon(null, 'America/Sao_Paulo')->utc);

// carbon(\DateTime)
$this->assertEquals(
'22',
carbon(\DateTime::createFromFormat('Y-m-d H', '2017-10-13 22'))->hour
);
$this->assertFalse(
carbon(new \DateTime('now', new \DateTimeZone('America/Sao_Paulo')))->utc
);
$this->assertFalse(
carbon(new \DateTime('now'), new \DateTimeZone('America/Sao_Paulo'))->utc
);

// Carbon helpers
$this->assertEquals(Carbon::yesterday(), carbon()->yesterday());
$this->assertEquals(Carbon::tomorrow(), carbon()->tomorrow());

// Relative phrases
$this->assertEquals(
Carbon::now()->addMonth()->month, carbon('next month')->month
);

// Addition and Subtraction
$this->assertEquals(
Carbon::tomorrow()->addWeek(),
carbon()->tomorrow()->addWeek()
);
}

public function testUnversionedElixir()
{
$file = 'unversioned.css';
Expand Down