public function localDateTime(TemporalAccessor $temporal) { try { return $this->dateFrom($temporal)->atTime(LocalTime::from($temporal)); } catch (DateTimeException $ex) { throw new DateTimeException("Unable to obtain ChronoLocalDateTime from TemporalAccessor: " . get_class($temporal), $ex); } }
/** * Obtains an instance of {@code LocalDateTime} from a temporal object. * <p> * This obtains a local 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 LocalDateTime}. * <p> * The conversion extracts and combines the {@code LocalDate} 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 LocalDateTime::from}. * * @param TemporalAccessor $temporal the temporal object to convert, not null * @return LocalDateTime the local date-time, not null * @throws DateTimeException if unable to convert to a {@code LocalDateTime} */ public static function from(TemporalAccessor $temporal) { if ($temporal instanceof LocalDateTime) { return $temporal; } else { if ($temporal instanceof ZonedDateTime) { return $temporal->toLocalDateTime(); } else { if ($temporal instanceof OffsetDateTime) { return $temporal->toLocalDateTime(); } } } try { $date = LocalDate::from($temporal); $time = LocalTime::from($temporal); return new LocalDateTime($date, $time); } catch (DateTimeException $ex) { throw new DateTimeException("Unable to obtain LocalDateTime from TemporalAccessor: " . $temporal . " of type " . get_class($temporal), $ex); } }
/** * Obtains an instance of {@code ZonedDateTime} from a temporal object. * <p> * This obtains a zoned 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 ZonedDateTime}. * <p> * The conversion will first obtain a {@code ZoneId} from the temporal object, * falling back to a {@code ZoneOffset} if necessary. It will then try to obtain * an {@code Instant}, falling back to a {@code LocalDateTime} if necessary. * The result will be either the combination of {@code ZoneId} or {@code ZoneOffset} * with {@code Instant} or {@code LocalDateTime}. * 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 ZonedDateTime::from}. * * @param TemporalAccessor $temporal the temporal object to convert, not null * @return ZonedDateTime the zoned date-time, not null * @throws DateTimeException if unable to convert to an {@code ZonedDateTime} */ public static function from(TemporalAccessor $temporal) { if ($temporal instanceof ZonedDateTime) { return $temporal; } try { $zone = ZoneId::from($temporal); if ($temporal->isSupported(ChronoField::INSTANT_SECONDS())) { $epochSecond = $temporal->getLong(ChronoField::INSTANT_SECONDS()); $nanoOfSecond = $temporal->get(ChronoField::NANO_OF_SECOND()); return self::create($epochSecond, $nanoOfSecond, $zone); } else { $date = LocalDate::from($temporal); $time = LocalTime::from($temporal); return self::ofDateAndTime($date, $time, $zone); } } catch (DateTimeException $ex) { throw new DateTimeException("Unable to obtain ZonedDateTime from TemporalAccessor: " . $temporal . " of type " . get_class($temporal), $ex); } }
public function test_factory_from_TemporalAccessor_null() { TestHelper::assertNullException($this, function () { LocalTime::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); } }
/** * @dataProvider data_time */ public function test_time_parse(LocalTime $time, FormatStyle $timeStyle, $timeStyleOld, Locale $locale) { $old = \IntlDateFormatter::create($locale->getLocale(), \IntlDateFormatter::NONE, $timeStyleOld, new \DateTimeZone('UTC')); $oldDate = new \DateTime('1970-0-0T' . $time->getHour() . ':' . $time->getMinute() . ':' . $time->getSecond(), new \DateTimeZone('UTC')); $text = $old->format($oldDate); $f = $this->builder->appendLocalized(null, $timeStyle)->toFormatter2($locale); $parsed = $f->parsePos($text, $this->pos); $this->assertEquals($this->pos->getIndex(), strlen($text)); $this->assertEquals($this->pos->getErrorIndex(), -1); $this->assertEquals(LocalTime::from($parsed), $time); }