Creates a DateTimeZone from a string or a DateTimeZone.

@link  \Carbon\Carbon::safeCreateDateTimeZone($object);

@param DateTimeZone|string|null $timezone

@return DateTimeZone
@throws \Xloit\DateTime\Exception\InvalidArgumentException
516079
public static function safeCreateDateTimeZone($timezone)
    {
        if ($timezone === null) {
            // Don't return null...
            return new DateTimeZone(date_default_timezone_get());
        }

        if ($timezone instanceof DateTimeZone) {
            return $timezone;
        }

        /** @noinspection PhpUsageOfSilenceOperatorInspection */
        $tz = @timezone_open((string) $timezone);

        if ($tz === false) {
            throw new Exception\InvalidArgumentException(
                sprintf(
                    'Unknown or bad timezone (%s)', is_scalar($timezone) ? $timezone : gettype($timezone)
                )
            );
        }

        return $tz;
    }
281445
public static function now(DateTimeZone $dateTimeZone = null)
    {
        $dateTimeZone = self::checkForDateTimeZone($dateTimeZone);

        return new DateTime(
            new GregorianDateTime('now', $dateTimeZone)
        );
    }
1276112
public static function isValidTimezone($timezone) {
		static $cache;
		if (!is_array($cache)) {
			$cache = array();
		}
		
		if (isset($cache[$timezone])) {
			return $cache[$timezone];
		}
		
		$cache[$timezone] = in_array($timezone, DateTimeZone::listIdentifiers());
		return $cache[$timezone];
	}
194928
public static function FromFormat( string $format, $time, \DateTimeZone $timezone = null ) : DateTime
   {
      if ( $timezone !== null )
      {
         $dt = parent::createFromFormat( $format, $time, $timezone );
      }
      else
      {
         $dt = parent::createFromFormat( $format, $time );
      }

      if ( $dt instanceof \DateTime )
      {
         return static::FromDateTime( $dt );
      }

      throw new ArgumentError(
         'format',
         $format,
         'Date',
         \join( \PHP_EOL, parent::getLastErrors() )
      );

   }
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);
    }
484435
public static function newImmutablePhpDateTime($time = 'now', \DateTimeZone $timeZone = null)
    {
        if (self::DATETIME_SYSTEM === self::$type) {
            return new \DateTimeImmutable($time, $timeZone);
        }

        if (self::DATETIME_FIXED === self::$type) {
            return self::getFixedImmutableTimeFromConfiguredTimestamp($time, $timeZone);
        }

        $date = self::getFixedImmutableTimeFromConfiguredTimestamp($time, $timeZone);
        $timePassedSinceInstantiation = abs(
            (new \DateTime())->getTimestamp() - self::$offsetTimestamp
        );

        return $date->modify("+{$timePassedSinceInstantiation} seconds");
    }
1330781
public function setTimezone($timezone)
    {
        if ($this->config->getTimezone() !== $timezone) {
            $this->config->setTimezone($timezone);
            if ($this->connection) {
                $this->query('SET time_zone = ?', [$timezone]);
            }
        }

        return $this;
    }
1152735
public static function timezone($timezone = null): \DateTimeZone
    {
        if ($timezone instanceof DateTimeZone) {
            return $timezone;
        }

        $timezone = $timezone ?: date_default_timezone_get();

        return new DateTimeZone($timezone);
    }
939306
public static function createFromFormat($format, $datetime, $timezone = null)
    {
        $datepoint = parent::createFromFormat($format, $datetime, $timezone);
        if (false !== $datepoint) {
            return self::create($datepoint);
        }

        return $datepoint;
    }
252506
public function buildTimeZone()
    {
        if (null !== $this->value) {
            $sign = $this->value < 0 ? '-' : '+';
            $value = intval(abs($this->value) / 60);

            return new DateTimeZone(sprintf(
                '%s%02d:%02d',
                $sign,
                intval($value / 60),
                $value % 60
            ));
        }
    }
1363326
public function setTimezone($timezone)
    {
        try {
            $test = new \DateTimeZone($timezone);
            $this->timezone = $timezone;
        } catch (\Exception $e) {
            throw new MonetivoException('Invalid timezone');
        }
    }
113278
public static function createFromFormat($format, $time, $object = null)
    {
        if ($object !== null) {
            $dt = parent::createFromFormat($format, $time, $object);
        } else {
            $dt = parent::createFromFormat($format, $time);
        }

        $lastErrors = parent::getLastErrors();

        if ($dt instanceof \DateTimeInterface) {
            return new static($dt->format('Y-m-d H:i:s.u'), $dt->getTimezone());
        }

        throw new \InvalidArgumentException(implode(PHP_EOL, $lastErrors['errors']));
    }
959176
public static function today($timeZone = null)
    {
        $now = null;
        $before = null;
        if ( is_null($timeZone) ) {
            $now = new \DateTime();
            $before = new \DateTime();
        } else {
            $now = new \DateTime( "now", new \DateTimeZone( $timeZone ) );
            $before = new \DateTime( "now", new \DateTimeZone( $timeZone ) );
        }
        $range = new Range( $before, $now );

        return $range;
    }
778980
protected function parseSuppliedTimezone($timezone)
        {
            if ($timezone instanceof \DateTimeZone or is_null($timezone)) {
                return $timezone;
            }

            try {
                $timezone = new \DateTimeZone($timezone);
            }
            catch (Exception $error) {
                throw new \InvalidArgumentException('The supplied timezone [' . $timezone . '] is not supported.');
            }

            return $timezone;
        }
186052
public static function fromString(string $dateTimeString, string $timeZoneId) : TimezonedDateTime
    {
        return new self(new \DateTimeImmutable($dateTimeString, new \DateTimeZone($timeZoneId)));
    }
297790
public function getDeletedAtDatetime(string $timezone = null): ?DateTime
    {
        if (!$this->isDeleted()) {
            return null;
        }

        return new DateTime($this->getDeletedAt(), new DateTimeZone($timezone ?? 'UTC'));
    }
1134925
public static function toDateTime($value, DateTimeZone $dtz) : DateTime
    {
        // TODO : Figure out where to move. Can vary for different source objects
        $format = 'Y-m-d H:i:s';

        if (is_string($value)) {
            $val = strtotime($value);
            if (false === $val) {
                throw new InvalidArgumentException(sprintf('Unsupported datetime string provided: %s', $value));
            }

            return new DateTime(date($format, $val), $dtz);
        }

        if ($value instanceof Time) {
            $value = $value->format($format);

            return new DateTime($value, $dtz);
        }

        if ($value instanceof DateTime) {
            return $value;
        }

        throw new InvalidArgumentException("Type [" . gettype($value) . "] is not supported for date/time");
    }