/** * Obtains an instance of `LocalDate` from a text string. * * @param string $text The text to parse, such as `--12-03`. * @param DateTimeParser|null $parser The parser to use, defaults to the ISO 8601 parser. * * @return MonthDay * * @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::monthDay(); } return MonthDay::from($parser->parse($text)); }
/** * 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); }
/** * @dataProvider providerAtMonthDay * * @param integer $year The base year. * @param integer $month The month-of-year of the month-day to apply. * @param integer $day The day-of-month of the month-day to apply. * @param integer $expectedDay The expected day of the resulting date. */ public function testAtMonthDay($year, $month, $day, $expectedDay) { $monthDay = MonthDay::of($month, $day); $this->assertLocalDateIs($year, $month, $expectedDay, Year::of($year)->atMonthDay($monthDay)); }
/** * @dataProvider providerToString * * @param integer $month The month of the month-day to test. * @param integer $day The day of the month-day to test. * @param string $string The expected result string. */ public function testToString($month, $day, $string) { $this->assertSame($string, (string) MonthDay::of($month, $day)); }
/** * Combines this Year with a MonthDay to create a LocalDate. * * A month-day of February 29th will be adjusted to February 28th * in the resulting date if the year is not a leap year. * * @param MonthDay $monthDay The month-day to use. * * @return LocalDate */ public function atMonthDay(MonthDay $monthDay) { return $monthDay->atYear($this->year); }
/** * @param integer $month The expected month. * @param integer $day The expected day. * @param MonthDay $monthDay The month-day to test. */ protected function assertMonthDayIs($month, $day, MonthDay $monthDay) { $this->compare([$month, $day], [$monthDay->getMonth(), $monthDay->getDay()]); }