/** * Obtains an instance of {@code ZonedDateTime} from an {@code Instant}. * <p> * This creates a zoned date-time with the same instant as that specified. * Calling {@link #toInstant()} will return an instant equal to the one used here. * <p> * Converting an instant to a zoned date-time is simple as there is only one valid * offset for each instant. * * @param Instant $instant the instant to create the date-time from, not null * @param ZoneId $zone the time-zone, not null * @return ZonedDateTime the zoned date-time, not null * @throws DateTimeException if the result exceeds the supported range */ public static function ofInstant(Instant $instant, ZoneId $zone) { return self::create($instant->getEpochSecond(), $instant->getNano(), $zone); }
/** * Gets the previous transition before the specified instant. * <p> * This returns details of the previous transition after the specified instant. * For example, if the instant represents a point where "summer" daylight saving time * applies, then the method will return the transition from the previous "winter" time. * * @param Instant|null $instant the instant to get the previous transition after, not null, but null * may be ignored if the rules have a single offset for all instants * @return ZoneOffsetTransition the previous transition after the specified instant, null if this is before the first transition */ public function previousTransition($instant) { if (empty($this->savingsInstantTransitions)) { return null; } $epochSec = $instant->getEpochSecond(); if ($instant->getNano() > 0 && $epochSec < Long::MAX_VALUE) { $epochSec += 1; // allow rest of method to only use seconds } // check if using last rules $lastHistoric = $this->savingsInstantTransitions[count($this->savingsInstantTransitions) - 1]; if (!empty($this->lastRules) && $epochSec > $lastHistoric) { // search year the instant is in $lastHistoricOffset = $this->wallOffsets[count($this->wallOffsets) - 1]; $year = $this->findYear($epochSec, $lastHistoricOffset); $transArray = $this->findTransitionArray($year); for ($i = count($transArray) - 1; $i >= 0; $i--) { if ($epochSec > $transArray[$i]->toEpochSecond()) { return $transArray[$i]; } } // use last from preceding year $lastHistoricYear = $this->findYear($lastHistoric, $lastHistoricOffset); if (--$year > $lastHistoricYear) { $transArray = $this->findTransitionArray($year); return $transArray[count($transArray) - 1]; } // drop through } // using historic rules $index = Math::binarySearch($this->savingsInstantTransitions, $epochSec); if ($index < 0) { $index = -$index - 1; } if ($index <= 0) { return null; } return ZoneOffsetTransition::ofEpoch($this->savingsInstantTransitions[$index - 1], $this->wallOffsets[$index - 1], $this->wallOffsets[$index]); }
private function check(Instant $instant, $epochSecs, $nos) { $this->assertEquals($instant->getEpochSecond(), $epochSecs); $this->assertEquals($instant->getNano(), $nos); $this->assertEquals($instant, $instant); }