/**
  * Adds a field-value pair to the map, checking for conflicts.
  * <p>
  * If the field is not already present, then the field-value pair is added to the map.
  * If the field is already present and it has the same value as that specified, no action occurs.
  * If the field is already present and it has a different value to that specified, then
  * an exception is thrown.
  *
  * @param FieldValues $fieldValues the map of fields to values, which can be updated, not null
  * @param ChronoField $field the field to add, not null
  * @param int $value the value to add, not null
  * @throws DateTimeException
  */
 protected static function addFieldValue(FieldValues $fieldValues, ChronoField $field, $value)
 {
     $old = $fieldValues->put($field, $value);
     // check first for better error message
     if ($old !== null && $old !== $value) {
         throw new DateTimeException("Conflict found: " . $field . " " . $old . " differs from " . $field . " " . $value);
     }
 }
Example #2
0
 private function resolveInstant()
 {
     // add instant seconds if we have date, time and zone
     if ($this->date !== null && $this->time !== null) {
         if ($this->zone !== null) {
             $instant = $this->date->atTime($this->time)->atZone($this->zone)->getLong(CF::INSTANT_SECONDS());
             $this->fieldValues->put(CF::INSTANT_SECONDS(), $instant);
         } else {
             $offsetSecs = $this->fieldValues->get(CF::OFFSET_SECONDS());
             if ($offsetSecs !== null) {
                 $offset = ZoneOffset::ofTotalSeconds($offsetSecs);
                 $instant = $this->date->atTime($this->time)->atZone($offset)->getLong(CF::INSTANT_SECONDS());
                 $this->fieldValues->put(CF::INSTANT_SECONDS(), $instant);
             }
         }
     }
 }
 /**
  * @dataProvider data_resolve_ymaa
  */
 public function test_resolve_ymaa_strict($y, $m, $w, $d, $expected, $smar, $strict)
 {
     $fieldValues = new FieldValues();
     $fieldValues->put(ChronoField::YEAR(), $y);
     $fieldValues->put(ChronoField::MONTH_OF_YEAR(), $m);
     $fieldValues->put(ChronoField::ALIGNED_WEEK_OF_MONTH(), $w);
     $fieldValues->put(ChronoField::ALIGNED_DAY_OF_WEEK_IN_MONTH(), $d);
     if ($strict) {
         $date = IsoChronology::INSTANCE()->resolveDate($fieldValues, ResolverStyle::STRICT());
         $this->assertEquals($date, $expected);
         $this->assertEquals($fieldValues->size(), 0);
     } else {
         try {
             IsoChronology::INSTANCE()->resolveDate($fieldValues, ResolverStyle::STRICT());
             $this->fail("Should have failed");
         } catch (DateTimeException $ex) {
             // $expected
         }
     }
 }
 public function setUp()
 {
     // slight abuse of FieldValues
     $this->fieldMap = new FieldValues();
     $this->fieldMap->put(ChronoField::ERA(), "era");
     $this->fieldMap->put(ChronoField::YEAR(), "year");
     $this->fieldMap->put(ChronoField::MONTH_OF_YEAR(), "month");
     $this->fieldMap->put(ChronoField::DAY_OF_MONTH(), "day");
     $this->fieldMap->put(ChronoField::AMPM_OF_DAY(), "dayperiod");
     $this->fieldMap->put(ChronoField::ALIGNED_WEEK_OF_YEAR(), "week");
     $this->fieldMap->put(ChronoField::DAY_OF_WEEK(), "weekday");
     $this->fieldMap->put(ChronoField::HOUR_OF_DAY(), "hour");
     $this->fieldMap->put(ChronoField::MINUTE_OF_HOUR(), "minute");
     $this->fieldMap->put(ChronoField::SECOND_OF_MINUTE(), "second");
     $this->fieldMap->put(ChronoField::OFFSET_SECONDS(), "zone");
 }
 public function resolve(FieldValues $fieldValues, TemporalAccessor $partialTemporal, ResolverStyle $resolverStyle)
 {
     $value = $fieldValues->get($this);
     $newValue = Math::toIntExact($value);
     // broad limit makes overflow checking lighter
     // first convert localized day-of-week to ISO day-of-week
     // doing this first handles case where both ISO and localized were parsed and might mismatch
     // day-of-week is always strict as two different day-of-week values makes lenient complex
     if ($this->rangeUnit == ChronoUnit::WEEKS()) {
         // day-of-week
         $checkedValue = $this->range->checkValidIntValue($value, $this);
         // no leniency as too complex
         $startDow = $this->weekDef->getFirstDayOfWeek()->getValue();
         $isoDow = Math::floorMod($startDow - 1 + ($checkedValue - 1), 7) + 1;
         $fieldValues->remove($this);
         $fieldValues->put(CF::DAY_OF_WEEK(), $isoDow);
         return null;
     }
     // can only build date if ISO day-of-week is present
     if (!$fieldValues->has(CF::DAY_OF_WEEK())) {
         return null;
     }
     $isoDow = CF::DAY_OF_WEEK()->checkValidIntValue($fieldValues->get(CF::DAY_OF_WEEK()));
     $dow = $this->localizedDayOfWeekNumerical($isoDow);
     // build date
     $chrono = AbstractChronology::from($partialTemporal);
     if ($fieldValues->has(CF::YEAR())) {
         $year = CF::YEAR()->checkValidIntValue($fieldValues->get(CF::YEAR()));
         // validate
         if ($this->rangeUnit == ChronoUnit::MONTHS() && $fieldValues->has(CF::MONTH_OF_YEAR())) {
             // week-of-month
             $month = $fieldValues->get(CF::MONTH_OF_YEAR());
             // not validated yet
             return $this->resolveWoM($fieldValues, $chrono, $year, $month, $newValue, $dow, $resolverStyle);
         }
         if ($this->rangeUnit == ChronoUnit::YEARS()) {
             // week-of-year
             return $this->resolveWoY($fieldValues, $chrono, $year, $newValue, $dow, $resolverStyle);
         }
     } else {
         if (($this->rangeUnit == IsoFields::WEEK_BASED_YEARS() || $this->rangeUnit == ChronoUnit::FOREVER()) && $fieldValues->has($this->weekDef->weekBasedYear) && $fieldValues->has($this->weekDef->weekOfWeekBasedYear)) {
             // week-of-week-based-year and year-of-week-based-year
             return $this->resolveWBY($fieldValues, $chrono, $dow, $resolverStyle);
         }
     }
     return null;
 }
 public function setOffset($offsetId)
 {
     if ($offsetId !== null) {
         $this->fields->put(ChronoField::OFFSET_SECONDS(), ZoneOffset::of($offsetId)->getTotalSeconds());
     }
 }