Esempio n. 1
0
 /**
  * Converts the specified local date-time to the local date-time actually
  * seen on a wall clock.
  * <p>
  * This method converts using the type of this enum.
  * The output is defined relative to the 'before' offset of the transition.
  * <p>
  * The UTC type uses the UTC offset.
  * The STANDARD type uses the standard offset.
  * The WALL type returns the input date-time.
  * The result is intended for use with the wall-offset.
  *
  * @param LocalDateTime $dateTime the local date-time, not null
  * @param ZoneOffset $standardOffset the standard offset, not null
  * @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;
     }
 }
Esempio n. 2
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);
 }
Esempio n. 3
0
 public function testMinDateEpochSec()
 {
     $offset = ZoneOffset::ofTotalSeconds(0);
     $seconds = LocalDateTime::MIN()->toEpochSecond($offset);
     $new = LocalDateTime::ofEpochSecond($seconds, 0, $offset);
     $this->assertEquals(LocalDateTime::MIN(), $new);
 }
Esempio n. 4
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);
 }
Esempio n. 5
0
 /**
  * @dataProvider data_signStyle
  */
 public function test_signStyle(LocalDate $localDate, SignStyle $style, $expectedEx, $expectedStr)
 {
     $builder = new DateTimeFormatterBuilder();
     $formatter = $builder->appendValue3(ChronoField::YEAR(), 2, 4, $style)->toFormatter();
     $formatter = $formatter->withZone(ZoneOffset::UTC());
     if ($expectedEx === null) {
         $output = $formatter->format($localDate);
         $this->assertEquals($output, $expectedStr);
     } else {
         try {
             $formatter->format($localDate);
             $this->fail();
         } catch (\Exception $ex) {
             $this->assertInstanceOf($expectedEx, $ex);
         }
     }
 }
Esempio n. 6
0
 /**
  * A query for {@code ZoneOffset} returning null if not found.
  * @param TemporalAccessor $temporal
  * @return null|ZoneOffset
  * @throws DateTimeException
  */
 public static function _offset(TemporalAccessor $temporal)
 {
     if ($temporal->isSupported(ChronoField::OFFSET_SECONDS())) {
         return ZoneOffset::ofTotalSeconds($temporal->get(ChronoField::OFFSET_SECONDS()));
     }
     return null;
 }
 /**
  * @dataProvider data_toInstant
  */
 public function test_toEpochSecond_UTC(LocalDateTime $ldt, $expectedEpSec, $expectedNos)
 {
     $dt = $ldt->atZone(ZoneOffset::UTC());
     $this->assertEquals($dt->toEpochSecond(), $expectedEpSec);
 }
 /**
  * Parse an offset following a prefix and set the ZoneId if it is valid.
  * To matching the parsing of ZoneId.of the values are not normalized
  * to ZoneOffsets.
  *
  * @param DateTimeParseContext $context the parse context
  * @param string $text the input text
  * @param int $prefixPos start of the prefix
  * @param int $position start of text after the prefix
  * @param OffsetIdPrinterParser $parser parser for the value after the prefix
  * @return int the position after the parse
  */
 private function parseOffsetBased(DateTimeParseContext $context, $text, $prefixPos, $position, OffsetIdPrinterParser $parser)
 {
     $prefix = strtoupper(substr($text, $prefixPos, $position - $prefixPos));
     if ($position >= strlen($text)) {
         $context->setParsedZone(ZoneId::of($prefix));
         return $position;
     }
     // '0' or 'Z' after prefix is not part of a valid ZoneId; use bare prefix
     if ($text[$position] === '0' || $context->charEquals($text[$position], 'Z')) {
         $context->setParsedZone(ZoneId::of($prefix));
         return $position;
     }
     $newContext = $context->copy();
     $endPos = $parser->parse($newContext, $text, $position);
     try {
         if ($endPos < 0) {
             if ($parser == OffsetIdPrinterParser::INSTANCE_ID_Z()) {
                 return ~$prefixPos;
             }
             $context->setParsedZone(ZoneId::of($prefix));
             return $position;
         }
         $offset = $newContext->getParsed(ChronoField::OFFSET_SECONDS());
         $zoneOffset = ZoneOffset::ofTotalSeconds($offset);
         $context->setParsedZone(ZoneId::ofOffset($prefix, $zoneOffset));
         return $endPos;
     } catch (DateTimeException $dte) {
         return ~$prefixPos;
     }
 }
 /**
  * @group long
  */
 public function test_toEpochSecond_beforeEpoch()
 {
     for ($i = 0; $i < 100000; $i++) {
         $a = LocalDateTime::of(1970, 1, 1, 0, 0)->minusSeconds($i);
         $this->assertEquals($a->toEpochSecond(ZoneOffset::UTC()), -$i);
     }
 }
