Parse a supplied timezone.

@param  string|DateTimeZone  $timezone
@return DateTimeZone
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;
        }
1363326
public function setTimezone($timezone)
    {
        try {
            $test = new \DateTimeZone($timezone);
            $this->timezone = $timezone;
        } catch (\Exception $e) {
            throw new MonetivoException('Invalid 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);
    }
964884
private static function _createTimeZone(string $tz): \DateTimeZone
    {
        try {
            return new \DateTimeZone($tz);
        } catch (\Exception $e) {
            throw new \UnexpectedValueException("Invalid timezone.", 0, $e);
        }
    }
461213
public static function parse(string $text, DateTimeParser $parser = null) : TimeZone
    {
        if (! $parser) {
            $parser = IsoParsers::timeZoneRegion();
        }

        return TimeZoneRegion::from($parser->parse($text));
    }
852466
public static function getMidnightTimestampTomorrow($timezone=null)
	{
		$tz = new DateTimeZone($timezone !== null ? $timezone : Yii::$app->timeZone);

		$dt = new DateTime('now', $tz);
		$dt->modify('+1 day');
		$dt->setTime(0, 0, 0);

		return $dt->getTimestamp() + $dt->getOffset();
	}
1368710
public static function defaultTimezone($timezone = null)
	{
		if ($timezone !== null)
		{
			if ( ! $timezone instanceOf DateTimeZone)
			{
				$timezone = new DateTimeZone($timezone);
			}

			static::$defaultTimezone = $timezone;
		}

		return static::$defaultTimezone;
	}
992222
public function setStartTimezone($timezone = 'UTC')
	{
		$this->start->setTimezone(is_string($timezone) ? new \DateTimeZone($timezone) : $timezone);
		return $this;
	}
1372663
public function dueEvents(\DateTimeZone $timeZone)
    {
        return \array_filter(
            $this->events,
            function (Event $event) use ($timeZone) {
                return $event->isDue($timeZone);
            }
        );
    }
745840
public function toDateTime(DateTimeZone $timeZone = null): DateTime
    {
        $date = \DateTime::createFromFormat('U.u', $this->sec . '.' . $this->uSec);
        if ($timeZone) {
            $date->setTimezone($timeZone);
        }

        return $date;
    }
1229941
public function setDateByTimezone($timezone) {
    if (is_string($timezone)) {
      $timezone = new DateTimeZone($timezone);
    }
    $this->convertedTimezone = $timezone;
    $new_date_utc = $this->date->getTimeStamp() - $timezone->getOffset($this->date);
    $new_date = new DateTime();
    $new_date->setTimestamp($new_date_utc);
    $this->date = $new_date;
  }
1231037
public static function setTimeZone($timezone)
    {
        if (self::_validateTimezone($timezone)) {
            self::$timezone = $timezone;
            return true;
        }
        return false;
    }
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;
    }
326595
public function format(string $format, TimeZone $timeZone = null): string
    {
        if (null !== $timeZone) {
            return $timeZone->translate($this)->format($format);
        }

        return $this->dateTime->format($format);
    }
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;
    }
538033
protected function getDateTimeInstance(string $time = "now", \DateTimeZone $timezone = null): DateTime
    {
        $result = new self::$_dateTimeClassName($time, $timezone);

        return $result;
    }
1169003
protected function getInternalFormatter($timezone = null)
    {
        if (!$timezone) {
            $timezone = date_default_timezone_get(); // Default to server timezone
        }

        $formatter = IntlDateFormatter::create(
            DBDate::ISO_LOCALE,
            IntlDateFormatter::MEDIUM,
            IntlDateFormatter::MEDIUM,
            $timezone
        );
        $formatter->setLenient(false);

        // Note we omit timezone from this format, and we always assume server TZ
        $formatter->setPattern(DBDatetime::ISO_DATETIME);

        return $formatter;
    }