Create a DateTime instance from a specific format.

@param string        $format
@param string        $time
@param \DateTimeZone $timezone
@throws \Beluga\ArgumentError
@return \Beluga\Date\DateTime
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() )
      );

   }
1119756
public static function newPhpDateTime($time = 'now', \DateTimeZone $timeZone = null)
    {
        if (self::DATETIME_SYSTEM === self::$type) {
            return new \DateTime($time, $timeZone);
        }

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

        $date = self::getFixedTimeFromConfiguredTimestamp($time, $timeZone);
        $timePassedSinceInstantiation = abs(
            (new \DateTime())->getTimestamp() - self::$offsetTimestamp
        );
        $date->modify("+{$timePassedSinceInstantiation} seconds");

        return $date;
    }
754308
public static function now(string $format = 'Y-m-d H:i:s', string $timezone = null): string
    {
        return self::factory(null, $timezone)->format($format);
    }
763497
public static function parseFormat($format,$datetime, $timezone = null) {
        $datetime = str_replace(self::$monthYear, range(1,12), $datetime);

        $parse = date_parse_from_format($format, $datetime);
        if($parse['error_count'] == 0 && self::isValideDate($parse['year'], $parse['month'], $parse['day']) && self::isValideTime($parse['hour'], $parse['minute'], $parse['second'])){
            list($year, $month, $day) = self::getGregorian($parse['year'], $parse['month'], $parse['day']);
            list($hour,$minute, $second) = array($parse['hour'], $parse['minute'], $parse['second']);

            $timezone = self::createTimeZone($timezone);
            $datetime = new DateTime(sprintf('%04s-%02s-%02s %02s:%02s:%02s', $year, $month, $day, $hour, $minute, $second), $timezone);
            return new static($datetime);
        }
        else{
            throw new InvalidArgumentException(sprintf("Unknown datetime '%s'", $datetime));
        }
    }
1123756
public static function parse($format, $time, DateTimeZone $timezone =  NULL) {
    if (!isset($timezone) && date_default_timezone_get() != '') {
      $timezone = new DateTimeZone(date_default_timezone_get());
    }

    $ret = parent::createFromFormat($format, $time, $timezone);
    
    if ($ret === FALSE) {
      throw new ParsingException('Aus '.Code::varInfo($time).' konnte keinen Zeitinformationen des Formates: '.Code::varInfo($format).' geparst werden. lastErrors: '.print_r(DateTime::getLastErrors(),true));
    }
    
    return new static($ret);
  }
592753
public static function create($time = null)
    {

        if (is_string($time)) {
            $time = new self($time);
        }

        if (self::isNullOrBnDateTimeObject($time)) {
            return $time;
        }

        $dateTime = new self();

        if ($time instanceof \DateTime) {
            $dateTime->setTimezone($time->getTimezone());
            $time = $time->getTimestamp();
        }

        $dateTime->setTimestamp($time);
        return $dateTime;
    }
1109558
public static function from($time): self
	{
		if ($time instanceof DateTimeInterface) {
			return new static($time->format('Y-m-d H:i:s'), $time->getTimezone());

		} elseif (is_numeric($time)) {
			return (new static('@' . $time))
				->setTimezone(new DateTimeZone(date_default_timezone_get()));

		} else {
			return new static($time);
		}
	}
449248
protected static function dateFormatter($dateTime, $format)
    {
        if (is_numeric($dateTime)) {
            return gmdate($format, (int) $dateTime);
        }

        if (is_string($dateTime)) {
            $dateTime = new \DateTime($dateTime);
        }

        if ($dateTime instanceof \DateTime) {
            return $dateTime->setTimezone(self::getUtcTimeZone())->format($format);
        }

        throw new InvalidArgumentException('Date/Time values must be either a string, integer, or DateTime object');
    }
