Returns a preconfigured IntlDateFormatter instance.

@param bool $ignoreTimezone use UTC regardless of the configured timezone

@throws TransformationFailedException in case the date formatter can not be constructed
965298
protected function getIntlDateFormatter(bool $ignoreTimezone = false): \IntlDateFormatter
    {
        $dateFormat = $this->dateFormat;
        $timeFormat = $this->timeFormat;
        $timezone = $ignoreTimezone ? 'UTC' : $this->outputTimezone;
        if (class_exists('IntlTimeZone', false)) {
            // see https://bugs.php.net/bug.php?id=66323
            $timezone = \IntlTimeZone::createTimeZone($timezone);
        }
        $calendar = $this->calendar;
        $pattern = $this->pattern;

        $intlDateFormatter = new \IntlDateFormatter(\Locale::getDefault(), $dateFormat, $timeFormat, $timezone, $calendar);

        // new \IntlDateFormatter may return null instead of false in case of failure, see https://bugs.php.net/bug.php?id=66323
        if (!$intlDateFormatter) {
            throw new TransformationFailedException(intl_get_error_message(), intl_get_error_code());
        }

        if ($pattern) {
            $intlDateFormatter->setPattern($pattern);
        }

        $intlDateFormatter->setLenient(false);

        return $intlDateFormatter;
    }
297790
public function getDeletedAtDatetime(string $timezone = null): ?DateTime
    {
        if (!$this->isDeleted()) {
            return null;
        }

        return new DateTime($this->getDeletedAt(), new DateTimeZone($timezone ?? 'UTC'));
    }
1162736
public static function getDateFormat($noTime = false, $useTimezone = false, $useUtc = false)
    {
        // Do not use UTC time (Z) if timezone support is enabled.
        if ($useTimezone || !$useUtc) {
            return $noTime ? 'Ymd' : 'Ymd\THis';
        }

        return $noTime ? 'Ymd' : 'Ymd\THis\Z';
    }
127180
public function setTimeZone($timeZone)
    {
        if ($timeZone instanceof \IntlTimeZone) {
            $timeZone = $timeZone->getID();
        }

        if ($timeZone instanceof \DateTimeZone) {
            $timeZone = $timeZone->getName();

            // DateTimeZone returns the GMT offset timezones without the leading GMT, while our parsing requires it.
            if (!empty($timeZone) && ('+' === $timeZone[0] || '-' === $timeZone[0])) {
                $timeZone = 'GMT'.$timeZone;
            }
        }

        return $this->setTimeZoneId($timeZone);
    }
1042338
protected function getInternalFormatter()
    {
        $formatter = IntlDateFormatter::create(
            DBDate::ISO_LOCALE,
            IntlDateFormatter::MEDIUM,
            IntlDateFormatter::NONE
        );
        $formatter->setLenient(false);
        // CLDR ISO 8601 date.
        $formatter->setPattern(DBDate::ISO_DATE);
        return $formatter;
    }
44936
private function to8601Utc(\DateTimeInterface $dateTime): string
    {
        // Convert to UTC if we can
        if ($dateTime instanceof DateTime) {
            $dateTime->setTimezone(new \DateTimeZone('UTC'));
        } elseif ($dateTime instanceof \DateTimeImmutable && $dateTime->getOffset() > 0) {
            // Some implementations will be wrong since we're effectively dropping the offset from the datetime format
            // and this interface doesn't allow us to make modifications so we reject the timestamp altogether to ensure
            // only correct timestamps make it through.
            throw new InvalidArgumentException('Timestamp must be UTC');
        }

        // PHP's default 8601 format contains a portion that includes the timezone offset.  The Mailbox API doesn't
        // like the offset and requires a timestamp without it, thus requiring all inbound timestamps to be UTC
        return $dateTime->format('Y-m-d\TH:i:s\Z');
    }