Esempio n. 10
0
 private static function ZDT()
 {
     return LocalDateTime::of(2008, 6, 30, 11, 30, 10, 500)->atZone(ZoneOffset::ofHours(2));
 }
 private function assertParsed(TemporalAccessor $parsed, ZoneOffset $expectedOffset)
 {
     if ($expectedOffset === null) {
         $this->assertEquals(null, $parsed);
     } else {
         $this->assertEquals(true, $parsed->isSupported(ChronoField::OFFSET_SECONDS()), true);
         $this->assertEquals($expectedOffset->getTotalSeconds(), $parsed->getLong(ChronoField::OFFSET_SECONDS()));
     }
 }
 /**
  * @dataProvider provider_sampleToString
  */
 public function test_toString($y, $o, $d, $h, $m, $s, $n, $offsetId, $expected)
 {
     $t = OffsetDateTime::of($y, $o, $d, $h, $m, $s, $n, ZoneOffset::of($offsetId));
     $str = $t->__toString();
     $this->assertEquals($str, $expected);
 }
Esempio n. 13
0
 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));
 }
Esempio n. 14
0
 /**
  * @expectedException \Celest\DateTimeException
  */
 public function test_now_Clock_tooLow()
 {
     $clock = Clock::fixed(self::MIN_INSTANT()->minusNanos(1), ZoneOffset::UTC());
     LocalDate::nowOf($clock);
 }
