예제 #1
0
 public function __toString()
 {
     // getLong() reduces chances of exceptions in toString()
     $yoe = $this->getLong(CF::YEAR_OF_ERA());
     $moy = $this->getLong(CF::MONTH_OF_YEAR());
     $dom = $this->getLong(CF::DAY_OF_MONTH());
     return $this->getChronology()->__toString() . " " . $this->getEra() . " " . $yoe . ($moy < 10 ? "-0" : "-") . $moy . ($dom < 10 ? "-0" : "-") . $dom;
 }
예제 #2
0
 public function test_getLong_TemporalField()
 {
     $this->assertEquals(self::TEST_2008_06()->getLong(CF::YEAR()), 2008);
     $this->assertEquals(self::TEST_2008_06()->getLong(CF::MONTH_OF_YEAR()), 6);
     $this->assertEquals(self::TEST_2008_06()->getLong(CF::YEAR_OF_ERA()), 2008);
     $this->assertEquals(self::TEST_2008_06()->getLong(CF::ERA()), 1);
     $this->assertEquals(self::TEST_2008_06()->getLong(CF::PROLEPTIC_MONTH()), 2008 * 12 + 6 - 1);
 }
예제 #3
0
 /**
  * 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);
 }
예제 #4
0
 protected function resolveYearOfEra(FieldValues $fieldValues, ResolverStyle $resolverStyle)
 {
     $yoeLong = $fieldValues->remove(CF::YEAR_OF_ERA());
     if ($yoeLong !== null) {
         if ($resolverStyle != ResolverStyle::LENIENT()) {
             CF::YEAR_OF_ERA()->checkValidValue($yoeLong);
         }
         $era = $fieldValues->remove(CF::ERA());
         if ($era === null) {
             $year = $fieldValues->get(CF::YEAR());
             if ($resolverStyle == ResolverStyle::STRICT()) {
                 // do not invent era if strict, but do cross-check with year
                 if ($year !== null) {
                     $this->addFieldValue($fieldValues, CF::YEAR(), $year > 0 ? $yoeLong : Math::subtractExact(1, $yoeLong));
                 } else {
                     // reinstate the field removed earlier, no cross-check issues
                     $fieldValues->put(CF::YEAR_OF_ERA(), $yoeLong);
                 }
             } else {
                 // invent era
                 $this->addFieldValue($fieldValues, CF::YEAR(), $year === null || $year > 0 ? $yoeLong : Math::subtractExact(1, $yoeLong));
             }
         } else {
             if ($era === 1) {
                 $this->addFieldValue($fieldValues, CF::YEAR(), $yoeLong);
             } else {
                 if ($era === 0) {
                     $this->addFieldValue($fieldValues, CF::YEAR(), Math::subtractExact(1, $yoeLong));
                 } else {
                     throw new DateTimeException("Invalid value for era: " . $era);
                 }
             }
         }
     } else {
         if ($fieldValues->has(CF::ERA())) {
             CF::ERA()->checkValidValue($fieldValues->get(CF::ERA()));
             // always validated
         }
     }
     return null;
 }
예제 #5
0
 public function test_with()
 {
     $base = Year::of(5);
     $result = $base->with(ChronoField::ERA(), 0);
     $ad = $base->adjust(IsoEra::of(0));
     $this->assertEquals($result, $ad);
     $prolepticYear = IsoChronology::INSTANCE()->prolepticYear(IsoEra::of(0), 5);
     $this->assertEquals($result->get(ChronoField::ERA()), 0);
     $this->assertEquals($result->get(ChronoField::YEAR()), $prolepticYear);
     $this->assertEquals($result->get(ChronoField::YEAR_OF_ERA()), 5);
     $result = $base->with(ChronoField::YEAR(), 10);
     $this->assertEquals($result->get(ChronoField::ERA()), $base->get(ChronoField::ERA()));
     $this->assertEquals($result->get(ChronoField::YEAR()), 10);
     $this->assertEquals($result->get(ChronoField::YEAR_OF_ERA()), 10);
     $result = $base->with(ChronoField::YEAR_OF_ERA(), 20);
     $this->assertEquals($result->get(ChronoField::ERA()), $base->get(ChronoField::ERA()));
     $this->assertEquals($result->get(ChronoField::YEAR()), 20);
     $this->assertEquals($result->get(ChronoField::YEAR_OF_ERA()), 20);
 }
예제 #6
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]];
 }
예제 #7
0
 /**
  * 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);
 }
예제 #8
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);
 }
 /**
  * @dataProvider provider_patternLocalDate
  */
 public function test_parse_textLocalDate($pattern, $text, $pos, $expectedPos, LocalDate $expectedValue)
 {
     $ppos = new ParsePosition($pos);
     $b = (new DateTimeFormatterBuilder())->appendPattern($pattern);
     $dtf = $b->toFormatter2($this->locale);
     $parsed = $dtf->parseUnresolved($text, $ppos);
     if ($ppos->getErrorIndex() != -1) {
         $this->assertEquals($ppos->getErrorIndex(), $expectedPos);
     } else {
         $this->assertEquals($ppos->getIndex(), $expectedPos, "Incorrect ending parse position");
         $this->assertEquals($parsed->isSupported(ChronoField::YEAR_OF_ERA()), true);
         $this->assertEquals($parsed->isSupported(WeekFields::ofLocale($this->locale)->dayOfWeek()), true);
         $this->assertEquals($parsed->isSupported(WeekFields::ofLocale($this->locale)->weekOfMonth()) || $parsed->isSupported(WeekFields::ofLocale($this->locale)->weekOfYear()), true);
         // ensure combination resolves into a date
         $result = LocalDate::parseWith($text, $dtf);
         $this->assertEquals($result, $expectedValue, "LocalDate incorrect for " . $pattern);
     }
 }
