public function test_factory_ofInstant_Instant_nullZone() { TestHelper::assertNullException($this, function () { ZonedDateTime::ofInstant(Instant::EPOCH(), null); }); }
/** * Normalizes the time-zone ID, returning a {@code ZoneOffset} where possible. * <p> * The returns a normalized {@code ZoneId} that can be used in place of this ID. * The result will have {@code ZoneRules} equivalent to those returned by this object, * however the ID returned by {@code getId()} may be different. * <p> * The normalization checks if the rules of this {@code ZoneId} have a fixed offset. * If they do, then the {@code ZoneOffset} equal to that offset is returned. * Otherwise {@code this} is returned. * * @return ZoneId the time-zone unique ID, not null */ public function normalized() { try { $rules = $this->getRules(); if ($rules->isFixedOffset()) { return $rules->getOffset(Instant::EPOCH()); } } catch (ZoneRulesException $ex) { // invalid ZoneRegion is not important to this method } return $this; }
/** * @dataProvider data_offsetBasedValidPrefix */ public function test_factory_of_String_offsetBasedValid_prefixUT($input, $id, $offsetId) { $test = ZoneId::of("UT" . $input); $this->assertEquals($test->getId(), "UT" . $id); $this->assertEquals($test->getRules(), ZoneOffset::of($offsetId)->getRules()); $this->assertEquals($test->normalized(), ZoneOffset::of($offsetId)); $this->assertEquals($test->getRules()->isFixedOffset(), true); $this->assertEquals($test->getRules()->getOffset(Instant::EPOCH()), ZoneOffset::of($offsetId)); if (defined('HHVM_VERSION')) { $this->markTestSkipped('See https://github.com/facebook/hhvm/issues/6852'); } $this->assertEquals($test->getDisplayName(TextStyle::FULL(), Locale::UK()), $this->displayName("UT" . $id)); }
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); }
function data_with() { return [[Instant::ofEpochSecond(10, 200), Instant::ofEpochSecond(20), Instant::ofEpochSecond(20), null], [Instant::ofEpochSecond(10), Instant::ofEpochSecond(20, -100), Instant::ofEpochSecond(20, -100), null], [Instant::ofEpochSecond(-10), Instant::EPOCH(), Instant::ofEpochSecond(0), null], [Instant::ofEpochSecond(10), Instant::MIN(), Instant::MIN(), null], [Instant::ofEpochSecond(10), Instant::MAX(), Instant::MAX(), null], [Instant::ofEpochSecond(10, 200), LocalDateTime::of(1970, 1, 1, 0, 0, 20)->toInstant(ZoneOffset::UTC()), Instant::ofEpochSecond(20), null], [Instant::ofEpochSecond(10, 200), LocalDateTime::of(1970, 1, 1, 0, 0, 20), null, DateTimeException::class]]; }