public function adjustInto(Temporal $temporal, $newValue) { // calls getFrom() to check if supported $this->range()->checkValidValue($newValue, $this); // lenient range return $temporal->plus(Math::subtractExact($newValue, $this->getFrom($temporal)), ChronoUnit::WEEKS()); }
/** * @inheritdoc */ public function isSupportedBy(Temporal $temporal) { if ($temporal instanceof LocalTime) { return $this->isTimeBased(); } if ($temporal instanceof ChronoLocalDate) { return $this->isDateBased(); } if ($temporal instanceof ChronoLocalDateTime || $temporal instanceof ChronoZonedDateTime) { return true; } try { $temporal->plus(1, $this); return true; } catch (UnsupportedTemporalTypeException $ex) { return false; } catch (\RuntimeException $ex) { try { $temporal->plus(-1, $this); return true; } catch (\RuntimeException $ex2) { return false; } } }
public function adjustInto(Temporal $temporal, $newValue) { if ($this->range()->isValidValue($newValue) == false) { throw new DateTimeException("Invalid value: " . $this->name . " " . $newValue); } return $temporal->with(CF::EPOCH_DAY(), Math::subtractExact($newValue, $this->offset)); }
public function addTo(Temporal $temporal, $amount) { switch ($this) { case IsoFields::WEEK_BASED_YEARS(): return $temporal->with(IsoFields::WEEK_BASED_YEAR(), Math::addExact($temporal->get(IsoFields::WEEK_BASED_YEAR()), $amount)); case IsoFields::QUARTER_YEARS(): // no overflow (256 is multiple of 4) return $temporal->plus($amount / 256, ChronoUnit::YEARS())->plus($amount % 256 * 3, ChronoUnit::MONTHS()); default: throw new IllegalStateException("Unreachable"); } }
/** * Adjusts the specified temporal object to have the same time as this object. * <p> * This returns a temporal object of the same observable type as the input * with the time changed to be the same as this. * <p> * The adjustment is equivalent to using {@link Temporal#with(TemporalField, long)} * passing {@link ChronoField#NANO_OF_DAY} as the field. * <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 = thisLocalTime.adjustInto(temporal); * temporal = temporal.with(thisLocalTime); * </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) { return $temporal->with(ChronoField::NANO_OF_DAY(), $this->toNanoOfDay()); }
/** * Adjusts the specified temporal object to have this instant. * <p> * This returns a temporal object of the same observable type as the input * with the instant changed to be the same as this. * <p> * The adjustment is equivalent to using {@link Temporal#with(TemporalField, long)} * twice, passing {@link ChronoField#INSTANT_SECONDS} and * {@link ChronoField#NANO_OF_SECOND} as the fields. * <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 = thisInstant.adjustInto(temporal); * temporal = temporal.with(thisInstant); * </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) { return $temporal->with(ChronoField::INSTANT_SECONDS(), $this->seconds)->with(ChronoField::NANO_OF_SECOND(), $this->nanos); }
/** * Obtains a {@code Duration} representing the duration between two temporal objects. * <p> * This calculates the duration between two temporal objects. If the objects * are of different types, then the duration is calculated based on the type * of the first object. For example, if the first argument is a {@code LocalTime} * then the second argument is converted to a {@code LocalTime}. * <p> * The specified temporal objects must support the {@link ChronoUnit#SECONDS SECONDS} unit. * For full accuracy, either the {@link ChronoUnit#NANOS NANOS} unit or the * {@link ChronoField#NANO_OF_SECOND NANO_OF_SECOND} field should be supported. * <p> * The result of this method can be a negative period if the end is before the start. * To guarantee to obtain a positive duration call {@link #abs()} on the result. * * @param Temporal $startInclusive the start instant, inclusive, not null * @param Temporal $endExclusive the end instant, exclusive, not null * @return Duration a {@code Duration}, not null * @throws DateTimeException if the seconds between the temporals cannot be obtained * @throws ArithmeticException if the calculation exceeds the capacity of {@code Duration} * * TODO check */ public static function between(Temporal $startInclusive, Temporal $endExclusive) { try { return self::ofNanos($startInclusive->until($endExclusive, ChronoUnit::NANOS())); } catch (\Exception $ex) { $secs = $startInclusive->until($endExclusive, ChronoUnit::SECONDS()); try { $nanos = $endExclusive->getLong(ChronoField::NANO_OF_SECOND()) - $startInclusive->getLong(ChronoField::NANO_OF_SECOND()); if ($secs > 0 && $nanos < 0) { $secs++; } else { if ($secs < 0 && $nanos > 0) { $secs--; } } } catch (DateTimeException $ex2) { $nanos = 0; } return self::ofSeconds($secs, $nanos); } }
public function between(Temporal $temporal1Inclusive, Temporal $temporal2Exclusive) { return $temporal1Inclusive->until($temporal2Exclusive, $this); }
public function subtractFrom(Temporal $temporal) { return $temporal->minus($this->amount, $this->unit); }
/** * Adjusts the specified temporal object to have the same offset as this object. * <p> * This returns a temporal object of the same observable type as the input * with the offset changed to be the same as this. * <p> * The adjustment is equivalent to using {@link Temporal#with(TemporalField, long)} * passing {@link ChronoField#OFFSET_SECONDS} as the field. * <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 = thisOffset.adjustInto(temporal); * temporal = temporal.with(thisOffset); * </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) { return $temporal->with(ChronoField::OFFSET_SECONDS(), $this->totalSeconds); }
/** * Adjusts the specified temporal object to have this day-of-week. * <p> * This returns a temporal object of the same observable type as the input * with the day-of-week changed to be the same as this. * <p> * The adjustment is equivalent to using {@link Temporal#with(TemporalField, long)} * passing {@link ChronoField#DAY_OF_WEEK} as the field. * Note that this adjusts forwards or backwards within a Monday to Sunday week. * See {@link java.time.temporal.WeekFields#dayOfWeek()} for localized week start days. * See {@code TemporalAdjuster} for other adjusters with more control, * such as {@code next(MONDAY)}. * <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 = thisDayOfWeek.adjustInto(temporal); * temporal = temporal.with(thisDayOfWeek); * </pre> * <p> * For example, given a date that is a Wednesday, the following are output: * <pre> * dateOnWed.with(MONDAY); // two days earlier * dateOnWed.with(TUESDAY); // one day earlier * dateOnWed.with(WEDNESDAY); // same date * dateOnWed.with(THURSDAY); // one day later * dateOnWed.with(FRIDAY); // two days later * dateOnWed.with(SATURDAY); // three days later * dateOnWed.with(SUNDAY); // four days later * </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) { return $temporal->with(ChronoField::DAY_OF_WEEK(), $this->getValue()); }
public function adjustInto(Temporal $temporal, $newValue) { // Check the new value and get the old value of the field $newVal = $this->range->checkValidIntValue($newValue, $this); // lenient check range $currentVal = $temporal->get($this); if ($newVal === $currentVal) { return $temporal; } if ($this->rangeUnit == ChronoUnit::FOREVER()) { // replace year of WeekBasedYear // Create a new date object with the same chronology, // the desired year and the same week and dow. $idow = $temporal->get($this->weekDef->dayOfWeek); $wowby = $temporal->get($this->weekDef->weekOfWeekBasedYear); return $this->ofWeekBasedYear(AbstractChronology::from($temporal), $newValue, $wowby, $idow); } else { // Compute the difference and add that using the base unit of the field return $temporal->plus($newVal - $currentVal, $this->baseUnit); } }
/** * @inheritdoc */ public function adjustInto(Temporal $temporal) { return $temporal->with(ChronoField::EPOCH_DAY(), $this->toLocalDate()->toEpochDay())->with(ChronoField::NANO_OF_DAY(), $this->toLocalTime()->toNanoOfDay()); }
/** * Adjusts the specified temporal object to have the same offset and time * as this object. * <p> * This returns a temporal object of the same observable type as the input * with the offset and time changed to be the same as this. * <p> * The adjustment is equivalent to using {@link Temporal#with(TemporalField, long)} * twice, passing {@link ChronoField#NANO_OF_DAY} and * {@link ChronoField#OFFSET_SECONDS} as the fields. * <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 = thisOffsetTime.adjustInto(temporal); * temporal = temporal.with(thisOffsetTime); * </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) { return $temporal->with(ChronoField::NANO_OF_DAY(), $this->time->toNanoOfDay())->with(ChronoField::OFFSET_SECONDS(), $this->offset->getTotalSeconds()); }
public function adjustInto(Temporal $temporal, $newValue) { return $temporal->with($this, $newValue); }
/** * Adjusts the specified temporal object to have the same offset, date * and time as this object. * <p> * This returns a temporal object of the same observable type as the input * with the offset, date and time changed to be the same as this. * <p> * The adjustment is equivalent to using {@link Temporal#with(TemporalField, long)} * three times, passing {@link ChronoField#EPOCH_DAY}, * {@link ChronoField#NANO_OF_DAY} and {@link ChronoField#OFFSET_SECONDS} as the fields. * <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 = thisOffsetDateTime.adjustInto(temporal); * temporal = temporal.with(thisOffsetDateTime); * </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) { // OffsetDateTime is treated as three separate fields, not an instant // this produces the most consistent set of results overall // the offset is set after the date and time, as it is typically a small // tweak to the result, with ZonedDateTime frequently ignoring the offset return $temporal->with(ChronoField::EPOCH_DAY(), $this->toLocalDate()->toEpochDay())->with(ChronoField::NANO_OF_DAY(), $this->toLocalTime()->toNanoOfDay())->with(ChronoField::OFFSET_SECONDS(), $this->getOffset()->getTotalSeconds()); }
/** * @inheritdoc */ public function adjustInto(Temporal $temporal) { return $temporal->with(ChronoField::EPOCH_DAY(), $this->toEpochDay()); }