예제 #10
0
 /**
  * @dataProvider data_resolve_yearOfEra
  */
 public function test_resolve_yearOfEra(ResolverStyle $style, $e, $yoe, $y, $field, $expected)
 {
     $fieldValues = new FieldValues();
     if ($e !== null) {
         $fieldValues->put(ChronoField::ERA(), $e);
     }
     if ($yoe !== null) {
         $fieldValues->put(ChronoField::YEAR_OF_ERA(), $yoe);
     }
     if ($y !== null) {
         $fieldValues->put(ChronoField::YEAR(), $y);
     }
     if ($field !== null) {
         $date = IsoChronology::INSTANCE()->resolveDate($fieldValues, $style);
         $this->assertEquals($date, null);
         $this->assertEquals($fieldValues->get($field), $expected);
         $this->assertEquals($fieldValues->size(), 1);
     } else {
         try {
             IsoChronology::INSTANCE()->resolveDate($fieldValues, $style);
             $this->{$this}->fail("Should have failed");
         } catch (DateTimeException $ex) {
             // $expected
         }
     }
 }
 function data_resolveThreeToDate()
 {
     return [[ChronoField::YEAR(), 2012, ChronoField::MONTH_OF_YEAR(), 2, ChronoField::DAY_OF_MONTH(), 1, LocalDate::of(2012, 2, 1)], [ChronoField::YEAR(), 2012, ChronoField::ALIGNED_WEEK_OF_YEAR(), 5, ChronoField::ALIGNED_DAY_OF_WEEK_IN_YEAR(), 4, LocalDate::of(2012, 2, 1)], [ChronoField::YEAR(), 2012, ChronoField::ALIGNED_WEEK_OF_YEAR(), 5, ChronoField::DAY_OF_WEEK(), 3, LocalDate::of(2012, 2, 1)], [ChronoField::YEAR(), 2012, ChronoField::DAY_OF_YEAR(), 32, ChronoField::DAY_OF_MONTH(), 1, LocalDate::of(2012, 2, 1)], [ChronoField::YEAR_OF_ERA(), 2012, ChronoField::DAY_OF_YEAR(), 32, ChronoField::DAY_OF_MONTH(), 1, LocalDate::of(2012, 2, 1)], [ChronoField::YEAR(), 2012, ChronoField::DAY_OF_YEAR(), 32, ChronoField::DAY_OF_WEEK(), 3, LocalDate::of(2012, 2, 1)], [ChronoField::PROLEPTIC_MONTH(), 2012 * 12 + (2 - 1), ChronoField::DAY_OF_MONTH(), 25, ChronoField::DAY_OF_WEEK(), 6, LocalDate::of(2012, 2, 25)]];
 }
예제 #12
0
 /**
  * @dataProvider provider_reducedWithChrono
  */
 public function test_reducedWithChronoYearOfEra(ChronoLocalDate $date)
 {
     $chrono = $date->getChronology();
     $df = (new DateTimeFormatterBuilder())->appendValueReduced2(ChronoField::YEAR_OF_ERA(), 2, 2, LocalDate::of(2000, 1, 1))->toFormatter()->withChronology($chrono);
     $expected = $date->get(ChronoField::YEAR_OF_ERA());
     $input = $df->format($date);
     $pos = new ParsePosition(0);
     $parsed = $df->parseUnresolved($input, $pos);
     $actual = $parsed->get(ChronoField::YEAR_OF_ERA());
     $this->assertEquals($actual, $expected, "Wrong date parsed, chrono: " . $chrono . ", input: " . $input);
 }
