getTimeZone example

            $criteria = new Criteria($data->prefixed('p'));
            $criteria->addState(Criteria::STATE_ELASTICSEARCH_AWARE);

            $criteria->addAggregation(
                new DateHistogramAggregation(
                    'release-histogram',
                    'product.releaseDate',
                    $case->getInterval(),
                    null,
                    null,
                    $case->getFormat(),
                    $case->getTimeZone()
                )
            );

            $result = $aggregator->aggregate($this->productDefinition, $criteria$context);

            static::assertTrue($result->has('release-histogram'));

            $histogram = $result->get('release-histogram');
            static::assertInstanceOf(DateHistogramResult::class$histogram);

            static::assertCount(\count($case->getBuckets())$histogram->getBuckets()print_r($histogram->getBuckets(), true));

            
$this->extendQuery($aggregationStruct$query$definition$context);
    }

    private function parseDateHistogramAggregation(
        DateHistogramAggregation $aggregation,
        QueryBuilder $query,
        EntityDefinition $definition,
        Context $context
    ): void {
        $accessor = $this->queryHelper->getFieldAccessor($aggregation->getField()$definition$definition->getEntityName()$context);

        if ($this->timeZoneSupportEnabled && $aggregation->getTimeZone()) {
            $accessor = 'CONVERT_TZ(' . $accessor . ', "UTC", "' . $aggregation->getTimeZone() . '")';
        }

        $groupBy = match ($aggregation->getInterval()) {
            DateHistogramAggregation::PER_MINUTE => 'DATE_FORMAT(' . $accessor . ', \'%Y-%m-%d %H:%i\')',
            DateHistogramAggregation::PER_HOUR => 'DATE_FORMAT(' . $accessor . ', \'%Y-%m-%d %H\')',
            DateHistogramAggregation::PER_DAY => 'DATE_FORMAT(' . $accessor . ', \'%Y-%m-%d\')',
            DateHistogramAggregation::PER_WEEK => 'DATE_FORMAT(' . $accessor . ', \'%Y-%v\')',
            DateHistogramAggregation::PER_MONTH => 'DATE_FORMAT(' . $accessor . ', \'%Y-%m\')',
            DateHistogramAggregation::PER_QUARTER => 'CONCAT(DATE_FORMAT(' . $accessor . ', \'%Y\'), \'-\', QUARTER(' . $accessor . '))',
            DateHistogramAggregation::PER_YEAR => 'DATE_FORMAT(' . $accessor . ', \'%Y\')',
            
$composite->addSource($sorting);
        }

        $histogram = new ElasticsearchDateHistogramAggregation(
            $aggregation->getName() . '.key',
            $fieldName,
            $aggregation->getInterval(),
            'yyyy-MM-dd HH:mm:ss'
        );

        if ($aggregation->getTimeZone()) {
            $histogram->addParameter('time_zone', $aggregation->getTimeZone());
        }

        $composite->addSource($histogram);

        if ($nested = $aggregation->getAggregation()) {
            $composite->addAggregation(
                $this->parseNestedAggregation($nested$definition$context)
            );
        }

        

  public function __construct(AccountInterface $current_user, ConfigFactoryInterface $config_factory) {
    $this->configFactory = $config_factory;
    $this->currentUser = $current_user;
  }

  /** * Sets the default time zone. */
  public function setDefaultTimeZone() {
    if ($time_zone = $this->getTimeZone()) {
      date_default_timezone_set($time_zone);
    }
  }

  /** * Updates the default time zone when time zone config changes. * * @param \Drupal\Core\Config\ConfigCrudEvent $event * The config crud event. */
  public function onConfigSave(ConfigCrudEvent $event) {
    
/** * {@inheritdoc} */
  public function getEmail() {
    return $this->getAccount()->getEmail();
  }

  /** * {@inheritdoc} */
  public function getTimeZone() {
    return $this->getAccount()->getTimeZone();
  }

  /** * {@inheritdoc} */
  public function getLastAccessedTime() {
    return $this->getAccount()->getLastAccessedTime();
  }

  /** * {@inheritdoc} */
$this->ids->getList(['p-1', 'p-2', 'p-3', 'p-4', 'p-5', 'p-6'])
        );

        $criteria->addAggregation(
            new DateHistogramAggregation(
                'release-histogram',
                'product.releaseDate',
                $case->getInterval(),
                null,
                null,
                $case->getFormat(),
                $case->getTimeZone()
            )
        );

        $result = $this->aggregator->aggregate($this->definition, $criteria$context);

        static::assertTrue($result->has('release-histogram'));

        $histogram = $result->get('release-histogram');
        static::assertInstanceOf(DateHistogramResult::class$histogram);

        static::assertCount(\count($case->getBuckets())$histogram->getBuckets()print_r($histogram->getBuckets(), true));

        

  public function testComplexExecute() {
    $executable = Views::getView('test_field_field_complex_test');
    $executable->execute();

    $timezones = [];
    foreach ($this->testUsers as $user) {
      $timezones[] = $user->getTimeZone();
    }

    $this->assertInstanceOf(EntityField::class$executable->field['field_test_multiple']);
    $this->assertInstanceOf(EntityField::class$executable->field['field_test_multiple_1']);
    $this->assertInstanceOf(EntityField::class$executable->field['field_test_multiple_2']);
    $this->assertInstanceOf(EntityField::class$executable->field['timezone']);

    $this->assertIdenticalResultset($executable,
      [
        ['timezone' => $timezones[0], 'field_test_multiple' => [1, 3], 'field_test_multiple_1' => [1, 3], 'field_test_multiple_2' => [1, 3]],
        ['timezone' => $timezones[1], 'field_test_multiple' => [7, 0], 'field_test_multiple_1' => [7, 0], 'field_test_multiple_2' => [7, 0]],
        [
// Clean up the error messages.     $this->checkErrors();
  }

  /** * Renders the timezone name. * * @return string */
  public function render() {
    return $this->format(static::FORMAT) . ' ' . $this->getTimeZone()->getName();
  }

  /** * Implements the magic __call method. * * Passes through all unknown calls onto the DateTime object. * * @param string $method * The method to call on the decorated object. * @param array $args * Call arguments. * * @return mixed * The return value from the method on the decorated object. If the proxied * method call returns a DateTime object, then return the original * DateTimePlus object, which allows function chaining to work properly. * Otherwise, the value from the proxied method call is returned. * * @throws \Exception * Thrown when the DateTime object is not set. * @throws \BadMethodCallException * Thrown when there is no corresponding method on the DateTime object to * call. */
/** @var \Drupal\user\UserInterface $user */
      $user = User::load($source->uid);
      $this->assertSame($source->uid, $user->id());
      $this->assertSame($source->name, $user->label());
      $this->assertSame($source->mail, $user->getEmail());
      $this->assertSame($source->created, $user->getCreatedTime());
      $this->assertSame($source->access, $user->getLastAccessedTime());
      $this->assertSame($source->login, $user->getLastLoginTime());
      $is_blocked = $source->status == 0;
      $this->assertSame($is_blocked$user->isBlocked());
      $expected_timezone_name = $source->timezone_name ?: $this->config('system.date')->get('timezone.default');
      $this->assertSame($expected_timezone_name$user->getTimeZone());
      $this->assertSame($source->init, $user->getInitialEmail());
      $this->assertSame($roles$user->getRoles());

      // Ensure the user's langcode, preferred_langcode and       // preferred_admin_langcode are valid.       // $user->getPreferredLangcode() might fallback to default language if the       // user preferred language is not configured on the site. We just want to       // test if the value was imported correctly.       $language_manager = $this->container->get('language_manager');
      $default_langcode = $language_manager->getDefaultLanguage()->getId();
      if (empty($source->language)) {
        
elseif ($language_manager->getLanguage($prefered_langcode) === NULL) {
      $this->assertSame($default_langcode$user->langcode->value);
      $this->assertSame($default_langcode$user->preferred_langcode->value);
      $this->assertSame($default_langcode$user->preferred_admin_langcode->value);
    }
    else {
      $this->assertSame($entity_langcode$user->langcode->value);
      $this->assertSame($prefered_langcode$user->preferred_langcode->value);
      $this->assertSame($prefered_langcode$user->preferred_admin_langcode->value);
    }

    $this->assertSame($timezone$user->getTimeZone());
    $this->assertSame($init$user->getInitialEmail());
    $this->assertSame($roles$user->getRoles());
    $this->assertSame($has_picture, !$user->user_picture->isEmpty());
    if (!is_null($field_integer)) {
      $this->assertTrue($user->hasField('field_integer'));
      $this->assertEquals($field_integer[0]$user->field_integer->value);
    }
    if (!empty($field_file_target_id)) {
      $this->assertTrue($user->hasField('field_file'));
      $this->assertSame($field_file_target_id$user->field_file->target_id);
    }
  }

  public function assertDateTimestamp(DateTimePlus $date, string $input, array $initial, array $transform): void {
    // Check format.     $value = $date->format($initial['format']);
    $this->assertEquals($initial['expected_date']$valuesprintf("Test new DateTimePlus(%s, %s): should be %s, found %s.", $input$initial['timezone']$initial['expected_date']$value));

    // Check timezone name.     $value = $date->getTimeZone()->getName();
    $this->assertEquals($initial['expected_timezone']$valuesprintf("The current timezone is %s: should be %s.", $value$initial['expected_timezone']));

    // Check offset.     $value = $date->getOffset();
    $this->assertEquals($initial['expected_offset']$valuesprintf("The current offset is %s: should be %s.", $value$initial['expected_offset']));

    // Transform the date to another timezone.     $date->setTimezone(new \DateTimeZone($transform['timezone']));

    // Check transformed format.     $value = $date->format($transform['format']);
    
return [$value instanceof $class ? $value : $class::createFromInterface($value)];
        }

        $format = null;

        if ($attributes = $argument->getAttributes(MapDateTime::class, ArgumentMetadata::IS_INSTANCEOF)) {
            $attribute = $attributes[0];
            $format = $attribute->format;
        }

        if (null !== $format) {
            $date = $class::createFromFormat($format$value$this->clock?->now()->getTimeZone());

            if (($class::getLastErrors() ?: ['warning_count' => 0])['warning_count']) {
                $date = false;
            }
        } else {
            if (false !== filter_var($value, \FILTER_VALIDATE_INT, ['options' => ['min_range' => 0]])) {
                $value = '@'.$value;
            }
            try {
                $date = new $class($value$this->clock?->now()->getTimeZone());
            } catch (\Exception) {
                
public static function castIntlCalendar(\IntlCalendar $c, array $a, Stub $stub, bool $isNested, int $filter = 0)
    {
        $a += [
            Caster::PREFIX_VIRTUAL.'type' => $c->getType(),
            Caster::PREFIX_VIRTUAL.'first_day_of_week' => $c->getFirstDayOfWeek(),
            Caster::PREFIX_VIRTUAL.'minimal_days_in_first_week' => $c->getMinimalDaysInFirstWeek(),
            Caster::PREFIX_VIRTUAL.'repeated_wall_time_option' => $c->getRepeatedWallTimeOption(),
            Caster::PREFIX_VIRTUAL.'skipped_wall_time_option' => $c->getSkippedWallTimeOption(),
            Caster::PREFIX_VIRTUAL.'time' => $c->getTime(),
            Caster::PREFIX_VIRTUAL.'in_daylight_time' => $c->inDaylightTime(),
            Caster::PREFIX_VIRTUAL.'is_lenient' => $c->isLenient(),
            Caster::PREFIX_VIRTUAL.'time_zone' => ($filter & Caster::EXCLUDE_VERBOSE) ? new CutStub($c->getTimeZone()) : $c->getTimeZone(),
        ];

        return self::castError($c$a);
    }

    /** * @return array */
    public static function castIntlDateFormatter(\IntlDateFormatter $c, array $a, Stub $stub, bool $isNested, int $filter = 0)
    {
        $a += [
            
public static function castIntlCalendar(\IntlCalendar $c, array $a, Stub $stub, bool $isNested, int $filter = 0)
    {
        $a += [
            Caster::PREFIX_VIRTUAL.'type' => $c->getType(),
            Caster::PREFIX_VIRTUAL.'first_day_of_week' => $c->getFirstDayOfWeek(),
            Caster::PREFIX_VIRTUAL.'minimal_days_in_first_week' => $c->getMinimalDaysInFirstWeek(),
            Caster::PREFIX_VIRTUAL.'repeated_wall_time_option' => $c->getRepeatedWallTimeOption(),
            Caster::PREFIX_VIRTUAL.'skipped_wall_time_option' => $c->getSkippedWallTimeOption(),
            Caster::PREFIX_VIRTUAL.'time' => $c->getTime(),
            Caster::PREFIX_VIRTUAL.'in_daylight_time' => $c->inDaylightTime(),
            Caster::PREFIX_VIRTUAL.'is_lenient' => $c->isLenient(),
            Caster::PREFIX_VIRTUAL.'time_zone' => ($filter & Caster::EXCLUDE_VERBOSE) ? new CutStub($c->getTimeZone()) : $c->getTimeZone(),
        ];

        return self::castError($c$a);
    }

    /** * @return array */
    public static function castIntlDateFormatter(\IntlDateFormatter $c, array $a, Stub $stub, bool $isNested, int $filter = 0)
    {
        $a += [
            
$entity->field_datetime = $value;
    $entity->name->value = $this->randomMachineName();
    $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[0]->getProperties()['value']->getDateTime()->getTimeZone()->getName());
    $this->assertEquals(DateTimeItemInterface::STORAGE_TIMEZONE, $entity->field_datetime->date->getTimeZone()->getName());

    // Verify changing the date value.     $new_value = '2016-11-04T00:21:00';
    $entity->field_datetime->value = $new_value;
    $this->assertEquals($new_value$entity->field_datetime->value);
    $this->assertEquals(DateTimeItemInterface::STORAGE_TIMEZONE, $entity->field_datetime[0]->getProperties()['value']->getDateTime()->getTimeZone()->getName());
    $this->assertEquals(DateTimeItemInterface::STORAGE_TIMEZONE, $entity->field_datetime->date->getTimeZone()->getName());

    // Read changed entity and assert changed values.     $this->entityValidateAndSave($entity);
    
Home | Imprint | This part of the site doesn't use cookies.