/** * @inheritdoc */ public function range(TemporalField $field) { if ($field instanceof ChronoField) { if ($field == ChronoField::INSTANT_SECONDS() || $field == ChronoField::OFFSET_SECONDS()) { return $field->range(); } return $this->toLocalDateTime()->range($field); } return $field->rangeRefinedBy($this); }
/** * @inheritdoc */ public function getLong(TemporalField $field) { if ($field == ChronoField::ERA()) { return $this->getValue(); } else { if ($field instanceof ChronoField) { throw new UnsupportedTemporalTypeException("Unsupported field: " . $field); } } return $field->getFrom($this); }
/** * Converts a fraction from 0 to 1 for this field to a value. * <p> * The fractional value must be between 0 (inclusive) and 1 (exclusive). * It can only be returned if the {@link java.time.temporal.TemporalField#range() value range} is fixed. * The value is obtained by calculation from the field range and a rounding * mode of {@link RoundingMode#FLOOR FLOOR}. * The calculation is inaccurate if the values do not run continuously from smallest to largest. * <p> * For example, the fractional second-of-minute of 0.25 would be converted to 15, * assuming the standard definition of 60 seconds in a minute. * * @param mixed $fraction TODO the fraction to convert, not null * @return int the value of the field, valid for this rule * @throws DateTimeException if the value cannot be converted */ private function convertFromFraction($fraction) { $range = $this->field->range(); $minBD = gmp_init($range->getMinimum()); $rangeBD = gmp_add(gmp_sub($range->getMaximum(), $minBD), 1); $valueBD = gmp_add(gmp_div(gmp_mul($fraction, $rangeBD), 1000000000), $minBD); return gmp_intval($valueBD); }
/** * Returns a copy of this time with the specified field set to a new value. * <p> * This returns a {@code LocalTime}, based on this one, with the value * for the specified field changed. * This can be used to change any supported field, such as the hour, minute or second. * If it is not possible to set the value, because the field is not supported or for * some other reason, an exception is thrown. * <p> * If the field is a {@link ChronoField} then the adjustment is implemented here. * The supported fields behave as follows: * <ul> * <li>{@code NANO_OF_SECOND} - * Returns a {@code LocalTime} with the specified nano-of-second. * The hour, minute and second will be unchanged. * <li>{@code NANO_OF_DAY} - * Returns a {@code LocalTime} with the specified nano-of-day. * This completely replaces the time and is equivalent to {@link #ofNanoOfDay(long)}. * <li>{@code MICRO_OF_SECOND} - * Returns a {@code LocalTime} with the nano-of-second replaced by the specified * micro-of-second multiplied by 1,000. * The hour, minute and second will be unchanged. * <li>{@code MICRO_OF_DAY} - * Returns a {@code LocalTime} with the specified micro-of-day. * This completely replaces the time and is equivalent to using {@link #ofNanoOfDay(long)} * with the micro-of-day multiplied by 1,000. * <li>{@code MILLI_OF_SECOND} - * Returns a {@code LocalTime} with the nano-of-second replaced by the specified * milli-of-second multiplied by 1,000,000. * The hour, minute and second will be unchanged. * <li>{@code MILLI_OF_DAY} - * Returns a {@code LocalTime} with the specified milli-of-day. * This completely replaces the time and is equivalent to using {@link #ofNanoOfDay(long)} * with the milli-of-day multiplied by 1,000,000. * <li>{@code SECOND_OF_MINUTE} - * Returns a {@code LocalTime} with the specified second-of-minute. * The hour, minute and nano-of-second will be unchanged. * <li>{@code SECOND_OF_DAY} - * Returns a {@code LocalTime} with the specified second-of-day. * The nano-of-second will be unchanged. * <li>{@code MINUTE_OF_HOUR} - * Returns a {@code LocalTime} with the specified minute-of-hour. * The hour, second-of-minute and nano-of-second will be unchanged. * <li>{@code MINUTE_OF_DAY} - * Returns a {@code LocalTime} with the specified minute-of-day. * The second-of-minute and nano-of-second will be unchanged. * <li>{@code HOUR_OF_AMPM} - * Returns a {@code LocalTime} with the specified hour-of-am-pm. * The AM/PM, minute-of-hour, second-of-minute and nano-of-second will be unchanged. * <li>{@code CLOCK_HOUR_OF_AMPM} - * Returns a {@code LocalTime} with the specified clock-hour-of-am-pm. * The AM/PM, minute-of-hour, second-of-minute and nano-of-second will be unchanged. * <li>{@code HOUR_OF_DAY} - * Returns a {@code LocalTime} with the specified hour-of-day. * The minute-of-hour, second-of-minute and nano-of-second will be unchanged. * <li>{@code CLOCK_HOUR_OF_DAY} - * Returns a {@code LocalTime} with the specified clock-hour-of-day. * The minute-of-hour, second-of-minute and nano-of-second will be unchanged. * <li>{@code AMPM_OF_DAY} - * Returns a {@code LocalTime} with the specified AM/PM. * The hour-of-am-pm, minute-of-hour, second-of-minute and nano-of-second will be unchanged. * </ul> * <p> * In all cases, if the new value is outside the valid range of values for the field * then a {@code DateTimeException} will be thrown. * <p> * All other {@code ChronoField} instances will throw an {@code UnsupportedTemporalTypeException}. * <p> * If the field is not a {@code ChronoField}, then the result of this method * is obtained by invoking {@code TemporalField.adjustInto(Temporal, long)} * passing {@code this} as the argument. In this case, the field determines * whether and how to adjust the instant. * <p> * This instance is immutable and unaffected by this method call. * * @param TemporalField $field the field to set in the result, not null * @param int $newValue the new value of the field in the result * @return LocalTime a {@code LocalTime} based on {@code this} with the specified field set, not null * @throws DateTimeException if the field cannot be set * @throws UnsupportedTemporalTypeException if the field is not supported * @throws ArithmeticException if numeric overflow occurs */ public function with(TemporalField $field, $newValue) { if ($field instanceof ChronoField) { $f = $field; $f->checkValidValue($newValue); switch ($f) { case ChronoField::NANO_OF_SECOND(): return $this->withNano((int) $newValue); case ChronoField::NANO_OF_DAY(): return self::ofNanoOfDay($newValue); case ChronoField::MICRO_OF_SECOND(): return $this->withNano((int) $newValue * 1000); case ChronoField::MICRO_OF_DAY(): return self::ofNanoOfDay($newValue * 1000); case ChronoField::MILLI_OF_SECOND(): return $this->withNano((int) $newValue * 1000000); case ChronoField::MILLI_OF_DAY(): return self::ofNanoOfDay($newValue * 1000000); case ChronoField::SECOND_OF_MINUTE(): return $this->withSecond((int) $newValue); case ChronoField::SECOND_OF_DAY(): return $this->plusSeconds($newValue - $this->toSecondOfDay()); case ChronoField::MINUTE_OF_HOUR(): return $this->withMinute((int) $newValue); case ChronoField::MINUTE_OF_DAY(): return $this->plusMinutes($newValue - ($this->hour * 60 + $this->minute)); case ChronoField::HOUR_OF_AMPM(): return $this->plusHours($newValue - $this->hour % 12); case ChronoField::CLOCK_HOUR_OF_AMPM(): return $this->plusHours(($newValue === 12 ? 0 : $newValue) - $this->hour % 12); case ChronoField::HOUR_OF_DAY(): return $this->withHour((int) $newValue); case ChronoField::CLOCK_HOUR_OF_DAY(): return $this->withHour((int) ($newValue === 24 ? 0 : $newValue)); case ChronoField::AMPM_OF_DAY(): return $this->plusHours(($newValue - Math::div($this->hour, 12)) * 12); } throw new UnsupportedTemporalTypeException("Unsupported field: " . $field); } return $field->adjustInto($this, $newValue); }
/** * Returns a copy of this date with the specified field set to a new value. * <p> * This returns a {@code LocalDate}, based on this one, with the value * for the specified field changed. * This can be used to change any supported field, such as the year, month or day-of-month. * If it is not possible to set the value, because the field is not supported or for * some other reason, an exception is thrown. * <p> * In some cases, changing the specified field can cause the resulting date to become invalid, * such as changing the month from 31st January to February would make the day-of-month invalid. * In cases like this, the field is responsible for resolving the date. Typically it will choose * the previous valid date, which would be the last valid day of February in this example. * <p> * If the field is a {@link ChronoField} then the adjustment is implemented here. * The supported fields behave as follows: * <ul> * <li>{@code DAY_OF_WEEK} - * Returns a {@code LocalDate} with the specified day-of-week. * The date is adjusted up to 6 days forward or backward within the boundary * of a Monday to Sunday week. * <li>{@code ALIGNED_DAY_OF_WEEK_IN_MONTH} - * Returns a {@code LocalDate} with the specified aligned-day-of-week. * The date is adjusted to the specified month-based aligned-day-of-week. * Aligned weeks are counted such that the first week of a given month starts * on the first day of that month. * This may cause the date to be moved up to 6 days into the following month. * <li>{@code ALIGNED_DAY_OF_WEEK_IN_YEAR} - * Returns a {@code LocalDate} with the specified aligned-day-of-week. * The date is adjusted to the specified year-based aligned-day-of-week. * Aligned weeks are counted such that the first week of a given year starts * on the first day of that year. * This may cause the date to be moved up to 6 days into the following year. * <li>{@code DAY_OF_MONTH} - * Returns a {@code LocalDate} with the specified day-of-month. * The month and year will be unchanged. If the day-of-month is invalid for the * year and month, then a {@code DateTimeException} is thrown. * <li>{@code DAY_OF_YEAR} - * Returns a {@code LocalDate} with the specified day-of-year. * The year will be unchanged. If the day-of-year is invalid for the * year, then a {@code DateTimeException} is thrown. * <li>{@code EPOCH_DAY} - * Returns a {@code LocalDate} with the specified epoch-day. * This completely replaces the date and is equivalent to {@link #ofEpochDay(long)}. * <li>{@code ALIGNED_WEEK_OF_MONTH} - * Returns a {@code LocalDate} with the specified aligned-week-of-month. * Aligned weeks are counted such that the first week of a given month starts * on the first day of that month. * This adjustment moves the date in whole week chunks to match the specified week. * The result will have the same day-of-week as this date. * This may cause the date to be moved into the following month. * <li>{@code ALIGNED_WEEK_OF_YEAR} - * Returns a {@code LocalDate} with the specified aligned-week-of-year. * Aligned weeks are counted such that the first week of a given year starts * on the first day of that year. * This adjustment moves the date in whole week chunks to match the specified week. * The result will have the same day-of-week as this date. * This may cause the date to be moved into the following year. * <li>{@code MONTH_OF_YEAR} - * Returns a {@code LocalDate} with the specified month-of-year. * The year will be unchanged. The day-of-month will also be unchanged, * unless it would be invalid for the new month and year. In that case, the * day-of-month is adjusted to the maximum valid value for the new month and year. * <li>{@code PROLEPTIC_MONTH} - * Returns a {@code LocalDate} with the specified proleptic-month. * The day-of-month will be unchanged, unless it would be invalid for the new month * and year. In that case, the day-of-month is adjusted to the maximum valid value * for the new month and year. * <li>{@code YEAR_OF_ERA} - * Returns a {@code LocalDate} with the specified year-of-era. * The era and month will be unchanged. The day-of-month will also be unchanged, * unless it would be invalid for the new month and year. In that case, the * day-of-month is adjusted to the maximum valid value for the new month and year. * <li>{@code YEAR} - * Returns a {@code LocalDate} with the specified year. * The month will be unchanged. The day-of-month will also be unchanged, * unless it would be invalid for the new month and year. In that case, the * day-of-month is adjusted to the maximum valid value for the new month and year. * <li>{@code ERA} - * Returns a {@code LocalDate} with the specified era. * The year-of-era and month will be unchanged. The day-of-month will also be unchanged, * unless it would be invalid for the new month and year. In that case, the * day-of-month is adjusted to the maximum valid value for the new month and year. * </ul> * <p> * In all cases, if the new value is outside the valid range of values for the field * then a {@code DateTimeException} will be thrown. * <p> * All other {@code ChronoField} instances will throw an {@code UnsupportedTemporalTypeException}. * <p> * If the field is not a {@code ChronoField}, then the result of this method * is obtained by invoking {@code TemporalField.adjustInto(Temporal, long)} * passing {@code this} as the argument. In this case, the field determines * whether and how to adjust the instant. * <p> * This instance is immutable and unaffected by this method call. * * @param TemporalField $field the field to set in the result, not null * @param int $newValue the new value of the field in the result * @return LocalDate a {@code LocalDate} based on {@code this} with the specified field set, not null * @throws DateTimeException if the field cannot be set * @throws UnsupportedTemporalTypeException if the field is not supported * @throws ArithmeticException if numeric overflow occurs */ public function with(TemporalField $field, $newValue) { if ($field instanceof ChronoField) { /** @var Chronofield $f */ $f = $field; $f->checkValidValue($newValue); switch ($f) { case ChronoField::DAY_OF_WEEK(): return $this->plusDays($newValue - $this->getDayOfWeek()->getValue()); case ChronoField::ALIGNED_DAY_OF_WEEK_IN_MONTH(): return $this->plusDays($newValue - $this->getLong(ChronoField::ALIGNED_DAY_OF_WEEK_IN_MONTH())); case ChronoField::ALIGNED_DAY_OF_WEEK_IN_YEAR(): return $this->plusDays($newValue - $this->getLong(ChronoField::ALIGNED_DAY_OF_WEEK_IN_YEAR())); case ChronoField::DAY_OF_MONTH(): return $this->withDayOfMonth((int) $newValue); case ChronoField::DAY_OF_YEAR(): return $this->withDayOfYear((int) $newValue); case ChronoField::EPOCH_DAY(): return LocalDate::ofEpochDay($newValue); case ChronoField::ALIGNED_WEEK_OF_MONTH(): return $this->plusWeeks($newValue - $this->getLong(ChronoField::ALIGNED_WEEK_OF_MONTH())); case ChronoField::ALIGNED_WEEK_OF_YEAR(): return $this->plusWeeks($newValue - $this->getLong(ChronoField::ALIGNED_WEEK_OF_YEAR())); case ChronoField::MONTH_OF_YEAR(): return $this->withMonth((int) $newValue); case ChronoField::PROLEPTIC_MONTH(): return $this->plusMonths($newValue - $this->getProlepticMonth()); case ChronoField::YEAR_OF_ERA(): return $this->withYear((int) ($this->year >= 1 ? $newValue : 1 - $newValue)); case ChronoField::YEAR(): return $this->withYear((int) $newValue); case ChronoField::ERA(): return $this->getLong(ChronoField::ERA()) == $newValue ? $this : $this->withYear(1 - $this->year); } throw new UnsupportedTemporalTypeException("Unsupported field: " . $field); } return $field->adjustInto($this, $newValue); }
/** * Returns a copy of this instant with the specified field set to a new value. * <p> * This returns an Instant {@code Instant}, based on this one, with the value * for the specified field changed. * If it is not possible to set the value, because the field is not supported or for * some other reason, an exception is thrown. * <p> * If the field is a {@link ChronoField} then the adjustment is implemented here. * The supported fields behave as follows: * <ul> * <li>{@code NANO_OF_SECOND} - * Returns an Instant {@code Instant} with the specified nano-of-second. * The epoch-second will be unchanged. * <li>{@code MICRO_OF_SECOND} - * Returns an Instant {@code Instant} with the nano-of-second replaced by the specified * micro-of-second multiplied by 1,000. The epoch-second will be unchanged. * <li>{@code MILLI_OF_SECOND} - * Returns an Instant {@code Instant} with the nano-of-second replaced by the specified * milli-of-second multiplied by 1,000,000. The epoch-second will be unchanged. * <li>{@code INSTANT_SECONDS} - * Returns an Instant {@code Instant} with the specified epoch-second. * The nano-of-second will be unchanged. * </ul> * <p> * In all cases, if the new value is outside the valid range of values for the field * then a {@code DateTimeException} will be thrown. * <p> * All other {@code ChronoField} instances will throw an {@code UnsupportedTemporalTypeException}. * <p> * If the field is not a {@code ChronoField}, then the result of this method * is obtained by invoking {@code TemporalField.adjustInto(Temporal, long)} * passing {@code this} as the argument. In this case, the field determines * whether and how to adjust the instant. * <p> * This instance is immutable and unaffected by this method call. * * @param TemporalField $field the field to set in the result, not null * @param int $newValue the new value of the field in the result * @return Instant an Instant {@code Instant} based on {@code this} with the specified field set, not null * @throws DateTimeException if the field cannot be set * @throws UnsupportedTemporalTypeException if the field is not supported * @throws ArithmeticException if numeric overflow occurs */ public function with(TemporalField $field, $newValue) { if ($field instanceof ChronoField) { $f = $field; $f->checkValidValue($newValue); switch ($f) { case ChronoField::MILLI_OF_SECOND(): $nval = (int) $newValue * 1000000; return $nval != $this->nanos ? self::create($this->seconds, $nval) : $this; case ChronoField::MICRO_OF_SECOND(): $nval = (int) $newValue * 1000; return $nval != $this->nanos ? self::create($this->seconds, $nval) : $this; case ChronoField::NANO_OF_SECOND(): return $newValue != $this->nanos ? self::create($this->seconds, (int) $newValue) : $this; case ChronoField::INSTANT_SECONDS(): return $newValue != $this->seconds ? self::create($newValue, $this->nanos) : $this; } throw new UnsupportedTemporalTypeException("Unsupported field: " . $field); } return $field->adjustInto($this, $newValue); }
/** * Returns a copy of this year with the specified field set to a new value. * <p> * This returns a {@code Year}, based on this one, with the value * for the specified field changed. * If it is not possible to set the value, because the field is not supported or for * some other reason, an exception is thrown. * <p> * If the field is a {@link ChronoField} then the adjustment is implemented here. * The supported fields behave as follows: * <ul> * <li>{@code YEAR_OF_ERA} - * Returns a {@code Year} with the specified year-of-era * The era will be unchanged. * <li>{@code YEAR} - * Returns a {@code Year} with the specified year. * This completely replaces the date and is equivalent to {@link #of(int)}. * <li>{@code ERA} - * Returns a {@code Year} with the specified era. * The year-of-era will be unchanged. * </ul> * <p> * In all cases, if the new value is outside the valid range of values for the field * then a {@code DateTimeException} will be thrown. * <p> * All other {@code ChronoField} instances will throw an {@code UnsupportedTemporalTypeException}. * <p> * If the field is not a {@code ChronoField}, then the result of this method * is obtained by invoking {@code TemporalField.adjustInto(Temporal, long)} * passing {@code this} as the argument. In this case, the field determines * whether and how to adjust the instant. * <p> * This instance is immutable and unaffected by this method call. * * @param TemporalField $field the field to set in the result, not null * @param int $newValue the new value of the field in the result * @return Year a {@code Year} based on {@code this} with the specified field set, not null * @throws DateTimeException if the field cannot be set * @throws UnsupportedTemporalTypeException if the field is not supported * @throws ArithmeticException if numeric overflow occurs */ public function with(TemporalField $field, $newValue) { if ($field instanceof ChronoField) { $f = $field; $f->checkValidValue($newValue); switch ($f) { case ChronoField::YEAR_OF_ERA(): return Year::of((int) ($this->year < 1 ? 1 - $newValue : $newValue)); case ChronoField::YEAR(): return Year::of((int) $newValue); case ChronoField::ERA(): return $this->getLong(ChronoField::ERA()) == $newValue ? $this : Year::of(1 - $this->year); } throw new UnsupportedTemporalTypeException("Unsupported field: " . $field); } return $field->adjustInto($this, $newValue); }
/** * @inheritdoc */ public function with(TemporalField $field, $newValue) { if ($field instanceof ChronoField) { throw new UnsupportedTemporalTypeException("Unsupported field: " . $field); } return ChronoLocalDateImpl::ensureValid($this->getChronology(), $field->adjustInto($this, $newValue)); }
public function getLong(TemporalField $field) { if ($this->effectiveDate != null && $field->isDateBased()) { return $this->effectiveDate->getLong($field); } return $this->temporal->getLong($field); }
/** * Gets the value of the specified field from this offset as a {@code long}. * <p> * This queries this offset for the value of the specified field. * If it is not possible to return the value, because the field is not supported * or for some other reason, an exception is thrown. * <p> * If the field is a {@link ChronoField} then the query is implemented here. * The {@code OFFSET_SECONDS} field returns the value of the offset. * All other {@code ChronoField} instances will throw an {@code UnsupportedTemporalTypeException}. * <p> * If the field is not a {@code ChronoField}, then the result of this method * is obtained by invoking {@code TemporalField.getFrom(TemporalAccessor)} * passing {@code this} as the argument. Whether the value can be obtained, * and what the value represents, is determined by the field. * * @param $field TemporalField the field to get, not null * @return int the value for the field * @throws DateTimeException if a value for the field cannot be obtained * @throws UnsupportedTemporalTypeException if the field is not supported * @throws ArithmeticException if numeric overflow occurs */ public function getLong(TemporalField $field) { if ($field == ChronoField::OFFSET_SECONDS()) { return $this->totalSeconds; } else { if ($field instanceof ChronoField) { throw new UnsupportedTemporalTypeException("Unsupported field: " . $field); } } return $field->getFrom($this); }
/** * Gets the value of the specified field from this month-day as a {@code long}. * <p> * This queries this month-day for the value of the specified field. * If it is not possible to return the value, because the field is not supported * or for some other reason, an exception is thrown. * <p> * If the field is a {@link ChronoField} then the query is implemented here. * The {@link #isSupported(TemporalField) supported fields} will return valid * values based on this month-day. * All other {@code ChronoField} instances will throw an {@code UnsupportedTemporalTypeException}. * <p> * If the field is not a {@code ChronoField}, then the result of this method * is obtained by invoking {@code TemporalField.getFrom(TemporalAccessor)} * passing {@code this} as the argument. Whether the value can be obtained, * and what the value represents, is determined by the field. * * @param TemporalField $field the field to get, not null * @return int the value for the field * @throws DateTimeException if a value for the field cannot be obtained * @throws UnsupportedTemporalTypeException if the field is not supported * @throws ArithmeticException if numeric overflow occurs */ public function getLong(TemporalField $field) { if ($field instanceof ChronoField) { switch ($field) { // alignedDOW and alignedWOM not supported because they cannot be set in with() case ChronoField::DAY_OF_MONTH(): return $this->day; case ChronoField::MONTH_OF_YEAR(): return $this->month; } throw new UnsupportedTemporalTypeException("Unsupported field: " . $field); } return $field->getFrom($this); }
public function isSupported(TemporalField $field) { return $field instanceof ChronoField || $field !== null && $field->isSupportedBy($this); }
/** * Checks if the specified field is supported. * <p> * This checks if this date-time can be queried for the specified field. * If false, then calling the {@link #range(TemporalField) range}, * {@link #get(TemporalField) get} and {@link #with(TemporalField, long)} * methods will throw an exception. * <p> * If the field is a {@link ChronoField} then the query is implemented here. * The supported fields are: * <ul> * <li>{@code NANO_OF_SECOND} * <li>{@code NANO_OF_DAY} * <li>{@code MICRO_OF_SECOND} * <li>{@code MICRO_OF_DAY} * <li>{@code MILLI_OF_SECOND} * <li>{@code MILLI_OF_DAY} * <li>{@code SECOND_OF_MINUTE} * <li>{@code SECOND_OF_DAY} * <li>{@code MINUTE_OF_HOUR} * <li>{@code MINUTE_OF_DAY} * <li>{@code HOUR_OF_AMPM} * <li>{@code CLOCK_HOUR_OF_AMPM} * <li>{@code HOUR_OF_DAY} * <li>{@code CLOCK_HOUR_OF_DAY} * <li>{@code AMPM_OF_DAY} * <li>{@code DAY_OF_WEEK} * <li>{@code ALIGNED_DAY_OF_WEEK_IN_MONTH} * <li>{@code ALIGNED_DAY_OF_WEEK_IN_YEAR} * <li>{@code DAY_OF_MONTH} * <li>{@code DAY_OF_YEAR} * <li>{@code EPOCH_DAY} * <li>{@code ALIGNED_WEEK_OF_MONTH} * <li>{@code ALIGNED_WEEK_OF_YEAR} * <li>{@code MONTH_OF_YEAR} * <li>{@code PROLEPTIC_MONTH} * <li>{@code YEAR_OF_ERA} * <li>{@code YEAR} * <li>{@code ERA} * </ul> * All other {@code ChronoField} instances will return false. * <p> * If the field is not a {@code ChronoField}, then the result of this method * is obtained by invoking {@code TemporalField.isSupportedBy(TemporalAccessor)} * passing {@code this} as the argument. * Whether the field is supported is determined by the field. * * @param TemporalField $field the field to check, null returns false * @return bool true if the field is supported on this date-time, false if not */ public function isSupported(TemporalField $field) { if ($field instanceof ChronoField) { $f = $field; return $f->isDateBased() || $f->isTimeBased(); } return $field != null && $field->isSupportedBy($this); }
/** * Returns a copy of this year-month with the specified field set to a new value. * <p> * This returns a {@code YearMonth}, based on this one, with the value * for the specified field changed. * This can be used to change any supported field, such as the year or month. * If it is not possible to set the value, because the field is not supported or for * some other reason, an exception is thrown. * <p> * If the field is a {@link ChronoField} then the adjustment is implemented here. * The supported fields behave as follows: * <ul> * <li>{@code MONTH_OF_YEAR} - * Returns a {@code YearMonth} with the specified month-of-year. * The year will be unchanged. * <li>{@code PROLEPTIC_MONTH} - * Returns a {@code YearMonth} with the specified proleptic-month. * This completely replaces the year and month of this object. * <li>{@code YEAR_OF_ERA} - * Returns a {@code YearMonth} with the specified year-of-era * The month and era will be unchanged. * <li>{@code YEAR} - * Returns a {@code YearMonth} with the specified year. * The month will be unchanged. * <li>{@code ERA} - * Returns a {@code YearMonth} with the specified era. * The month and year-of-era will be unchanged. * </ul> * <p> * In all cases, if the new value is outside the valid range of values for the field * then a {@code DateTimeException} will be thrown. * <p> * All other {@code ChronoField} instances will throw an {@code UnsupportedTemporalTypeException}. * <p> * If the field is not a {@code ChronoField}, then the result of this method * is obtained by invoking {@code TemporalField.adjustInto(Temporal, long)} * passing {@code this} as the argument. In this case, the field determines * whether and how to adjust the instant. * <p> * This instance is immutable and unaffected by this method call. * * @param TemporalField $field the field to set in the result, not null * @param int $newValue the new value of the field in the result * @return YearMonth a {@code YearMonth} based on {@code this} with the specified field set, not null * @throws DateTimeException if the field cannot be set * @throws UnsupportedTemporalTypeException if the field is not supported * @throws ArithmeticException if numeric overflow occurs */ public function with(TemporalField $field, $newValue) { if ($field instanceof ChronoField) { $f = $field; $f->checkValidValue($newValue); switch ($f) { case ChronoField::MONTH_OF_YEAR(): return $this->withMonth((int) $newValue); case ChronoField::PROLEPTIC_MONTH(): return $this->plusMonths($newValue - $this->getProlepticMonth()); case ChronoField::YEAR_OF_ERA(): return $this->withYear((int) ($this->year < 1 ? 1 - $newValue : $newValue)); case ChronoField::YEAR(): return $this->withYear((int) $newValue); case ChronoField::ERA(): return $this->getLong(ChronoField::ERA()) == $newValue ? $this : $this->withYear(1 - $this->year); } throw new UnsupportedTemporalTypeException("Unsupported field: " . $field); } return $field->adjustInto($this, $newValue); }
/** * @dataProvider data_samples */ public function test_samples_set(TemporalField $field, LocalDate $date, $value) { $this->assertEquals($field->adjustInto(LocalDate::MAX(), $value), $date); $this->assertEquals($field->adjustInto(LocalDate::MIN(), $value), $date); $this->assertEquals($field->adjustInto(self::JAN01_1970(), $value), $date); $this->assertEquals($field->adjustInto(self::DEC31_1969(), $value), $date); $this->assertEquals($field->adjustInto(self::NOV12_1945(), $value), $date); }
/** * Gets the range of valid values for the specified field. * <p> * The range object expresses the minimum and maximum valid values for a field. * This time is used to enhance the accuracy of the returned range. * If it is not possible to return the range, because the field is not supported * or for some other reason, an exception is thrown. * <p> * If the field is a {@link ChronoField} then the query is implemented here. * The {@link #isSupported(TemporalField) supported fields} will return * appropriate range instances. * All other {@code ChronoField} instances will throw an {@code UnsupportedTemporalTypeException}. * <p> * If the field is not a {@code ChronoField}, then the result of this method * is obtained by invoking {@code TemporalField.rangeRefinedBy(TemporalAccessor)} * passing {@code this} as the argument. * Whether the range can be obtained is determined by the field. * * @param TemporalField $field the field to query the range for, not null * @return ValueRange the range of valid values for the field, not null * @throws DateTimeException if the range for the field cannot be obtained * @throws UnsupportedTemporalTypeException if the field is not supported */ public function range(TemporalField $field) { if ($field instanceof ChronoField) { if ($field == ChronoField::OFFSET_SECONDS()) { return $field->range(); } return $this->time->range($field); } return $field->rangeRefinedBy($this); }