/** * @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_factory_CalendricalObject() { $this->assertEquals(ZoneOffset::from(ZonedDateTime::ofDateTime(LocalDateTime::ofDateAndTime(LocalDate::of(2007, 7, 15), LocalTime::of(17, 30)), ZoneOffset::ofHours(2))), ZoneOffset::ofHours(2)); }
/** * 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_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))); }
/** * 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); }
/** * @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); }
public function test_atZone_dstGap() { $t = LocalDateTime::of(2007, 4, 1, 0, 0); $this->assertEquals($t->atZone(self::ZONE_GAZA()), ZonedDateTime::ofDateTime(LocalDateTime::of(2007, 4, 1, 1, 0), self::ZONE_GAZA())); }
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 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"]]; }
/** * Combines this date-time with a time-zone to create a {@code ZonedDateTime}. * <p> * This returns a {@code ZonedDateTime} formed from this date-time at the * specified time-zone. The result will match this date-time as closely as possible. * 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 be adjusted. * <p> * The local date-time is resolved to a single instant on the time-line. * This is achieved by finding a valid offset from UTC/Greenwich for the local * date-time as defined by the {@link ZoneRules rules} of the zone ID. *<p> * In most cases, there is only one valid offset for a local date-time. * In the case of an overlap, where clocks are set back, there are two valid offsets. * This method uses the earlier offset typically corresponding to "summer". * <p> * In the case of a gap, where clocks jump forward, there is no valid offset. * Instead, the local date-time is adjusted to be later by the length of the gap. * For a typical one hour daylight savings change, the local date-time will be * moved one hour later into the offset typically corresponding to "summer". * <p> * To obtain the later offset during an overlap, call * {@link ZonedDateTime#withLaterOffsetAtOverlap()} on the result of this method. * To throw an exception when there is a gap or overlap, use * {@link ZonedDateTime#ofStrict(LocalDateTime, ZoneOffset, ZoneId)}. * * @param ZoneId $zone the time-zone to use, not null * @return ZonedDateTime the zoned date-time formed from this date-time, not null */ public function atZone(ZoneId $zone) { return ZonedDateTime::ofDateTime($this, $zone); }
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; }
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]]; }