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.8] Allow Carbon 2 and custom date handling #25320

Merged
merged 7 commits into from Oct 3, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion composer.json
Expand Up @@ -23,7 +23,7 @@
"erusev/parsedown": "^1.7",
"league/flysystem": "^1.0.8",
"monolog/monolog": "^1.12",
"nesbot/carbon": "^1.26.3",
"nesbot/carbon": "^1.26.3 || ^2.0",
"psr/container": "^1.0",
"psr/simple-cache": "^1.0",
"ramsey/uuid": "^3.7",
Expand Down
3 changes: 2 additions & 1 deletion src/Illuminate/Console/Scheduling/Event.php
Expand Up @@ -7,6 +7,7 @@
use Illuminate\Support\Arr;
use Illuminate\Support\Carbon;
use GuzzleHttp\Client as HttpClient;
use Illuminate\Support\Facades\Date;
use Illuminate\Contracts\Mail\Mailer;
use Symfony\Component\Process\Process;
use Illuminate\Support\Traits\Macroable;
Expand Down Expand Up @@ -691,7 +692,7 @@ public function getSummaryForDisplay()
*/
public function nextRunDate($currentTime = 'now', $nth = 0, $allowCurrentDate = false)
{
return Carbon::instance(CronExpression::factory(
return Date::instance(CronExpression::factory(
$this->getExpression()
)->getNextRunDate($currentTime, $nth, $allowCurrentDate, $this->timezone));
}
Expand Down
4 changes: 2 additions & 2 deletions src/Illuminate/Console/Scheduling/ScheduleRunCommand.php
Expand Up @@ -2,8 +2,8 @@

namespace Illuminate\Console\Scheduling;

use Illuminate\Support\Carbon;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Date;

class ScheduleRunCommand extends Command
{
Expand Down Expand Up @@ -52,7 +52,7 @@ public function __construct(Schedule $schedule)
{
$this->schedule = $schedule;

$this->startedAt = Carbon::now();
$this->startedAt = Date::now();

parent::__construct();
}
Expand Down
14 changes: 8 additions & 6 deletions src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php
Expand Up @@ -4,9 +4,11 @@

use LogicException;
use DateTimeInterface;
use Carbon\CarbonInterface;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Date;
kylekatarnls marked this conversation as resolved.
Show resolved Hide resolved
use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Database\Eloquent\Relations\Relation;
use Illuminate\Support\Collection as BaseCollection;
Expand Down Expand Up @@ -734,15 +736,15 @@ protected function asDateTime($value)
// If this value is already a Carbon instance, we shall just return it as is.
// This prevents us having to re-instantiate a Carbon instance when we know
// it already is one, which wouldn't be fulfilled by the DateTime check.
if ($value instanceof Carbon) {
return $value;
if ($value instanceof Carbon || $value instanceof CarbonInterface) {
return Date::instance($value);
}

// If the value is already a DateTime instance, we will just skip the rest of
// these checks since they will be a waste of time, and hinder performance
// when checking the field. We will just return the DateTime right away.
if ($value instanceof DateTimeInterface) {
return new Carbon(
return Date::parse(
$value->format('Y-m-d H:i:s.u'), $value->getTimezone()
);
}
Expand All @@ -751,20 +753,20 @@ protected function asDateTime($value)
// and format a Carbon object from this timestamp. This allows flexibility
// when defining your date fields as they might be UNIX timestamps here.
if (is_numeric($value)) {
return Carbon::createFromTimestamp($value);
return Date::createFromTimestamp($value);
}

// If the value is in simply year, month, day format, we will instantiate the
// Carbon instances from that format. Again, this provides for simple date
// fields on the database, while still supporting Carbonized conversion.
if ($this->isStandardDateFormat($value)) {
return Carbon::createFromFormat('Y-m-d', $value)->startOfDay();
return Date::instance(Carbon::createFromFormat('Y-m-d', $value)->startOfDay());
}

// Finally, we will just assume this date is in the format used by default on
// the database connection and use that format to create the Carbon object
// that is returned back out to the developers after we convert it here.
return Carbon::createFromFormat(
return Date::createFromFormat(
str_replace('.v', '.u', $this->getDateFormat()), $value
);
}
Expand Down
4 changes: 2 additions & 2 deletions src/Illuminate/Database/Eloquent/Concerns/HasTimestamps.php
Expand Up @@ -2,7 +2,7 @@

namespace Illuminate\Database\Eloquent\Concerns;

use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Date;

trait HasTimestamps
{
Expand Down Expand Up @@ -81,7 +81,7 @@ public function setUpdatedAt($value)
*/
public function freshTimestamp()
{
return new Carbon;
return Date::now();
}

/**
Expand Down
Expand Up @@ -4,6 +4,7 @@

use Exception;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Date;
use Symfony\Component\HttpKernel\Exception\ServiceUnavailableHttpException;

class MaintenanceModeException extends ServiceUnavailableHttpException
Expand Down Expand Up @@ -43,12 +44,12 @@ public function __construct($time, $retryAfter = null, $message = null, Exceptio
{
parent::__construct($retryAfter, $message, $previous, $code);

$this->wentDownAt = Carbon::createFromTimestamp($time);
$this->wentDownAt = Date::createFromTimestamp($time);

if ($retryAfter) {
$this->retryAfter = $retryAfter;

$this->willBeAvailableAt = Carbon::createFromTimestamp($time)->addSeconds($this->retryAfter);
$this->willBeAvailableAt = Date::instance(Carbon::createFromTimestamp($time)->addRealSeconds($this->retryAfter));
}
}
}
6 changes: 5 additions & 1 deletion src/Illuminate/Foundation/Testing/TestCase.php
Expand Up @@ -3,7 +3,8 @@
namespace Illuminate\Foundation\Testing;

use Mockery;
use Illuminate\Support\Carbon;
use Carbon\Carbon;
use Carbon\CarbonImmutable;
use Illuminate\Support\Facades\Facade;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Console\Application as Artisan;
Expand Down Expand Up @@ -165,6 +166,9 @@ protected function tearDown()
if (class_exists(Carbon::class)) {
Carbon::setTestNow();
}
if (class_exists(CarbonImmutable::class)) {
CarbonImmutable::setTestNow();
}

$this->afterApplicationCreatedCallbacks = [];
$this->beforeApplicationDestroyedCallbacks = [];
Expand Down
6 changes: 3 additions & 3 deletions src/Illuminate/Foundation/helpers.php
@@ -1,9 +1,9 @@
<?php

use Illuminate\Support\Str;
use Illuminate\Support\Carbon;
use Illuminate\Support\HtmlString;
use Illuminate\Container\Container;
use Illuminate\Support\Facades\Date;
use Illuminate\Contracts\Bus\Dispatcher;
use Illuminate\Contracts\Auth\Access\Gate;
use Illuminate\Contracts\Support\Responsable;
Expand Down Expand Up @@ -623,7 +623,7 @@ function mix($path, $manifestDirectory = '')
*/
function now($tz = null)
{
return Carbon::now($tz);
return Date::now($tz);
}
}

Expand Down Expand Up @@ -886,7 +886,7 @@ function storage_path($path = '')
*/
function today($tz = null)
{
return Carbon::today($tz);
return Date::today($tz);
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/Illuminate/Queue/Failed/DatabaseFailedJobProvider.php
Expand Up @@ -2,7 +2,7 @@

namespace Illuminate\Queue\Failed;

use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Date;
use Illuminate\Database\ConnectionResolverInterface;

class DatabaseFailedJobProvider implements FailedJobProviderInterface
Expand Down Expand Up @@ -54,7 +54,7 @@ public function __construct(ConnectionResolverInterface $resolver, $database, $t
*/
public function log($connection, $queue, $payload, $exception)
{
$failed_at = Carbon::now();
$failed_at = Date::now();

$exception = (string) $exception;

Expand Down
5 changes: 3 additions & 2 deletions src/Illuminate/Session/Middleware/StartSession.php
Expand Up @@ -5,6 +5,7 @@
use Closure;
use Illuminate\Http\Request;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Date;
use Illuminate\Session\SessionManager;
use Illuminate\Contracts\Session\Session;
use Illuminate\Session\CookieSessionHandler;
Expand Down Expand Up @@ -194,13 +195,13 @@ protected function getSessionLifetimeInSeconds()
/**
* Get the cookie lifetime in seconds.
*
* @return \DateTimeInterface
* @return \DateTimeInterface|int
*/
protected function getCookieExpirationDate()
{
$config = $this->manager->getSessionConfig();

return $config['expire_on_close'] ? 0 : Carbon::now()->addMinutes($config['lifetime']);
return $config['expire_on_close'] ? 0 : Date::instance(Carbon::now()->addRealMinutes($config['lifetime']));
}

/**
Expand Down