public function equals($obj) { if ($obj instanceof FixedClock) { return $this->instant->equals($obj->instant) && $this->zone->equals($obj->zone); } return false; }
public function equals($obj) { if ($obj instanceof SystemClock) { return $this->zone->equals($obj->zone); } return false; }
public function withZone(ZoneId $zone) { if ($zone->equals($this->baseClock->getZone())) { // intentional NPE return $this; } return new TickClock($this->baseClock->withZone($zone), $this->tickNanos); }
public function withZone(ZoneId $zone) { if ($zone->equals($this->baseClock->getZone())) { // intentional NPE return $this; } return new OffsetClock($this->baseClock->withZone($zone), $this->offset); }
/** * @setUp */ public function setUp() { $this->builder = new DateTimeFormatterBuilder(); $this->dta = ZonedDateTime::ofDateTime(LocalDateTime::of(2011, 6, 30, 12, 30, 40, 0), ZoneId::of("Europe/Paris")); $this->locale = Locale::of("en"); $this->decimalStyle = DecimalStyle::STANDARD(); }
public function test_now_ZoneId() { $zone = ZoneId::of("UTC+01:02:03"); $expected = Year::nowOf(Clock::system($zone)); $test = Year::nowIn($zone); for ($i = 0; $i < 100; $i++) { if ($expected->equals($test)) { return; } $expected = Year::nowOf(Clock::system($zone)); $test = Year::nowIn($zone); } $this->assertEquals($test, $expected); }
/** * Gets the system default time-zone. * <p> * This queries {@link TimeZone#getDefault()} to find the default time-zone * and converts it to a {@code ZoneId}. If the system default time-zone is changed, * then the result of this method will also change. * * @return ZoneId the zone ID, not null * @throws DateTimeException if the converted zone ID has an invalid format * @throws ZoneRulesException if the converted zone region ID cannot be found */ public static function systemDefault() { return ZoneId::_of(date_default_timezone_get(), true); }
/** * Outputs this date-time as a {@code String}, such as * {@code 2007-12-03T10:15:30+01:00[Europe/Paris]}. * <p> * The format consists of the {@code LocalDateTime} followed by the {@code ZoneOffset}. * If the {@code ZoneId} is not the same as the offset, then the ID is output. * The output is compatible with ISO-8601 if the offset and ID are the same. * * @return string a string representation of this date-time, not null */ public function __toString() { $str = $this->dateTime->__toString() . $this->offset->__toString(); // equals if ($this->offset != $this->zone) { $str .= '[' . $this->zone->__toString() . ']'; } return $str; }
public function test_adjustInto_ZonedDateTime() { $base = ZoneOffset::ofHoursMinutesSeconds(1, 1, 1); foreach (ZoneId::getAvailableZoneIds() as $zoneId) { //Do not change $offset of ZonedDateTime after adjustInto() $zonedDateTime_target = ZonedDateTime::ofDateAndTime(LocalDate::of(1909, 2, 2), LocalTime::of(10, 10, 10), ZoneId::of($zoneId)); $zonedDateTime_result = $base->adjustInto($zonedDateTime_target); $this->assertEquals($zonedDateTime_target->getOffset(), $zonedDateTime_result->getOffset()); $offsetDateTime_target = $zonedDateTime_target->toOffsetDateTime(); $offsetDateTime_result = $base->adjustInto($offsetDateTime_target); $this->assertEquals($base, $offsetDateTime_result->getOffset()); } }
private static function PARIS() { return ZoneId::of("Europe/Paris"); }
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); }
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]]; }
private static function ZONE_GAZA() { return ZoneId::of("Asia/Gaza"); }
/** * Constructor. * TODO Package visiblity * @param string $id the time-zone ID, not null * @param ZoneRules $rules the rules, null for lazy lookup */ public function __construct($id, ZoneRules $rules) { parent::__construct(); $this->id = $id; $this->rules = $rules; }
/** * @expectedException \Celest\DateTimeParseException */ public function test_fieldResolvesToChronoZonedDateTime_overrideZone_wrongZone() { $zdt = ZonedDateTime::of(2010, 6, 30, 12, 30, 0, 0, self::EUROPE_PARIS()); $f = (new DateTimeFormatterBuilder())->appendValue(new ResolvingField($zdt))->toFormatter(); $f = $f->withZone(ZoneId::of("Europe/London")); $f->parse("1234567890"); }
public function test_get_TzdbFixed() { $test = ZoneId::of("+01:30"); $this->assertEquals($test->getId(), "+01:30"); $this->assertEquals($test->getRules()->isFixedOffset(), true); }
private static function preferred_s() { return [ZoneId::of("CET"), ZoneId::of("Australia/South"), ZoneId::of("Australia/West"), ZoneId::of("Asia/Shanghai")]; }
public function zonedDateTimeFrom(TemporalAccessor $temporal) { try { $zone = ZoneId::from($temporal); try { $instant = Instant::from($temporal); return $this->zonedDateTime($instant, $zone); } catch (DateTimeException $ex1) { $cldt = ChronoLocalDateTimeImpl::ensureValid($this, $this->localDateTime($temporal)); return ChronoZonedDateTimeImpl::ofBest($cldt, $zone, null); } } catch (DateTimeException $ex) { throw new DateTimeException("Unable to obtain ChronoZonedDateTime from TemporalAccessor: " . get_class($temporal), $ex); } }
/** * Obtains an instance of {@code LocalDateTime} from an {@code Instant} and zone ID. * <p> * This creates a local date-time based on the specified instant. * First, the offset from UTC/Greenwich is obtained using the zone ID and instant, * which is simple as there is only one valid offset for each instant. * Then, the instant and offset are used to calculate the local date-time. * * @param Instant $instant the instant to create the date-time from, not null * @param ZoneId $zone the time-zone, which may be an offset, not null * @return LocalDateTime the local date-time, not null * @throws DateTimeException if the result exceeds the supported range */ public static function ofInstant(Instant $instant, ZoneId $zone) { $rules = $zone->getRules(); $offset = $rules->getOffset($instant); return self::ofEpochSecond($instant->getEpochSecond(), $instant->getNano(), $offset); }
/** * @dataProvider provider_sampleToString */ public function test_toString($y, $o, $d, $h, $m, $s, $n, $zoneId, $expected) { $z = ZonedDateTime::ofDateTime($this->dateTime($y, $o, $d, $h, $m, $s, $n), ZoneId::of($zoneId)); $str = $z->__toString(); $this->assertEquals($str, $expected); }
/** * @dataProvider data_toString */ public function test_toString($id, $expected) { $test = ZoneId::of($id); $this->assertEquals($test->__toString(), $expected); }
public function withZoneSameInstant(ZoneId $zone) { return $this->zone->equals($zone) ? $this : $this->create($this->dateTime->toInstant($this->offset), $zone); }
/** * Returns a zoned date-time from this date at the earliest valid time according * to the rules in the time-zone. * <p> * Time-zone rules, such as daylight savings, mean that not every local date-time * is valid for the specified zone, thus the local date-time may not be midnight. * <p> * In most cases, there is only one valid offset for a local date-time. * In the case of an overlap, there are two valid offsets, and the earlier one is used, * corresponding to the first occurrence of midnight on the date. * In the case of a gap, the zoned date-time will represent the instant just after the gap. * <p> * If the zone ID is a {@link ZoneOffset}, then the result always has a time of midnight. * <p> * To convert to a specific time in a given time-zone call {@link #atTime(LocalTime)} * followed by {@link LocalDateTime#atZone(ZoneId)}. * * @param ZoneId $zone the zone ID to use, not null * @return ZonedDateTime the zoned date-time formed from this date and the earliest valid time for the zone, not null */ public function atStartOfDayWithZone(ZoneId $zone) { // need to handle case where there is a gap from 11:30 to 00:30 // standard ZDT factory would result in 01:00 rather than 00:30 $ldt = $this->atTime(LocalTime::MIDNIGHT()); if ($zone instanceof ZoneOffset === false) { $rules = $zone->getRules(); $trans = $rules->getTransition($ldt); if ($trans != null && $trans->isGap()) { $ldt = $trans->getDateTimeAfter(); } } return ZonedDateTime::ofDateTime($ldt, $zone); }
private function resolveFields() { // resolve CF $this->resolveInstantFields(); $this->resolveDateFields(); $this->resolveTimeFields(); // if any other fields, handle them // any lenient date resolution should return epoch-day if (!$this->fieldValues->isEmpty()) { $changedCount = 0; outer: while ($changedCount < 50) { foreach ($this->fieldValues as $targetField => $value) { /** @var CF $targetField */ $resolvedObject = $targetField->resolve($this->fieldValues, $this, $this->resolverStyle); if ($resolvedObject !== null) { if ($resolvedObject instanceof ChronoZonedDateTime) { $czdt = $resolvedObject; if ($this->zone === null) { $this->zone = $czdt->getZone(); } else { if ($this->zone->equals($czdt->getZone()) == false) { throw new DateTimeException("ChronoZonedDateTime must use the effective parsed zone: " . $this->zone); } } $resolvedObject = $czdt->toLocalDateTime(); } if ($resolvedObject instanceof ChronoLocalDateTime) { $cldt = $resolvedObject; $this->updateCheckConflict($cldt->toLocalTime(), Period::ZERO()); $this->updateCheckConflict1($cldt->toLocalDate()); $changedCount++; continue 2; // have to restart to avoid concurrent modification } if ($resolvedObject instanceof ChronoLocalDate) { $this->updateCheckConflict1($resolvedObject); $changedCount++; continue 2; // have to restart to avoid concurrent modification } if ($resolvedObject instanceof LocalTime) { $this->updateCheckConflict($resolvedObject, Period::ZERO()); $changedCount++; continue 2; // have to restart to avoid concurrent modification } throw new DateTimeException("Method resolve() can only return ChronoZonedDateTime, " . "ChronoLocalDateTime, ChronoLocalDate or LocalTime"); } else { if ($this->fieldValues->has($targetField) === false) { $changedCount++; continue 2; // have to restart to avoid concurrent modification } } } break; } if ($changedCount === 50) { // catch infinite loops throw new DateTimeException("One of the parsed fields has an incorrectly implemented resolve method"); } // if something changed then have to redo CF resolve if ($changedCount > 0) { $this->resolveInstantFields(); $this->resolveDateFields(); $this->resolveTimeFields(); } } }
public function test_Apia_jumpForwardOverInternationalDateLine_P12_to_M12() { // transition occurred at 1879-07-04T00:00+12:33:04 $test = $this->pacificApia(); $instantBefore = LocalDate::of(1879, 7, 2)->atStartOfDayWithZone(ZoneOffset::UTC())->toInstant(); $trans = $test->nextTransition($instantBefore); $this->assertEquals($trans->getDateTimeBefore(), LocalDateTime::of(1879, 7, 5, 0, 0)); $this->assertEquals($trans->getDateTimeAfter(), LocalDateTime::of(1879, 7, 4, 0, 0)); $this->assertEquals($trans->isGap(), false); $this->assertEquals($trans->isOverlap(), true); $this->assertEquals($trans->isValidOffset(ZoneOffset::ofHoursMinutesSeconds(+12, 33, 4)), true); $this->assertEquals($trans->isValidOffset(ZoneOffset::ofHoursMinutesSeconds(-11, -26, -56)), true); $this->assertEquals($trans->getDuration(), Duration::ofHours(-24)); $this->assertEquals($trans->getInstant(), LocalDateTime::of(1879, 7, 4, 0, 0)->toInstant(ZoneOffset::ofHoursMinutesSeconds(-11, -26, -56))); $zdt = ZonedDateTime::of(1879, 7, 4, 23, 0, 0, 0, ZoneId::of("Pacific/Apia")); $this->assertEquals($zdt->plusHours(2)->toLocalDateTime(), LocalDateTime::of(1879, 7, 4, 1, 0, 0)); }
/** * Constructor. * TODO visiblity * @param int $totalSeconds the total time-zone offset in seconds, from -64800 to +64800 */ public function __construct($totalSeconds) { parent::__construct(); $this->totalSeconds = $totalSeconds; $this->id = $this->buildId($totalSeconds); }
/** * Obtains an instance of {@code OffsetDateTime} from an {@code Instant} and zone ID. * <p> * This creates an offset date-time with the same instant as that specified. * Finding the offset from UTC/Greenwich is simple as there is only one valid * offset for each instant. * * @param Instant $instant the instant to create the date-time from, not null * @param ZoneId $zone the time-zone, which may be an offset, not null * @return OffsetDateTime the offset date-time, not null * @throws DateTimeException if the result exceeds the supported range */ public static function ofInstant(Instant $instant, ZoneId $zone) { $rules = $zone->getRules(); $offset = $rules->getOffset($instant); $ldt = LocalDateTime::ofEpochSecond($instant->getEpochSecond(), $instant->getNano(), $offset); return new OffsetDateTime($ldt, $offset); }
/** * TODO performance * * @param string $text * @param ParsePosition $ppos * @param bool $isCaseSensitive * @return null|string */ private function match($text, ParsePosition $ppos, $isCaseSensitive) { $ids = ZoneId::getAvailableZoneIds(); if (!$isCaseSensitive) { $ids = \array_map('\\strtolower', $ids); $text = \strtolower($text); } $ids = \array_flip($ids); $pos = $ppos->getIndex(); $max = \strlen($text) - 1; for ($i = $max; $i >= $pos; $i--) { $str = \substr($text, $pos, $i - $pos + 1); if (isset($ids[$str])) { $ppos->setIndex($i + 1); return ZoneId::getAvailableZoneIds()[$ids[$str]]; } } return null; }
/** * Obtains a clock that returns the current instant using the best available * system clock, converting to date and time using the default time-zone. * <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> * Using this method hard codes a dependency to the default time-zone into your application. * It is recommended to avoid this and use a specific time-zone whenever possible. * The {@link #systemUTC() UTC clock} should be used when you need the current instant * without the date or time. * <p> * The returned implementation is immutable, thread-safe and {@code Serializable}. * It is equivalent to {@code system(ZoneId.systemDefault())}. * * @return Clock clock that uses the best available system clock in the default zone, not null * @see ZoneId#systemDefault() */ public static function systemDefaultZone() { return new SystemClock(ZoneId::systemDefault()); }
/** * @param Expected $expected * @param $offsetId * @param $zoneId * @throws DateTimeException */ private function buildCalendrical($expected, $offsetId, $zoneId) { if ($offsetId !== null) { $expected->add(ZoneOffset::of($offsetId)); } if ($zoneId !== null) { $expected->zone = ZoneId::of($zoneId); } }