예제 #13
0
 /**
  * @dataProvider data_resolverStyle
  */
 public function test_resolverStyle($str, ResolverStyle $style, $expectedEx, $year, $month, $day)
 {
     $builder = new DateTimeFormatterBuilder();
     $builder->appendValue(ChronoField::YEAR_OF_ERA());
     $builder->appendLiteral("/");
     $builder->appendValue(ChronoField::MONTH_OF_YEAR());
     $builder->appendLiteral("/");
     $builder->appendValue(ChronoField::DAY_OF_MONTH());
     $eraMap = [1 => "CE", 0 => "BCE"];
     $optionalFormatter = (new DateTimeFormatterBuilder())->appendLiteral(" ")->appendText3(ChronoField::ERA(), $eraMap)->toFormatter();
     $formatter = $builder->appendOptional($optionalFormatter)->toFormatter();
     $formatter = $formatter->withResolverStyle($style);
     if ($expectedEx == null) {
         $accessor = $formatter->parse($str);
         $this->assertEquals($accessor->get(ChronoField::YEAR_OF_ERA()), $year);
         $this->assertEquals($accessor->get(ChronoField::MONTH_OF_YEAR()), $month);
         $this->assertEquals($accessor->get(ChronoField::DAY_OF_MONTH()), $day);
     } else {
         try {
             $formatter->parse($str);
             $this->fail();
         } catch (\Exception $ex) {
             $this->assertInstanceOf($expectedEx, $ex);
         }
     }
 }
예제 #14
0
 public function with(TemporalField $field, $newValue)
 {
     if ($field instanceof ChronoField) {
         $f = $field;
         if ($this->getLong($f) === $newValue) {
             return $this;
         }
         switch ($f) {
             case CF::PROLEPTIC_MONTH():
                 $this->getChronology()->range($f)->checkValidValue($newValue, $f);
                 return $this->plusMonths($newValue - $this->getProlepticMonth());
             case CF::YEAR_OF_ERA():
             case CF::YEAR():
             case ERA:
                 $nvalue = $this->getChronology()->range($f)->checkValidIntValue($newValue, $f);
                 switch ($f) {
                     case CF::YEAR_OF_ERA():
                         return $this->withDate($this->isoDate->withYear(($this->getProlepticYear() >= 1 ? $nvalue : 1 - $nvalue) - ThaiBuddhistChronology::YEARS_DIFFERENCE));
                     case CF::YEAR():
                         return $this->withDate($this->isoDate->withYear($nvalue - ThaiBuddhistChronology::YEARS_DIFFERENCE));
                     case ERA:
                         return $this->withDate($this->isoDate->withYear(1 - $this->getProlepticYear() - ThaiBuddhistChronology::YEARS_DIFFERENCE));
                 }
         }
         return $this->withDate($this->isoDate->with($field, $newValue));
     }
     return parent::with($field, $newValue);
 }
예제 #15
0
 protected function resolveYearOfEra(FieldValues $fieldValues, ResolverStyle $resolverStyle)
 {
     $yoeLong = $fieldValues->remove(ChronoField::YEAR_OF_ERA());
     if ($yoeLong != null) {
         $eraLong = $fieldValues->remove(ChronoField::ERA());
         if ($resolverStyle != ResolverStyle::LENIENT()) {
             $yoe = $this->range(ChronoField::YEAR_OF_ERA())->checkValidIntValue($yoeLong, ChronoField::YEAR_OF_ERA());
         } else {
             $yoe = Math::toIntExact($yoeLong);
         }
         if ($eraLong != null) {
             $eraObj = $this->eraOf($this->range(ChronoField::ERA())->checkValidIntValue($eraLong, ChronoField::ERA()));
             self::addFieldValue($fieldValues, ChronoField::YEAR(), $this->prolepticYear($eraObj, $yoe));
         } else {
             if ($fieldValues->has(ChronoField::YEAR())) {
                 $year = $this->range(ChronoField::YEAR())->checkValidIntValue($fieldValues[ChronoField::YEAR()->__toString()][1], ChronoField::YEAR());
                 $chronoDate = $this->dateYearDay($year, 1);
                 self::addFieldValue($fieldValues, ChronoField::YEAR(), $this->prolepticYear($chronoDate->getEra(), $yoe));
             } else {
                 if ($resolverStyle == ResolverStyle::STRICT()) {
                     // do not invent era if strict
                     // reinstate the field removed earlier, no cross-check issues
                     $fieldValues[ChronoField::YEAR_OF_ERA()->__toString()][1] = $yoeLong;
                 } else {
                     $eras = $this->eras();
                     if (empty($eras)) {
                         self::addFieldValue($fieldValues, ChronoField::YEAR(), $yoe);
                     } else {
                         $eraObj = $eras[count($eras) - 1];
                         $this->addFieldValue($fieldValues, ChronoField::YEAR(), $this->prolepticYear($eraObj, $yoe));
                     }
                 }
             }
         }
     } else {
         if ($fieldValues->has(ChronoField::ERA())) {
             $this->range(ChronoField::ERA())->checkValidValue($fieldValues[ChronoField::ERA()->__toString()][1], ChronoField::ERA());
             // always validated
         }
     }
     return null;
 }
