Skip to content

Commit

Permalink
Keep asDateTime as is
Browse files Browse the repository at this point in the history
  • Loading branch information
kylekatarnls committed Aug 23, 2018
1 parent 08eea67 commit cfd35ff
Show file tree
Hide file tree
Showing 22 changed files with 62 additions and 24 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"erusev/parsedown": "^1.7",
"league/flysystem": "^1.0.8",
"monolog/monolog": "^1.12",
"nesbot/carbon": "^2.0.0-beta.5",
"nesbot/carbon": "^2.0",
"psr/container": "^1.0",
"psr/simple-cache": "^1.0",
"ramsey/uuid": "^3.7",
Expand Down
2 changes: 1 addition & 1 deletion src/Illuminate/Cache/Repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

namespace Illuminate\Cache;

use Carbon\Factory;
use Closure;
use ArrayAccess;
use Carbon\Factory;
use DateTimeInterface;
use BadMethodCallException;
use Illuminate\Cache\Events\CacheHit;
Expand Down
4 changes: 2 additions & 2 deletions src/Illuminate/Console/Scheduling/Event.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

namespace Illuminate\Console\Scheduling;

use Closure;
use Carbon\Carbon;
use Carbon\CarbonImmutable;
use Carbon\Factory;
use Closure;
use Cron\CronExpression;
use Carbon\CarbonImmutable;
use Illuminate\Support\Arr;
use GuzzleHttp\Client as HttpClient;
use Illuminate\Contracts\Mail\Mailer;
Expand Down
40 changes: 39 additions & 1 deletion src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Carbon\Factory;
use LogicException;
use DateTimeInterface;
use Carbon\CarbonInterface;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
use Illuminate\Contracts\Support\Arrayable;
Expand Down Expand Up @@ -731,7 +732,44 @@ protected function asDate($value)
*/
protected function asDateTime($value)
{
return app(Factory::class)->make($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 CarbonInterface) {
return $value;
}

$dateFactory = app(Factory::class);

// 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 $dateFactory->parse(
$value->format('Y-m-d H:i:s.u'), $value->getTimezone()
);
}

// If this value is an integer, we will assume it is a UNIX timestamp's 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 $dateFactory->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 $dateFactory->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 $dateFactory->createFromFormat(
str_replace('.v', '.u', $this->getDateFormat()), $value
);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

namespace Illuminate\Foundation\Http\Exceptions;

use Exception;
use Carbon\Carbon;
use Carbon\Factory;
use Exception;
use Symfony\Component\HttpKernel\Exception\ServiceUnavailableHttpException;

class MaintenanceModeException extends ServiceUnavailableHttpException
Expand Down
2 changes: 1 addition & 1 deletion src/Illuminate/Foundation/Testing/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

namespace Illuminate\Foundation\Testing;

use Mockery;
use Carbon\Carbon;
use Carbon\CarbonImmutable;
use Mockery;
use Illuminate\Support\Facades\Facade;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Console\Application as Artisan;
Expand Down
4 changes: 2 additions & 2 deletions src/Illuminate/Foundation/Testing/TestResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

namespace Illuminate\Foundation\Testing;

use Closure;
use Carbon\Carbon;
use Carbon\CarbonImmutable;
use Carbon\Factory;
use Closure;
use Carbon\CarbonImmutable;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
use Illuminate\Contracts\View\View;
Expand Down
2 changes: 1 addition & 1 deletion src/Illuminate/Queue/Worker.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

namespace Illuminate\Queue;

use Carbon\Factory;
use Exception;
use Throwable;
use Carbon\Factory;
use Illuminate\Contracts\Events\Dispatcher;
use Illuminate\Database\DetectsLostConnections;
use Illuminate\Contracts\Debug\ExceptionHandler;
Expand Down
2 changes: 1 addition & 1 deletion src/Illuminate/Routing/UrlGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

namespace Illuminate\Routing;

use Carbon\Factory;
use Closure;
use Carbon\Factory;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
use Illuminate\Http\Request;
Expand Down
2 changes: 1 addition & 1 deletion src/Illuminate/Session/Middleware/StartSession.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

namespace Illuminate\Session\Middleware;

