public function parse(DateTimeParseContext $context, $text, $position)
 {
     // simple looping parser to find the chronology
     if ($position < 0 || $position > strlen($text)) {
         throw new IndexOutOfBoundsException();
     }
     $chronos = AbstractChronology::getAvailableChronologies();
     $bestMatch = null;
     $matchLen = -1;
     foreach ($chronos as $chrono) {
         if ($this->textStyle === null) {
             $name = $chrono->getId();
         } else {
             $name = $this->getChronologyName($chrono, $context->getLocale());
         }
         $nameLen = strlen($name);
         if ($nameLen > $matchLen && $context->subSequenceEquals($text, $position, $name, 0, $nameLen)) {
             $bestMatch = $chrono;
             $matchLen = $nameLen;
         }
     }
     if ($bestMatch === null) {
         return ~$position;
     }
     $context->setParsed($bestMatch);
     return $position + $matchLen;
 }
 public function getValue(DateTimePrintContext $context, $value)
 {
     $absValue = Math::abs($value);
     $baseValue = $this->baseValue;
     if ($this->baseDate != null) {
         $chrono = AbstractChronology::from($context->getTemporal());
         $baseValue = $chrono->dateFrom($this->baseDate)->get($this->field);
     }
     if ($value >= $baseValue && $value < $baseValue + self::$EXCEED_POINTS[$this->minWidth]) {
         // Use the reduced value if it fits in minWidth
         return $absValue % self::$EXCEED_POINTS[$this->minWidth];
     }
     // Otherwise truncate to fit in maxWidth
     return $absValue % self::$EXCEED_POINTS[$this->maxWidth];
 }
 /**
  * @return ThaiBuddhistDate
  */
 public function resolveDate(FieldValues $fieldValues, ResolverStyle $resolverStyle)
 {
     return parent::resolveDate($fieldValues, $resolverStyle);
 }
Example #4
0
 /**
  * Adjusts the specified temporal object to have this year.
  * <p>
  * This returns a temporal object of the same observable type as the input
  * with the year changed to be the same as this.
  * <p>
  * The adjustment is equivalent to using {@link Temporal#with(TemporalField, long)}
  * passing {@link ChronoField#YEAR} as the field.
  * If the specified temporal object does not use the ISO calendar system then
  * a {@code DateTimeException} is thrown.
  * <p>
  * In most cases, it is clearer to reverse the calling pattern by using
  * {@link Temporal#with(TemporalAdjuster)}:
  * <pre>
  *   // these two lines are equivalent, but the second approach is recommended
  *   temporal = thisYear.adjustInto(temporal);
  *   temporal = temporal.with(thisYear);
  * </pre>
  * <p>
  * This instance is immutable and unaffected by this method call.
  *
  * @param Temporal $temporal the target object to be adjusted, not null
  * @return Temporal the adjusted object, not null
  * @throws DateTimeException if unable to make the adjustment
  * @throws ArithmeticException if numeric overflow occurs
  */
 public function adjustInto(Temporal $temporal)
 {
     if (AbstractChronology::from($temporal)->equals(IsoChronology::INSTANCE()) == false) {
         throw new DateTimeException("Adjustment only supported on ISO date-time");
     }
     return $temporal->with(ChronoField::YEAR(), $this->year);
 }
Example #5
0
 /**
  * @internal
  * @param TemporalAccessor $temporal
  * @return bool
  */
 public static function isIso(TemporalAccessor $temporal)
 {
     return AbstractChronology::from($temporal)->equals(IsoChronology::INSTANCE());
 }
 /**
  * @inheritdoc
  */
 public static function timeLineOrder()
 {
     return AbstractChronology::DATE_ORDER();
 }
