/** * Obtains an instance of `LocalDateRange` from a set of date-time fields. * * This method is only useful to parsers. * * @param DateTimeParseResult $result * * @return LocalDateRange * * @throws DateTimeException If the date range is not valid. * @throws DateTimeParseException If required fields are missing from the result. */ public static function from(DateTimeParseResult $result) { $startDate = LocalDate::from($result); if ($result->hasField(Field\MonthOfYear::NAME)) { if ($result->hasField(Field\Year::NAME)) { $endDate = LocalDate::from($result); } else { $endDate = MonthDay::from($result)->atYear($startDate->getYear()); } } else { $endDate = $startDate->withDay((int) $result->getField(Field\DayOfMonth::NAME)); } return LocalDateRange::of($startDate, $endDate); }
/** * @param DateTimeParseResult $result * * @return LocalDateTime * * @throws DateTimeException If the date-time is not valid. * @throws DateTimeParseException If required fields are missing from the result. */ public static function from(DateTimeParseResult $result) { return new LocalDateTime(LocalDate::from($result), LocalTime::from($result)); }
/** * Obtains an instance of `LocalDate` from a text string. * * @param string $text The text to parse, such as `2007-12-03`. * @param DateTimeParser|null $parser The parser to use, defaults to the ISO 8601 parser. * * @return LocalDate * * @throws DateTimeException If the date 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::localDate(); } return LocalDate::from($parser->parse($text)); }