1128581
protected function calculateTimezone($timezone)
    {
        if (!isset($timezone)) {
            return null;
        }
        $key = $timezone instanceof \DateTimeZone ? $timezone->getName() : $timezone;
        // lazy loading
        if (!isset(static::$timezonesInstances[$key])) {
            static::$timezonesInstances[$key] = is_string($timezone) ? new \DateTimezone($timezone) : $timezone;
        }
        return static::$timezonesInstances[$key];
    }
1041556
public static function ofInstant(Instant $instant, TimeZone $timeZone) : ZonedDateTime
    {
        $dateTimeZone = $timeZone->toDateTimeZone();

        // We need to pass a DateTimeZone to avoid a PHP warning...
        $dateTime = new \DateTime('@' . $instant->getEpochSecond(), $dateTimeZone);

        // ... but this DateTimeZone is ignored because of the timestamp, so we set it again.
        $dateTime->setTimezone($dateTimeZone);

        $localDateTime = LocalDateTime::parse($dateTime->format('Y-m-d\TH:i:s'));
        $localDateTime = $localDateTime->withNano($instant->getNano());

        if ($timeZone instanceof TimeZoneOffset) {
            $timeZoneOffset = $timeZone;
        } else {
            $timeZoneOffset = TimeZoneOffset::ofTotalSeconds($dateTime->getOffset());
        }

        return new ZonedDateTime($localDateTime, $timeZoneOffset, $timeZone, $instant);
    }
923755
public function transform($value)
    {
        if (null === $value) {
            return null;
        }

        if (!$value instanceof APIRoleAssignment) {
            throw new TransformationFailedException('Expected a ' . APIRoleAssignment::class . ' object.');
        }

        return $value->id;
    }
1093484
public function reverseTransform($value)
    {
        if (empty($value)) {
            return null;
        }

        if (!is_numeric($value)) {
            throw new TransformationFailedException(
                sprintf('Expected a numeric, got %s instead', gettype($value))
            );
        }

        return Value::fromTimestamp($value);
    }
612459
public function withTimezone($timezone): DateTime
    {
        return new static(
            $this->date(),
            $this->time(),
            Timezone::create($timezone)
        );
    }
101615
public static function now(?string $timezone = null): Date
    {
        $timezone = $timezone ?: date_default_timezone_get();
        assert(Validate::isTimezone($timezone), sprintf('Invalid timezone: %s', $timezone));

        $dateTime = new DateTimeImmutable('now', new DateTimeZone($timezone));
        $year = (int) $dateTime->format('Y');
        $month = (int) $dateTime->format('n');
        $day = (int) $dateTime->format('j');

        return new static($year, $month, $day);
    }
82013
public function withTimeZone(\DateTimeZone $timezone) : self
    {
        if ($this->timezone()->getName() === $timezone->getName()) {
            return $this;
        }

        $instance = clone $this;

        $instance->date->setTimezone($timezone);

        return $instance;
    }
248442
public function setTimezone($timezone)
    {
        if (self::$_immutable) {
            self::$_immutable = false;
            $newDate = clone $this;
            $newDate->setTimezone($timezone);
            self::$_immutable = true;

            return $newDate;
        } else {
            return parent::setTimezone($timezone);
        }
    }
837365
public function formatDate($date, $pattern = null, $locale = null, $timezone = null, $dateType = null)
    {
        if ($pattern) {
            return $this->helper->format($date, $pattern, $locale, $timezone);
        }

        return $this->helper->formatDate($date, $locale, $timezone, $dateType);
    }
964884
private static function _createTimeZone(string $tz): \DateTimeZone
    {
        try {
            return new \DateTimeZone($tz);
        } catch (\Exception $e) {
            throw new \UnexpectedValueException("Invalid timezone.", 0, $e);
        }
    }
1163434
public function transform($dateTime)
    {
        if (null === $dateTime) {
            return '';
        }

        if (!$dateTime instanceof \DateTime) {
            throw new TransformationFailedException('Expected a \DateTime.');
        }

        // convert time to UTC before passing it to the formatter
        $dateTime = clone $dateTime;
        $value = $this->getIntlDateFormatter()->format((int) $dateTime->format('U'));

        return $value;
    }