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)); }
/** * @dataProvider data_instantNoZone */ public function test_parse_instantNoZone_supported(DateTimeFormatter $formatter, $text, Instant $expected) { $actual = $formatter->parse($text); $this->assertEquals($actual->isSupported(CF::INSTANT_SECONDS()), true); $this->assertEquals($actual->isSupported(CF::EPOCH_DAY()), false); $this->assertEquals($actual->isSupported(CF::SECOND_OF_DAY()), false); $this->assertEquals($actual->isSupported(CF::NANO_OF_SECOND()), true); $this->assertEquals($actual->isSupported(CF::MICRO_OF_SECOND()), true); $this->assertEquals($actual->isSupported(CF::MILLI_OF_SECOND()), true); }
public function isSupportedBy(TemporalAccessor $temporal) { return $temporal->isSupported(ChronoField::EPOCH_DAY()) && IsoFields::isIso($temporal); }
function data_fieldBased() { return [[CF::DAY_OF_WEEK(), true, false], [CF::ALIGNED_DAY_OF_WEEK_IN_MONTH(), true, false], [CF::ALIGNED_DAY_OF_WEEK_IN_YEAR(), true, false], [CF::DAY_OF_MONTH(), true, false], [CF::DAY_OF_YEAR(), true, false], [CF::EPOCH_DAY(), true, false], [CF::ALIGNED_WEEK_OF_MONTH(), true, false], [CF::ALIGNED_WEEK_OF_YEAR(), true, false], [CF::MONTH_OF_YEAR(), true, false], [CF::PROLEPTIC_MONTH(), true, false], [CF::YEAR_OF_ERA(), true, false], [CF::YEAR(), true, false], [CF::ERA(), true, false], [CF::AMPM_OF_DAY(), false, true], [CF::CLOCK_HOUR_OF_DAY(), false, true], [CF::HOUR_OF_DAY(), false, true], [CF::CLOCK_HOUR_OF_AMPM(), false, true], [CF::HOUR_OF_AMPM(), false, true], [CF::MINUTE_OF_DAY(), false, true], [CF::MINUTE_OF_HOUR(), false, true], [CF::SECOND_OF_DAY(), false, true], [CF::SECOND_OF_MINUTE(), false, true], [CF::MILLI_OF_DAY(), false, true], [CF::MILLI_OF_SECOND(), false, true], [CF::MICRO_OF_DAY(), false, true], [CF::MICRO_OF_SECOND(), false, true], [CF::NANO_OF_DAY(), false, true], [CF::NANO_OF_SECOND(), false, true]]; }
/** * Resolves parsed {@code ChronoField} values into a date during parsing. * <p> * Most {@code TemporalField} implementations are resolved using the * resolve method on the field. By contrast, the {@code ChronoField} class * defines fields that only have meaning relative to the chronology. * As such, {@code ChronoField} date fields are resolved here in the * context of a specific chronology. * <p> * {@code ChronoField} instances are resolved by this method, which may * be overridden in subclasses. * <ul> * <li>{@code EPOCH_DAY} - If present, this is converted to a date and * all other date fields are then cross-checked against the date. * <li>{@code PROLEPTIC_MONTH} - If present, then it is split into the * {@code YEAR} and {@code MONTH_OF_YEAR}. If the mode is strict or smart * then the field is validated. * <li>{@code YEAR_OF_ERA} and {@code ERA} - If both are present, then they * are combined to form a {@code YEAR}. In lenient mode, the {@code YEAR_OF_ERA} * range is not validated, in smart and strict mode it is. The {@code ERA} is * validated for range in all three modes. If only the {@code YEAR_OF_ERA} is * present, and the mode is smart or lenient, then the last available era * is assumed. In strict mode, no era is assumed and the {@code YEAR_OF_ERA} is * left untouched. If only the {@code ERA} is present, then it is left untouched. * <li>{@code YEAR}, {@code MONTH_OF_YEAR} and {@code DAY_OF_MONTH} - * If all three are present, then they are combined to form a date. * In all three modes, the {@code YEAR} is validated. * If the mode is smart or strict, then the month and day are validated. * If the mode is lenient, then the date is combined in a manner equivalent to * creating a date on the first day of the first month in the requested year, * then adding the difference in months, then the difference in days. * If the mode is smart, and the day-of-month is greater than the maximum for * the year-month, then the day-of-month is adjusted to the last day-of-month. * If the mode is strict, then the three fields must form a valid date. * <li>{@code YEAR} and {@code DAY_OF_YEAR} - * If both are present, then they are combined to form a date. * In all three modes, the {@code YEAR} is validated. * If the mode is lenient, then the date is combined in a manner equivalent to * creating a date on the first day of the requested year, then adding * the difference in days. * If the mode is smart or strict, then the two fields must form a valid date. * <li>{@code YEAR}, {@code MONTH_OF_YEAR}, {@code ALIGNED_WEEK_OF_MONTH} and * {@code ALIGNED_DAY_OF_WEEK_IN_MONTH} - * If all four are present, then they are combined to form a date. * In all three modes, the {@code YEAR} is validated. * If the mode is lenient, then the date is combined in a manner equivalent to * creating a date on the first day of the first month in the requested year, then adding * the difference in months, then the difference in weeks, then in days. * If the mode is smart or strict, then the all four fields are validated to * their outer ranges. The date is then combined in a manner equivalent to * creating a date on the first day of the requested year and month, then adding * the amount in weeks and days to reach their values. If the mode is strict, * the date is additionally validated to check that the day and week adjustment * did not change the month. * <li>{@code YEAR}, {@code MONTH_OF_YEAR}, {@code ALIGNED_WEEK_OF_MONTH} and * {@code DAY_OF_WEEK} - If all four are present, then they are combined to * form a date. The approach is the same as described above for * years, months and weeks in {@code ALIGNED_DAY_OF_WEEK_IN_MONTH}. * The day-of-week is adjusted as the next or same matching day-of-week once * the years, months and weeks have been handled. * <li>{@code YEAR}, {@code ALIGNED_WEEK_OF_YEAR} and {@code ALIGNED_DAY_OF_WEEK_IN_YEAR} - * If all three are present, then they are combined to form a date. * In all three modes, the {@code YEAR} is validated. * If the mode is lenient, then the date is combined in a manner equivalent to * creating a date on the first day of the requested year, then adding * the difference in weeks, then in days. * If the mode is smart or strict, then the all three fields are validated to * their outer ranges. The date is then combined in a manner equivalent to * creating a date on the first day of the requested year, then adding * the amount in weeks and days to reach their values. If the mode is strict, * the date is additionally validated to check that the day and week adjustment * did not change the year. * <li>{@code YEAR}, {@code ALIGNED_WEEK_OF_YEAR} and {@code DAY_OF_WEEK} - * If all three are present, then they are combined to form a date. * The approach is the same as described above for years and weeks in * {@code ALIGNED_DAY_OF_WEEK_IN_YEAR}. The day-of-week is adjusted as the * next or same matching day-of-week once the years and weeks have been handled. * </ul> * <p> * The default implementation is suitable for most calendar systems. * If {@link java.time.temporal.ChronoField#YEAR_OF_ERA} is found without an {@link java.time.temporal.ChronoField#ERA} * then the last era in {@link #eras()} is used. * The implementation assumes a 7 day week, that the first day-of-month * has the value 1, that first day-of-year has the value 1, and that the * first of the month and year always exists. * * @param FieldValues $fieldValues the map of fields to values, which can be updated, not null * @param ResolverStyle $resolverStyle the requested type of resolve, not null * @return ChronoLocalDate the resolved date, null if insufficient information to create a date * @throws DateTimeException if the date cannot be resolved, typically * because of a conflict in the input data */ public function resolveDate(FieldValues $fieldValues, ResolverStyle $resolverStyle) { // check epoch-day before inventing era if ($fieldValues->has(ChronoField::EPOCH_DAY())) { return $this->dateEpochDay($fieldValues->remove(ChronoField::EPOCH_DAY())); } // fix proleptic month before inventing era $this->resolveProlepticMonth($fieldValues, $resolverStyle); // invent era if necessary to resolve year-of-era $resolved = $this->resolveYearOfEra($fieldValues, $resolverStyle); if ($resolved !== null) { return $resolved; } // build date if ($fieldValues->has(ChronoField::YEAR())) { if ($fieldValues->has(ChronoField::MONTH_OF_YEAR())) { if ($fieldValues->has(ChronoField::DAY_OF_MONTH())) { return $this->resolveYMD($fieldValues, $resolverStyle); } if ($fieldValues->has(ChronoField::ALIGNED_WEEK_OF_MONTH())) { if ($fieldValues->has(ChronoField::ALIGNED_DAY_OF_WEEK_IN_MONTH())) { return $this->resolveYMAA($fieldValues, $resolverStyle); } if ($fieldValues->has(ChronoField::DAY_OF_WEEK())) { return $this->resolveYMAD($fieldValues, $resolverStyle); } } } if ($fieldValues->has(ChronoField::DAY_OF_YEAR())) { return $this->resolveYD($fieldValues, $resolverStyle); } if ($fieldValues->has(ChronoField::ALIGNED_WEEK_OF_YEAR())) { if ($fieldValues->has(ChronoField::ALIGNED_DAY_OF_WEEK_IN_YEAR())) { return $this->resolveYAA($fieldValues, $resolverStyle); } if ($fieldValues->has(ChronoField::DAY_OF_WEEK())) { return $this->resolveYAD($fieldValues, $resolverStyle); } } } return null; }
/** * @inheritdoc */ public function toEpochDay() { return $this->getLong(ChronoField::EPOCH_DAY()); }
/** * 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); }
public function test_withZone_parsedZone_override() { $f = (new DateTimeFormatterBuilder())->parseDefaulting(ChronoField::EPOCH_DAY(), 2)->appendZoneId()->toFormatter(); $f = $f->withZone(self::EUROPE_ATHENS()); $accessor = $f->parse("Europe/Paris"); $this->assertEquals($accessor->query(TemporalQueries::localDate()), LocalDate::of(1970, 1, 3)); $this->assertEquals($accessor->query(TemporalQueries::localTime()), null); $this->assertEquals($accessor->query(TemporalQueries::zoneId()), self::EUROPE_PARIS()); }
public function isSupportedBy(TemporalAccessor $temporal) { return $temporal->isSupported(ChronoField::EPOCH_DAY()) && AbstractChronology::from($temporal)->equals(IsoChronology::INSTANCE()); }
function data_samples() { return [[ChronoField::EPOCH_DAY(), self::JAN01_1970(), 0], [JulianFields::JULIAN_DAY(), self::JAN01_1970(), 2400001 + 40587], [JulianFields::MODIFIED_JULIAN_DAY(), self::JAN01_1970(), 40587], [JulianFields::RATA_DIE(), self::JAN01_1970(), 710347 + (40587 - 31771)], [ChronoField::EPOCH_DAY(), self::DEC31_1969(), -1], [JulianFields::JULIAN_DAY(), self::DEC31_1969(), 2400001 + 40586], [JulianFields::MODIFIED_JULIAN_DAY(), self::DEC31_1969(), 40586], [JulianFields::RATA_DIE(), self::DEC31_1969(), 710347 + (40586 - 31771)], [ChronoField::EPOCH_DAY(), self::NOV12_1945(), -24 * 365 - 6 - 31 - 30 + 11], [JulianFields::JULIAN_DAY(), self::NOV12_1945(), 2431772], [JulianFields::MODIFIED_JULIAN_DAY(), self::NOV12_1945(), 31771], [JulianFields::RATA_DIE(), self::NOV12_1945(), 710347], [ChronoField::EPOCH_DAY(), self::JAN01_0001(), -24 * 365 - 6 - 31 - 30 + 11 - 710346], [JulianFields::JULIAN_DAY(), self::JAN01_0001(), 2431772 - 710346], [JulianFields::MODIFIED_JULIAN_DAY(), self::JAN01_0001(), 31771 - 710346], [JulianFields::RATA_DIE(), self::JAN01_0001(), 1]]; }
function data_combine() { return [[ChronoField::YEAR(), 2012, ChronoField::MONTH_OF_YEAR(), 6, ChronoField::DAY_OF_MONTH(), 3, null, null, LocalDate::class, LocalDate::of(2012, 6, 3)], [ChronoField::PROLEPTIC_MONTH(), 2012 * 12 + 6 - 1, ChronoField::DAY_OF_MONTH(), 3, null, null, null, null, LocalDate::class, LocalDate::of(2012, 6, 3)], [ChronoField::YEAR(), 2012, ChronoField::ALIGNED_WEEK_OF_YEAR(), 6, ChronoField::DAY_OF_WEEK(), 3, null, null, LocalDate::class, LocalDate::of(2012, 2, 8)], [ChronoField::YEAR(), 2012, ChronoField::DAY_OF_YEAR(), 155, null, null, null, null, LocalDate::class, LocalDate::of(2012, 6, 3)], [ChronoField::YEAR(), 2012, ChronoField::MONTH_OF_YEAR(), 6, null, null, null, null, LocalDate::class, null], [ChronoField::EPOCH_DAY(), 12, null, null, null, null, null, null, LocalDate::class, LocalDate::of(1970, 1, 13)]]; }
/** * @inheritdoc */ public function adjustInto(Temporal $temporal) { return $temporal->with(ChronoField::EPOCH_DAY(), $this->toLocalDate()->toEpochDay())->with(ChronoField::NANO_OF_DAY(), $this->toLocalTime()->toNanoOfDay()); }
public function test_isSupported_TemporalField() { // TODO check //$this->assertEquals(TEST_2008.isSupported((TemporalField) null()), false); $this->assertEquals(self::$TEST_2008->isSupported(ChronoField::NANO_OF_SECOND()), false); $this->assertEquals(self::$TEST_2008->isSupported(ChronoField::NANO_OF_DAY()), false); $this->assertEquals(self::$TEST_2008->isSupported(ChronoField::MICRO_OF_SECOND()), false); $this->assertEquals(self::$TEST_2008->isSupported(ChronoField::MICRO_OF_DAY()), false); $this->assertEquals(self::$TEST_2008->isSupported(ChronoField::MILLI_OF_SECOND()), false); $this->assertEquals(self::$TEST_2008->isSupported(ChronoField::MILLI_OF_DAY()), false); $this->assertEquals(self::$TEST_2008->isSupported(ChronoField::SECOND_OF_MINUTE()), false); $this->assertEquals(self::$TEST_2008->isSupported(ChronoField::SECOND_OF_DAY()), false); $this->assertEquals(self::$TEST_2008->isSupported(ChronoField::MINUTE_OF_HOUR()), false); $this->assertEquals(self::$TEST_2008->isSupported(ChronoField::MINUTE_OF_DAY()), false); $this->assertEquals(self::$TEST_2008->isSupported(ChronoField::HOUR_OF_AMPM()), false); $this->assertEquals(self::$TEST_2008->isSupported(ChronoField::CLOCK_HOUR_OF_AMPM()), false); $this->assertEquals(self::$TEST_2008->isSupported(ChronoField::HOUR_OF_DAY()), false); $this->assertEquals(self::$TEST_2008->isSupported(ChronoField::CLOCK_HOUR_OF_DAY()), false); $this->assertEquals(self::$TEST_2008->isSupported(ChronoField::AMPM_OF_DAY()), false); $this->assertEquals(self::$TEST_2008->isSupported(ChronoField::DAY_OF_WEEK()), false); $this->assertEquals(self::$TEST_2008->isSupported(ChronoField::ALIGNED_DAY_OF_WEEK_IN_MONTH()), false); $this->assertEquals(self::$TEST_2008->isSupported(ChronoField::ALIGNED_DAY_OF_WEEK_IN_YEAR()), false); $this->assertEquals(self::$TEST_2008->isSupported(ChronoField::DAY_OF_MONTH()), false); $this->assertEquals(self::$TEST_2008->isSupported(ChronoField::DAY_OF_YEAR()), false); $this->assertEquals(self::$TEST_2008->isSupported(ChronoField::EPOCH_DAY()), false); $this->assertEquals(self::$TEST_2008->isSupported(ChronoField::ALIGNED_WEEK_OF_MONTH()), false); $this->assertEquals(self::$TEST_2008->isSupported(ChronoField::ALIGNED_WEEK_OF_YEAR()), false); $this->assertEquals(self::$TEST_2008->isSupported(ChronoField::MONTH_OF_YEAR()), false); $this->assertEquals(self::$TEST_2008->isSupported(ChronoField::PROLEPTIC_MONTH()), false); $this->assertEquals(self::$TEST_2008->isSupported(ChronoField::YEAR()), true); $this->assertEquals(self::$TEST_2008->isSupported(ChronoField::YEAR_OF_ERA()), true); $this->assertEquals(self::$TEST_2008->isSupported(ChronoField::ERA()), true); $this->assertEquals(self::$TEST_2008->isSupported(ChronoField::INSTANT_SECONDS()), false); $this->assertEquals(self::$TEST_2008->isSupported(ChronoField::OFFSET_SECONDS()), false); }
public function until(Temporal $endExclusive, TemporalUnit $unit) { $end = $this->getChronology()->localDateTime($endExclusive); if ($unit instanceof ChronoUnit) { if ($unit->isTimeBased()) { $amount = $end->getLong(ChronoField::EPOCH_DAY()) - $this->date->getLong(ChronoField::EPOCH_DAY()); switch ($unit) { case ChronoUnit::NANOS(): $amount = Math::multiplyExact($amount, self::NANOS_PER_DAY); break; case ChronoUnit::MICROS(): $amount = Math::multiplyExact($amount, self::MICROS_PER_DAY); break; case ChronoUnit::MILLIS(): $amount = Math::multiplyExact($amount, self::MILLIS_PER_DAY); break; case ChronoUnit::SECONDS(): $amount = Math::multiplyExact($amount, self::SECONDS_PER_DAY); break; case ChronoUnit::MINUTES(): $amount = Math::multiplyExact($amount, self::MINUTES_PER_DAY); break; case ChronoUnit::HOURS(): $amount = Math::multiplyExact($amount, self::HOURS_PER_DAY); break; case ChronoUnit::HALF_DAYS(): $amount = Math::multiplyExact($amount, 2); break; } return Math::addExact($amount, $this->time->until($end->toLocalTime(), $unit)); } $endDate = $end->toLocalDate(); if ($end->toLocalTime()->isBefore($this->time)) { $endDate = $endDate->minus(1, ChronoUnit::DAYS()); } return $this->date->until($endDate, $unit); } return $unit->between($this, $end); }
/** * A query for {@code LocalDate} returning null if not found. * @param TemporalAccessor $temporal * @return null|LocalDate */ public static function _localDate(TemporalAccessor $temporal) { if ($temporal->isSupported(ChronoField::EPOCH_DAY())) { return LocalDate::ofEpochDay($temporal->getLong(ChronoField::EPOCH_DAY())); } return null; }
public function getLong(TemporalField $field) { if ($field == ChronoField::EPOCH_DAY()) { return LocalDate::of(2012, 6, 30)->toEpochDay(); } throw new UnsupportedOperationException(); }
public function test_isSupported_TemporalField() { // TODO $this->assertEquals($this->TEST_DATE_TIME->isSupported(null), false); $this->assertEquals($this->TEST_DATE_TIME->isSupported(CF::NANO_OF_SECOND()), true); $this->assertEquals($this->TEST_DATE_TIME->isSupported(CF::NANO_OF_DAY()), true); $this->assertEquals($this->TEST_DATE_TIME->isSupported(CF::MICRO_OF_SECOND()), true); $this->assertEquals($this->TEST_DATE_TIME->isSupported(CF::MICRO_OF_DAY()), true); $this->assertEquals($this->TEST_DATE_TIME->isSupported(CF::MILLI_OF_SECOND()), true); $this->assertEquals($this->TEST_DATE_TIME->isSupported(CF::MILLI_OF_DAY()), true); $this->assertEquals($this->TEST_DATE_TIME->isSupported(CF::SECOND_OF_MINUTE()), true); $this->assertEquals($this->TEST_DATE_TIME->isSupported(CF::SECOND_OF_DAY()), true); $this->assertEquals($this->TEST_DATE_TIME->isSupported(CF::MINUTE_OF_HOUR()), true); $this->assertEquals($this->TEST_DATE_TIME->isSupported(CF::MINUTE_OF_DAY()), true); $this->assertEquals($this->TEST_DATE_TIME->isSupported(CF::HOUR_OF_AMPM()), true); $this->assertEquals($this->TEST_DATE_TIME->isSupported(CF::CLOCK_HOUR_OF_AMPM()), true); $this->assertEquals($this->TEST_DATE_TIME->isSupported(CF::HOUR_OF_DAY()), true); $this->assertEquals($this->TEST_DATE_TIME->isSupported(CF::CLOCK_HOUR_OF_DAY()), true); $this->assertEquals($this->TEST_DATE_TIME->isSupported(CF::AMPM_OF_DAY()), true); $this->assertEquals($this->TEST_DATE_TIME->isSupported(CF::DAY_OF_WEEK()), true); $this->assertEquals($this->TEST_DATE_TIME->isSupported(CF::ALIGNED_DAY_OF_WEEK_IN_MONTH()), true); $this->assertEquals($this->TEST_DATE_TIME->isSupported(CF::ALIGNED_DAY_OF_WEEK_IN_YEAR()), true); $this->assertEquals($this->TEST_DATE_TIME->isSupported(CF::DAY_OF_MONTH()), true); $this->assertEquals($this->TEST_DATE_TIME->isSupported(CF::DAY_OF_YEAR()), true); $this->assertEquals($this->TEST_DATE_TIME->isSupported(CF::EPOCH_DAY()), true); $this->assertEquals($this->TEST_DATE_TIME->isSupported(CF::ALIGNED_WEEK_OF_MONTH()), true); $this->assertEquals($this->TEST_DATE_TIME->isSupported(CF::ALIGNED_WEEK_OF_YEAR()), true); $this->assertEquals($this->TEST_DATE_TIME->isSupported(CF::MONTH_OF_YEAR()), true); $this->assertEquals($this->TEST_DATE_TIME->isSupported(CF::PROLEPTIC_MONTH()), true); $this->assertEquals($this->TEST_DATE_TIME->isSupported(CF::YEAR()), true); $this->assertEquals($this->TEST_DATE_TIME->isSupported(CF::YEAR_OF_ERA()), true); $this->assertEquals($this->TEST_DATE_TIME->isSupported(CF::ERA()), true); $this->assertEquals($this->TEST_DATE_TIME->isSupported(CF::INSTANT_SECONDS()), true); $this->assertEquals($this->TEST_DATE_TIME->isSupported(CF::OFFSET_SECONDS()), true); }
private static function adjust(TemporalAccessor $temporal, DateTimeFormatter $formatter) { // normal case first (early return is an optimization) $overrideChrono = $formatter->getChronology(); $overrideZone = $formatter->getZone(); if ($overrideChrono == null && $overrideZone == null) { return $temporal; } // ensure minimal change (early return is an optimization) $temporalChrono = $temporal->query(TemporalQueries::chronology()); $temporalZone = $temporal->query(TemporalQueries::zoneId()); if ($temporalChrono !== null && $temporalChrono->equals($overrideChrono)) { $overrideChrono = null; } if ($temporalZone !== null && $temporalZone->equals($overrideZone)) { $overrideZone = null; } if ($overrideChrono === null && $overrideZone === null) { return $temporal; } // make adjustment $effectiveChrono = $overrideChrono != null ? $overrideChrono : $temporalChrono; if ($overrideZone != null) { // if have zone and instant, calculation is simple, defaulting chrono if necessary if ($temporal->isSupported(ChronoField::INSTANT_SECONDS())) { $chrono = $effectiveChrono != null ? $effectiveChrono : IsoChronology::INSTANCE(); return $chrono->zonedDateTime(Instant::from($temporal), $overrideZone); } // block changing zone on OffsetTime, and similar problem cases if ($overrideZone->normalized() instanceof ZoneOffset && $temporal->isSupported(ChronoField::OFFSET_SECONDS()) && $temporal->get(ChronoField::OFFSET_SECONDS()) != $overrideZone->getRules()->getOffset(Instant::EPOCH())->getTotalSeconds()) { throw new DateTimeException("Unable to apply override zone '" . $overrideZone . "' because the temporal object being formatted has a different offset but" . " does not represent an instant: " . $temporal); } } $effectiveZone = $overrideZone !== null ? $overrideZone : $temporalZone; $effectiveDate = null; if ($overrideChrono !== null) { if ($temporal->isSupported(ChronoField::EPOCH_DAY())) { $effectiveDate = $effectiveChrono->dateFrom($temporal); } else { // check for date fields other than epoch-day, ignoring case of converting null to ISO if (!($overrideChrono == IsoChronology::INSTANCE() && $temporalChrono === null)) { foreach (ChronoField::values() as $f) { if ($f->isDateBased() && $temporal->isSupported($f)) { throw new DateTimeException("Unable to apply override chronology '" . $overrideChrono . "' because the temporal object being formatted contains date fields but" . " does not represent a whole date: " . $temporal); } } } $effectiveDate = null; } } else { $effectiveDate = null; } // combine available data // this is a non-standard temporal that is almost a pure delegate // this better handles map-like underlying temporal instances return new Test($effectiveDate, $temporal, $effectiveZone, $effectiveChrono); }
/** * 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()); }
public function isSupportedBy(Temporal $temporal) { return $temporal->isSupported(ChronoField::EPOCH_DAY()); }