Esempio n. 15
0
 /**
  * Creates the wall offset for the local date-time at the end of the window.
  *
  * @param int $savingsSecs the amount of savings in use in seconds
  * @return ZoneOffset the created date-time epoch second in the wall offset, not null
  */
 function createWallOffset($savingsSecs)
 {
     return ZoneOffset::ofTotalSeconds($this->standardOffset->getTotalSeconds() + $savingsSecs);
 }
 public function provider_offsets()
 {
     return [["+HH", "NO-OFFSET", ZoneOffset::UTC()], ["+HH", "+01", ZoneOffset::ofHours(1)], ["+HH", "-01", ZoneOffset::ofHours(-1)], ["+HHMM", "NO-OFFSET", ZoneOffset::UTC()], ["+HHMM", "+0102", ZoneOffset::ofHoursMinutes(1, 2)], ["+HHMM", "-0102", ZoneOffset::ofHoursMinutes(-1, -2)], ["+HH:MM", "NO-OFFSET", ZoneOffset::UTC()], ["+HH:MM", "+01:02", ZoneOffset::ofHoursMinutes(1, 2)], ["+HH:MM", "-01:02", ZoneOffset::ofHoursMinutes(-1, -2)], ["+HHMMss", "NO-OFFSET", ZoneOffset::UTC()], ["+HHMMss", "+0100", ZoneOffset::ofHoursMinutesSeconds(1, 0, 0)], ["+HHMMss", "+0102", ZoneOffset::ofHoursMinutesSeconds(1, 2, 0)], ["+HHMMss", "+0159", ZoneOffset::ofHoursMinutesSeconds(1, 59, 0)], ["+HHMMss", "+0200", ZoneOffset::ofHoursMinutesSeconds(2, 0, 0)], ["+HHMMss", "+1800", ZoneOffset::ofHoursMinutesSeconds(18, 0, 0)], ["+HHMMss", "+010215", ZoneOffset::ofHoursMinutesSeconds(1, 2, 15)], ["+HHMMss", "-0100", ZoneOffset::ofHoursMinutesSeconds(-1, 0, 0)], ["+HHMMss", "-0200", ZoneOffset::ofHoursMinutesSeconds(-2, 0, 0)], ["+HHMMss", "-1800", ZoneOffset::ofHoursMinutesSeconds(-18, 0, 0)], ["+HHMMss", "NO-OFFSET", ZoneOffset::UTC()], ["+HHMMss", "+0100", ZoneOffset::ofHoursMinutesSeconds(1, 0, 0)], ["+HHMMss", "+010203", ZoneOffset::ofHoursMinutesSeconds(1, 2, 3)], ["+HHMMss", "+015959", ZoneOffset::ofHoursMinutesSeconds(1, 59, 59)], ["+HHMMss", "+0200", ZoneOffset::ofHoursMinutesSeconds(2, 0, 0)], ["+HHMMss", "+1800", ZoneOffset::ofHoursMinutesSeconds(18, 0, 0)], ["+HHMMss", "-0100", ZoneOffset::ofHoursMinutesSeconds(-1, 0, 0)], ["+HHMMss", "-0200", ZoneOffset::ofHoursMinutesSeconds(-2, 0, 0)], ["+HHMMss", "-1800", ZoneOffset::ofHoursMinutesSeconds(-18, 0, 0)], ["+HH:MM:ss", "NO-OFFSET", ZoneOffset::UTC()], ["+HH:MM:ss", "+01:00", ZoneOffset::ofHoursMinutesSeconds(1, 0, 0)], ["+HH:MM:ss", "+01:02", ZoneOffset::ofHoursMinutesSeconds(1, 2, 0)], ["+HH:MM:ss", "+01:59", ZoneOffset::ofHoursMinutesSeconds(1, 59, 0)], ["+HH:MM:ss", "+02:00", ZoneOffset::ofHoursMinutesSeconds(2, 0, 0)], ["+HH:MM:ss", "+18:00", ZoneOffset::ofHoursMinutesSeconds(18, 0, 0)], ["+HH:MM:ss", "+01:02:15", ZoneOffset::ofHoursMinutesSeconds(1, 2, 15)], ["+HH:MM:ss", "-01:00", ZoneOffset::ofHoursMinutesSeconds(-1, 0, 0)], ["+HH:MM:ss", "-02:00", ZoneOffset::ofHoursMinutesSeconds(-2, 0, 0)], ["+HH:MM:ss", "-18:00", ZoneOffset::ofHoursMinutesSeconds(-18, 0, 0)], ["+HH:MM:ss", "NO-OFFSET", ZoneOffset::UTC()], ["+HH:MM:ss", "+01:00", ZoneOffset::ofHoursMinutesSeconds(1, 0, 0)], ["+HH:MM:ss", "+01:02:03", ZoneOffset::ofHoursMinutesSeconds(1, 2, 3)], ["+HH:MM:ss", "+01:59:59", ZoneOffset::ofHoursMinutesSeconds(1, 59, 59)], ["+HH:MM:ss", "+02:00", ZoneOffset::ofHoursMinutesSeconds(2, 0, 0)], ["+HH:MM:ss", "+18:00", ZoneOffset::ofHoursMinutesSeconds(18, 0, 0)], ["+HH:MM:ss", "-01:00", ZoneOffset::ofHoursMinutesSeconds(-1, 0, 0)], ["+HH:MM:ss", "-02:00", ZoneOffset::ofHoursMinutesSeconds(-2, 0, 0)], ["+HH:MM:ss", "-18:00", ZoneOffset::ofHoursMinutesSeconds(-18, 0, 0)], ["+HHMMSS", "NO-OFFSET", ZoneOffset::UTC()], ["+HHMMSS", "+010203", ZoneOffset::ofHoursMinutesSeconds(1, 2, 3)], ["+HHMMSS", "-010203", ZoneOffset::ofHoursMinutesSeconds(-1, -2, -3)], ["+HHMMSS", "+010200", ZoneOffset::ofHoursMinutesSeconds(1, 2, 0)], ["+HHMMSS", "-010200", ZoneOffset::ofHoursMinutesSeconds(-1, -2, 0)], ["+HH:MM:SS", "NO-OFFSET", ZoneOffset::UTC()], ["+HH:MM:SS", "+01:02:03", ZoneOffset::ofHoursMinutesSeconds(1, 2, 3)], ["+HH:MM:SS", "-01:02:03", ZoneOffset::ofHoursMinutesSeconds(-1, -2, -3)], ["+HH:MM:SS", "+01:02:00", ZoneOffset::ofHoursMinutesSeconds(1, 2, 0)], ["+HH:MM:SS", "-01:02:00", ZoneOffset::ofHoursMinutesSeconds(-1, -2, 0)]];
 }
Esempio n. 17
0
 private static function OFFSET_PTWO()
 {
     return ZoneOffset::of("+02:00");
 }
