/** * @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(); }
private static function dateTimeZoned($year, $month, $dayOfMonth, $hour, $minute, $second, $nanoOfSecond, ZoneOffset $offset, ZoneId $zoneId) { return ZonedDateTime::ofStrict(LocalDateTime::of($year, $month, $dayOfMonth, $hour, $minute, $second, $nanoOfSecond), $offset, $zoneId); }
/** * Combines this instant with a time-zone to create a {@code ZonedDateTime}. * <p> * This returns an {@code ZonedDateTime} formed from this instant at the * specified time-zone. An exception will be thrown if the instant is too * large to fit into a zoned date-time. * <p> * This method is equivalent to * {@link ZonedDateTime#ofInstant(Instant, ZoneId) ZonedDateTime.ofInstant(this, zone)}. * * @param ZoneId $zone the zone to combine with, not null * @return ZonedDateTime the zoned date-time formed from this instant and the specified zone, not null * @throws DateTimeException if the result exceeds the supported range */ public function atZone(ZoneId $zone) { return ZonedDateTime::ofInstant($this, $zone); }
public function test_atZone_dstOverlap() { $t = LocalDateTime::of(2007, 10, 28, 2, 30); $this->assertEquals($t->atZone(self::ZONE_PARIS()), ZonedDateTime::ofStrict(LocalDateTime::of(2007, 10, 28, 2, 30), self::OFFSET_PTWO(), self::ZONE_PARIS())); }
/** * Obtains an ISO zoned date-time in this chronology from an {@code Instant}. * <p> * This is equivalent to {@link ZonedDateTime#ofInstant(Instant, ZoneId)}. * * @param Instant $instant the instant to create the date-time from, not null * @param ZoneId $zone the time-zone, not null * @return ZonedDateTime the zoned date-time, not null * @throws DateTimeException if the result exceeds the supported range */ public function zonedDateTime(Instant $instant, ZoneId $zone) { return ZonedDateTime::ofInstant($instant, $zone); }
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]]; }
/** * Converts this date-time to a {@code ZonedDateTime} using the offset as the zone ID. * <p> * This creates the simplest possible {@code ZonedDateTime} using the offset * as the zone ID. * <p> * To control the time-zone used, see {@link #atZoneSameInstant(ZoneId)} and * {@link #atZoneSimilarLocal(ZoneId)}. * * @return ZonedDateTime a zoned date-time representing the same local date-time and offset, not null */ public function toZonedDateTime() { return ZonedDateTime::ofDateTime($this->dateTime, $this->offset); }
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)); }
public function data_formatStyle() { return [[ZonedDateTime::ofDateTime(LocalDateTime::of(2001, 10, 2, 1, 2, 3), self::ZONEID_PARIS()), FormatStyle::FULL(), "Tuesday, October 2, 2001 1:02:03 AM CEST Europe/Paris"], [ZonedDateTime::ofDateTime(LocalDateTime::of(2001, 10, 2, 1, 2, 3), self::ZONEID_PARIS()), FormatStyle::LONG(), "October 2, 2001 1:02:03 AM CEST Europe/Paris"], [ZonedDateTime::ofDateTime(LocalDateTime::of(2001, 10, 2, 1, 2, 3), self::ZONEID_PARIS()), FormatStyle::MEDIUM(), "Oct 2, 2001, 1:02:03 AM Europe/Paris"], [ZonedDateTime::ofDateTime(LocalDateTime::of(2001, 10, 2, 1, 2, 3), self::ZONEID_PARIS()), FormatStyle::SHORT(), "10/2/01, 1:02 AM Europe/Paris"], [ZonedDateTime::ofDateTime(LocalDateTime::of(2001, 10, 2, 1, 2, 3), self::OFFSET_PTWO()), FormatStyle::FULL(), "Tuesday, October 2, 2001 at 1:02:03 AM +02:00 +02:00"], [ZonedDateTime::ofDateTime(LocalDateTime::of(2001, 10, 2, 1, 2, 3), self::OFFSET_PTWO()), FormatStyle::LONG(), "October 2, 2001 at 1:02:03 AM +02:00 +02:00"], [ZonedDateTime::ofDateTime(LocalDateTime::of(2001, 10, 2, 1, 2, 3), self::OFFSET_PTWO()), FormatStyle::MEDIUM(), "Oct 2, 2001, 1:02:03 AM +02:00"], [ZonedDateTime::ofDateTime(LocalDateTime::of(2001, 10, 2, 1, 2, 3), self::OFFSET_PTWO()), FormatStyle::SHORT(), "10/2/01, 1:02 AM +02:00"]]; }
/** * @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"); }
/** * Calculates the amount of time until another date-time in terms of the specified unit. * <p> * This calculates the amount of time between two {@code ZonedDateTime} * objects in terms of a single {@code TemporalUnit}. * The start and end points are {@code this} and the specified date-time. * The result will be negative if the end is before the start. * For example, the amount in days between two date-times can be calculated * using {@code startDateTime.until(endDateTime, DAYS)}. * <p> * The {@code Temporal} passed to this method is converted to a * {@code ZonedDateTime} using {@link #from(TemporalAccessor)}. * If the time-zone differs between the two zoned date-times, the specified * end date-time is normalized to have the same zone as this date-time. * <p> * The calculation returns a whole number, representing the number of * complete units between the two date-times. * For example, the amount in months between 2012-06-15T00:00Z and 2012-08-14T23:59Z * will only be one month as it is one minute short of two months. * <p> * There are two equivalent ways of using this method. * The first is to invoke this method. * The second is to use {@link TemporalUnit#between(Temporal, Temporal)}: * <pre> * // these two lines are equivalent * amount = start.until(end, MONTHS); * amount = MONTHS.between(start, end); * </pre> * The choice should be made based on which makes the code more readable. * <p> * The calculation is implemented in this method for {@link ChronoUnit}. * The units {@code NANOS}, {@code MICROS}, {@code MILLIS}, {@code SECONDS}, * {@code MINUTES}, {@code HOURS} and {@code HALF_DAYS}, {@code DAYS}, * {@code WEEKS}, {@code MONTHS}, {@code YEARS}, {@code DECADES}, * {@code CENTURIES}, {@code MILLENNIA} and {@code ERAS} are supported. * Other {@code ChronoUnit} values will throw an exception. * <p> * The calculation for date and time units differ. * <p> * Date units operate on the local time-line, using the local date-time. * For example, the period from noon on day 1 to noon the following day * in days will always be counted as exactly one day, irrespective of whether * there was a daylight savings change or not. * <p> * Time units operate on the instant time-line. * The calculation effectively converts both zoned date-times to instants * and then calculates the period between the instants. * For example, the period from noon on day 1 to noon the following day * in hours may be 23, 24 or 25 hours (or some other amount) depending on * whether there was a daylight savings change or not. * <p> * If the unit is not a {@code ChronoUnit}, then the result of this method * is obtained by invoking {@code TemporalUnit.between(Temporal, Temporal)} * passing {@code this} as the first argument and the converted input temporal * as the second argument. * <p> * This instance is immutable and unaffected by this method call. * * @param Temporal $endExclusive the end date, exclusive, which is converted to a {@code ZonedDateTime}, not null * @param TemporalUnit $unit the unit to measure the amount in, not null * @return int the amount of time between this date-time and the end date-time * @throws DateTimeException if the amount cannot be calculated, or the end * temporal cannot be converted to a {@code ZonedDateTime} * @throws UnsupportedTemporalTypeException if the unit is not supported * @throws ArithmeticException if numeric overflow occurs */ public function until(Temporal $endExclusive, TemporalUnit $unit) { $end = ZonedDateTime::from($endExclusive); if ($unit instanceof ChronoUnit) { $end = $end->withZoneSameInstant($this->zone); if ($unit->isDateBased()) { return $this->dateTime->until($end->dateTime, $unit); } else { return $this->toOffsetDateTime()->until($end->toOffsetDateTime(), $unit); } } return $unit->between($this, $end); }
function data_adjustInto() { return [[LocalTime::of(23, 5), LocalTime::of(4, 1, 1, 100), LocalTime::of(23, 5, 0, 0), null], [LocalTime::of(23, 5, 20), LocalTime::of(4, 1, 1, 100), LocalTime::of(23, 5, 20, 0), null], [LocalTime::of(23, 5, 20, 1000), LocalTime::of(4, 1, 1, 100), LocalTime::of(23, 5, 20, 1000), null], [LocalTime::of(23, 5, 20, 1000), LocalTime::MAX(), LocalTime::of(23, 5, 20, 1000), null], [LocalTime::of(23, 5, 20, 1000), LocalTime::MIN(), LocalTime::of(23, 5, 20, 1000), null], [LocalTime::of(23, 5, 20, 1000), LocalTime::NOON(), LocalTime::of(23, 5, 20, 1000), null], [LocalTime::of(23, 5, 20, 1000), LocalTime::MIDNIGHT(), LocalTime::of(23, 5, 20, 1000), null], [LocalTime::MAX(), LocalTime::of(23, 5, 20, 1000), LocalTime::of(23, 59, 59, 999999999), null], [LocalTime::MIN(), LocalTime::of(23, 5, 20, 1000), LocalTime::of(0, 0, 0), null], [LocalTime::NOON(), LocalTime::of(23, 5, 20, 1000), LocalTime::of(12, 0, 0), null], [LocalTime::MIDNIGHT(), LocalTime::of(23, 5, 20, 1000), LocalTime::of(0, 0, 0), null], [LocalTime::of(23, 5), LocalDateTime::of(2210, 2, 2, 1, 1), LocalDateTime::of(2210, 2, 2, 23, 5), null], [LocalTime::of(23, 5), OffsetTime::of(1, 1, 0, 0, self::OFFSET_PTWO()), OffsetTime::of(23, 5, 0, 0, self::OFFSET_PTWO()), null], [LocalTime::of(23, 5), OffsetDateTime::of(2210, 2, 2, 1, 1, 0, 0, self::OFFSET_PTWO()), OffsetDateTime::of(2210, 2, 2, 23, 5, 0, 0, self::OFFSET_PTWO()), null], [LocalTime::of(23, 5), ZonedDateTime::of(2210, 2, 2, 1, 1, 0, 0, self::ZONE_PARIS()), ZonedDateTime::of(2210, 2, 2, 23, 5, 0, 0, self::ZONE_PARIS()), null], [LocalTime::of(23, 5), LocalDate::of(2210, 2, 2), null, DateTimeException::class]]; }
function provider_weekDate() { $date = ZonedDateTime::ofDateTime(LocalDateTime::of(2003, 12, 29, 11, 5, 30), ZoneId::of("Europe/Paris")); $endDate = $date->withYear(2005)->withMonth(1)->withDayOfMonth(2); $week = 1; $day = 1; $ret = []; while (!$date->isAfter($endDate)) { $sb = "2004-W"; if ($week < 10) { $sb .= '0'; } $sb .= $week . '-' . $day . $date->getOffset(); $ret[] = [$date, $sb]; $date = $date->plusDays(1); $day += 1; if ($day == 8) { $day = 1; $week++; } } return $ret; }
/** * 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); }
function data_atStartOfDayZoneId() { return [[LocalDate::of(2008, 6, 30), self::ZONE_PARIS(), ZonedDateTime::ofDateTime(LocalDateTime::of(2008, 6, 30, 0, 0), self::ZONE_PARIS())], [LocalDate::of(2008, 6, 30), self::OFFSET_PONE(), ZonedDateTime::ofDateTime(LocalDateTime::of(2008, 6, 30, 0, 0), self::OFFSET_PONE())], [LocalDate::of(2007, 4, 1), self::ZONE_GAZA(), ZonedDateTime::ofDateTime(LocalDateTime::of(2007, 4, 1, 1, 0), self::ZONE_GAZA())]]; }
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))); }
public function test_atZoneSimilarLocal_dstGap() { $t = OffsetDateTime::of(2007, 4, 1, 0, 0, 0, 0, self::OFFSET_MTWO()); $this->assertEquals($t->atZoneSimilarLocal(self::ZONE_GAZA()), ZonedDateTime::of(2007, 4, 1, 1, 0, 0, 0, self::ZONE_GAZA())); }
/** * @dataProvider data_text */ public function test_getText(TemporalField $field, $value, TextStyle $style, Locale $locale, $expected) { $fmt = $this->getFormatterFieldStyle($field, $style)->withLocale($locale); $this->assertEquals($expected, $fmt->format(ZonedDateTime::now()->with($field, $value))); }
/** * @dataProvider data_instantNoZone * @expectedException \Celest\DateTimeException */ public function test_parse_instantNoZone_ZDT(DateTimeFormatter $formatter, $text, Instant $expected) { $actual = $formatter->parse($text); ZonedDateTime::from($actual); }
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()); } }
function data_adjustInto() { return [[Instant::ofEpochSecond(10, 200), Instant::ofEpochSecond(20), Instant::ofEpochSecond(10, 200), null], [Instant::ofEpochSecond(10, -200), Instant::now(), Instant::ofEpochSecond(10, -200), null], [Instant::ofEpochSecond(-10), Instant::EPOCH(), Instant::ofEpochSecond(-10), null], [Instant::ofEpochSecond(10), Instant::MIN(), Instant::ofEpochSecond(10), null], [Instant::ofEpochSecond(10), Instant::MAX(), Instant::ofEpochSecond(10), null], [Instant::ofEpochSecond(10, 200), LocalDateTime::of(1970, 1, 1, 0, 0, 20)->toInstant(ZoneOffset::UTC()), Instant::ofEpochSecond(10, 200), null], [Instant::ofEpochSecond(10, 200), OffsetDateTime::of(1970, 1, 1, 0, 0, 20, 10, ZoneOffset::UTC()), OffsetDateTime::of(1970, 1, 1, 0, 0, 10, 200, ZoneOffset::UTC()), null], [Instant::ofEpochSecond(10, 200), OffsetDateTime::of(1970, 1, 1, 0, 0, 20, 10, self::OFFSET_PTWO()), OffsetDateTime::of(1970, 1, 1, 2, 0, 10, 200, self::OFFSET_PTWO()), null], [Instant::ofEpochSecond(10, 200), ZonedDateTime::of(1970, 1, 1, 0, 0, 20, 10, self::ZONE_PARIS()), ZonedDateTime::of(1970, 1, 1, 1, 0, 10, 200, self::ZONE_PARIS()), null], [Instant::ofEpochSecond(10, 200), LocalDateTime::of(1970, 1, 1, 0, 0, 20), null, DateTimeException::class]]; }