Example #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);
 }
 /**
  * Adds a window that applies until the end of time to the builder that can be
  * used to filter a set of rules.
  * <p>
  * This method defines and adds a window to the zone where the standard offset is specified.
  * The window limits the effect of subsequent additions of transition rules
  * or fixed savings. If neither rules or fixed savings are added to the window
  * then the window will default to no savings.
  * <p>
  * This must be added after all other windows.
  * No more windows can be added after this one.
  *
  * @param ZoneOffset $standardOffset the standard offset, not null
  * @return ZoneRulesBuilder $this, for chaining
  * @throws \LogicException if a forever window has already been added
  */
 public function addWindowForever(ZoneOffset $standardOffset)
 {
     return $this->addWindow($standardOffset, LocalDateTime::MAX(), TimeDefinition::WALL());
 }
 public function test_of()
 {
     //used for standard offset
     $stdOffset1 = ZoneOffset::UTC();
     $stdOffset2 = ZoneOffset::ofHours(1);
     $time_of_stdOffsetTransition1 = LocalDateTime::of(2013, 1, 5, 1, 0);
     $stdOffsetTransition1 = ZoneOffsetTransition::of($time_of_stdOffsetTransition1, $stdOffset1, $stdOffset2);
     $stdOffsetTransition_list = [];
     $stdOffsetTransition_list[] = $stdOffsetTransition1;
     //used for wall offset
     $wallOffset1 = ZoneOffset::ofHours(2);
     $wallOffset2 = ZoneOffset::ofHours(4);
     $wallOffset3 = ZoneOffset::ofHours(7);
     $time_of_wallOffsetTransition1 = LocalDateTime::of(2013, 2, 5, 1, 0);
     $time_of_wallOffsetTransition2 = LocalDateTime::of(2013, 3, 5, 1, 0);
     $time_of_wallOffsetTransition3 = LocalDateTime::of(2013, 10, 5, 1, 0);
     $wallOffsetTransition1 = ZoneOffsetTransition::of($time_of_wallOffsetTransition1, $wallOffset1, $wallOffset2);
     $wallOffsetTransition2 = ZoneOffsetTransition::of($time_of_wallOffsetTransition2, $wallOffset2, $wallOffset3);
     $wallOffsetTransition3 = ZoneOffsetTransition::of($time_of_wallOffsetTransition3, $wallOffset3, $wallOffset1);
     $wallOffsetTransition_list = [];
     $wallOffsetTransition_list[] = $wallOffsetTransition1;
     $wallOffsetTransition_list[] = $wallOffsetTransition2;
     $wallOffsetTransition_list[] = $wallOffsetTransition3;
     //used for ZoneOffsetTransitionRule
     $ruleOffset = ZoneOffset::ofHours(3);
     $timeDefinition = TimeDefinition::WALL();
     $rule1 = ZoneOffsetTransitionRule::of(Month::FEBRUARY(), 2, DayOfWeek::MONDAY(), LocalTime::of(1, 0), false, $timeDefinition, ZoneOffset::UTC(), ZoneOffset::UTC(), $ruleOffset);
     $rule_list = [];
     $rule_list[] = $rule1;
     //Begin verification
     $zoneRule = ZoneRules::of($stdOffset1, $wallOffset1, $stdOffsetTransition_list, $wallOffsetTransition_list, $rule_list);
     $before_time_of_stdOffsetTransition1 = OffsetDateTime::ofDateTime($time_of_stdOffsetTransition1, $stdOffset1)->minusSeconds(1);
     $after_time_of_stdOffsetTransition1 = OffsetDateTime::ofDateTime($time_of_stdOffsetTransition1, $stdOffset1)->plusSeconds(1);
     $this->assertEquals($zoneRule->getStandardOffset($before_time_of_stdOffsetTransition1->toInstant()), $stdOffset1);
     $this->assertEquals($zoneRule->getStandardOffset($after_time_of_stdOffsetTransition1->toInstant()), $stdOffset2);
     $before_time_of_wallOffsetTransition1 = OffsetDateTime::ofDateTime($time_of_wallOffsetTransition1, $wallOffset1)->minusSeconds(1);
     $after_time_of_wallOffsetTransition1 = OffsetDateTime::ofDateTime($time_of_wallOffsetTransition1, $wallOffset1)->plusSeconds(1);
     $this->assertEquals($zoneRule->nextTransition($before_time_of_wallOffsetTransition1->toInstant()), $wallOffsetTransition1);
     $this->assertEquals($zoneRule->nextTransition($after_time_of_wallOffsetTransition1->toInstant()), $wallOffsetTransition2);
     $before_time_of_wallOffsetTransition2 = OffsetDateTime::ofDateTime($time_of_wallOffsetTransition2, $wallOffset2)->minusSeconds(1);
     $after_time_of_wallOffsetTransition2 = OffsetDateTime::ofDateTime($time_of_wallOffsetTransition2, $wallOffset2)->plusSeconds(1);
     $this->assertEquals($zoneRule->nextTransition($before_time_of_wallOffsetTransition2->toInstant()), $wallOffsetTransition2);
     $this->assertEquals($zoneRule->nextTransition($after_time_of_wallOffsetTransition2->toInstant()), $wallOffsetTransition3);
     $before_time_of_wallOffsetTransition3 = OffsetDateTime::ofDateTime($time_of_wallOffsetTransition3, $wallOffset3)->minusSeconds(1);
     $after_time_of_wallOffsetTransition3 = OffsetDateTime::ofDateTime($time_of_wallOffsetTransition3, $wallOffset3)->plusSeconds(1);
     $this->assertEquals($zoneRule->nextTransition($before_time_of_wallOffsetTransition3->toInstant()), $wallOffsetTransition3);
     $this->assertEquals($zoneRule->nextTransition($after_time_of_wallOffsetTransition3->toInstant()), $rule1->createTransition(2014));
 }
 public function __construct()
 {
     $this->month = Month::JANUARY();
     $this->time = LocalTime::MIDNIGHT();
     $this->timeDefinition = TimeDefinition::WALL();
 }
