/**
  * @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);
 }
예제 #2
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");
 }
 function data_withFieldLong()
 {
     return [[self::TEST_2008_6_30_11_30_59_000000500(), CF::YEAR(), 2009, OffsetDateTime::of(2009, 6, 30, 11, 30, 59, 500, self::OFFSET_PONE())], [self::TEST_2008_6_30_11_30_59_000000500(), CF::MONTH_OF_YEAR(), 7, OffsetDateTime::of(2008, 7, 30, 11, 30, 59, 500, self::OFFSET_PONE())], [self::TEST_2008_6_30_11_30_59_000000500(), CF::DAY_OF_MONTH(), 15, OffsetDateTime::of(2008, 6, 15, 11, 30, 59, 500, self::OFFSET_PONE())], [self::TEST_2008_6_30_11_30_59_000000500(), CF::HOUR_OF_DAY(), 14, OffsetDateTime::of(2008, 6, 30, 14, 30, 59, 500, self::OFFSET_PONE())], [self::TEST_2008_6_30_11_30_59_000000500(), CF::OFFSET_SECONDS(), -3600, OffsetDateTime::of(2008, 6, 30, 11, 30, 59, 500, self::OFFSET_MONE())]];
 }
 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);
 }
예제 #5
0
 /**
  * Parse an offset following a prefix and set the ZoneId if it is valid.
  * To matching the parsing of ZoneId.of the values are not normalized
  * to ZoneOffsets.
  *
  * @param DateTimeParseContext $context the parse context
  * @param string $text the input text
  * @param int $prefixPos start of the prefix
  * @param int $position start of text after the prefix
  * @param OffsetIdPrinterParser $parser parser for the value after the prefix
  * @return int the position after the parse
  */
 private function parseOffsetBased(DateTimeParseContext $context, $text, $prefixPos, $position, OffsetIdPrinterParser $parser)
 {
     $prefix = strtoupper(substr($text, $prefixPos, $position - $prefixPos));
     if ($position >= strlen($text)) {
         $context->setParsedZone(ZoneId::of($prefix));
         return $position;
     }
     // '0' or 'Z' after prefix is not part of a valid ZoneId; use bare prefix
     if ($text[$position] === '0' || $context->charEquals($text[$position], 'Z')) {
         $context->setParsedZone(ZoneId::of($prefix));
         return $position;
     }
     $newContext = $context->copy();
     $endPos = $parser->parse($newContext, $text, $position);
     try {
         if ($endPos < 0) {
             if ($parser == OffsetIdPrinterParser::INSTANCE_ID_Z()) {
                 return ~$prefixPos;
             }
             $context->setParsedZone(ZoneId::of($prefix));
             return $position;
         }
         $offset = $newContext->getParsed(ChronoField::OFFSET_SECONDS());
         $zoneOffset = ZoneOffset::ofTotalSeconds($offset);
         $context->setParsedZone(ZoneId::ofOffset($prefix, $zoneOffset));
         return $endPos;
     } catch (DateTimeException $dte) {
         return ~$prefixPos;
     }
 }
