Ejemplo n.º 1
0
 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);
 }
Ejemplo n.º 2
0
 /**
  * Gets the effective chronology during parsing.
  *
  * @return Chronology the effective parsing chronology, not null
  */
 public function getEffectiveChronology()
 {
     $chrono = $this->currentParsed()->chrono;
     if ($chrono == null) {
         $chrono = $this->formatter->getChronology();
         if ($chrono == null) {
             $chrono = IsoChronology::INSTANCE();
         }
     }
     return $chrono;
 }