Ejemplo n.º 1
0
 /**
  * Converts this to a transition.
  *
  * @param ZoneOffset $standardOffset the active standard offset, not null
  * @param int $savingsBeforeSecs the active savings in seconds
  * @return ZoneOffsetTransition the transition, not null
  */
 function toTransition(ZoneOffset $standardOffset, $savingsBeforeSecs)
 {
     // copy of code in ZoneOffsetTransitionRule to avoid infinite loop
     $date = $this->toLocalDate();
     $date = ZoneRulesBuilder::deduplicate($date);
     $ldt = ZoneRulesBuilder::deduplicate(LocalDateTime::ofDateAndTime($date, $this->time));
     /** @var ZoneOffset $wallOffset */
     $wallOffset = ZoneRulesBuilder::deduplicate(ZoneOffset::ofTotalSeconds($standardOffset->getTotalSeconds() + $savingsBeforeSecs));
     $dt = ZoneRulesBuilder::deduplicate($this->timeDefinition->createDateTime($ldt, $standardOffset, $wallOffset));
     $offsetAfter = ZoneRulesBuilder::deduplicate(ZoneOffset::ofTotalSeconds($standardOffset->getTotalSeconds() + $this->savingAmountSecs));
     return new ZoneOffsetTransition($dt, $wallOffset, $offsetAfter);
 }
Ejemplo n.º 2
0
 private function toDateTime($year)
 {
     $this->adjustToFowards($year);
     if ($this->dayOfMonth === -1) {
         $dayOfMonth = $this->month->length(Year::isLeapYear($year));
         $date = LocalDate::ofMonth($year, $this->month, $dayOfMonth);
         if ($this->dayOfWeek !== null) {
             $date = $date->adjust(TemporalAdjusters::previousOrSame($this->dayOfWeek));
         }
     } else {
         $date = LocalDate::ofMonth($year, $this->month, $this->dayOfMonth);
         if ($this->dayOfWeek !== null) {
             $date = $date->adjust(TemporalAdjusters::nextOrSame($this->dayOfWeek));
         }
     }
     $ldt = LocalDateTime::ofDateAndTime($date, $this->time);
     if ($this->endOfDay) {
         $ldt = $ldt->plusDays(1);
     }
     return $ldt;
 }
Ejemplo n.º 3
0
 public function test_factory_CalendricalObject()
 {
     $this->assertEquals(ZoneOffset::from(ZonedDateTime::ofDateTime(LocalDateTime::ofDateAndTime(LocalDate::of(2007, 7, 15), LocalTime::of(17, 30)), ZoneOffset::ofHours(2))), ZoneOffset::ofHours(2));
 }
Ejemplo n.º 4
0
 /**
  * Obtains an instance of {@code OffsetDateTime} from a date, time and offset.
  * <p>
  * This creates an offset date-time with the specified local date, time and offset.
  *
  * @param LocalDate $date the local date, not null
  * @param LocalTime $time the local time, not null
  * @param ZoneOffset $offset the zone offset, not null
  * @return OffsetDateTime the offset date-time, not null
  */
 public static function ofDateAndTime(LocalDate $date, LocalTime $time, ZoneOffset $offset)
 {
     $dt = LocalDateTime::ofDateAndTime($date, $time);
     return new OffsetDateTime($dt, $offset);
 }
Ejemplo n.º 5
0
 /**
  * Combines this time with a date to create a {@code LocalDateTime}.
  * <p>
  * This returns a {@code LocalDateTime} formed from this time at the specified date.
  * All possible combinations of date and time are valid.
  *
  * @param LocalDate $date the date to combine with, not null
  * @return LocalDateTime LocalTime the local date-time formed from this time and the specified date, not null
  */
 public function atDate(LocalDate $date)
 {
     return LocalDateTime::ofDateAndTime($date, $this);
 }