Esempio n. 18
0
 /**
  * Parse once a prefix is established.
  *
  * @param string $zoneId the time-zone ID, not null
  * @param int $prefixLength the length of the prefix, 2 or 3
  * @param bool $checkAvailable
  * @return ZoneId the zone ID, not null
  * @throws DateTimeException if the zone ID has an invalid format
  */
 private static function ofWithPrefix($zoneId, $prefixLength, $checkAvailable)
 {
     $prefix = substr($zoneId, 0, $prefixLength);
     if (strlen($zoneId) === $prefixLength) {
         return self::ofOffset($prefix, ZoneOffset::UTC());
     }
     if ($zoneId[$prefixLength] != '+' && $zoneId[$prefixLength] != '-') {
         return ZoneRegion::ofId($zoneId, $checkAvailable);
         // drop through to ZoneRulesProvider
     }
     try {
         $offset = ZoneOffset::of(substr($zoneId, $prefixLength));
         if ($offset == ZoneOffset::UTC()) {
             return self::ofOffset($prefix, $offset);
         }
         return self::ofOffset($prefix, $offset);
     } catch (DateTimeException $ex) {
         throw new DateTimeException("Invalid ID for offset-based ZoneId: " . $zoneId, $ex);
     }
 }
 public function test_parse_leapSecond()
 {
     $expected = OffsetDateTime::of(1970, 2, 3, 23, 59, 59, 123456789, ZoneOffset::UTC())->toInstant();
     $f = (new DateTimeFormatterBuilder())->appendInstant4(-1)->toFormatter();
     foreach (ResolverStyle::values() as $style) {
         $pared = $f->withResolverStyle($style)->parse("1970-02-03T23:59:60.123456789Z");
         $this->assertEquals($pared->query(Instant::fromQuery()), $expected);
         $this->assertEquals($pared->query(DateTimeFormatter::parsedExcessDays()), Period::ZERO());
         $this->assertEquals($pared->query(DateTimeFormatter::parsedLeapSecond()), true);
     }
 }
 function data_instantNoZone()
 {
     return [[self::INSTANT(), "2014-06-30T01:02:03Z", ZonedDateTime::of(2014, 6, 30, 1, 2, 3, 0, ZoneOffset::UTC())->toInstant()], [self::INSTANTSECONDS(), "86402", Instant::ofEpochSecond(86402)], [self::INSTANTSECONDS_NOS(), "86402.123456789", Instant::ofEpochSecond(86402, 123456789)]];
 }
 public function test_parseBest_firstOption()
 {
     $test = DateTimeFormatter::ofPattern("yyyy-MM-dd HH:mm[XXX]");
     $result = $test->parseBest("2011-06-30 12:30+03:00", TemporalQueries::fromCallable([ZonedDateTime::class, "from"]), TemporalQueries::fromCallable([LocalDateTime::class, "from"]));
     $ldt = LocalDateTime::of(2011, 6, 30, 12, 30);
     $this->assertEquals($result, ZonedDateTime::ofDateTime($ldt, ZoneOffset::ofHours(3)));
 }
Esempio n. 22
0
 private function doTestOffset(ZoneOffset $offset, $hours, $minutes, $seconds)
 {
     $this->assertEquals($offset->getTotalSeconds(), $hours * 60 * 60 + $minutes * 60 + $seconds);
     if ($hours == 0 && $minutes == 0 && $seconds == 0) {
         $id = "Z";
     } else {
         $str = $hours < 0 || $minutes < 0 || $seconds < 0 ? "-" : "+";
         $str .= substr(Math::abs($hours) + 100, 1);
         $str .= ":";
         $str .= substr(Math::abs($minutes) + 100, 1);
         if ($seconds != 0) {
             $str .= ":";
             $str .= substr(Math::abs($seconds) + 100, 1);
         }
         $id = $str;
     }
     $this->assertEquals($offset->getId(), $id);
     $this->assertEquals($offset, ZoneOffset::ofHoursMinutesSeconds($hours, $minutes, $seconds));
     if ($seconds == 0) {
         $this->assertEquals($offset, ZoneOffset::ofHoursMinutes($hours, $minutes));
         if ($minutes == 0) {
             $this->assertEquals($offset, ZoneOffset::ofHours($hours));
         }
     }
     $this->assertEquals(ZoneOffset::of($id), $offset);
     $this->assertEquals($offset->__toString(), $id);
 }
