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); }
/** * 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; } }
/** * @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); } } }
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)]]; }
function data_toString() { return [[Instant::ofEpochSecond(65, 567), "1970-01-01T00:01:05.000000567Z"], [Instant::ofEpochSecond(65, 560), "1970-01-01T00:01:05.000000560Z"], [Instant::ofEpochSecond(65, 560000), "1970-01-01T00:01:05.000560Z"], [Instant::ofEpochSecond(65, 560000000), "1970-01-01T00:01:05.560Z"], [Instant::ofEpochSecond(1, 0), "1970-01-01T00:00:01Z"], [Instant::ofEpochSecond(60, 0), "1970-01-01T00:01:00Z"], [Instant::ofEpochSecond(3600, 0), "1970-01-01T01:00:00Z"], [Instant::ofEpochSecond(-1, 0), "1969-12-31T23:59:59Z"], [LocalDateTime::of(0, 1, 2, 0, 0)->toInstant(ZoneOffset::UTC()), "0000-01-02T00:00:00Z"], [LocalDateTime::of(0, 1, 1, 12, 30)->toInstant(ZoneOffset::UTC()), "0000-01-01T12:30:00Z"], [LocalDateTime::of(0, 1, 1, 0, 0, 0, 1)->toInstant(ZoneOffset::UTC()), "0000-01-01T00:00:00.000000001Z"], [LocalDateTime::of(0, 1, 1, 0, 0)->toInstant(ZoneOffset::UTC()), "0000-01-01T00:00:00Z"], [LocalDateTime::of(-1, 12, 31, 23, 59, 59, 999999999)->toInstant(ZoneOffset::UTC()), "-0001-12-31T23:59:59.999999999Z"], [LocalDateTime::of(-1, 12, 31, 12, 30)->toInstant(ZoneOffset::UTC()), "-0001-12-31T12:30:00Z"], [LocalDateTime::of(-1, 12, 30, 12, 30)->toInstant(ZoneOffset::UTC()), "-0001-12-30T12:30:00Z"], [LocalDateTime::of(-9999, 1, 2, 12, 30)->toInstant(ZoneOffset::UTC()), "-9999-01-02T12:30:00Z"], [LocalDateTime::of(-9999, 1, 1, 12, 30)->toInstant(ZoneOffset::UTC()), "-9999-01-01T12:30:00Z"], [LocalDateTime::of(-9999, 1, 1, 0, 0)->toInstant(ZoneOffset::UTC()), "-9999-01-01T00:00:00Z"], [LocalDateTime::of(-10000, 12, 31, 23, 59, 59, 999999999)->toInstant(ZoneOffset::UTC()), "-10000-12-31T23:59:59.999999999Z"], [LocalDateTime::of(-10000, 12, 31, 12, 30)->toInstant(ZoneOffset::UTC()), "-10000-12-31T12:30:00Z"], [LocalDateTime::of(-10000, 12, 30, 12, 30)->toInstant(ZoneOffset::UTC()), "-10000-12-30T12:30:00Z"], [LocalDateTime::of(-15000, 12, 31, 12, 30)->toInstant(ZoneOffset::UTC()), "-15000-12-31T12:30:00Z"], [LocalDateTime::of(-19999, 1, 2, 12, 30)->toInstant(ZoneOffset::UTC()), "-19999-01-02T12:30:00Z"], [LocalDateTime::of(-19999, 1, 1, 12, 30)->toInstant(ZoneOffset::UTC()), "-19999-01-01T12:30:00Z"], [LocalDateTime::of(-19999, 1, 1, 0, 0)->toInstant(ZoneOffset::UTC()), "-19999-01-01T00:00:00Z"], [LocalDateTime::of(-20000, 12, 31, 23, 59, 59, 999999999)->toInstant(ZoneOffset::UTC()), "-20000-12-31T23:59:59.999999999Z"], [LocalDateTime::of(-20000, 12, 31, 12, 30)->toInstant(ZoneOffset::UTC()), "-20000-12-31T12:30:00Z"], [LocalDateTime::of(-20000, 12, 30, 12, 30)->toInstant(ZoneOffset::UTC()), "-20000-12-30T12:30:00Z"], [LocalDateTime::of(-25000, 12, 31, 12, 30)->toInstant(ZoneOffset::UTC()), "-25000-12-31T12:30:00Z"], [LocalDateTime::of(9999, 12, 30, 12, 30)->toInstant(ZoneOffset::UTC()), "9999-12-30T12:30:00Z"], [LocalDateTime::of(9999, 12, 31, 12, 30)->toInstant(ZoneOffset::UTC()), "9999-12-31T12:30:00Z"], [LocalDateTime::of(9999, 12, 31, 23, 59, 59, 999999999)->toInstant(ZoneOffset::UTC()), "9999-12-31T23:59:59.999999999Z"], [LocalDateTime::of(10000, 1, 1, 0, 0)->toInstant(ZoneOffset::UTC()), "+10000-01-01T00:00:00Z"], [LocalDateTime::of(10000, 1, 1, 12, 30)->toInstant(ZoneOffset::UTC()), "+10000-01-01T12:30:00Z"], [LocalDateTime::of(10000, 1, 2, 12, 30)->toInstant(ZoneOffset::UTC()), "+10000-01-02T12:30:00Z"], [LocalDateTime::of(15000, 12, 31, 12, 30)->toInstant(ZoneOffset::UTC()), "+15000-12-31T12:30:00Z"], [LocalDateTime::of(19999, 12, 30, 12, 30)->toInstant(ZoneOffset::UTC()), "+19999-12-30T12:30:00Z"], [LocalDateTime::of(19999, 12, 31, 12, 30)->toInstant(ZoneOffset::UTC()), "+19999-12-31T12:30:00Z"], [LocalDateTime::of(19999, 12, 31, 23, 59, 59, 999999999)->toInstant(ZoneOffset::UTC()), "+19999-12-31T23:59:59.999999999Z"], [LocalDateTime::of(20000, 1, 1, 0, 0)->toInstant(ZoneOffset::UTC()), "+20000-01-01T00:00:00Z"], [LocalDateTime::of(20000, 1, 1, 12, 30)->toInstant(ZoneOffset::UTC()), "+20000-01-01T12:30:00Z"], [LocalDateTime::of(20000, 1, 2, 12, 30)->toInstant(ZoneOffset::UTC()), "+20000-01-02T12:30:00Z"], [LocalDateTime::of(25000, 12, 31, 12, 30)->toInstant(ZoneOffset::UTC()), "+25000-12-31T12:30:00Z"], [LocalDateTime::of(-999999999, 1, 1, 12, 30)->toInstant(ZoneOffset::UTC())->minus(1, CU::DAYS()), "-1000000000-12-31T12:30:00Z"], [LocalDateTime::of(999999999, 12, 31, 12, 30)->toInstant(ZoneOffset::UTC())->plus(1, CU::DAYS()), "+1000000000-01-01T12:30:00Z"], [Instant::MIN(), "-1000000000-01-01T00:00:00Z"], [Instant::MAX(), "+1000000000-12-31T23:59:59.999999999Z"]]; }
public function test_toString() { $offset = ZoneOffset::ofHoursMinutesSeconds(1, 0, 0); $this->assertEquals($offset->__toString(), "+01:00"); $offset = ZoneOffset::ofHoursMinutesSeconds(1, 2, 3); $this->assertEquals($offset->__toString(), "+01:02:03"); $offset = ZoneOffset::UTC(); $this->assertEquals($offset->__toString(), "Z"); }
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 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); } }
/** * @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); } }
public function test_Paris_getOffsetInfo_overlap() { $test = ZoneId::of("Europe/Paris"); $dateTime = LocalDateTime::of(2008, 10, 26, 2, 0, 0, 0); $trans = $this->checkOffset($test->getRules(), $dateTime, ZoneOffset::ofHours(2), self::$OVERLAP); $this->assertEquals($trans->isGap(), false); $this->assertEquals($trans->isOverlap(), true); $this->assertEquals($trans->getOffsetBefore(), ZoneOffset::ofHours(2)); $this->assertEquals($trans->getOffsetAfter(), ZoneOffset::ofHours(1)); $this->assertEquals($trans->getInstant(), $this->createInstant8(2008, 10, 26, 1, 0, 0, 0, ZoneOffset::UTC())); $this->assertEquals($trans->isValidOffset(ZoneOffset::ofHours(0)), false); $this->assertEquals($trans->isValidOffset(ZoneOffset::ofHours(1)), true); $this->assertEquals($trans->isValidOffset(ZoneOffset::ofHours(2)), true); $this->assertEquals($trans->isValidOffset(ZoneOffset::ofHours(3)), false); $this->assertEquals($trans->__toString(), "Transition[Overlap at 2008-10-26T03:00+02:00 to +01:00]"); $this->assertFalse($trans->equals(null)); $this->assertFalse($trans->equals(ZoneOffset::ofHours(2))); $this->assertTrue($trans->equals($trans)); $otherDis = $test->getRules()->getTransition($dateTime); $this->assertTrue($trans->equals($otherDis)); }
function data_parseSuccess() { return [["Z", 1, -1, ZoneId::of("Z")], ["UTC", 3, -1, ZoneId::of("UTC")], ["UT", 2, -1, ZoneId::of("UT")], ["GMT", 3, -1, ZoneId::of("GMT")], ["+00:00", 6, -1, ZoneOffset::UTC()], ["UTC+00:00", 9, -1, ZoneId::of("UTC")], ["UT+00:00", 8, -1, ZoneId::of("UT")], ["GMT+00:00", 9, -1, ZoneId::of("GMT")], ["-00:00", 6, -1, ZoneOffset::UTC()], ["UTC-00:00", 9, -1, ZoneId::of("UTC")], ["UT-00:00", 8, -1, ZoneId::of("UT")], ["GMT-00:00", 9, -1, ZoneId::of("GMT")], ["+01:30", 6, -1, ZoneOffset::ofHoursMinutes(1, 30)], ["UTC+01:30", 9, -1, ZoneId::of("UTC+01:30")], ["UT+02:30", 8, -1, ZoneId::of("UT+02:30")], ["GMT+03:30", 9, -1, ZoneId::of("GMT+03:30")], ["-01:30", 6, -1, ZoneOffset::ofHoursMinutes(-1, -30)], ["UTC-01:30", 9, -1, ZoneId::of("UTC-01:30")], ["UT-02:30", 8, -1, ZoneId::of("UT-02:30")], ["GMT-03:30", 9, -1, ZoneId::of("GMT-03:30")], ["UTC-01:WW", 3, -1, ZoneId::of("UTC")], ["UT-02:WW", 2, -1, ZoneId::of("UT")], ["GMT-03:WW", 3, -1, ZoneId::of("GMT")], ["Z0", 1, -1, ZoneOffset::UTC()], ["UTC1", 3, -1, ZoneId::of("UTC")], ["UTCZ", 3, -1, ZoneId::of("UTC")], ["UTZ", 2, -1, ZoneId::of("UT")], ["GMTZ", 3, -1, ZoneId::of("GMT")], ["UTC0", 3, -1, ZoneId::of("UTC")], ["UT0", 2, -1, ZoneId::of("UT")], ["", 0, 0, null], ["A", 0, 0, null], ["UZ", 0, 0, null], ["GMA", 0, 0, null], ["0", 0, 0, null], ["+", 0, 0, null], ["-", 0, 0, null], ["Europe/London", 13, -1, ZoneId::of("Europe/London")], ["America/New_York", 16, -1, ZoneId::of("America/New_York")], ["America/Bogusville", 0, 0, null]]; }
public function parse(DateTimeParseContext $context, $text, $position) { // TODO cache formatter // new context to avoid overwriting fields like year/month/day $minDigits = $this->fractionalDigits < 0 ? 0 : $this->fractionalDigits; $maxDigits = $this->fractionalDigits < 0 ? 9 : $this->fractionalDigits; $parser = (new DateTimeFormatterBuilder())->append(DateTimeFormatter::ISO_LOCAL_DATE())->appendLiteral('T')->appendValue2(ChronoField::HOUR_OF_DAY(), 2)->appendLiteral(':')->appendValue2(ChronoField::MINUTE_OF_HOUR(), 2)->appendLiteral(':')->appendValue2(ChronoField::SECOND_OF_MINUTE(), 2)->appendFraction(ChronoField::NANO_OF_SECOND(), $minDigits, $maxDigits, true)->appendLiteral('Z')->toFormatter()->toPrinterParser(false); $newContext = $context->copy(); $pos = $parser->parse($newContext, $text, $position); if ($pos < 0) { return $pos; } // parser restricts most fields to 2 digits, so definitely int // correctly parsed nano is also guaranteed to be valid $yearParsed = $newContext->getParsed(ChronoField::YEAR()); $month = $newContext->getParsed(ChronoField::MONTH_OF_YEAR()); $day = $newContext->getParsed(ChronoField::DAY_OF_MONTH()); $hour = $newContext->getParsed(ChronoField::HOUR_OF_DAY()); $min = $newContext->getParsed(ChronoField::MINUTE_OF_HOUR()); $secVal = $newContext->getParsed(ChronoField::SECOND_OF_MINUTE()); $nanoVal = $newContext->getParsed(ChronoField::NANO_OF_SECOND()); $sec = $secVal !== null ? $secVal : 0; $nano = $nanoVal !== null ? $nanoVal : 0; $days = 0; if ($hour === 24 && $min === 0 && $sec === 0 && $nano === 0) { $hour = 0; $days = 1; } else { if ($hour === 23 && $min === 59 && $sec === 60) { $context->setParsedLeapSecond(); $sec = 59; } } $year = $yearParsed % 10000; try { $ldt = LocalDateTime::of($year, $month, $day, $hour, $min, $sec, 0)->plusDays($days); $instantSecs = $ldt->toEpochSecond(ZoneOffset::UTC()); $instantSecs += Math::multiplyExact(Math::div($yearParsed, 10000), self::SECONDS_PER_10000_YEARS); } catch (RuntimeException $ex) { // TODO What do we actually catch here and why return ~$position; } $successPos = $pos; $successPos = $context->setParsedField(ChronoField::INSTANT_SECONDS(), $instantSecs, $position, $successPos); return $context->setParsedField(ChronoField::NANO_OF_SECOND(), $nano, $position, $successPos); }
public function test_now_Clock_min() { $clock = Clock::fixed(Instant::MIN(), ZoneOffset::UTC()); $test = LocalTime::nowOf($clock); $this->assertEquals($test->getHour(), 0); $this->assertEquals($test->getMinute(), 0); $this->assertEquals($test->getSecond(), 0); $this->assertEquals($test->getNano(), 0); }
function data_adjustInto() { return [[OffsetTime::ofLocalTime(LocalTime::of(23, 5), self::OFFSET_PONE()), OffsetTime::ofLocalTime(LocalTime::of(1, 1, 1, 100), ZoneOffset::UTC()), OffsetTime::ofLocalTime(LocalTime::of(23, 5), self::OFFSET_PONE()), null], [OffsetTime::ofLocalTime(LocalTime::of(23, 5), self::OFFSET_PONE()), OffsetTime::MAX(), OffsetTime::ofLocalTime(LocalTime::of(23, 5), self::OFFSET_PONE()), null], [OffsetTime::ofLocalTime(LocalTime::of(23, 5), self::OFFSET_PONE()), OffsetTime::MIN(), OffsetTime::ofLocalTime(LocalTime::of(23, 5), self::OFFSET_PONE()), null], [OffsetTime::MAX(), OffsetTime::ofLocalTime(LocalTime::of(23, 5), self::OFFSET_PONE()), OffsetTime::ofLocalTime(OffsetTime::MAX()->toLocalTime(), ZoneOffset::ofHours(-18)), null], [OffsetTime::MIN(), OffsetTime::ofLocalTime(LocalTime::of(23, 5), self::OFFSET_PONE()), OffsetTime::ofLocalTime(OffsetTime::MIN()->toLocalTime(), ZoneOffset::ofHours(18)), null], [OffsetTime::ofLocalTime(LocalTime::of(23, 5), self::OFFSET_PONE()), ZonedDateTime::ofDateTime(LocalDateTime::of(2012, 3, 4, 1, 1, 1, 100), self::ZONE_GAZA()), ZonedDateTime::ofDateTime(LocalDateTime::of(2012, 3, 4, 23, 5), self::ZONE_GAZA()), null], [OffsetTime::ofLocalTime(LocalTime::of(23, 5), self::OFFSET_PONE()), OffsetDateTime::ofDateTime(LocalDateTime::of(2012, 3, 4, 1, 1, 1, 100), ZoneOffset::UTC()), OffsetDateTime::ofDateTime(LocalDateTime::of(2012, 3, 4, 23, 5), self::OFFSET_PONE()), null], [OffsetTime::ofLocalTime(LocalTime::of(23, 5), self::OFFSET_PONE()), LocalDateTime::of(2012, 3, 4, 1, 1, 1, 100), null, DateTimeException::class], [OffsetTime::ofLocalTime(LocalTime::of(23, 5), self::OFFSET_PONE()), LocalDate::of(2210, 2, 2), null, DateTimeException::class], [OffsetTime::ofLocalTime(LocalTime::of(23, 5), self::OFFSET_PONE()), LocalTime::of(22, 3, 0), null, DateTimeException::class]]; }
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)); }
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()); }
private static function OFFSET_UTC() { return ZoneOffset::UTC(); }
/** * @dataProvider data_toInstant */ public function test_toEpochSecond_UTC(LocalDateTime $ldt, $expectedEpSec, $expectedNos) { $dt = $ldt->atZone(ZoneOffset::UTC()); $this->assertEquals($dt->toEpochSecond(), $expectedEpSec); }
/** * This implementation looks for the longest matching string. * For example, parsing Etc/GMT-2 will return Etc/GMC-2 rather than just * Etc/GMC although both are valid. */ public function parse(DateTimeParseContext $context, $text, $position) { $length = strlen($text); if ($position > $length) { throw new \OutOfRangeException(); } if ($position === $length) { return ~$position; } // handle fixed time-zone IDs $nextChar = $text[$position]; if ($nextChar === '+' || $nextChar === '-') { return $this->parseOffsetBased($context, $text, $position, $position, OffsetIdPrinterParser::INSTANCE_ID_Z()); } else { if ($length >= $position + 2) { $nextNextChar = $text[$position + 1]; if ($context->charEquals($nextChar, 'U') && $context->charEquals($nextNextChar, 'T')) { if ($length >= $position + 3 && $context->charEquals($text[$position + 2], 'C')) { return $this->parseOffsetBased($context, $text, $position, $position + 3, OffsetIdPrinterParser::INSTANCE_ID_ZERO()); } return $this->parseOffsetBased($context, $text, $position, $position + 2, OffsetIdPrinterParser::INSTANCE_ID_ZERO()); } else { if ($context->charEquals($nextChar, 'G') && $length >= $position + 3 && $context->charEquals($nextNextChar, 'M') && $context->charEquals($text[$position + 2], 'T')) { return $this->parseOffsetBased($context, $text, $position, $position + 3, OffsetIdPrinterParser::INSTANCE_ID_ZERO()); } } } } // parse $ppos = new ParsePosition($position); $parsedZoneId = $this->match($text, $ppos, $context->isCaseSensitive()); /* $tree = $this->getTree($context); $parsedZoneId = $tree->match($text, $ppos); */ if ($parsedZoneId === null) { if ($context->charEquals($nextChar, 'Z')) { $context->setParsedZone(ZoneOffset::UTC()); return $position + 1; } return ~$position; } $context->setParsedZone(ZoneId::of($parsedZoneId)); return $ppos->getIndex(); }
public function test_withZone() { $test = $this->fmt; $this->assertEquals($test->getZone(), null); $test = $test->withZone(ZoneId::of("Europe/Paris")); $this->assertEquals($test->getZone(), ZoneId::of("Europe/Paris")); $test = $test->withZone(ZoneOffset::UTC()); $this->assertEquals($test->getZone(), ZoneOffset::UTC()); $test = $test->withZone(null); $this->assertEquals($test->getZone(), null); }
/** * 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_caseInsensitiveUTC_unmatchedCase() { $this->setCaseSensitive(false); $pos = new ParsePosition(0); $parsed = $this->getFormatterPattern("+HH:MM:ss", "Z")->parseUnresolved("z", $pos); $this->assertEquals($pos->getIndex(), 1); $this->assertParsed($parsed, ZoneOffset::UTC()); }
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)]]; }
/** * 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()); }
/** * @expectedException \Celest\DateTimeException */ public function test_now_Clock_tooLow() { $clock = Clock::fixed(self::MIN_INSTANT()->minusNanos(1), ZoneOffset::UTC()); LocalDate::nowOf($clock); }