Ejemplo n.º 6
0
 /**
  * Combines this date with the time of midnight to create a {@code LocalDateTime}
  * at the start of this date.
  * <p>
  * This returns a {@code LocalDateTime} formed from this date at the time of
  * midnight, 00:00, at the start of this date.
  *
  * @return LocalDateTime the local date-time of midnight at the start of this date, not null
  */
 public function atStartOfDay()
 {
     return LocalDateTime::ofDateAndTime($this, LocalTime::MIDNIGHT());
 }
Ejemplo n.º 7
0
 /**
  * @dataProvider provider_sampleTimes
  */
 public function test_get($y, $o, $d, $h, $m, $s, $n, ZoneId $zone)
 {
     $localDate = LocalDate::of($y, $o, $d);
     $localTime = LocalTime::of($h, $m, $s, $n);
     $localDateTime = LocalDateTime::ofDateAndTime($localDate, $localTime);
     $offset = $zone->getRules()->getOffsetDateTime($localDateTime);
     $a = ZonedDateTime::ofDateTime($localDateTime, $zone);
     $this->assertEquals($a->getYear(), $localDate->getYear());
     $this->assertEquals($a->getMonth(), $localDate->getMonth());
     $this->assertEquals($a->getDayOfMonth(), $localDate->getDayOfMonth());
     $this->assertEquals($a->getDayOfYear(), $localDate->getDayOfYear());
     $this->assertEquals($a->getDayOfWeek(), $localDate->getDayOfWeek());
     $this->assertEquals($a->getHour(), $localTime->getHour());
     $this->assertEquals($a->getMinute(), $localTime->getMinute());
     $this->assertEquals($a->getSecond(), $localTime->getSecond());
     $this->assertEquals($a->getNano(), $localTime->getNano());
     $this->assertEquals($a->toLocalDate(), $localDate);
     $this->assertEquals($a->toLocalTime(), $localTime);
     $this->assertEquals($a->toLocalDateTime(), $localDateTime);
     if ($zone instanceof ZoneOffset) {
         $this->assertEquals($a->__toString(), $localDateTime->__toString() . $offset->__toString());
     } else {
         $this->assertEquals($a->__toString(), $localDateTime->__toString() . $offset->__toString() . "[" . $zone->__toString() . "]");
     }
 }
Ejemplo n.º 8
0
 function comparisons_LocalDateTime2(array $localDates, array $localTimes)
 {
     $localDateTimes = [];
     foreach ($localDates as $localDate) {
         foreach ($localTimes as $localTime) {
             $localDateTimes[] = LocalDateTime::ofDateAndTime($localDate, $localTime);
         }
     }
     $this->doTest_comparisons_LocalDateTime($localDateTimes);
 }
 public function test_with_adjustment_LocalDateTime()
 {
     $test = self::TEST_2008_6_30_11_30_59_000000500()->adjust(LocalDateTime::ofDateAndTime(LocalDate::of(2012, 9, 3), LocalTime::of(19, 15)));
     $this->assertEquals($test, OffsetDateTime::ofDateAndTime(LocalDate::of(2012, 9, 3), LocalTime::of(19, 15), self::OFFSET_PONE()));
 }
 /**
  * Creates a transition instance for the specified year.
  * <p>
  * Calculations are performed using the ISO-8601 chronology.
  *
  * @param int $year the year to create a transition for, not null
  * @return ZoneOffsetTransition the transition instance, not null
  */
 public function createTransition($year)
 {
     if ($this->dom < 0) {
         $date = LocalDate::ofMonth($year, $this->month, $this->month->length(IsoChronology::INSTANCE()->isLeapYear($year)) + 1 + $this->dom);
         if ($this->dow !== null) {
             $date = $date->adjust(TemporalAdjusters::previousOrSame($this->dow));
         }
     } else {
         $date = LocalDate::ofMonth($year, $this->month, $this->dom);
         if ($this->dow !== null) {
             $date = $date->adjust(TemporalAdjusters::nextOrSame($this->dow));
         }
     }
     if ($this->timeEndOfDay) {
         $date = $date->plusDays(1);
     }
     $localDT = LocalDateTime::ofDateAndTime($date, $this->time);
     $transition = $this->timeDefinition->createDateTime($localDT, $this->standardOffset, $this->offsetBefore);
     return ZoneOffsetTransition::of($transition, $this->offsetBefore, $this->offsetAfter);
 }