예제 #6
0
 function data_with_longTemporalField()
 {
     return [[Instant::ofEpochSecond(10, 200), CF::INSTANT_SECONDS(), 100, Instant::ofEpochSecond(100, 200), null], [Instant::ofEpochSecond(10, 200), CF::INSTANT_SECONDS(), 0, Instant::ofEpochSecond(0, 200), null], [Instant::ofEpochSecond(10, 200), CF::INSTANT_SECONDS(), -100, Instant::ofEpochSecond(-100, 200), null], [Instant::ofEpochSecond(10, 200), CF::NANO_OF_SECOND(), 100, Instant::ofEpochSecond(10, 100), null], [Instant::ofEpochSecond(10, 200), CF::NANO_OF_SECOND(), 0, Instant::ofEpochSecond(10), null], [Instant::ofEpochSecond(10, 200), CF::MICRO_OF_SECOND(), 100, Instant::ofEpochSecond(10, 100 * 1000), null], [Instant::ofEpochSecond(10, 200), CF::MICRO_OF_SECOND(), 0, Instant::ofEpochSecond(10), null], [Instant::ofEpochSecond(10, 200), CF::MILLI_OF_SECOND(), 100, Instant::ofEpochSecond(10, 100 * 1000 * 1000), null], [Instant::ofEpochSecond(10, 200), CF::MILLI_OF_SECOND(), 0, Instant::ofEpochSecond(10), null], [Instant::ofEpochSecond(10, 200), CF::NANO_OF_SECOND(), 1000000000, null, DateTimeException::class], [Instant::ofEpochSecond(10, 200), CF::MICRO_OF_SECOND(), 1000000, null, DateTimeException::class], [Instant::ofEpochSecond(10, 200), CF::MILLI_OF_SECOND(), 1000, null, DateTimeException::class], [Instant::ofEpochSecond(10, 200), CF::SECOND_OF_MINUTE(), 1, null, DateTimeException::class], [Instant::ofEpochSecond(10, 200), CF::SECOND_OF_DAY(), 1, null, DateTimeException::class], [Instant::ofEpochSecond(10, 200), CF::OFFSET_SECONDS(), 1, null, DateTimeException::class], [Instant::ofEpochSecond(10, 200), CF::NANO_OF_DAY(), 1, null, DateTimeException::class], [Instant::ofEpochSecond(10, 200), CF::MINUTE_OF_HOUR(), 1, null, DateTimeException::class], [Instant::ofEpochSecond(10, 200), CF::MINUTE_OF_DAY(), 1, null, DateTimeException::class], [Instant::ofEpochSecond(10, 200), CF::MILLI_OF_DAY(), 1, null, DateTimeException::class], [Instant::ofEpochSecond(10, 200), CF::MICRO_OF_DAY(), 1, null, DateTimeException::class]];
 }
예제 #7
0
 /**
  * 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 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);
 }
예제 #9
0
 /**
  * A query for {@code ZoneOffset} returning null if not found.
  * @param TemporalAccessor $temporal
  * @return null|ZoneOffset
  * @throws DateTimeException
  */
 public static function _offset(TemporalAccessor $temporal)
 {
     if ($temporal->isSupported(ChronoField::OFFSET_SECONDS())) {
         return ZoneOffset::ofTotalSeconds($temporal->get(ChronoField::OFFSET_SECONDS()));
     }
     return null;
 }
 public function with(TemporalField $field, $newValue)
 {
     if ($field instanceof ChronoField) {
         $f = $field;
         switch ($f) {
             case ChronoField::INSTANT_SECONDS():
                 return $this->plus($newValue - $this->toEpochSecond(), ChronoUnit::SECONDS());
             case ChronoField::OFFSET_SECONDS():
                 $offset = ZoneOffset::ofTotalSeconds($f->checkValidIntValue($newValue));
                 return $this->create($this->dateTime->toInstant($offset), $this->zone);
         }
         return $this->ofBest($this->dateTime->with($field, $newValue), $this->zone, $this->offset);
     }
     return ChronoZonedDateTimeImpl::ensureValid($this->getChronology(), $field->adjustInto($this, $newValue));
 }
예제 #11
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);
 }
