You are here

class TimestampNormalizer in Drupal 10

Same name and namespace in other branches
  1. 8 core/modules/serialization/src/Normalizer/TimestampNormalizer.php \Drupal\serialization\Normalizer\TimestampNormalizer
  2. 9 core/modules/serialization/src/Normalizer/TimestampNormalizer.php \Drupal\serialization\Normalizer\TimestampNormalizer

Converts values for the Timestamp data type to and from common formats.

@internal

Note that \Drupal\Core\TypedData\Plugin\DataType\Timestamp::getDateTime() explicitly sets a default timezone of UTC. This ensures the string representation generated by DateTimeNormalizer::normalize() is also in UTC.

Hierarchy

  • class \Drupal\serialization\Normalizer\TimestampNormalizer extends \Drupal\serialization\Normalizer\DateTimeNormalizer

Expanded class hierarchy of TimestampNormalizer

1 file declares its use of TimestampNormalizer
TimestampNormalizerTest.php in core/modules/serialization/tests/src/Unit/Normalizer/TimestampNormalizerTest.php
1 string reference to 'TimestampNormalizer'
serialization.services.yml in core/modules/serialization/serialization.services.yml
core/modules/serialization/serialization.services.yml
1 service uses TimestampNormalizer
serializer.normalizer.timestamp in core/modules/serialization/serialization.services.yml
Drupal\serialization\Normalizer\TimestampNormalizer

File

core/modules/serialization/src/Normalizer/TimestampNormalizer.php, line 16

Namespace

Drupal\serialization\Normalizer
View source
class TimestampNormalizer extends DateTimeNormalizer {

  /**
   * {@inheritdoc}
   */
  protected $allowedFormats = [
    'UNIX timestamp' => 'U',
    'ISO 8601' => \DateTime::ISO8601,
    'RFC 3339' => \DateTime::RFC3339,
  ];

  /**
   * {@inheritdoc}
   */
  protected $supportedInterfaceOrClass = Timestamp::class;

  /**
   * {@inheritdoc}
   */
  protected function getNormalizationTimezone() {
    return new \DateTimeZone('UTC');
  }

  /**
   * {@inheritdoc}
   */
  public function denormalize($data, $class, $format = NULL, array $context = []) : mixed {
    $denormalized = parent::denormalize($data, $class, $format, $context);
    return $denormalized
      ->getTimestamp();
  }

}

Members