Example #5
0
 /**
  * Creates the offset date-time for the local date-time at the end of the window.
  *
  * @param int $savingsSecs the amount of savings in use in seconds
  * @return int the created date-time epoch second in the wall offset, not null
  */
 function createDateTimeEpochSecond($savingsSecs)
 {
     $wallOffset = $this->createWallOffset($savingsSecs);
     $ldt = $this->timeDefinition->createDateTime($this->windowEnd, $this->standardOffset, $wallOffset);
     return $ldt->toEpochSecond($wallOffset);
 }
 /**
  * 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);
 }
 public function test_toString_fixedDate()
 {
     $test = ZoneOffsetTransitionRule::of(Month::MARCH(), 20, null, self::TIME_0100(), false, TimeDefinition::STANDARD(), self::OFFSET_0200(), self::OFFSET_0200(), self::OFFSET_0300());
     $this->assertEquals($test->__toString(), "TransitionRule[Gap +02:00 to +03:00, MARCH 20 at 01:00 STANDARD, standard offset +02:00]");
 }
 private function parseTimeDefinition($c)
 {
     switch ($c) {
         case 's':
         case 'S':
             // standard time
             return TimeDefinition::STANDARD();
         case 'u':
         case 'U':
         case 'g':
         case 'G':
         case 'z':
         case 'Z':
             // UTC
             return TimeDefinition::UTC();
         case 'w':
         case 'W':
         default:
             // wall time
             return TimeDefinition::WALL();
     }
 }
Example #9
0
     * @param ZoneOffset $wallOffset the wall offset, not null
     * @return LocalDateTime the date-time relative to the wall/before offset, not null
     */
    public function createDateTime(LocalDateTime $dateTime, ZoneOffset $standardOffset, ZoneOffset $wallOffset)
    {
        switch ($this->val) {
            case 0:
                $difference = $wallOffset->getTotalSeconds() - ZoneOffset::UTC()->getTotalSeconds();
                return $dateTime->plusSeconds($difference);
            case 2:
                $difference = $wallOffset->getTotalSeconds() - $standardOffset->getTotalSeconds();
                return $dateTime->plusSeconds($difference);
            default:
                // WALL
                return $dateTime;
        }
    }
    public function __toString()
    {
        switch ($this->val) {
            case 0:
                return "UTC";
            case 1:
                return "WALL";
            case 2:
                return "STANDARD";
        }
    }
}
TimeDefinition::init();