Use Carbon for dates

This commit is contained in:
Oliver Davies 2020-05-09 21:51:04 +01:00
parent 929b3ea852
commit 7e2bd98d93
4 changed files with 7 additions and 4 deletions

View file

@ -33,6 +33,7 @@
"drupal/xmlsitemap": "^1.0", "drupal/xmlsitemap": "^1.0",
"drush/drush": "^9", "drush/drush": "^9",
"illuminate/support": "^7.10", "illuminate/support": "^7.10",
"nesbot/carbon": "^2.33",
"tightenco/collect": "^6.15" "tightenco/collect": "^6.15"
}, },
"require-dev": { "require-dev": {

2
composer.lock generated
View file

@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically" "This file is @generated automatically"
], ],
"content-hash": "89691b515b3a79b86272092c47301edd", "content-hash": "9bfa466922e45768b4123877e46728c6",
"packages": [ "packages": [
{ {
"name": "asm89/stack-cors", "name": "asm89/stack-cors",

View file

@ -4,6 +4,7 @@ declare(strict_types=1);
namespace Drupal\custom\EventSubscriber; namespace Drupal\custom\EventSubscriber;
use Carbon\Carbon;
use Drupal\Core\Entity\EntityInterface; use Drupal\Core\Entity\EntityInterface;
use Drupal\hook_event_dispatcher\Event\Entity\BaseEntityEvent; use Drupal\hook_event_dispatcher\Event\Entity\BaseEntityEvent;
use Drupal\hook_event_dispatcher\HookEventDispatcherInterface; use Drupal\hook_event_dispatcher\HookEventDispatcherInterface;
@ -40,7 +41,7 @@ final class UpdateTalkCreatedDateOnSave implements EventSubscriberInterface {
return; return;
} }
$talkDate = (new \DateTime($eventDate))->getTimestamp(); $talkDate = Carbon::parse($eventDate)->getTimestamp();
if ($talkDate == $talk->get('created')->getString()) { if ($talkDate == $talk->get('created')->getString()) {
return; return;

View file

@ -4,6 +4,7 @@ declare(strict_types=1);
namespace Drupal\Tests\custom\Kernel; namespace Drupal\Tests\custom\Kernel;
use Carbon\Carbon;
use Drupal\Core\Entity\EntityInterface; use Drupal\Core\Entity\EntityInterface;
use Drupal\datetime\Plugin\Field\FieldType\DateTimeItemInterface; use Drupal\datetime\Plugin\Field\FieldType\DateTimeItemInterface;
use Drupal\KernelTests\Core\Entity\EntityKernelTestBase; use Drupal\KernelTests\Core\Entity\EntityKernelTestBase;
@ -33,7 +34,7 @@ final class UpdatesTalkCreatedDateTest extends EntityKernelTestBase {
]; ];
public function testCreatingNode() { public function testCreatingNode() {
$eventDate = (new \DateTime('today'))->modify('+1 week'); $eventDate = Carbon::today()->addWeek();
$eventDateToFormat = $eventDate->format(DateTimeItemInterface::DATE_STORAGE_FORMAT); $eventDateToFormat = $eventDate->format(DateTimeItemInterface::DATE_STORAGE_FORMAT);
$eventDateToTimestamp = $eventDate->getTimestamp(); $eventDateToTimestamp = $eventDate->getTimestamp();
@ -46,7 +47,7 @@ final class UpdatesTalkCreatedDateTest extends EntityKernelTestBase {
public function testUpdatingNode() { public function testUpdatingNode() {
$talk = $this->createTalk(); $talk = $this->createTalk();
$eventDate = (new \DateTime('today'))->modify('+1 week'); $eventDate = Carbon::today()->addWeek();
$eventDateToFormat = $eventDate->format(DateTimeItemInterface::DATE_STORAGE_FORMAT); $eventDateToFormat = $eventDate->format(DateTimeItemInterface::DATE_STORAGE_FORMAT);
$eventDateToTimestamp = $eventDate->getTimestamp(); $eventDateToTimestamp = $eventDate->getTimestamp();