/**
  * Gets the printerParser to use based on the field and the locale.
  *
  * @param Locale $locale the locale to use, not null
  * @return DateTimePrinterParser the formatter, not null
  * @throws IllegalArgumentException if the formatter cannot be found
  */
 private function printerParser(Locale $locale)
 {
     $weekDef = WeekFields::ofLocale($locale);
     $field = null;
     switch ($this->chr) {
         case 'Y':
             $field = $weekDef->weekBasedYear();
             if ($this->count === 2) {
                 return new ReducedPrinterParser($field, 2, 2, 0, ReducedPrinterParser::BASE_DATE(), 0);
             } else {
                 return new NumberPrinterParser($field, $this->count, 19, $this->count < 4 ? SignStyle::NORMAL() : SignStyle::EXCEEDS_PAD(), -1);
             }
         case 'e':
         case 'c':
             $field = $weekDef->dayOfWeek();
             break;
         case 'w':
             $field = $weekDef->weekOfWeekBasedYear();
             break;
         case 'W':
             $field = $weekDef->weekOfMonth();
             break;
         default:
             throw new IllegalStateException("unreachable");
     }
     return new NumberPrinterParser($field, $this->count == 2 ? 2 : 1, 2, SignStyle::NOT_NEGATIVE());
 }
 /**
  * @dataProvider provider_patternLocalWeekBasedYearDate
  */
 public function test_print_WeekBasedYear($pattern, $expectedText, LocalDate $date)
 {
     $dtf = DateTimeFormatter::ofPatternLocale($pattern, $this->locale);
     $result = $dtf->format($date);
     $weekDef = WeekFields::ofLocale($this->locale);
     $this->assertEquals($result, $expectedText, "Incorrect formatting for " - $pattern - ", weekDef: " . $weekDef);
 }
Пример #3
0
 public function test_equals()
 {
     $weekDef_iso = WeekFields::ISO();
     $weekDef_sundayStart = WeekFields::SUNDAY_START();
     $this->assertTrue($weekDef_iso->equals(WeekFields::of(DayOfWeek::MONDAY(), 4)));
     $this->assertTrue($weekDef_sundayStart->equals(WeekFields::of(DayOfWeek::SUNDAY(), 1)));
     //$this->assertEquals($weekDef_iso->hashCode(), WeekFields::of(DayOfWeek::MONDAY(), 4)->hashCode());
     // $this->assertEquals($weekDef_sundayStart->hashCode(), WeekFields::of(DayOfWeek::SUNDAY(), 1)->hashCode());
     $this->assertFalse($weekDef_iso->equals($weekDef_sundayStart));
     //$this->assertNotEquals($weekDef_iso->hashCode(), $weekDef_sundayStart->hashCode());
 }
Пример #4
0
 public function test_nullWeekFields_week_based_year()
 {
     TestHelper::assertNullException($this, function () {
         $weekOfYearField = WeekFields::SUNDAY_START()->weekOfYear();
         $weekOfYearField->getDisplayName(null);
     });
 }
 /**
  * @dataProvider provider_patternLocalWeekBasedYearDate
  */
 public function test_parse_WeekBasedYear($pattern, $text, $pos, $expectedPos, LocalDate $expectedValue)
 {
     $ppos = new ParsePosition($pos);
     $b = (new DateTimeFormatterBuilder())->appendPattern($pattern);
     $dtf = $b->toFormatter2($this->locale);
     $parsed = $dtf->parseUnresolved($text, $ppos);
     if ($ppos->getErrorIndex() != -1) {
         $this->assertEquals($ppos->getErrorIndex(), $expectedPos);
     } else {
         $weekDef = WeekFields::ofLocale($this->locale);
         $this->assertEquals($ppos->getIndex(), $expectedPos, "Incorrect ending parse position");
         $this->assertEquals($parsed->isSupported($weekDef->dayOfWeek()), strpos($pattern, 'e') >= 0);
         $this->assertEquals($parsed->isSupported($weekDef->weekOfWeekBasedYear()), strpos($pattern, 'w') >= 0);
         $this->assertEquals($parsed->isSupported($weekDef->weekBasedYear()), strpos($pattern, 'Y') >= 0);
         // ensure combination resolves into a date
         $result = LocalDate::parseWith($text, $dtf);
         $this->assertEquals($result, $expectedValue, "LocalDate incorrect for " . $pattern . ", {$weekDef}: " . $weekDef);
     }
 }
Пример #6
0
 /**
  * Obtains an instance of {@code WeekFields} appropriate for a locale.
  * <p>
  * This will look up appropriate values from the provider of localization data.
  *
  * @param Locale $locale the locale to use, not null
  * @return WeekFields the week-definition, not null
  */
 public static function ofLocale(Locale $locale)
 {
     // TODO $locale = Locale::of($locale->getLanguage(), $locale->getCountry());  // elminate variants
     $cal = IntlCalendar::createInstance(null, $locale->getLocale());
     $calDow = $cal->getFirstDayOfWeek();
     $dow = DayOfWeek::SUNDAY()->plus($calDow - 1);
     $minDays = $cal->getMinimalDaysInFirstWeek();
     return WeekFields::of($dow, $minDays);
 }
Пример #7
0
 public function __toString()
 {
     return $this->name . "[" . $this->weekDef->__toString() . "]";
 }