Example #7
0
 /**
  * Adjusts the specified temporal object to have this month-day.
  * <p>
  * This returns a temporal object of the same observable type as the input
  * with the month and day-of-month changed to be the same as this.
  * <p>
  * The adjustment is equivalent to using {@link Temporal#with(TemporalField, long)}
  * twice, passing {@link ChronoField#MONTH_OF_YEAR} and
  * {@link ChronoField#DAY_OF_MONTH} as the fields.
  * If the specified temporal object does not use the ISO calendar system then
  * a {@code DateTimeException} is thrown.
  * <p>
  * In most cases, it is clearer to reverse the calling pattern by using
  * {@link Temporal#with(TemporalAdjuster)}:
  * <pre>
  *   // these two lines are equivalent, but the second approach is recommended
  *   temporal = thisMonthDay.adjustInto(temporal);
  *   temporal = temporal.with(thisMonthDay);
  * </pre>
  * <p>
  * This instance is immutable and unaffected by this method call.
  *
  * @param Temporal $temporal the target object to be adjusted, not null
  * @return Temporal the adjusted object, not null
  * @throws DateTimeException if unable to make the adjustment
  * @throws ArithmeticException if numeric overflow occurs
  */
 public function adjustInto(Temporal $temporal)
 {
     if (AbstractChronology::from($temporal)->equals(IsoChronology::INSTANCE()) == false) {
         throw new DateTimeException("Adjustment only supported on ISO date-time");
     }
     $temporal = $temporal->with(ChronoField::MONTH_OF_YEAR(), $this->month);
     return $temporal->with(ChronoField::DAY_OF_MONTH(), Math::min($temporal->range(ChronoField::DAY_OF_MONTH())->getMaximum(), $this->day));
 }
 public function format(DateTimePrintContext $context, &$buf)
 {
     $chrono = AbstractChronology::from($context->getTemporal());
     return $this->formatter($context->getLocale(), $chrono)->toPrinterParser(false)->format($context, $buf);
 }
 public function test_factory_from_TemporalAccessor_null()
 {
     TestHelper::assertNullException($this, function () {
         AbstractChronology::from(null);
     });
 }
Example #10
0
 public function isSupportedBy(TemporalAccessor $temporal)
 {
     return $temporal->isSupported(ChronoField::EPOCH_DAY()) && AbstractChronology::from($temporal)->equals(IsoChronology::INSTANCE());
 }
 /**
  * Map the field range to a week range of a week year.
  * @param TemporalAccessor $temporal the temporal
  * @return ValueRange the ValueRange with the range adjusted to weeks.
  */
 private function rangeWeekOfWeekBasedYear(TemporalAccessor $temporal)
 {
     if (!$temporal->isSupported(CF::DAY_OF_YEAR())) {
         return self::WEEK_OF_YEAR_RANGE();
     }
     $dow = $this->localizedDayOfWeek($temporal);
     $doy = $temporal->get(CF::DAY_OF_YEAR());
     $offset = $this->startOfWeekOffset($doy, $dow);
     $week = $this->computeWeek($offset, $doy);
     if ($week === 0) {
         // Day is in end of week of previous year
         // Recompute from the last day of the previous year
         $date = AbstractChronology::from($temporal)->dateFrom($temporal);
         $date = $date->minus($doy + 7, ChronoUnit::DAYS());
         // Back down into previous year
         return $this->rangeWeekOfWeekBasedYear($date);
     }
     // Check if day of year is in partial week associated with next year
     $dayRange = $temporal->range(CF::DAY_OF_YEAR());
     $yearLen = $dayRange->getMaximum();
     $newYearWeek = $this->computeWeek($offset, $yearLen + $this->weekDef->getMinimalDaysInFirstWeek());
     if ($week >= $newYearWeek) {
         // Overlaps with weeks of following year; recompute from a week in following year
         $date = AbstractChronology::from($temporal)->dateFrom($temporal);
         $date = $date->plus($yearLen - $doy + 1 + 7, ChronoUnit::DAYS());
         return $this->rangeWeekOfWeekBasedYear($date);
     }
     return ValueRange::of(1, $newYearWeek - 1);
 }
Example #12
0
 public function resolve(FieldValues $fieldValues, TemporalAccessor $partialTemporal, ResolverStyle $resolverStyle)
 {
     $value = $fieldValues->remove($this);
     $chrono = AbstractChronology::from($partialTemporal);
     if ($resolverStyle == ResolverStyle::LENIENT()) {
         return $chrono->dateEpochDay(Math::subtractExact($value, $this->offset));
     }
     $this->range()->checkValidValue($value, $this);
     return $chrono->dateEpochDay($value - $this->offset);
 }