Esempio n. 23
0
 private function make(ZoneOffset $offset)
 {
     return $offset->getRules();
 }
 /**
  * Returns a string describing this object.
  *
  * @return string a string for debugging, not null
  */
 public function __toString()
 {
     $buf = "TransitionRule[" . ($this->offsetBefore->compareTo($this->offsetAfter) > 0 ? "Gap " : "Overlap ") . $this->offsetBefore . " to " . $this->offsetAfter . ", ";
     if ($this->dow != null) {
         if ($this->dom == -1) {
             $buf .= $this->dow . " on or before last day of " . $this->month;
         } else {
             if ($this->dom < 0) {
                 $buf .= $this->dow . " on or before last day minus " . (-$this->dom - 1) . " of " . $this->month;
             } else {
                 $buf .= $this->dow . " on or after " . $this->month . ' ' . $this->dom;
             }
         }
     } else {
         $buf .= $this->month . ' ' . $this->dom;
     }
     $buf .= " at " . ($this->timeEndOfDay ? "24:00" : $this->time) . " " . $this->timeDefinition . ", standard offset " . $this->standardOffset . ']';
     return $buf;
 }
Esempio n. 25
0
 /**
  * Completes the build converting the builder to a set of time-zone rules.
  * <p>
  * Calling this method alters the state of the builder.
  * Further rules should not be added to this builder once this method is called.
  *
  * @param string $zoneId the time-zone ID, not null
  * @param array $deduplicateMap a map for deduplicating the values, not null
  * @return ZoneRules the zone rules, not null
  * @throws \LogicException if no windows have been added
  * @throws \LogicException if there is only one rule defined as being forever for any given window
  */
 public function _toRules($zoneId, &$deduplicateMap)
 {
     $this->deduplicateMap = $deduplicateMap;
     if (empty($this->windowList)) {
         throw new \LogicException("No windows have been added to the builder");
     }
     /** @var ZoneOffsetTransition[] $standardTransitionList */
     $standardTransitionList = [];
     /** @var ZoneOffsetTransition[] */
     $transitionList = [];
     /** @var ZoneOffsetTransitionRule[] */
     $lastTransitionRuleList = [];
     // initialize the standard offset calculation
     $firstWindow = $this->windowList[0];
     $loopStandardOffset = $firstWindow->standardOffset;
     $loopSavings = 0;
     if ($firstWindow->fixedSavingAmountSecs !== null) {
         $loopSavings = $firstWindow->fixedSavingAmountSecs;
     }
     /** @var ZoneOffset $firstWallOffset */
     $firstWallOffset = $this->deduplicate(ZoneOffset::ofTotalSeconds($loopStandardOffset->getTotalSeconds() + $loopSavings));
     /** @var LocalDateTime $loopWindowStart */
     $loopWindowStart = $this->deduplicate(LocalDateTime::of(Year::MIN_VALUE, 1, 1, 0, 0));
     $loopWindowOffset = $firstWallOffset;
     // build the windows and rules to interesting data
     foreach ($this->windowList as $window) {
         // tidy the state
         $window->tidy($loopWindowStart->getYear());
         // calculate effective savings at the start of the window
         $effectiveSavings = $window->fixedSavingAmountSecs;
         if ($effectiveSavings === null) {
             // apply rules from this window together with the standard offset and
             // savings from the last window to find the savings amount applicable
             // at start of this window
             $effectiveSavings = 0;
             foreach ($window->ruleList as $rule) {
                 $trans = $rule->toTransition($loopStandardOffset, $loopSavings);
                 if ($trans->toEpochSecond() > $loopWindowStart->toEpochSecond($loopWindowOffset)) {
                     // previous savings amount found, which could be the savings amount at
                     // the instant that the window starts (hence isAfter)
                     break;
                 }
                 $effectiveSavings = $rule->savingAmountSecs;
             }
         }
         // check if standard offset changed, and update it
         if ($loopStandardOffset->equals($window->standardOffset) === false) {
             $standardTransitionList[] = $this->deduplicate(ZoneOffsetTransition::of(LocalDateTime::ofEpochSecond($loopWindowStart->toEpochSecond($loopWindowOffset), 0, $loopStandardOffset), $loopStandardOffset, $window->standardOffset));
             $loopStandardOffset = $this->deduplicate($window->standardOffset);
         }
         // check if the start of the window represents a transition
         $effectiveWallOffset = $this->deduplicate(ZoneOffset::ofTotalSeconds($loopStandardOffset->getTotalSeconds() + $effectiveSavings));
         if ($loopWindowOffset->equals($effectiveWallOffset) === false) {
             $trans = $this->deduplicate(ZoneOffsetTransition::of($loopWindowStart, $loopWindowOffset, $effectiveWallOffset));
             $transitionList[] = $trans;
         }
         $loopSavings = $effectiveSavings;
         // apply rules within the window
         foreach ($window->ruleList as $rule) {
             /** @var ZoneOffsetTransition $trans */
             $trans = $this->deduplicate($rule->toTransition($loopStandardOffset, $loopSavings));
             if ($trans !== null && $trans->toEpochSecond() < $loopWindowStart->toEpochSecond($loopWindowOffset) === false && $trans->toEpochSecond() < $window->createDateTimeEpochSecond($loopSavings) && $trans->getOffsetBefore()->equals($trans->getOffsetAfter()) === false) {
                 $transitionList[] = $trans;
                 $loopSavings = $rule->savingAmountSecs;
             }
         }
         // calculate last rules
         foreach ($window->lastRuleList as $lastRule) {
             $transitionRule = $this->deduplicate($lastRule->toTransitionRule($loopStandardOffset, $loopSavings));
             $lastTransitionRuleList[] = $transitionRule;
             $loopSavings = $lastRule->savingAmountSecs;
         }
         // finally we can calculate the true end of the window, passing it to the next window
         $loopWindowOffset = $this->deduplicate($window->createWallOffset($loopSavings));
         $loopWindowStart = $this->deduplicate(LocalDateTime::ofEpochSecond($window->createDateTimeEpochSecond($loopSavings), 0, $loopWindowOffset));
     }
     return ZoneRules::of($firstWindow->standardOffset, $firstWallOffset, $standardTransitionList, $transitionList, $lastTransitionRuleList);
 }
