diff --git a/core/lib/Drupal/Core/Datetime/DateHelper.php b/core/lib/Drupal/Core/Datetime/DateHelper.php index 168ef62740..a5feebf1d2 100644 --- a/core/lib/Drupal/Core/Datetime/DateHelper.php +++ b/core/lib/Drupal/Core/Datetime/DateHelper.php @@ -291,10 +291,10 @@ public static function weekDaysOrdered($weekdays) { public static function years($min = 0, $max = 0, $required = FALSE) { // Ensure $min and $max are valid values. if (empty($min)) { - $min = intval(date('Y', REQUEST_TIME) - 3); + $min = intval(date('Y', \Drupal::time()->getRequestTime()) - 3); } if (empty($max)) { - $max = intval(date('Y', REQUEST_TIME) + 3); + $max = intval(date('Y', \Drupal::time()->getRequestTime()) + 3); } $none = ['' => '']; $range = range($min, $max); diff --git a/core/lib/Drupal/Core/Field/Plugin/Field/FieldFormatter/TimestampFormatter.php b/core/lib/Drupal/Core/Field/Plugin/Field/FieldFormatter/TimestampFormatter.php index cbc6d7ddd5..55463da624 100644 --- a/core/lib/Drupal/Core/Field/Plugin/Field/FieldFormatter/TimestampFormatter.php +++ b/core/lib/Drupal/Core/Field/Plugin/Field/FieldFormatter/TimestampFormatter.php @@ -106,7 +106,7 @@ public function settingsForm(array $form, FormStateInterface $form_state) { $date_formats = []; foreach ($this->dateFormatStorage->loadMultiple() as $machine_name => $value) { - $date_formats[$machine_name] = $this->t('@name format: @date', ['@name' => $value->label(), '@date' => $this->dateFormatter->format(REQUEST_TIME, $machine_name)]); + $date_formats[$machine_name] = $this->t('@name format: @date', ['@name' => $value->label(), '@date' => $this->dateFormatter->format(\Drupal::time()->getRequestTime(), $machine_name)]); } $date_formats['custom'] = $this->t('Custom'); diff --git a/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/ChangedItem.php b/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/ChangedItem.php index bd2e129f29..9e737758ab 100644 --- a/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/ChangedItem.php +++ b/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/ChangedItem.php @@ -30,7 +30,7 @@ public function preSave() { // Set the timestamp to request time if it is not set. if (!$this->value) { - $this->value = REQUEST_TIME; + $this->value = \Drupal::time()->getRequestTime(); } else { // On an existing entity translation, the changed timestamp will only be @@ -47,7 +47,7 @@ public function preSave() { if (!$entity->isNew() && $original && $original->hasTranslation($langcode)) { $original_value = $original->getTranslation($langcode)->get($this->getFieldDefinition()->getName())->value; if ($this->value == $original_value && $entity->hasTranslationChanges()) { - $this->value = REQUEST_TIME; + $this->value = \Drupal::time()->getRequestTime(); } } } diff --git a/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/CreatedItem.php b/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/CreatedItem.php index da2e2a7450..78a66a7f84 100644 --- a/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/CreatedItem.php +++ b/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/CreatedItem.php @@ -22,7 +22,7 @@ class CreatedItem extends TimestampItem { public function applyDefaultValue($notify = TRUE) { parent::applyDefaultValue($notify); // Created fields default to the current timestamp. - $this->setValue(['value' => REQUEST_TIME], $notify); + $this->setValue(['value' => \Drupal::time()->getRequestTime()], $notify); return $this; } diff --git a/core/lib/Drupal/Core/KeyValueStore/DatabaseStorageExpirable.php b/core/lib/Drupal/Core/KeyValueStore/DatabaseStorageExpirable.php index 86d3dabc53..8eba51ffea 100644 --- a/core/lib/Drupal/Core/KeyValueStore/DatabaseStorageExpirable.php +++ b/core/lib/Drupal/Core/KeyValueStore/DatabaseStorageExpirable.php @@ -36,7 +36,7 @@ public function has($key) { return (bool) $this->connection->query('SELECT 1 FROM {' . $this->connection->escapeTable($this->table) . '} WHERE collection = :collection AND name = :key AND expire > :now', [ ':collection' => $this->collection, ':key' => $key, - ':now' => REQUEST_TIME, + ':now' => \Drupal::time()->getRequestTime(), ])->fetchField(); } @@ -47,7 +47,7 @@ public function getMultiple(array $keys) { $values = $this->connection->query( 'SELECT name, value FROM {' . $this->connection->escapeTable($this->table) . '} WHERE expire > :now AND name IN ( :keys[] ) AND collection = :collection', [ - ':now' => REQUEST_TIME, + ':now' => \Drupal::time()->getRequestTime(), ':keys[]' => $keys, ':collection' => $this->collection, ])->fetchAllKeyed(); @@ -62,7 +62,7 @@ public function getAll() { 'SELECT name, value FROM {' . $this->connection->escapeTable($this->table) . '} WHERE collection = :collection AND expire > :now', [ ':collection' => $this->collection, - ':now' => REQUEST_TIME, + ':now' => \Drupal::time()->getRequestTime(), ])->fetchAllKeyed(); return array_map([$this->serializer, 'decode'], $values); } @@ -78,7 +78,7 @@ public function setWithExpire($key, $value, $expire) { ]) ->fields([ 'value' => $this->serializer->encode($value), - 'expire' => REQUEST_TIME + $expire, + 'expire' => \Drupal::time()->getRequestTime() + $expire, ]) ->execute(); } diff --git a/core/lib/Drupal/Core/Queue/DatabaseQueue.php b/core/lib/Drupal/Core/Queue/DatabaseQueue.php index 1d0621e0e5..c2b76c97e3 100644 --- a/core/lib/Drupal/Core/Queue/DatabaseQueue.php +++ b/core/lib/Drupal/Core/Queue/DatabaseQueue.php @@ -87,9 +87,10 @@ protected function doCreateItem($data) { ->fields([ 'name' => $this->name, 'data' => serialize($data), - // We cannot rely on REQUEST_TIME because many items might be created - // by a single request which takes longer than 1 second. - 'created' => time(), + // We cannot rely on \Drupal::time()->getRequestTime() + // because many items might be created by a single request which takes + // longer than 1 second. + 'created' => \Drupal::time()->getCurrentTime(), ]); // Return the new serial ID, or FALSE on failure. return $query->execute(); @@ -214,7 +215,7 @@ public function garbageCollection() { try { // Clean up the queue for failed batches. $this->connection->delete(static::TABLE_NAME) - ->condition('created', REQUEST_TIME - 864000, '<') + ->condition('created', \Drupal::time()->getRequestTime() - 864000, '<') ->condition('name', 'drupal_batch:%', 'LIKE') ->execute(); @@ -225,7 +226,7 @@ public function garbageCollection() { 'expire' => 0, ]) ->condition('expire', 0, '<>') - ->condition('expire', REQUEST_TIME, '<') + ->condition('expire', \Drupal::time()->getRequestTime(), '<') ->execute(); } catch (\Exception $e) { diff --git a/core/lib/Drupal/Core/Template/TwigPhpStorageCache.php b/core/lib/Drupal/Core/Template/TwigPhpStorageCache.php index 995ce97803..d50075e234 100644 --- a/core/lib/Drupal/Core/Template/TwigPhpStorageCache.php +++ b/core/lib/Drupal/Core/Template/TwigPhpStorageCache.php @@ -111,7 +111,7 @@ public function write($key, $content) { $this->storage()->save($key, $content); // Save the last mtime. $cid = 'twig:' . $key; - $this->cache->set($cid, REQUEST_TIME); + $this->cache->set($cid, \Drupal::time()->getRequestTime()); } /** diff --git a/core/modules/aggregator/src/Entity/Feed.php b/core/modules/aggregator/src/Entity/Feed.php index 4c3a5b856e..3e8dd3bea8 100644 --- a/core/modules/aggregator/src/Entity/Feed.php +++ b/core/modules/aggregator/src/Entity/Feed.php @@ -85,7 +85,7 @@ public function refreshItems() { $success = \Drupal::service('aggregator.items.importer')->refresh($this); // Regardless of successful or not, indicate that it has been checked. - $this->setLastCheckedTime(REQUEST_TIME); + $this->setLastCheckedTime(\Drupal::time()->getRequestTime()); $this->setQueuedTime(0); $this->save(); diff --git a/core/modules/config_translation/src/FormElement/DateFormat.php b/core/modules/config_translation/src/FormElement/DateFormat.php index d0ae033a42..8cd218e0ec 100644 --- a/core/modules/config_translation/src/FormElement/DateFormat.php +++ b/core/modules/config_translation/src/FormElement/DateFormat.php @@ -16,7 +16,7 @@ public function getTranslationElement(LanguageInterface $translation_language, $ /** @var \Drupal\Core\Datetime\DateFormatterInterface $date_formatter */ $date_formatter = \Drupal::service('date.formatter'); $description = $this->t('A user-defined date format. See the PHP manual for available options.'); - $format = $this->t('Displayed as %date_format', ['%date_format' => $date_formatter->format(REQUEST_TIME, 'custom', $translation_config)]); + $format = $this->t('Displayed as %date_format', ['%date_format' => $date_formatter->format(\Drupal::time()->getRequestTime(), 'custom', $translation_config)]); return [ '#type' => 'textfield', diff --git a/core/modules/datetime/src/Plugin/Field/FieldType/DateTimeItem.php b/core/modules/datetime/src/Plugin/Field/FieldType/DateTimeItem.php index 3264069008..2c3ac8e90d 100644 --- a/core/modules/datetime/src/Plugin/Field/FieldType/DateTimeItem.php +++ b/core/modules/datetime/src/Plugin/Field/FieldType/DateTimeItem.php @@ -107,7 +107,7 @@ public static function generateSampleValue(FieldDefinitionInterface $field_defin // Just pick a date in the past year. No guidance is provided by this Field // type. - $timestamp = REQUEST_TIME - mt_rand(0, 86400 * 365); + $timestamp = \Drupal::time()->getRequestTime() - mt_rand(0, 86400 * 365); if ($type == DateTimeItem::DATETIME_TYPE_DATE) { $values['value'] = gmdate(static::DATE_STORAGE_FORMAT, $timestamp); } diff --git a/core/modules/datetime_range/src/Plugin/Field/FieldType/DateRangeItem.php b/core/modules/datetime_range/src/Plugin/Field/FieldType/DateRangeItem.php index 7c34ed127c..3a7d742525 100644 --- a/core/modules/datetime_range/src/Plugin/Field/FieldType/DateRangeItem.php +++ b/core/modules/datetime_range/src/Plugin/Field/FieldType/DateRangeItem.php @@ -94,7 +94,7 @@ public static function generateSampleValue(FieldDefinitionInterface $field_defin // Just pick a date in the past year. No guidance is provided by this Field // type. - $start = REQUEST_TIME - mt_rand(0, 86400 * 365) - 86400; + $start = \Drupal::time()->getRequestTime() - mt_rand(0, 86400 * 365) - 86400; $end = $start + 86400; if ($type == static::DATETIME_TYPE_DATETIME) { $values['value'] = gmdate(DateTimeItemInterface::DATETIME_STORAGE_FORMAT, $start); diff --git a/core/modules/migrate_drupal_ui/src/Batch/MigrateUpgradeImportBatch.php b/core/modules/migrate_drupal_ui/src/Batch/MigrateUpgradeImportBatch.php index c9c19a5c30..1e1a54b40b 100644 --- a/core/modules/migrate_drupal_ui/src/Batch/MigrateUpgradeImportBatch.php +++ b/core/modules/migrate_drupal_ui/src/Batch/MigrateUpgradeImportBatch.php @@ -286,7 +286,7 @@ public static function finished($success, $results, $operations, $elapsed) { */ public static function onPostRowSave(MigratePostRowSaveEvent $event) { // We want to interrupt this batch and start a fresh one. - if ((time() - REQUEST_TIME) > static::$maxExecTime) { + if ((\Drupal::time()->getCurrentTime() - \Drupal::time()->getRequestTime()) > static::$maxExecTime) { $event->getMigration()->interruptMigration(MigrationInterface::RESULT_INCOMPLETE); } } @@ -317,7 +317,7 @@ public static function onPostImport(MigrateImportEvent $event) { */ public static function onPostRowDelete(MigrateRowDeleteEvent $event) { // We want to interrupt this batch and start a fresh one. - if ((time() - REQUEST_TIME) > static::$maxExecTime) { + if ((\Drupal::time()->getCurrentTime() - \Drupal::time()->getRequestTime()) > static::$maxExecTime) { $event->getMigration()->interruptMigration(MigrationInterface::RESULT_INCOMPLETE); } } diff --git a/core/modules/views/src/ViewExecutable.php b/core/modules/views/src/ViewExecutable.php index 9ee37041c0..716067623e 100644 --- a/core/modules/views/src/ViewExecutable.php +++ b/core/modules/views/src/ViewExecutable.php @@ -1686,7 +1686,7 @@ public function preExecute($args = []) { \Drupal::moduleHandler()->invokeAll('views_pre_view', [$this, $display_id, &$this->args]); // Allow hook_views_pre_view() to set the dom_id, then ensure it is set. - $this->dom_id = !empty($this->dom_id) ? $this->dom_id : hash('sha256', $this->storage->id() . REQUEST_TIME . mt_rand()); + $this->dom_id = !empty($this->dom_id) ? $this->dom_id : hash('sha256', $this->storage->id() . \Drupal::time()->getRequestTime() . mt_rand()); // Allow the display handler to set up for execution $this->display_handler->preExecute();