Esempio n. 1
0
 public function test_now_Clock()
 {
     $instant = OffsetDateTime::ofDateAndTime(LocalDate::of(2010, 12, 31), LocalTime::of(0, 0), ZoneOffset::UTC())->toInstant();
     $clock = Clock::fixed($instant, ZoneOffset::UTC());
     $test = Year::nowof($clock);
     $this->assertEquals($test->getValue(), 2010);
 }
 public function test_adjustInto_OffsetDateTime()
 {
     $base = ZoneOffset::ofHoursMinutesSeconds(1, 1, 1);
     for ($i = -18; $i <= 18; $i++) {
         $offsetDateTime_target = OffsetDateTime::ofDateAndTime(LocalDate::of(1909, 2, 2), LocalTime::of(10, 10, 10), ZoneOffset::ofHours($i));
         $offsetDateTime_result = $base->adjustInto($offsetDateTime_target);
         $this->assertEquals($base, $offsetDateTime_result->getOffset());
         //Do not change $offset of ZonedDateTime after adjustInto()
         $zonedDateTime_target = $offsetDateTime_target->toZonedDateTime();
         $zonedDateTime_result = $base->adjustInto($zonedDateTime_target);
         $this->assertEquals($zonedDateTime_target->getOffset(), $zonedDateTime_result->getOffset());
     }
 }
Esempio n. 3
0
 /**
  * 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_withNanoOfSecond_normal()
 {
     $base = OffsetDateTime::ofDateAndTime(LocalDate::of(2008, 6, 30), LocalTime::of(11, 30, 59, 1), self::OFFSET_PONE());
     $test = $base->withNano(15);
     $this->assertEquals($test, OffsetDateTime::ofDateAndTime(LocalDate::of(2008, 6, 30), LocalTime::of(11, 30, 59, 15), self::OFFSET_PONE()));
 }
Esempio n. 5
0
 /**
  * Combines this time with a date to create an {@code OffsetDateTime}.
  * <p>
  * This returns an {@code OffsetDateTime} formed from this time and the specified date.
  * All possible combinations of date and time are valid.
  *
  * @param LocalDate $date the date to combine with, not null
  * @return OffsetDateTime the offset date-time formed from this time and the specified date, not null
  */
 public function atDate(LocalDate $date)
 {
     return OffsetDateTime::ofDateAndTime($date, $this->time, $this->offset);
 }