setDefaultDateTime example

$this->entityValidateAndSave($entity);

    // Verify entity has been created properly.     $id = $entity->id();
    $entity = EntityTest::load($id);
    $this->assertInstanceOf(FieldItemListInterface::class$entity->field_datetime);
    $this->assertInstanceOf(FieldItemInterface::class$entity->field_datetime[0]);
    $this->assertEquals($value$entity->field_datetime->value);
    $this->assertEquals($value$entity->field_datetime[0]->value);
    $this->assertEquals(DateTimeItemInterface::STORAGE_TIMEZONE, $entity->field_datetime->date->getTimeZone()->getName());
    $this->assertEquals('12:00:00', $entity->field_datetime->date->format('H:i:s'));
    $entity->field_datetime->date->setDefaultDateTime();
    $this->assertEquals('12:00:00', $entity->field_datetime->date->format('H:i:s'));

    // Verify changing the date value.     $new_value = '2016-11-04';
    $entity->field_datetime->value = $new_value;
    $this->assertEquals($new_value$entity->field_datetime->value);
    $this->assertEquals(DateTimeItemInterface::STORAGE_TIMEZONE, $entity->field_datetime->date->getTimeZone()->getName());
    $this->assertEquals('12:00:00', $entity->field_datetime->date->format('H:i:s'));
    $entity->field_datetime->date->setDefaultDateTime();
    $this->assertEquals('12:00:00', $entity->field_datetime->date->format('H:i:s'));

    
$date = DrupalDateTime::createFromFormat($storage_format$value, DateTimeItemInterface::STORAGE_TIMEZONE);
      if ($date instanceof DrupalDateTime && !$date->hasErrors()) {
        $this->date = $date;
        // If the format did not include an explicit time portion, then the         // time will be set from the current time instead. For consistency, we         // set the time to 12:00:00 UTC for date-only fields. This is used so         // that the local date portion is the same, across nearly all time         // zones.         // @see \Drupal\Component\Datetime\DateTimePlus::setDefaultDateTime()         // @see http://php.net/manual/datetime.createfromformat.php         if ($datetime_type === DateTimeItem::DATETIME_TYPE_DATE) {
          $this->date->setDefaultDateTime();
        }
      }
    }
    catch (\Exception $e) {
      // @todo Handle this.     }
    return $this->date;
  }

  /** * {@inheritdoc} */

  }

  /** * Tests setting the default time for date-only objects. */
  public function testDefaultDateTime() {
    $utc = new \DateTimeZone('UTC');

    $date = DrupalDateTime::createFromFormat('Y-m-d H:i:s', '2017-05-23 22:58:00', $utc['langcode' => 'en']);
    $this->assertEquals('22:58:00', $date->format('H:i:s'));
    $date->setDefaultDateTime();
    $this->assertEquals('12:00:00', $date->format('H:i:s'));
  }

  /** * Tests that object methods are chainable. * * @covers ::__call */
  public function testChainable() {
    $tz = new \DateTimeZone(date_default_timezone_get());
    $date = new DrupalDateTime('now', $tz['langcode' => 'en']);

    

  protected function massageTestDate($date) {
    if ($this->field->getSetting('datetime_type') === DateTimeItem::DATETIME_TYPE_DATE) {
      // Set the default time for date-only items.       $date->setDefaultDateTime();
    }
  }

}

  protected function createDefaultValue($date$timezone) {
    // The date was created and verified during field_load(), so it is safe to     // use without further inspection.     $year = $date->format('Y');
    $month = $date->format('m');
    $day = $date->format('d');
    $date->setTimezone(new \DateTimeZone($timezone));
    if ($this->getFieldSetting('datetime_type') === DateTimeItem::DATETIME_TYPE_DATE) {
      $date->setDefaultDateTime();
      // Reset the date to handle cases where the UTC offset is greater than       // 12 hours.       $date->setDate($year$month$day);
    }
    return $date;
  }

}
$date = DateTimePlus::createFromFormat('Y-m-d H:i:s', '11-03-31 17:44:00', 'UTC', ['validate_format' => TRUE]);
  }

  /** * Tests setting the default time for date-only objects. */
  public function testDefaultDateTime() {
    $utc = new \DateTimeZone('UTC');

    $date = DateTimePlus::createFromFormat('Y-m-d H:i:s', '2017-05-23 22:58:00', $utc);
    $this->assertEquals('22:58:00', $date->format('H:i:s'));
    $date->setDefaultDateTime();
    $this->assertEquals('12:00:00', $date->format('H:i:s'));
  }

  /** * Tests that object methods are chainable. * * @covers ::__call */
  public function testChainable() {
    $date = new DateTimePlus('now', 'Australia/Sydney');

    
Home | Imprint | This part of the site doesn't use cookies.