public function adjustInto(Temporal $temporal, $newValue) { if ($this->range()->isValidValue($newValue) == false) { throw new DateTimeException("Invalid value: " . $this->name . " " . $newValue); } return $temporal->with(CF::EPOCH_DAY(), Math::subtractExact($newValue, $this->offset)); }
public function adjustInto(Temporal $temporal, $newValue) { // calls getFrom() to check if supported $curValue = $this->getFrom($temporal); $this->range()->checkValidValue($newValue, $this); // strictly check from 1 to 4 return $temporal->with(ChronoField::MONTH_OF_YEAR(), $temporal->getLong(ChronoField::MONTH_OF_YEAR()) + ($newValue - $curValue) * 3); }
/** * @inheritdoc */ public function getLong(TemporalField $field) { if ($field instanceof ChronoField) { switch ($field) { case ChronoField::INSTANT_SECONDS(): return $this->toEpochSecond(); case ChronoField::OFFSET_SECONDS(): return $this->getOffset()->getTotalSeconds(); } return $this->toLocalDateTime()->getLong($field); } return $field->getFrom($this); }
/** * @dataProvider data_signStyle */ public function test_signStyle(LocalDate $localDate, SignStyle $style, $expectedEx, $expectedStr) { $builder = new DateTimeFormatterBuilder(); $formatter = $builder->appendValue3(ChronoField::YEAR(), 2, 4, $style)->toFormatter(); $formatter = $formatter->withZone(ZoneOffset::UTC()); if ($expectedEx === null) { $output = $formatter->format($localDate); $this->assertEquals($output, $expectedStr); } else { try { $formatter->format($localDate); $this->fail(); } catch (\Exception $ex) { $this->assertInstanceOf($expectedEx, $ex); } } }
public function format(DateTimePrintContext $context, &$buf) { /** @var ZoneID $zone */ $zone = $context->getValue(TemporalQueries::zoneId()); if ($zone === null) { return false; } $zname = $zone->getId(); if (!$zone instanceof ZoneOffset) { $dt = $context->getTemporal(); $name = $this->getDisplayName($zname, $dt->isSupported(ChronoField::INSTANT_SECONDS()) ? $zone->getRules()->isDaylightSavings(Instant::from($dt)) ? self::$DST : self::$STD : self::$GENERIC, $context->getLocale()); if ($name !== null) { $zname = $name; } } $buf .= $zname; return true; }
public function adjustInto(Temporal $temporal, $newValue) { if ($this->isSupportedBy($temporal) === false) { throw new UnsupportedTemporalTypeException("Unsupported field: WeekBasedYear"); } $newWby = $this->range()->checkValidIntValue($newValue, IsoFields::WEEK_BASED_YEAR()); // strict check $date = LocalDate::from($temporal); $dow = $date->get(ChronoField::DAY_OF_WEEK()); $week = IsoFields::getWeek($date); if ($week == 53 && IsoFields::getWeekRangeInt($newWby) == 52) { $week = 52; } $resolved = LocalDate::of($newWby, 1, 4); // 4th is guaranteed to be in week one $days = $dow - $resolved->get(ChronoField::DAY_OF_WEEK()) + ($week - 1) * 7; $resolved = $resolved->plusDays($days); return $temporal->adjust($resolved); }
/** * 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()); }
private function assertParsed(TemporalAccessor $parsed, ZoneOffset $expectedOffset) { if ($expectedOffset === null) { $this->assertEquals(null, $parsed); } else { $this->assertEquals(true, $parsed->isSupported(ChronoField::OFFSET_SECONDS()), true); $this->assertEquals($expectedOffset->getTotalSeconds(), $parsed->getLong(ChronoField::OFFSET_SECONDS())); } }
/** * Adds a multi-year transition rule to the current window. * <p> * This adds a rule such that the offset, expressed as a daylight savings amount, * changes at the specified date-time for each year in the range. * * @param int $startYear the start year of the rule, from MIN_VALUE to MAX_VALUE * @param int $endYear the end year of the rule, from MIN_VALUE to MAX_VALUE * @param Month $month the month of the transition, not null * @param int $dayOfMonthIndicator the day-of-month of the transition, adjusted by dayOfWeek, * from 1 to 31 adjusted later, or -1 to -28 adjusted earlier from the last day of the month * @param DayOfWeek|null $dayOfWeek the day-of-week to adjust to, null if day-of-month should not be adjusted * @param LocalTime $time the time that the transition occurs as defined by timeDefintion, not null * @param bool $timeEndOfDay whether midnight is at the end of day * @param TimeDefinition $timeDefinition the definition of how to convert local to actual time, not null * @param int $savingAmountSecs the amount of saving from the standard offset after the transition in seconds * @return ZoneRulesBuilder $this, for chaining * @throws DateTimeException if a date-time field is out of range * @throws IllegalArgumentException if the day of month indicator is invalid * @throws IllegalArgumentException if the end of day midnight flag does not match the time * @throws \LogicException if no window has yet been added * @throws \LogicException if the window already has fixed savings * @throws \LogicException if the window has reached the maximum capacity of 2000 rules */ public function addRuleToWindow9($startYear, $endYear, Month $month, $dayOfMonthIndicator, $dayOfWeek, LocalTime $time, $timeEndOfDay, TimeDefinition $timeDefinition, $savingAmountSecs) { ChronoField::YEAR()->checkValidValue($startYear); ChronoField::YEAR()->checkValidValue($endYear); if ($dayOfMonthIndicator < -28 || $dayOfMonthIndicator > 31 || $dayOfMonthIndicator === 0) { throw new IllegalArgumentException("Day of month indicator must be between -28 and 31 inclusive excluding zero"); } if ($timeEndOfDay && $time->equals(LocalTime::MIDNIGHT()) == false) { throw new IllegalArgumentException("Time must be midnight when end of day flag is true"); } if (empty($this->windowList)) { throw new \LogicException("Must add a window before adding a rule"); } $window = $this->windowList[count($this->windowList) - 1]; $window->addRule($startYear, $endYear, $month, $dayOfMonthIndicator, $dayOfWeek, $time, $timeEndOfDay, $timeDefinition, $savingAmountSecs); return $this; }
public function data_text() { return [[ChronoField::DAY_OF_WEEK(), 1, TextStyle::SHORT(), self::enUS(), "Mon"], [ChronoField::DAY_OF_WEEK(), 2, TextStyle::SHORT(), self::enUS(), "Tue"], [ChronoField::DAY_OF_WEEK(), 3, TextStyle::SHORT(), self::enUS(), "Wed"], [ChronoField::DAY_OF_WEEK(), 4, TextStyle::SHORT(), self::enUS(), "Thu"], [ChronoField::DAY_OF_WEEK(), 5, TextStyle::SHORT(), self::enUS(), "Fri"], [ChronoField::DAY_OF_WEEK(), 6, TextStyle::SHORT(), self::enUS(), "Sat"], [ChronoField::DAY_OF_WEEK(), 7, TextStyle::SHORT(), self::enUS(), "Sun"], [ChronoField::DAY_OF_WEEK(), 1, TextStyle::SHORT(), self::ptBR(), "seg"], [ChronoField::DAY_OF_WEEK(), 2, TextStyle::SHORT(), self::ptBR(), "ter"], [ChronoField::DAY_OF_WEEK(), 3, TextStyle::SHORT(), self::ptBR(), "qua"], [ChronoField::DAY_OF_WEEK(), 4, TextStyle::SHORT(), self::ptBR(), "qui"], [ChronoField::DAY_OF_WEEK(), 5, TextStyle::SHORT(), self::ptBR(), "sex"], [ChronoField::DAY_OF_WEEK(), 6, TextStyle::SHORT(), self::ptBR(), "sáb"], [ChronoField::DAY_OF_WEEK(), 7, TextStyle::SHORT(), self::ptBR(), "dom"], [ChronoField::DAY_OF_WEEK(), 1, TextStyle::FULL(), self::enUS(), "Monday"], [ChronoField::DAY_OF_WEEK(), 2, TextStyle::FULL(), self::enUS(), "Tuesday"], [ChronoField::DAY_OF_WEEK(), 3, TextStyle::FULL(), self::enUS(), "Wednesday"], [ChronoField::DAY_OF_WEEK(), 4, TextStyle::FULL(), self::enUS(), "Thursday"], [ChronoField::DAY_OF_WEEK(), 5, TextStyle::FULL(), self::enUS(), "Friday"], [ChronoField::DAY_OF_WEEK(), 6, TextStyle::FULL(), self::enUS(), "Saturday"], [ChronoField::DAY_OF_WEEK(), 7, TextStyle::FULL(), self::enUS(), "Sunday"], [ChronoField::DAY_OF_WEEK(), 1, TextStyle::FULL(), self::ptBR(), "segunda-feira"], [ChronoField::DAY_OF_WEEK(), 2, TextStyle::FULL(), self::ptBR(), "terça-feira"], [ChronoField::DAY_OF_WEEK(), 3, TextStyle::FULL(), self::ptBR(), "quarta-feira"], [ChronoField::DAY_OF_WEEK(), 4, TextStyle::FULL(), self::ptBR(), "quinta-feira"], [ChronoField::DAY_OF_WEEK(), 5, TextStyle::FULL(), self::ptBR(), "sexta-feira"], [ChronoField::DAY_OF_WEEK(), 6, TextStyle::FULL(), self::ptBR(), "sábado"], [ChronoField::DAY_OF_WEEK(), 7, TextStyle::FULL(), self::ptBR(), "domingo"], [ChronoField::MONTH_OF_YEAR(), 1, TextStyle::SHORT(), self::enUS(), "Jan"], [ChronoField::MONTH_OF_YEAR(), 2, TextStyle::SHORT(), self::enUS(), "Feb"], [ChronoField::MONTH_OF_YEAR(), 3, TextStyle::SHORT(), self::enUS(), "Mar"], [ChronoField::MONTH_OF_YEAR(), 4, TextStyle::SHORT(), self::enUS(), "Apr"], [ChronoField::MONTH_OF_YEAR(), 5, TextStyle::SHORT(), self::enUS(), "May"], [ChronoField::MONTH_OF_YEAR(), 6, TextStyle::SHORT(), self::enUS(), "Jun"], [ChronoField::MONTH_OF_YEAR(), 7, TextStyle::SHORT(), self::enUS(), "Jul"], [ChronoField::MONTH_OF_YEAR(), 8, TextStyle::SHORT(), self::enUS(), "Aug"], [ChronoField::MONTH_OF_YEAR(), 9, TextStyle::SHORT(), self::enUS(), "Sep"], [ChronoField::MONTH_OF_YEAR(), 10, TextStyle::SHORT(), self::enUS(), "Oct"], [ChronoField::MONTH_OF_YEAR(), 11, TextStyle::SHORT(), self::enUS(), "Nov"], [ChronoField::MONTH_OF_YEAR(), 12, TextStyle::SHORT(), self::enUS(), "Dec"], [ChronoField::MONTH_OF_YEAR(), 1, TextStyle::SHORT(), self::ptBR(), "jan"], [ChronoField::MONTH_OF_YEAR(), 2, TextStyle::SHORT(), self::ptBR(), "fev"], [ChronoField::MONTH_OF_YEAR(), 3, TextStyle::SHORT(), self::ptBR(), "mar"], [ChronoField::MONTH_OF_YEAR(), 4, TextStyle::SHORT(), self::ptBR(), "abr"], [ChronoField::MONTH_OF_YEAR(), 5, TextStyle::SHORT(), self::ptBR(), "mai"], [ChronoField::MONTH_OF_YEAR(), 6, TextStyle::SHORT(), self::ptBR(), "jun"], [ChronoField::MONTH_OF_YEAR(), 7, TextStyle::SHORT(), self::ptBR(), "jul"], [ChronoField::MONTH_OF_YEAR(), 8, TextStyle::SHORT(), self::ptBR(), "ago"], [ChronoField::MONTH_OF_YEAR(), 9, TextStyle::SHORT(), self::ptBR(), "set"], [ChronoField::MONTH_OF_YEAR(), 10, TextStyle::SHORT(), self::ptBR(), "out"], [ChronoField::MONTH_OF_YEAR(), 11, TextStyle::SHORT(), self::ptBR(), "nov"], [ChronoField::MONTH_OF_YEAR(), 12, TextStyle::SHORT(), self::ptBR(), "dez"], [ChronoField::MONTH_OF_YEAR(), 1, TextStyle::FULL(), self::enUS(), "January"], [ChronoField::MONTH_OF_YEAR(), 2, TextStyle::FULL(), self::enUS(), "February"], [ChronoField::MONTH_OF_YEAR(), 3, TextStyle::FULL(), self::enUS(), "March"], [ChronoField::MONTH_OF_YEAR(), 4, TextStyle::FULL(), self::enUS(), "April"], [ChronoField::MONTH_OF_YEAR(), 5, TextStyle::FULL(), self::enUS(), "May"], [ChronoField::MONTH_OF_YEAR(), 6, TextStyle::FULL(), self::enUS(), "June"], [ChronoField::MONTH_OF_YEAR(), 7, TextStyle::FULL(), self::enUS(), "July"], [ChronoField::MONTH_OF_YEAR(), 8, TextStyle::FULL(), self::enUS(), "August"], [ChronoField::MONTH_OF_YEAR(), 9, TextStyle::FULL(), self::enUS(), "September"], [ChronoField::MONTH_OF_YEAR(), 10, TextStyle::FULL(), self::enUS(), "October"], [ChronoField::MONTH_OF_YEAR(), 11, TextStyle::FULL(), self::enUS(), "November"], [ChronoField::MONTH_OF_YEAR(), 12, TextStyle::FULL(), self::enUS(), "December"], [ChronoField::MONTH_OF_YEAR(), 1, TextStyle::FULL(), self::ptBR(), "janeiro"], [ChronoField::MONTH_OF_YEAR(), 2, TextStyle::FULL(), self::ptBR(), "fevereiro"], [ChronoField::MONTH_OF_YEAR(), 3, TextStyle::FULL(), self::ptBR(), "março"], [ChronoField::MONTH_OF_YEAR(), 4, TextStyle::FULL(), self::ptBR(), "abril"], [ChronoField::MONTH_OF_YEAR(), 5, TextStyle::FULL(), self::ptBR(), "maio"], [ChronoField::MONTH_OF_YEAR(), 6, TextStyle::FULL(), self::ptBR(), "junho"], [ChronoField::MONTH_OF_YEAR(), 7, TextStyle::FULL(), self::ptBR(), "julho"], [ChronoField::MONTH_OF_YEAR(), 8, TextStyle::FULL(), self::ptBR(), "agosto"], [ChronoField::MONTH_OF_YEAR(), 9, TextStyle::FULL(), self::ptBR(), "setembro"], [ChronoField::MONTH_OF_YEAR(), 10, TextStyle::FULL(), self::ptBR(), "outubro"], [ChronoField::MONTH_OF_YEAR(), 11, TextStyle::FULL(), self::ptBR(), "novembro"], [ChronoField::MONTH_OF_YEAR(), 12, TextStyle::FULL(), self::ptBR(), "dezembro"], [ChronoField::AMPM_OF_DAY(), 0, TextStyle::SHORT(), self::enUS(), "AM"], [ChronoField::AMPM_OF_DAY(), 1, TextStyle::SHORT(), self::enUS(), "PM"]]; }
public function test_parseUnresolved_StringParsePosition_duplicateFieldDifferentValue() { $test = (new DateTimeFormatterBuilder())->appendValue(CF::MONTH_OF_YEAR())->appendLiteral('-')->appendValue(CF::MONTH_OF_YEAR())->toFormatter(); $pos = new ParsePosition(3); $result = $test->parseUnresolved("XXX6-7", $pos); $this->assertEquals($pos->getIndex(), 3); $this->assertEquals($pos->getErrorIndex(), 5); $this->assertEquals($result, null); }
public function test_toString3() { $this->assertEquals($this->getFormatterWidth(ChronoField::HOUR_OF_DAY(), 1, 2, SignStyle::NOT_NEGATIVE())->__toString(), "Value(HourOfDay,1,2,NOT_NEGATIVE)"); }
/** * Returns a copy of this duration with the specified nano-of-second. * <p> * This returns a duration with the specified nano-of-second, retaining the * seconds part of this duration. * <p> * This instance is immutable and unaffected by this method call. * * @param int $nanoOfSecond the nano-of-second to represent, from 0 to 999,999,999 * @return Duration a {@code Duration} based on this period with the requested nano-of-second, not null * @throws DateTimeException if the nano-of-second is invalid */ public function withNanos($nanoOfSecond) { ChronoField::NANO_OF_SECOND()->checkValidIntValue($nanoOfSecond); return self::create($this->seconds, $nanoOfSecond); }
/** * Calculates the amount of time until another year in terms of the specified unit. * <p> * This calculates the amount of time between two {@code Year} * objects in terms of a single {@code TemporalUnit}. * The start and end points are {@code this} and the specified year. * The result will be negative if the end is before the start. * The {@code Temporal} passed to this method is converted to a * {@code Year} using {@link #from(TemporalAccessor)}. * For example, the amount in decades between two year can be calculated * using {@code startYear.until(endYear, DECADES)}. * <p> * The calculation returns a whole number, representing the number of * complete units between the two years. * For example, the amount in decades between 2012 and 2031 * will only be one decade as it is one year short of two decades. * <p> * There are two equivalent ways of using this method. * The first is to invoke this method. * The second is to use {@link TemporalUnit#between(Temporal, Temporal)}: * <pre> * // these two lines are equivalent * amount = start.until(end, YEARS); * amount = YEARS.between(start, end); * </pre> * The choice should be made based on which makes the code more readable. * <p> * The calculation is implemented in this method for {@link ChronoUnit}. * The units {@code YEARS}, {@code DECADES}, {@code CENTURIES}, * {@code MILLENNIA} and {@code ERAS} are supported. * Other {@code ChronoUnit} values will throw an exception. * <p> * If the unit is not a {@code ChronoUnit}, then the result of this method * is obtained by invoking {@code TemporalUnit.between(Temporal, Temporal)} * passing {@code this} as the first argument and the converted input temporal * as the second argument. * <p> * This instance is immutable and unaffected by this method call. * * @param Temporal $endExclusive the end date, exclusive, which is converted to a {@code Year}, not null * @param TemporalUnit $unit the unit to measure the amount in, not null * @return int the amount of time between this year and the end year * @throws DateTimeException if the amount cannot be calculated, or the end * temporal cannot be converted to a {@code Year} * @throws UnsupportedTemporalTypeException if the unit is not supported * @throws ArithmeticException if numeric overflow occurs */ public function until(Temporal $endExclusive, TemporalUnit $unit) { $end = Year::from($endExclusive); if ($unit instanceof ChronoUnit) { $yearsUntil = $end->year - $this->year; // no overflow switch ($unit) { case ChronoUnit::YEARS(): return $yearsUntil; case ChronoUnit::DECADES(): return Math::div($yearsUntil, 10); case ChronoUnit::CENTURIES(): return Math::div($yearsUntil, 100); case ChronoUnit::MILLENNIA(): return Math::div($yearsUntil, 1000); case ChronoUnit::ERAS(): return $end->getLong(ChronoField::ERA()) - $this->getLong(ChronoField::ERA()); } throw new UnsupportedTemporalTypeException("Unsupported unit: " . $unit); } return $unit->between($this, $end); }
/** * @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); }
/** * @dataProvider data_withFieldLong */ public function test_with_adjuster_ensureOffsetDateTimeConsistent(ZonedDateTime $base, TemporalField $setField, $setValue, ZonedDateTime $expected) { if ($setField == CF::OFFSET_SECONDS()) { $odt = $base->toOffsetDateTime()->with($setField, $setValue); $this->assertEquals($base->adjust($odt), $expected); } }
/** * Calculates the amount of time until another date in terms of the specified unit. * <p> * This calculates the amount of time between two {@code LocalDate} * objects in terms of a single {@code TemporalUnit}. * The start and end points are {@code this} and the specified date. * The result will be negative if the end is before the start. * The {@code Temporal} passed to this method is converted to a * {@code LocalDate} using {@link #from(TemporalAccessor)}. * For example, the amount in days between two dates can be calculated * using {@code startDate.until(endDate, DAYS)}. * <p> * The calculation returns a whole number, representing the number of * complete units between the two dates. * For example, the amount in months between 2012-06-15 and 2012-08-14 * will only be one month as it is one day short of two months. * <p> * There are two equivalent ways of using this method. * The first is to invoke this method. * The second is to use {@link TemporalUnit#between(Temporal, Temporal)}: * <pre> * // these two lines are equivalent * amount = start.until(end, MONTHS); * amount = MONTHS.between(start, end); * </pre> * The choice should be made based on which makes the code more readable. * <p> * The calculation is implemented in this method for {@link ChronoUnit}. * The units {@code DAYS}, {@code WEEKS}, {@code MONTHS}, {@code YEARS}, * {@code DECADES}, {@code CENTURIES}, {@code MILLENNIA} and {@code ERAS} * are supported. Other {@code ChronoUnit} values will throw an exception. * <p> * If the unit is not a {@code ChronoUnit}, then the result of this method * is obtained by invoking {@code TemporalUnit.between(Temporal, Temporal)} * passing {@code this} as the first argument and the converted input temporal * as the second argument. * <p> * This instance is immutable and unaffected by this method call. * * @param Temporal $endExclusive the end date, exclusive, which is converted to a {@code LocalDate}, not null * @param TemporalUnit $unit the unit to measure the amount in, not null * @return int the amount of time between this date and the end date * @throws DateTimeException if the amount cannot be calculated, or the end * temporal cannot be converted to a {@code LocalDate} * @throws UnsupportedTemporalTypeException if the unit is not supported * @throws ArithmeticException if numeric overflow occurs */ public function until(Temporal $endExclusive, TemporalUnit $unit) { $end = LocalDate::from($endExclusive); if ($unit instanceof ChronoUnit) { switch ($unit) { case ChronoUnit::DAYS(): return $this->daysUntil($end); case ChronoUnit::WEEKS(): return Math::div($this->daysUntil($end), 7); case ChronoUnit::MONTHS(): return $this->monthsUntil($end); case ChronoUnit::YEARS(): return Math::div($this->monthsUntil($end), 12); case ChronoUnit::DECADES(): return Math::div($this->monthsUntil($end), 120); case ChronoUnit::CENTURIES(): return Math::div($this->monthsUntil($end), 1200); case ChronoUnit::MILLENNIA(): return Math::div($this->monthsUntil($end), 12000); case ChronoUnit::ERAS(): return $end->getLong(ChronoField::ERA()) - $this->getLong(ChronoField::ERA()); } throw new UnsupportedTemporalTypeException("Unsupported unit: " . $unit); } return $unit->between($this, $end); }
public function range(CF $field) { return $field->range(); }
/** * Adjusts the specified temporal object to have the same time as this object. * <p> * This returns a temporal object of the same observable type as the input * with the time changed to be the same as this. * <p> * The adjustment is equivalent to using {@link Temporal#with(TemporalField, long)} * passing {@link ChronoField#NANO_OF_DAY} as the field. * <p> * In most cases, it is clearer to reverse the calling pattern by using * {@link Temporal#with(TemporalAdjuster)}: * <pre> * // these two lines are equivalent, but the second approach is recommended * temporal = thisLocalTime.adjustInto(temporal); * temporal = temporal.with(thisLocalTime); * </pre> * <p> * This instance is immutable and unaffected by this method call. * * @param Temporal $temporal the target object to be adjusted, not null * @return Temporal the adjusted object, not null * @throws DateTimeException if unable to make the adjustment * @throws ArithmeticException if numeric overflow occurs */ public function adjustInto(Temporal $temporal) { return $temporal->with(ChronoField::NANO_OF_DAY(), $this->toNanoOfDay()); }
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; }
* If no resolution is possible, the resolve method must return null. * <p> * When resolving time fields, the map will be altered and null returned. * When resolving date fields, the date is normally returned from the method, * with the map altered to remove the resolved fields. However, it would also * be acceptable for the date fields to be resolved into other {@code ChronoField} * instances that can produce a date, such as {@code EPOCH_DAY}. * <p> * Not all {@code TemporalAccessor} implementations are accepted as return values. * Implementations that call this method must accept {@code ChronoLocalDate}, * {@code ChronoLocalDateTime}, {@code ChronoZonedDateTime} and {@code LocalTime}. * <p> * The default implementation must return null. * * @param FieldValues $fieldValues the map of fields to values, which can be updated, not null * @param TemporalAccessor $partialTemporal the partially complete temporal to query for zone and * chronology; querying for other things is undefined and not recommended, not null * @param ResolverStyle $resolverStyle the requested type of resolve, not null * @return TemporalAccessor the resolved temporal object; null if resolving only * changed the map, or no resolve occurred * @throws ArithmeticException if numeric overflow occurs * @throws DateTimeException if resolving results in an error. This must not be thrown * by querying a field on the temporal without first checking if it is supported */ public function resolve(FieldValues $fieldValues, TemporalAccessor $partialTemporal, ResolverStyle $resolverStyle) { return null; } } ChronoField::init();
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); }
public function parse(DateTimeParseContext $context, $text, $position) { $pos = $position; $end = $pos + strlen($text); $gmtText = "GMT"; // TODO: get localized version of 'GMT' if ($gmtText !== null) { if (!$context->subSequenceEquals($text, $pos, $gmtText, 0, strlen($gmtText))) { return ~$position; } $pos += strlen($gmtText); } // parse normal plus/minus offset if ($pos == $end) { return $context->setParsedField(ChronoField::OFFSET_SECONDS(), 0, $position, $pos); } $sign = $text[$pos]; // IOOBE if invalid position if ($sign == '+') { $negative = 1; } else { if ($sign == '-') { $negative = -1; } else { return $context->setParsedField(ChronoField::OFFSET_SECONDS(), 0, $position, $pos); } } $pos++; $m = 0; $s = 0; if ($this->style == TextStyle::FULL()) { $h1 = $this->getDigit($text, $pos++); $h2 = $this->getDigit($text, $pos++); if ($h1 < 0 || $h2 < 0 || $text[$pos++] !== ':') { return ~$position; } $h = $h1 * 10 + $h2; $m1 = $this->getDigit($text, $pos++); $m2 = $this->getDigit($text, $pos++); if ($m1 < 0 || $m2 < 0) { return ~$position; } $m = $m1 * 10 + $m2; if ($pos + 2 < $end && $text[$pos] === ':') { $s1 = $this->getDigit($text, $pos + 1); $s2 = $this->getDigit($text, $pos + 2); if ($s1 >= 0 && $s2 >= 0) { $s = $s1 * 10 + $s2; $pos += 3; } } } else { $h = $this->getDigit($text, $pos++); if ($h < 0) { return ~$position; } if ($pos < $end) { $h2 = $this->getDigit($text, $pos); if ($h2 >= 0) { $h = $h * 10 + $h2; $pos++; } if ($pos + 2 < $end && $text[$pos] === ':') { if ($pos + 2 < $end && $text[$pos] === ':') { $m1 = $this->getDigit($text, $pos + 1); $m2 = $this->getDigit($text, $pos + 2); if ($m1 >= 0 && $m2 >= 0) { $m = $m1 * 10 + $m2; $pos += 3; if ($pos + 2 < $end && $text[$pos] === ':') { $s1 = $this->getDigit($text, $pos + 1); $s2 = $this->getDigit($text, $pos + 2); if ($s1 >= 0 && $s2 >= 0) { $s = $s1 * 10 + $s2; $pos += 3; } } } } } } } $offsetSecs = $negative * ($h * 3600 + $m * 60 + $s); return $context->setParsedField(ChronoField::OFFSET_SECONDS(), $offsetSecs, $position, $pos); }
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); }
/** * @dataProvider data_success */ public function test_parse_success($s, $caseSensitive, $text, $pos, $expectedPos) { $this->setCaseSensitive($caseSensitive); $ppos = new ParsePosition($pos); $parsed = $this->getFormatterString($s)->parseUnresolved($text, $ppos); if ($ppos->getErrorIndex() != -1) { $this->assertEquals($ppos->getIndex(), $expectedPos); } else { $this->assertEquals($ppos->getIndex(), $expectedPos); $this->assertEquals($parsed->isSupported(ChronoField::YEAR()), false); $this->assertEquals($parsed->query(TemporalQueries::chronology()), null); $this->assertEquals($parsed->query(TemporalQueries::zoneId()), null); } }
public function test_getLong_TemporalField() { $test = LocalDateTime::of(2008, 6, 30, 12, 30, 40, 987654321); $this->assertEquals($test->getLong(CF::YEAR()), 2008); $this->assertEquals($test->getLong(CF::MONTH_OF_YEAR()), 6); $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); $this->assertEquals($test->getLong(CF::HOUR_OF_DAY()), 12); $this->assertEquals($test->getLong(CF::MINUTE_OF_HOUR()), 30); $this->assertEquals($test->getLong(CF::SECOND_OF_MINUTE()), 40); $this->assertEquals($test->getLong(CF::NANO_OF_SECOND()), 987654321); $this->assertEquals($test->getLong(CF::HOUR_OF_AMPM()), 0); $this->assertEquals($test->getLong(CF::AMPM_OF_DAY()), 1); }
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(); }
/** * Adjusts the specified temporal object to have this instant. * <p> * This returns a temporal object of the same observable type as the input * with the instant changed to be the same as this. * <p> * The adjustment is equivalent to using {@link Temporal#with(TemporalField, long)} * twice, passing {@link ChronoField#INSTANT_SECONDS} and * {@link ChronoField#NANO_OF_SECOND} as the fields. * <p> * In most cases, it is clearer to reverse the calling pattern by using * {@link Temporal#with(TemporalAdjuster)}: * <pre> * // these two lines are equivalent, but the second approach is recommended * temporal = thisInstant.adjustInto(temporal); * temporal = temporal.with(thisInstant); * </pre> * <p> * This instance is immutable and unaffected by this method call. * * @param Temporal $temporal the target object to be adjusted, not null * @return Temporal the adjusted object, not null * @throws DateTimeException if unable to make the adjustment * @throws ArithmeticException if numeric overflow occurs */ public function adjustInto(Temporal $temporal) { return $temporal->with(ChronoField::INSTANT_SECONDS(), $this->seconds)->with(ChronoField::NANO_OF_SECOND(), $this->nanos); }
public function test_getLong_TemporalField() { $this->assertEquals(ZoneOffset::UTC()->getLong(ChronoField::OFFSET_SECONDS()), 0); $this->assertEquals(ZoneOffset::ofHours(-2)->getLong(ChronoField::OFFSET_SECONDS()), -7200); $this->assertEquals(ZoneOffset::ofHoursMinutesSeconds(0, 1, 5)->getLong(ChronoField::OFFSET_SECONDS()), 65); }
/** * A query for {@code LocalTime} returning null if not found. * @param TemporalAccessor $temporal * @return null|LocalTime */ public static function _localTime(TemporalAccessor $temporal) { if ($temporal->isSupported(ChronoField::NANO_OF_DAY())) { return LocalTime::ofNanoOfDay($temporal->getLong(ChronoField::NANO_OF_DAY())); } return null; }