/** * Obtains an instance of {@code OffsetDateTime} from a temporal object. * <p> * This obtains an offset date-time based on the specified temporal. * A {@code TemporalAccessor} represents an arbitrary set of date and time information, * which this factory converts to an instance of {@code OffsetDateTime}. * <p> * The conversion will first obtain a {@code ZoneOffset} from the temporal object. * It will then try to obtain a {@code LocalDateTime}, falling back to an {@code Instant} if necessary. * The result will be the combination of {@code ZoneOffset} with either * with {@code LocalDateTime} or {@code Instant}. * Implementations are permitted to perform optimizations such as accessing * those fields that are equivalent to the relevant objects. * <p> * This method matches the signature of the functional interface {@link TemporalQuery} * allowing it to be used as a query via method reference, {@code OffsetDateTime::from}. * * @param TemporalAccessor $temporal the temporal object to convert, not null * @return OffsetDateTime the offset date-time, not null * @throws DateTimeException if unable to convert to an {@code OffsetDateTime} */ public static function from(TemporalAccessor $temporal) { if ($temporal instanceof OffsetDateTime) { return $temporal; } try { $offset = ZoneOffset::from($temporal); $date = $temporal->query(TemporalQueries::localDate()); $time = $temporal->query(TemporalQueries::localTime()); if ($date !== null && $time !== null) { return OffsetDateTime::ofDateAndTime($date, $time, $offset); } else { $instant = Instant::from($temporal); return OffsetDateTime::ofInstant($instant, $offset); } } catch (DateTimeException $ex) { throw new DateTimeException("Unable to obtain OffsetDateTime from TemporalAccessor: " . $temporal . " of type " . get_class($temporal), $ex); } }
public function test_factory_CalendricalObject_null() { TestHelper::assertNullException($this, function () { ZoneOffset::from(null); }); }
/** * Obtains an instance of {@code OffsetTime} from a temporal object. * <p> * This obtains an offset time based on the specified temporal. * A {@code TemporalAccessor} represents an arbitrary set of date and time information, * which this factory converts to an instance of {@code OffsetTime}. * <p> * The conversion extracts and combines the {@code ZoneOffset} and the * {@code LocalTime} from the temporal object. * Implementations are permitted to perform optimizations such as accessing * those fields that are equivalent to the relevant objects. * <p> * This method matches the signature of the functional interface {@link TemporalQuery} * allowing it to be used as a query via method reference, {@code OffsetTime::from}. * * @param TemporalAccessor $temporal the temporal object to convert, not null * @return OffsetTime the offset time, not null * @throws DateTimeException if unable to convert to an {@code OffsetTime} */ public static function from(TemporalAccessor $temporal) { if ($temporal instanceof OffsetTime) { return $temporal; } try { $time = LocalTime::from($temporal); $offset = ZoneOffset::from($temporal); return new OffsetTime($time, $offset); } catch (DateTimeException $ex) { throw new DateTimeException("Unable to obtain OffsetTime from TemporalAccessor: " . $temporal . " of type " . get_class($temporal), $ex); } }