예제 #1
0
 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");
 }
예제 #2
0
 /**
  * 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_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);
 }
예제 #4
0
 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]];
 }
예제 #5
0
 protected function resolveYAD(FieldValues $fieldValues, ResolverStyle $resolverStyle)
 {
     $y = $this->range(ChronoField::YEAR())->checkValidIntValue($fieldValues->remove(ChronoField::YEAR()), ChronoField::YEAR());
     if ($resolverStyle == ResolverStyle::LENIENT()) {
         $weeks = Math::subtractExact($fieldValues->remove(ChronoField::ALIGNED_WEEK_OF_YEAR()), 1);
         $dow = Math::subtractExact($fieldValues->remove(ChronoField::DAY_OF_WEEK()), 1);
         return $this->resolveAligned($this->dateYearDay($y, 1), 0, $weeks, $dow);
     }
     $aw = $this->range(ChronoField::ALIGNED_WEEK_OF_YEAR())->checkValidIntValue($fieldValues->remove(ChronoField::ALIGNED_WEEK_OF_YEAR()), ChronoField::ALIGNED_WEEK_OF_YEAR());
     $dow = $this->range(ChronoField::DAY_OF_WEEK())->checkValidIntValue($fieldValues->remove(ChronoField::DAY_OF_WEEK()), ChronoField::DAY_OF_WEEK());
     $date = $this->dateYearDay($y, 1)->plus(($aw - 1) * 7, ChronoUnit::DAYS())->adjust(TemporalAdjusters::nextOrSame(DayOfWeek::of($dow)));
     if ($resolverStyle == ResolverStyle::STRICT() && $date->get(ChronoField::YEAR()) != $y) {
         throw new DateTimeException("Strict mode rejected resolved date as it is in a different year");
     }
     return $date;
 }
 function data_resolveFourToDate()
 {
     return [[ChronoField::YEAR(), 2012, ChronoField::MONTH_OF_YEAR(), 2, ChronoField::ALIGNED_WEEK_OF_MONTH(), 1, ChronoField::ALIGNED_DAY_OF_WEEK_IN_MONTH(), 1, LocalDate::of(2012, 2, 1)], [ChronoField::YEAR(), 2012, ChronoField::MONTH_OF_YEAR(), 2, ChronoField::ALIGNED_WEEK_OF_MONTH(), 1, ChronoField::DAY_OF_WEEK(), 3, LocalDate::of(2012, 2, 1)], [ChronoField::YEAR(), 2012, ChronoField::MONTH_OF_YEAR(), 2, ChronoField::DAY_OF_MONTH(), 1, ChronoField::DAY_OF_WEEK(), 3, LocalDate::of(2012, 2, 1)], [ChronoField::YEAR(), 2012, ChronoField::ALIGNED_WEEK_OF_YEAR(), 5, ChronoField::ALIGNED_DAY_OF_WEEK_IN_YEAR(), 4, ChronoField::DAY_OF_WEEK(), 3, LocalDate::of(2012, 2, 1)], [ChronoField::YEAR(), 2012, ChronoField::ALIGNED_WEEK_OF_YEAR(), 5, ChronoField::DAY_OF_WEEK(), 3, ChronoField::DAY_OF_MONTH(), 1, LocalDate::of(2012, 2, 1)]];
 }
 function data_normalized()
 {
     return [[ChronoField::YEAR(), 2127, ChronoField::YEAR(), 2127], [ChronoField::MONTH_OF_YEAR(), 12, ChronoField::MONTH_OF_YEAR(), 12], [ChronoField::DAY_OF_YEAR(), 127, ChronoField::DAY_OF_YEAR(), 127], [ChronoField::DAY_OF_MONTH(), 23, ChronoField::DAY_OF_MONTH(), 23], [ChronoField::DAY_OF_WEEK(), 127, ChronoField::DAY_OF_WEEK(), 127], [ChronoField::ALIGNED_WEEK_OF_YEAR(), 23, ChronoField::ALIGNED_WEEK_OF_YEAR(), 23], [ChronoField::ALIGNED_DAY_OF_WEEK_IN_YEAR(), 4, ChronoField::ALIGNED_DAY_OF_WEEK_IN_YEAR(), 4], [ChronoField::ALIGNED_WEEK_OF_MONTH(), 4, ChronoField::ALIGNED_WEEK_OF_MONTH(), 4], [ChronoField::ALIGNED_DAY_OF_WEEK_IN_MONTH(), 3, ChronoField::ALIGNED_DAY_OF_WEEK_IN_MONTH(), 3], [ChronoField::PROLEPTIC_MONTH(), 27, ChronoField::PROLEPTIC_MONTH(), null], [ChronoField::PROLEPTIC_MONTH(), 27, ChronoField::YEAR(), 2], [ChronoField::PROLEPTIC_MONTH(), 27, ChronoField::MONTH_OF_YEAR(), 4]];
 }
예제 #8
0
 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);
 }