Ejemplo n.º 11
0
 public static function init()
 {
     self::$MIN = LocalDateTime::ofDateAndTime(LocalDate::MIN(), LocalTime::MIN());
     self::$MAX = LocalDateTime::ofDateAndTime(LocalDate::MAX(), LocalTime::MAX());
 }
Ejemplo n.º 12
0
 /**
  * Returns an adjusted copy of this date-time.
  * <p>
  * This returns a {@code ZonedDateTime}, based on this one, with the date-time adjusted.
  * The adjustment takes place using the specified adjuster strategy object.
  * Read the documentation of the adjuster to understand what adjustment will be made.
  * <p>
  * A simple adjuster might simply set the one of the fields, such as the year field.
  * A more complex adjuster might set the date to the last day of the month.
  * A selection of common adjustments is provided in
  * {@link java.time.temporal.TemporalAdjusters TemporalAdjusters}.
  * These include finding the "last day of the month" and "next Wednesday".
  * Key date-time classes also implement the {@code TemporalAdjuster} interface,
  * such as {@link Month} and {@link java.time.MonthDay MonthDay}.
  * The adjuster is responsible for handling special cases, such as the varying
  * lengths of month and leap years.
  * <p>
  * For example this code returns a date on the last day of July:
  * <pre>
  *  import static java.time.Month.*;
  *  import static java.time.temporal.TemporalAdjusters.*;
  *
  *  result = zonedDateTime.with(JULY).with(lastDayOfMonth());
  * </pre>
  * <p>
  * The classes {@link LocalDate} and {@link LocalTime} implement {@code TemporalAdjuster},
  * thus this method can be used to change the date, time or offset:
  * <pre>
  *  result = zonedDateTime.with(date);
  *  result = zonedDateTime.with(time);
  * </pre>
  * <p>
  * {@link ZoneOffset} also implements {@code TemporalAdjuster} however using it
  * as an argument typically has no effect. The offset of a {@code ZonedDateTime} is
  * controlled primarily by the time-zone. As such, changing the offset does not generally
  * make sense, because there is only one valid offset for the local date-time and zone.
  * If the zoned date-time is in a daylight savings overlap, then the offset is used
  * to switch between the two valid offsets. In all other cases, the offset is ignored.
  * <p>
  * The result of this method is obtained by invoking the
  * {@link TemporalAdjuster#adjustInto(Temporal)} method on the
  * specified adjuster passing {@code this} as the argument.
  * <p>
  * This instance is immutable and unaffected by this method call.
  *
  * @param TemporalAdjuster $adjuster the adjuster to use, not null
  * @return ZonedDateTime a {@code ZonedDateTime} based on {@code this} with the adjustment made, not null
  * @throws DateTimeException if the adjustment cannot be made
  * @throws ArithmeticException if numeric overflow occurs
  */
 public function adjust(TemporalAdjuster $adjuster)
 {
     // optimizations
     if ($adjuster instanceof LocalDate) {
         return $this->resolveLocal(LocalDateTime::ofDateAndTime($adjuster, $this->dateTime->toLocalTime()));
     } else {
         if ($adjuster instanceof LocalTime) {
             return $this->resolveLocal(LocalDateTime::ofDateAndTime($this->dateTime->toLocalDate(), $adjuster));
         } else {
             if ($adjuster instanceof LocalDateTime) {
                 return $this->resolveLocal($adjuster);
             } else {
                 if ($adjuster instanceof OffsetDateTime) {
                     $odt = $adjuster;
                     return self::ofLocal($odt->toLocalDateTime(), $this->zone, $odt->getOffset());
                 } else {
                     if ($adjuster instanceof Instant) {
                         $instant = $adjuster;
                         return self::create($instant->getEpochSecond(), $instant->getNano(), $this->zone);
                     } else {
                         if ($adjuster instanceof ZoneOffset) {
                             return $this->resolveOffset($adjuster);
                         }
                     }
                 }
             }
         }
     }
     return $adjuster->adjustInto($this);
 }