Esempio n. 26
0
 public function test_now_Clock()
 {
     $instant = LocalDateTime::of(2010, 12, 31, 0, 0)->toInstant(ZoneOffset::UTC());
     $clock = Clock::fixed($instant, ZoneOffset::UTC());
     $test = YearMonth::nowOf($clock);
     $this->assertEquals($test->getYear(), 2010);
     $this->assertEquals($test->getMonth(), Month::DECEMBER());
 }
Esempio n. 27
0
 /**
  * Outputs this date-time as a {@code String}, such as {@code 2007-12-03T10:15:30+01:00}.
  * <p>
  * The output will be one of the following ISO-8601 formats:
  * <ul>
  * <li>{@code uuuu-MM-dd'T'HH:mmXXXXX}</li>
  * <li>{@code uuuu-MM-dd'T'HH:mm:ssXXXXX}</li>
  * <li>{@code uuuu-MM-dd'T'HH:mm:ss.SSSXXXXX}</li>
  * <li>{@code uuuu-MM-dd'T'HH:mm:ss.SSSSSSXXXXX}</li>
  * <li>{@code uuuu-MM-dd'T'HH:mm:ss.SSSSSSSSSXXXXX}</li>
  * </ul>
  * The format used will be the shortest that outputs the full value of
  * the time where the omitted parts are implied to be zero.
  *
  * @return string a string representation of this date-time, not null
  */
 public function __toString()
 {
     return $this->dateTime->__toString() . $this->offset->__toString();
 }
Esempio n. 28
0
 private function findYear($epochSecond, ZoneOffset $offset)
 {
     // inline for performance
     $localSecond = $epochSecond + $offset->getTotalSeconds();
     $localEpochDay = Math::floorDiv($localSecond, 86400);
     return LocalDate::ofEpochDay($localEpochDay)->getYear();
 }
Esempio n. 29
0
 /**
  * Obtains a clock that returns the current instant using the best available
  * system clock, converting to date and time using the UTC time-zone.
  * <p>
  * This clock, rather than {@link #systemDefaultZone()}, should be used when
  * you need the current instant without the date or time.
  * <p>
  * This clock is based on the best available system clock.
  * This may use {@link System#currentTimeMillis()}, or a higher resolution
  * clock if one is available.
  * <p>
  * Conversion from instant to date or time uses the {@linkplain ZoneOffset#UTC UTC time-zone}.
  * <p>
  * The returned implementation is immutable, thread-safe and {@code Serializable}.
  * It is equivalent to {@code system(ZoneOffset.UTC)}.
  *
  * @return Clock clock that uses the best available system clock in the UTC zone, not null
  */
 public static function systemUTC()
 {
     return new SystemClock(ZoneOffset::UTC());
 }
Esempio n. 30
0
 public function test_factory_between_TemporalTemporal_mixedTypes()
 {
     $start = Instant::ofEpochSecond(1);
     $end = Instant::ofEpochSecond(4)->atZone(ZoneOffset::UTC());
     $this->assertEquals(Duration::between($start, $end), Duration::ofSeconds(3));
 }