예제 #16
0
 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);
 }
예제 #17
0
 public function test_getLong_TemporalField()
 {
     $test = LocalDate::of(2008, 6, 30);
     $this->assertEquals($test->getLong(CF::YEAR()), 2008);
     $this->assertEquals($test->getLong(CF::MONTH_OF_YEAR()), 6);
     $this->assertEquals($test->getLong(CF::YEAR_OF_ERA()), 2008);
     $this->assertEquals($test->getLong(CF::ERA()), 1);
     $this->assertEquals($test->getLong(CF::PROLEPTIC_MONTH()), 2008 * 12 + 6 - 1);
     $this->assertEquals($test->getLong(CF::DAY_OF_MONTH()), 30);
     $this->assertEquals($test->getLong(CF::DAY_OF_WEEK()), 1);
     $this->assertEquals($test->getLong(CF::DAY_OF_YEAR()), 182);
 }
 public function range(ChronoField $field)
 {
     switch ($field) {
         case CF::PROLEPTIC_MONTH():
             $range = CF::PROLEPTIC_MONTH()->range();
             return ValueRange::of($range->getMinimum() + self::YEARS_DIFFERENCE * 12, $range->getMaximum() + self::YEARS_DIFFERENCE * 12);
         case CF::YEAR_OF_ERA():
             $range = CF::YEAR()->range();
             return ValueRange::ofVariable(1, -($range->getMinimum() + self::YEARS_DIFFERENCE) + 1, $range->getMaximum() + self::YEARS_DIFFERENCE);
         case CF::YEAR():
             $range = CF::YEAR()->range();
             return ValueRange::of($range->getMinimum() + self::YEARS_DIFFERENCE, $range->getMaximum() + self::YEARS_DIFFERENCE);
     }
     return $field->range();
 }
 public static function init()
 {
     self::$FIELD_MAP = ['G' => ChronoField::ERA(), 'y' => ChronoField::YEAR_OF_ERA(), 'u' => ChronoField::YEAR(), 'Q' => IsoFields::QUARTER_OF_YEAR(), 'q' => IsoFields::QUARTER_OF_YEAR(), 'M' => ChronoField::MONTH_OF_YEAR(), 'L' => ChronoField::MONTH_OF_YEAR(), 'D' => ChronoField::DAY_OF_YEAR(), 'd' => ChronoField::DAY_OF_MONTH(), 'F' => ChronoField::ALIGNED_DAY_OF_WEEK_IN_MONTH(), 'E' => ChronoField::DAY_OF_WEEK(), 'c' => ChronoField::DAY_OF_WEEK(), 'e' => ChronoField::DAY_OF_WEEK(), 'a' => ChronoField::AMPM_OF_DAY(), 'H' => ChronoField::HOUR_OF_DAY(), 'k' => ChronoField::CLOCK_HOUR_OF_DAY(), 'K' => ChronoField::HOUR_OF_AMPM(), 'h' => ChronoField::CLOCK_HOUR_OF_AMPM(), 'm' => ChronoField::MINUTE_OF_HOUR(), 's' => ChronoField::SECOND_OF_MINUTE(), 'S' => ChronoField::NANO_OF_SECOND(), 'A' => ChronoField::MILLI_OF_DAY(), 'n' => ChronoField::NANO_OF_SECOND(), 'N' => ChronoField::NANO_OF_DAY()];
 }