/** * @dataProvider data_week */ public function test_WOWBY(LocalDate $date, DayOfWeek $dow, $week, $wby) { $weekDef = WeekFields::ISO(); $dowField = $weekDef->dayOfWeek(); $wowbyField = $weekDef->weekOfWeekBasedYear(); $yowbyField = $weekDef->weekBasedYear(); $this->assertEquals($date->get($dowField), $dow->getValue()); $this->assertEquals($date->get($wowbyField), $week); $this->assertEquals($date->get($yowbyField), $wby); }
/** * @dataProvider data_week */ public function test_parse_weeks_LENIENT($date, DayOfWeek $dow, $week, $wby) { $f = (new DateTimeFormatterBuilder())->appendValue(IsoFields::WEEK_BASED_YEAR())->appendLiteral('-')->appendValue(IsoFields::WEEK_OF_WEEK_BASED_YEAR())->appendLiteral('-')->appendValue(CF::DAY_OF_WEEK())->toFormatter()->withResolverStyle(ResolverStyle::LENIENT()); $parsed = LocalDate::parseWith($wby . "-" . $week . "-" . $dow->getValue(), $f); $this->assertEquals($parsed, $date); }
/** * Returns the previous-or-same day-of-week adjuster, which adjusts the date to the * first occurrence of the specified day-of-week before the date being adjusted * unless it is already on that day in which case the same object is returned. * <p> * The ISO calendar system behaves as follows:<br> * The input 2011-01-15 (a Saturday) for parameter (MONDAY) will return 2011-01-10 (five days earlier).<br> * The input 2011-01-15 (a Saturday) for parameter (WEDNESDAY) will return 2011-01-12 (three days earlier).<br> * The input 2011-01-15 (a Saturday) for parameter (SATURDAY) will return 2011-01-15 (same as input). * <p> * The behavior is suitable for use with most calendar systems. * It uses the {@code DAY_OF_WEEK} field and the {@code DAYS} unit, * and assumes a seven day week. * * @param DayOfWeek $dayOfWeek the day-of-week to check for or move the date to, not null * @return TemporalAdjuster the previous-or-same day-of-week adjuster, not null */ public static function previousOrSame(DayOfWeek $dayOfWeek) { $dowValue = $dayOfWeek->getValue(); return self::fromCallable(function (Temporal $temporal) use($dowValue) { $calDow = $temporal->get(ChronoField::DAY_OF_WEEK()); if ($calDow == $dowValue) { return $temporal; } $daysDiff = $dowValue - $calDow; return $temporal->minus($daysDiff >= 0 ? 7 - $daysDiff : -$daysDiff, ChronoUnit::DAYS()); }); }