Example #1
0
 /**
  * Creates a ZonedDateTime from an instant and a time zone.
  *
  * This resolves the instant to a date and time without ambiguity.
  *
  * @param Instant  $instant  The instant.
  * @param TimeZone $timeZone The time zone.
  *
  * @return ZonedDateTime
  */
 public static function ofInstant(Instant $instant, TimeZone $timeZone)
 {
     $dateTimeZone = $timeZone->toDateTimeZone();
     // We need to pass a DateTimeZone to avoid a PHP warning...
     $dateTime = new \DateTime('@' . $instant->getEpochSecond(), $dateTimeZone);
     // ... but this DateTimeZone is ignored because of the timestamp, so we set it again.
     $dateTime->setTimezone($dateTimeZone);
     $localDateTime = LocalDateTime::parse($dateTime->format('Y-m-d\\TH:i:s'));
     $localDateTime = $localDateTime->withNano($instant->getNano());
     if ($timeZone instanceof TimeZoneOffset) {
         $timeZoneOffset = $timeZone;
     } else {
         $timeZoneOffset = TimeZoneOffset::ofTotalSeconds($dateTimeZone->getOffset($dateTime));
     }
     return new ZonedDateTime($localDateTime, $timeZoneOffset, $timeZone, $dateTime);
 }