682656
public static function FromDateTime( \DateTime $dt ) : DateTime
   {

      if ( $dt instanceof DateTime )
      {
         return clone $dt;
      }

      return new DateTime( $dt->format('Y-m-d H:i:s.u'), $dt->getTimezone() );

   }
80215
public static function fromString($date, $format = \DateTime::ATOM)
    {
        $dateTime = \DateTime::createFromFormat($format, $date);

        $err = \DateTime::getLastErrors();
        if ($err['error_count'] > 0) {
            throw new \InvalidArgumentException(sprintf('%s', implode(', ', $err['errors'])));
        }

        return self::fromDateTime($dateTime);
    }
605471
public static function createFromFormat($time, $format = null)
    {
        if (self::isNull($format)) {
            $format = self::$defaultFormat;
        }

        try {
            $date = \DateTime::createFromFormat($format, $time);
            if (!$date) {
                throw new DateTimeObjectException(DateTimeObjectException::MSG_UNABLE_TO_CREATE_FROM_FORMAT);
            }
        } catch (\Exception $e) {
            throw new DateTimeObjectException(DateTimeObjectException::MSG_UNABLE_TO_CREATE_FROM_FORMAT);
        }

        try {
            $dt = new DateTimeObject();
            $dt->setTimestamp($date->getTimestamp());
            $dt->setFormat($format);
        } catch (\Exception $e) {
            throw new DateTimeObjectException(DateTimeObjectException::MSG_UNABLE_TO_CREATE_FROM_FORMAT);
        }

        return $dt;
    }
19648
public function convertTo($zone, $format = null)
    {
        Argument::i()
            //argument 1 must be a string
            ->test(1, 'string')
            //argument 1 must be a timezone identifier
            ->test(1, 'location', 'utc', 'abbr')
            //argument 2 must be a string or null
            ->test(2, 'string', 'null');

        $time = $this->time + $this->calculateOffset($zone);

        if (!is_null($format)) {
            return date($format, $time);
        }

        return $time;
    }
931226
public static function createTimestamp($time = null, $format = 'Y-m-d H:i:s') {
        if ($time === null) {
            $time = time();
        }
        elseif (is_string($time)) {
            $time = strtotime($time);
        }

        return date($format, $time);
    }
411735
public static function createFromFormat($format, $time, $tz = null)
    {
        if ($tz !== null) {
            $dt = parent::createFromFormat($format, $time, static::safeCreateDateTimeZone($tz));
        } else {
            $dt = parent::createFromFormat($format, $time);
        }

        if ($dt instanceof DateTimeInterface) {
            return static::instance($dt);
        }

        $errors = static::getLastErrors();
        throw new InvalidArgumentException(implode(PHP_EOL, $errors['errors']));
    }
677607
public static function date($format = "Y-m-d H:i:s", $time = null)
    {
        $time = ($time !== null ? $time : time());

        if (!is_numeric($time)) {
            $time = static::toUnix($time);
        }

        return date($format, $time);
    }
1195047
public function timeAgo($time = null, $format = null) {
		if (empty($time)) {
			$time = time();
		}
		if (empty($format)) {
			$format = '%x %X';
		}

		$result = $this->Html->tag(
			'time',
			$this->Time->i18nFormat($time, $format),
			[
				'data-toggle' => 'timeago',
				'datetime' => date('c', $this->Time->fromString($time)),
				'class' => 'help'
			]
		);

		return $result;
	}
1104462
public function toDateTime(\DateTimeZone $timeZone = null): \DateTime
    {
        if (null === self::$utc) {
            self::$utc = new \DateTimeZone('UTC');
        }

        $date = \DateTime::createFromFormat('!Y-m-d', $this->getValue(), $timeZone ?: self::$utc);
        if (!$date instanceof \DateTime) {
            $date = \DateTime::createFromFormat('!Y-m-d', (new \DateTime())->format('Y-m-d'), $timeZone ?: self::$utc);
        }

        if ($date->getOffset() !== 0) {
            $date->setTimezone(self::$utc);
        }

        return $date;
    }