/** * Obtains an instance of `LocalDateTime` from a text string. * * @param string $text The text to parse, such as `2007-12-03T10:15:30`. * @param DateTimeParser|null $parser The parser to use, defaults to the ISO 8601 parser. * * @return LocalDateTime * * @throws DateTimeException If the date-time is not valid. * @throws DateTimeParseException If the text string does not follow the expected format. */ public static function parse($text, DateTimeParser $parser = null) { if (!$parser) { $parser = IsoParsers::localDateTime(); } return LocalDateTime::from($parser->parse($text)); }
/** * Obtains an instance of `ZonedDateTime` from a set of date-time fields. * * This method is only useful to parsers. * * @param DateTimeParseResult $result * * @return ZonedDateTime * * @throws DateTimeException If the zoned date-time is not valid. * @throws DateTimeParseException If required fields are missing from the result. */ public static function from(DateTimeParseResult $result) { $localDateTime = LocalDateTime::from($result); $timeZoneOffset = TimeZoneOffset::from($result); if ($result->hasField(Field\TimeZoneRegion::NAME)) { $timeZone = TimeZoneRegion::from($result); } else { $timeZone = $timeZoneOffset; } return ZonedDateTime::of($localDateTime, $timeZone, $timeZoneOffset); }