use Carbon\Factory;
use Closure;
use Carbon\Factory;
use Illuminate\Http\Request;
use Illuminate\Session\SessionManager;
use Illuminate\Contracts\Session\Session;
Expand Down
2 changes: 1 addition & 1 deletion src/Illuminate/Support/InteractsWithTime.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

namespace Illuminate\Support;

use Carbon\Factory;
use DateInterval;
use Carbon\Factory;
use DateTimeInterface;

trait InteractsWithTime
Expand Down
2 changes: 1 addition & 1 deletion src/Illuminate/Support/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"ext-mbstring": "*",
"doctrine/inflector": "^1.1",
"illuminate/contracts": "5.8.*",
"nesbot/carbon": "^1.24.1"
"nesbot/carbon": "^2.0"
},
"conflict": {
"tightenco/collect": "<5.5.33"
Expand Down
2 changes: 1 addition & 1 deletion src/Illuminate/Validation/Concerns/ValidatesAttributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

namespace Illuminate\Validation\Concerns;

use Carbon\Factory;
use DateTime;
use Countable;
use Exception;
use Throwable;
use DateTimeZone;
use Carbon\Factory;
use DateTimeInterface;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
Expand Down
2 changes: 1 addition & 1 deletion tests/Cache/CacheRepositoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
use DateTime;
use DateInterval;
use Mockery as m;
use DateTimeImmutable;
use Carbon\Carbon;
use DateTimeImmutable;
use PHPUnit\Framework\TestCase;

class CacheRepositoryTest extends TestCase
Expand Down
2 changes: 1 addition & 1 deletion tests/Database/DatabaseEloquentModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
use stdClass;
use Exception;
use Mockery as m;
use Carbon\Carbon;
use ReflectionClass;
use DateTimeImmutable;
use DateTimeInterface;
use Carbon\Carbon;
use PHPUnit\Framework\TestCase;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Builder;
Expand Down
2 changes: 1 addition & 1 deletion tests/Integration/Mail/SendingMailWithLocaleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
namespace Illuminate\Tests\Integration\Mail;

use Mockery;
use Illuminate\Mail\Mailable;
use Carbon\Carbon;
use Illuminate\Mail\Mailable;
use Orchestra\Testbench\TestCase;
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Facades\View;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

namespace Illuminate\Tests\Integration\Notifications;

use Illuminate\Mail\Mailable;
use Carbon\Carbon;
use Illuminate\Mail\Mailable;
use Orchestra\Testbench\TestCase;
use Illuminate\Support\Facades\View;
use Illuminate\Support\Facades\Event;
Expand Down
2 changes: 1 addition & 1 deletion tests/Integration/Routing/UrlSigningTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

namespace Illuminate\Tests\Integration\Routing;

use Illuminate\Http\Request;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Orchestra\Testbench\TestCase;
use Illuminate\Support\Facades\URL;
use Illuminate\Support\Facades\Route;
Expand Down
2 changes: 1 addition & 1 deletion tests/Queue/QueueWorkerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
namespace Illuminate\Tests\Queue;

use Mockery;
use RuntimeException;
use Carbon\Carbon;
use RuntimeException;
use PHPUnit\Framework\TestCase;
use Illuminate\Container\Container;
use Illuminate\Queue\WorkerOptions;
Expand Down
2 changes: 1 addition & 1 deletion tests/Support/SupportArrTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

use stdClass;
use ArrayObject;
use Illuminate\Support\Arr;
use Carbon\Carbon;
use Illuminate\Support\Arr;
use PHPUnit\Framework\TestCase;
use Illuminate\Support\Collection;

Expand Down
2 changes: 1 addition & 1 deletion tests/Support/SupportCarbonTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
namespace Illuminate\Tests\Support;

use DateTime;
use DateTimeInterface;
use Carbon\Carbon;
use DateTimeInterface;
use PHPUnit\Framework\TestCase;
use Carbon\Carbon as BaseCarbon;

Expand Down
2 changes: 1 addition & 1 deletion tests/Validation/ValidationValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

use DateTime;
use Mockery as m;
use Carbon\Carbon;
use DateTimeImmutable;
use Illuminate\Support\Arr;
use Carbon\Carbon;
use PHPUnit\Framework\TestCase;
use Illuminate\Validation\Validator;
use Illuminate\Validation\Rules\Exists;
Expand Down

0 comments on commit cfd35ff

Please sign in to comment.