예제 #12
0
 /**
  * Returns a copy of this date-time with the specified field set to a new value.
  * <p>
  * This returns a {@code ZonedDateTime}, 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-time 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.
  * <p>
  * The {@code INSTANT_SECONDS} field will return a date-time with the specified instant.
  * The zone and nano-of-second are unchanged.
  * The result will have an offset derived from the new instant and original zone.
  * If the new instant value is outside the valid range then a {@code DateTimeException} will be thrown.
  * <p>
  * The {@code OFFSET_SECONDS} field will typically be ignored.
  * The offset of a {@code ZonedDateTime} is controlled primarily by the time-zone.
  * As such, changing the offset does not generally make sense, because there is only
  * one valid offset for the local date-time and zone.
  * If the zoned date-time is in a daylight savings overlap, then the offset is used
  * to switch between the two valid offsets. In all other cases, the offset is ignored.
  * If the new offset value is outside the valid range then a {@code DateTimeException} will be thrown.
  * <p>
  * The other {@link #isSupported(TemporalField) supported fields} will behave as per
  * the matching method on {@link LocalDateTime#with(TemporalField, long) LocalDateTime}.
  * The zone is not part of the calculation and will be unchanged.
  * When converting back to {@code ZonedDateTime}, if the local date-time is in an overlap,
  * then the offset will be retained if possible, otherwise the earlier offset will be used.
  * If in a gap, the local date-time will be adjusted forward by the length of the gap.
  * <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 ZonedDateTime a {@code ZonedDateTime} 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;
         switch ($f) {
             case ChronoField::INSTANT_SECONDS():
                 return self::create($newValue, $this->getNano(), $this->zone);
             case ChronoField::OFFSET_SECONDS():
                 $offset = ZoneOffset::ofTotalSeconds($f->checkValidIntValue($newValue));
                 return $this->resolveOffset($offset);
         }
         return $this->resolveLocal($this->dateTime->with($field, $newValue));
     }
     return $field->adjustInto($this, $newValue);
 }
 public function setOffset($offsetId)
 {
     if ($offsetId !== null) {
         $this->fields->put(ChronoField::OFFSET_SECONDS(), ZoneOffset::of($offsetId)->getTotalSeconds());
     }
 }
예제 #14
0
 public function test_with_TemporalField()
 {
     $test = OffsetTime::of(12, 30, 40, 987654321, self::OFFSET_PONE());
     $this->assertEquals($test->with(CF::HOUR_OF_DAY(), 15), OffsetTime::of(15, 30, 40, 987654321, self::OFFSET_PONE()));
     $this->assertEquals($test->with(CF::MINUTE_OF_HOUR(), 50), OffsetTime::of(12, 50, 40, 987654321, self::OFFSET_PONE()));
     $this->assertEquals($test->with(CF::SECOND_OF_MINUTE(), 50), OffsetTime::of(12, 30, 50, 987654321, self::OFFSET_PONE()));
     $this->assertEquals($test->with(CF::NANO_OF_SECOND(), 12345), OffsetTime::of(12, 30, 40, 12345, self::OFFSET_PONE()));
     $this->assertEquals($test->with(CF::HOUR_OF_AMPM(), 6), OffsetTime::of(18, 30, 40, 987654321, self::OFFSET_PONE()));
     $this->assertEquals($test->with(CF::AMPM_OF_DAY(), 0), OffsetTime::of(0, 30, 40, 987654321, self::OFFSET_PONE()));
     $this->assertEquals($test->with(CF::OFFSET_SECONDS(), 7205), OffsetTime::of(12, 30, 40, 987654321, ZoneOffset::ofHoursMinutesSeconds(2, 0, 5)));
 }
예제 #15
0
 /**
  * Adjusts the specified temporal object to have the same offset and time
  * as this object.
  * <p>
  * This returns a temporal object of the same observable type as the input
  * with the offset and time changed to be the same as this.
  * <p>
  * The adjustment is equivalent to using {@link Temporal#with(TemporalField, long)}
  * twice, passing {@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 = thisOffsetTime.adjustInto(temporal);
  *   temporal = temporal.with(thisOffsetTime);
  * </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->time->toNanoOfDay())->with(ChronoField::OFFSET_SECONDS(), $this->offset->getTotalSeconds());
 }
예제 #16
0
 private static function INSTANTSECONDS_OFFSETSECONDS()
 {
     return (new DateTimeFormatterBuilder())->appendValue(CF::INSTANT_SECONDS())->appendLiteral(' ')->appendValue(CF::OFFSET_SECONDS())->toFormatter();
 }
예제 #17
0
 public function test_isSupported_TemporalField()
 {
     //$this->assertEquals(self::TEST_200707_15_12_30_40_987654321()->isSupported(null), false); TODO
     $this->assertEquals(self::TEST_200707_15_12_30_40_987654321()->isSupported(CF::NANO_OF_SECOND()), true);
     $this->assertEquals(self::TEST_200707_15_12_30_40_987654321()->isSupported(CF::NANO_OF_DAY()), true);
     $this->assertEquals(self::TEST_200707_15_12_30_40_987654321()->isSupported(CF::MICRO_OF_SECOND()), true);
     $this->assertEquals(self::TEST_200707_15_12_30_40_987654321()->isSupported(CF::MICRO_OF_DAY()), true);
     $this->assertEquals(self::TEST_200707_15_12_30_40_987654321()->isSupported(CF::MILLI_OF_SECOND()), true);
     $this->assertEquals(self::TEST_200707_15_12_30_40_987654321()->isSupported(CF::MILLI_OF_DAY()), true);
     $this->assertEquals(self::TEST_200707_15_12_30_40_987654321()->isSupported(CF::SECOND_OF_MINUTE()), true);
     $this->assertEquals(self::TEST_200707_15_12_30_40_987654321()->isSupported(CF::SECOND_OF_DAY()), true);
     $this->assertEquals(self::TEST_200707_15_12_30_40_987654321()->isSupported(CF::MINUTE_OF_HOUR()), true);
     $this->assertEquals(self::TEST_200707_15_12_30_40_987654321()->isSupported(CF::MINUTE_OF_DAY()), true);
     $this->assertEquals(self::TEST_200707_15_12_30_40_987654321()->isSupported(CF::HOUR_OF_AMPM()), true);
     $this->assertEquals(self::TEST_200707_15_12_30_40_987654321()->isSupported(CF::CLOCK_HOUR_OF_AMPM()), true);
     $this->assertEquals(self::TEST_200707_15_12_30_40_987654321()->isSupported(CF::HOUR_OF_DAY()), true);
     $this->assertEquals(self::TEST_200707_15_12_30_40_987654321()->isSupported(CF::CLOCK_HOUR_OF_DAY()), true);
     $this->assertEquals(self::TEST_200707_15_12_30_40_987654321()->isSupported(CF::AMPM_OF_DAY()), true);
     $this->assertEquals(self::TEST_200707_15_12_30_40_987654321()->isSupported(CF::DAY_OF_WEEK()), true);
     $this->assertEquals(self::TEST_200707_15_12_30_40_987654321()->isSupported(CF::ALIGNED_DAY_OF_WEEK_IN_MONTH()), true);
     $this->assertEquals(self::TEST_200707_15_12_30_40_987654321()->isSupported(CF::ALIGNED_DAY_OF_WEEK_IN_YEAR()), true);
     $this->assertEquals(self::TEST_200707_15_12_30_40_987654321()->isSupported(CF::DAY_OF_MONTH()), true);
     $this->assertEquals(self::TEST_200707_15_12_30_40_987654321()->isSupported(CF::DAY_OF_YEAR()), true);
     $this->assertEquals(self::TEST_200707_15_12_30_40_987654321()->isSupported(CF::EPOCH_DAY()), true);
     $this->assertEquals(self::TEST_200707_15_12_30_40_987654321()->isSupported(CF::ALIGNED_WEEK_OF_MONTH()), true);
     $this->assertEquals(self::TEST_200707_15_12_30_40_987654321()->isSupported(CF::ALIGNED_WEEK_OF_YEAR()), true);
     $this->assertEquals(self::TEST_200707_15_12_30_40_987654321()->isSupported(CF::MONTH_OF_YEAR()), true);
     $this->assertEquals(self::TEST_200707_15_12_30_40_987654321()->isSupported(CF::PROLEPTIC_MONTH()), true);
     $this->assertEquals(self::TEST_200707_15_12_30_40_987654321()->isSupported(CF::YEAR()), true);
     $this->assertEquals(self::TEST_200707_15_12_30_40_987654321()->isSupported(CF::YEAR_OF_ERA()), true);
     $this->assertEquals(self::TEST_200707_15_12_30_40_987654321()->isSupported(CF::ERA()), true);
     $this->assertEquals(self::TEST_200707_15_12_30_40_987654321()->isSupported(CF::INSTANT_SECONDS()), false);
     $this->assertEquals(self::TEST_200707_15_12_30_40_987654321()->isSupported(CF::OFFSET_SECONDS()), false);
 }
예제 #18
0
 private function resolveInstant()
 {
     // add instant seconds if we have date, time and zone
     if ($this->date !== null && $this->time !== null) {
         if ($this->zone !== null) {
             $instant = $this->date->atTime($this->time)->atZone($this->zone)->getLong(CF::INSTANT_SECONDS());
             $this->fieldValues->put(CF::INSTANT_SECONDS(), $instant);
         } else {
             $offsetSecs = $this->fieldValues->get(CF::OFFSET_SECONDS());
             if ($offsetSecs !== null) {
                 $offset = ZoneOffset::ofTotalSeconds($offsetSecs);
                 $instant = $this->date->atTime($this->time)->atZone($offset)->getLong(CF::INSTANT_SECONDS());
                 $this->fieldValues->put(CF::INSTANT_SECONDS(), $instant);
             }
         }
     }
 }
예제 #19
0
 /**
  * @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);
     }
 }
예제 #20
0
 /**
  * Adjusts the specified temporal object to have the same offset as this object.
  * <p>
  * This returns a temporal object of the same observable type as the input
  * with the offset changed to be the same as this.
  * <p>
  * The adjustment is equivalent to using {@link Temporal#with(TemporalField, long)}
  * passing {@link ChronoField#OFFSET_SECONDS} 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 = thisOffset.adjustInto(temporal);
  *   temporal = temporal.with(thisOffset);
  * </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::OFFSET_SECONDS(), $this->totalSeconds);
 }
예제 #21
0
 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()));
     }
 }
예제 #22
0
 public function test_isSupported_TemporalField()
 {
     //$this->assertEquals(DayOfWeek::THURSDAY()->isSupported(null), false); TODO
     $this->assertEquals(DayOfWeek::THURSDAY()->isSupported(ChronoField::NANO_OF_SECOND()), false);
     $this->assertEquals(DayOfWeek::THURSDAY()->isSupported(ChronoField::NANO_OF_DAY()), false);
     $this->assertEquals(DayOfWeek::THURSDAY()->isSupported(ChronoField::MICRO_OF_SECOND()), false);
     $this->assertEquals(DayOfWeek::THURSDAY()->isSupported(ChronoField::MICRO_OF_DAY()), false);
     $this->assertEquals(DayOfWeek::THURSDAY()->isSupported(ChronoField::MILLI_OF_SECOND()), false);
     $this->assertEquals(DayOfWeek::THURSDAY()->isSupported(ChronoField::MILLI_OF_DAY()), false);
     $this->assertEquals(DayOfWeek::THURSDAY()->isSupported(ChronoField::SECOND_OF_MINUTE()), false);
     $this->assertEquals(DayOfWeek::THURSDAY()->isSupported(ChronoField::SECOND_OF_DAY()), false);
     $this->assertEquals(DayOfWeek::THURSDAY()->isSupported(ChronoField::MINUTE_OF_HOUR()), false);
     $this->assertEquals(DayOfWeek::THURSDAY()->isSupported(ChronoField::MINUTE_OF_DAY()), false);
     $this->assertEquals(DayOfWeek::THURSDAY()->isSupported(ChronoField::HOUR_OF_AMPM()), false);
     $this->assertEquals(DayOfWeek::THURSDAY()->isSupported(ChronoField::CLOCK_HOUR_OF_AMPM()), false);
     $this->assertEquals(DayOfWeek::THURSDAY()->isSupported(ChronoField::HOUR_OF_DAY()), false);
     $this->assertEquals(DayOfWeek::THURSDAY()->isSupported(ChronoField::CLOCK_HOUR_OF_DAY()), false);
     $this->assertEquals(DayOfWeek::THURSDAY()->isSupported(ChronoField::AMPM_OF_DAY()), false);
     $this->assertEquals(DayOfWeek::THURSDAY()->isSupported(ChronoField::DAY_OF_WEEK()), true);
     $this->assertEquals(DayOfWeek::THURSDAY()->isSupported(ChronoField::ALIGNED_DAY_OF_WEEK_IN_MONTH()), false);
     $this->assertEquals(DayOfWeek::THURSDAY()->isSupported(ChronoField::ALIGNED_DAY_OF_WEEK_IN_YEAR()), false);
     $this->assertEquals(DayOfWeek::THURSDAY()->isSupported(ChronoField::DAY_OF_MONTH()), false);
     $this->assertEquals(DayOfWeek::THURSDAY()->isSupported(ChronoField::DAY_OF_YEAR()), false);
     $this->assertEquals(DayOfWeek::THURSDAY()->isSupported(ChronoField::EPOCH_DAY()), false);
     $this->assertEquals(DayOfWeek::THURSDAY()->isSupported(ChronoField::ALIGNED_WEEK_OF_MONTH()), false);
     $this->assertEquals(DayOfWeek::THURSDAY()->isSupported(ChronoField::ALIGNED_WEEK_OF_YEAR()), false);
     $this->assertEquals(DayOfWeek::THURSDAY()->isSupported(ChronoField::MONTH_OF_YEAR()), false);
     $this->assertEquals(DayOfWeek::THURSDAY()->isSupported(ChronoField::PROLEPTIC_MONTH()), false);
     $this->assertEquals(DayOfWeek::THURSDAY()->isSupported(ChronoField::YEAR()), false);
     $this->assertEquals(DayOfWeek::THURSDAY()->isSupported(ChronoField::YEAR_OF_ERA()), false);
     $this->assertEquals(DayOfWeek::THURSDAY()->isSupported(ChronoField::ERA()), false);
     $this->assertEquals(DayOfWeek::THURSDAY()->isSupported(ChronoField::INSTANT_SECONDS()), false);
     $this->assertEquals(DayOfWeek::THURSDAY()->isSupported(ChronoField::OFFSET_SECONDS()), false);
 }
예제 #23
0
 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);
 }
 public function parse(DateTimeParseContext $context, $text, $position)
 {
     $length = strlen($text);
     $noOffsetLen = strlen($this->noOffsetText);
     if ($noOffsetLen === 0) {
         if ($position === $length) {
             return $context->setParsedField(ChronoField::OFFSET_SECONDS(), 0, $position, $position);
         }
     } else {
         if ($position === $length) {
             return ~$position;
         }
         if ($context->subSequenceEquals($text, $position, $this->noOffsetText, 0, $noOffsetLen)) {
             return $context->setParsedField(ChronoField::OFFSET_SECONDS(), 0, $position, $position + $noOffsetLen);
         }
     }
     // parse normal plus/minus offset
     $sign = $text[$position];
     // IOOBE if invalid position
     if ($sign === '+' || $sign === '-') {
         // starts
         $negative = $sign === '-' ? -1 : 1;
         $array = [0, 0, 0, 0];
         $array[0] = $position + 1;
         if (($this->parseNumber($array, 1, $text, true) || $this->parseNumber($array, 2, $text, $this->type >= 3) || $this->parseNumber($array, 3, $text, false)) === false) {
             // success
             $offsetSecs = $negative * ($array[1] * 3600 + $array[2] * 60 + $array[3]);
             return $context->setParsedField(ChronoField::OFFSET_SECONDS(), $offsetSecs, $position, $array[0]);
         }
     }
     // handle special case of empty no offset text
     if ($noOffsetLen === 0) {
         return $context->setParsedField(ChronoField::OFFSET_SECONDS(), 0, $position, $position + $noOffsetLen);
     }
     return ~$position;
 }