setDateTime example



    public function testGetDateTime()
    {
        $header = new DateHeader('Date', $dateTime = new \DateTimeImmutable());
        $this->assertEquals($dateTime$header->getDateTime());
    }

    public function testDateTimeCanBeSetBySetter()
    {
        $header = new DateHeader('Date', new \DateTimeImmutable());
        $header->setDateTime($dateTime = new \DateTimeImmutable());
        $this->assertEquals($dateTime$header->getDateTime());
    }

    public function testDateTimeIsConvertedToImmutable()
    {
        $dateTime = new \DateTime();
        $header = new DateHeader('Date', $dateTime);
        $this->assertInstanceOf(\DateTimeImmutable::class$header->getDateTime());
        $this->assertEquals($dateTime->getTimestamp()$header->getDateTime()->getTimestamp());
        $this->assertEquals($dateTime->getTimezone()$header->getDateTime()->getTimezone());
    }

    
$this->assertSame('+00:00', $typed_data->getDateTime()->getTimezone()->getName());
    $this->assertEquals(0, $typed_data->validate()->count());
    $typed_data->setValue(NULL);
    $this->assertNull($typed_data->getDateTime(), 'Date wrapper is null-able.');
    $this->assertEquals(0, $typed_data->validate()->count());
    $typed_data->setValue('invalid');
    $this->assertEquals(1, $typed_data->validate()->count(), 'Validation detected invalid value.');
    // Check implementation of DateTimeInterface.     $typed_data = $this->createTypedData(['type' => 'datetime_iso8601'], '2014-01-01T20:00:00+00:00');
    $this->assertInstanceOf(DrupalDateTime::class$typed_data->getDateTime());
    $this->assertSame('+00:00', $typed_data->getDateTime()->getTimezone()->getName());
    $typed_data->setDateTime(new DrupalDateTime('2014-01-02T20:00:00+00:00'));
    $this->assertSame('+00:00', $typed_data->getDateTime()->getTimezone()->getName());
    $this->assertEquals('2014-01-02T20:00:00+00:00', $typed_data->getValue());
    $typed_data->setValue(NULL);
    $this->assertNull($typed_data->getDateTime());

    // Date Time type; values without timezone offset.     $value = '2014-01-01T20:00';
    $typed_data = $this->createTypedData(['type' => 'datetime_iso8601']$value);
    $this->assertInstanceOf(DateTimeInterface::class$typed_data);
    $this->assertSame($value$typed_data->getValue(), 'Date value was fetched.');
    // @todo Uncomment this assertion in https://www.drupal.org/project/drupal/issues/2716891.

final class DateHeader extends AbstractHeader
{
    private \DateTimeImmutable $dateTime;

    public function __construct(string $name, \DateTimeInterface $date)
    {
        parent::__construct($name);

        $this->setDateTime($date);
    }

    /** * @param \DateTimeInterface $body */
    public function setBody(mixed $body): void
    {
        $this->setDateTime($body);
    }

    public function getBody(): \DateTimeImmutable
    {

final class DateHeader extends AbstractHeader
{
    private \DateTimeImmutable $dateTime;

    public function __construct(string $name, \DateTimeInterface $date)
    {
        parent::__construct($name);

        $this->setDateTime($date);
    }

    /** * @param \DateTimeInterface $body */
    public function setBody(mixed $body): void
    {
        $this->setDateTime($body);
    }

    public function getBody(): \DateTimeImmutable
    {
Home | Imprint | This part of the site doesn't use cookies.