Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x

TimestampNormalizer.php

Namespace

Drupal\serialization\Normalizer

File

core/modules/serialization/src/Normalizer/TimestampNormalizer.php

View source
<?php

namespace Drupal\serialization\Normalizer;

use Drupal\Core\TypedData\Plugin\DataType\Timestamp;

/**
 * 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.
 */
class TimestampNormalizer extends DateTimeNormalizer {
    
    /**
     * {@inheritdoc}
     */
    protected $allowedFormats = [
        'UNIX timestamp' => 'U',
        'ISO 8601' => \DateTime::ISO8601,
        'RFC 3339' => \DateTime::RFC3339,
    ];
    
    /**
     * {@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();
    }
    
    /**
     * {@inheritdoc}
     */
    public function getSupportedTypes(?string $format) : array {
        return [
            Timestamp::class => TRUE,
        ];
    }

}

Classes

Title Deprecated Summary
TimestampNormalizer Converts values for the Timestamp data type to and from common formats.

API Navigation

  • Drupal Core 11.1.x
  • Topics
  • Classes
  • Functions
  • Constants
  • Globals
  • Files
  • Namespaces
  • Deprecated
  • Services
RSS feed
Powered by Drupal