public function getFrom(TemporalAccessor $temporal) { if ($this->isSupportedBy($temporal) === false) { throw new UnsupportedTemporalTypeException("Unsupported field: WeekOfWeekBasedYear"); } return IsoFields::getWeek(LocalDate::from($temporal)); }
public function adjustInto(Temporal $temporal, $newValue) { if ($this->isSupportedBy($temporal) === false) { throw new UnsupportedTemporalTypeException("Unsupported field: WeekBasedYear"); } $newWby = $this->range()->checkValidIntValue($newValue, IsoFields::WEEK_BASED_YEAR()); // strict check $date = LocalDate::from($temporal); $dow = $date->get(ChronoField::DAY_OF_WEEK()); $week = IsoFields::getWeek($date); if ($week == 53 && IsoFields::getWeekRangeInt($newWby) == 52) { $week = 52; } $resolved = LocalDate::of($newWby, 1, 4); // 4th is guaranteed to be in week one $days = $dow - $resolved->get(ChronoField::DAY_OF_WEEK()) + ($week - 1) * 7; $resolved = $resolved->plusDays($days); return $temporal->adjust($resolved); }
public function dateFrom(TemporalAccessor $temporal) { if ($temporal instanceof ThaiBuddhistDate) { return $temporal; } return ThaiBuddhistDate::ofIsoDate(LocalDate::from($temporal)); }
/** * Calculates the period between this date and another date as a {@code Period}. * <p> * This calculates the period between two dates in terms of years, months and days. * The start and end points are {@code this} and the specified date. * The result will be negative if the end is before the start. * The negative sign will be the same in each of year, month and day. * <p> * The calculation is performed using the ISO calendar system. * If necessary, the input date will be converted to ISO. * <p> * The start date is included, but the end date is not. * The period is calculated by removing complete months, then calculating * the remaining number of days, adjusting to ensure that both have the same sign. * The number of months is then normalized into years and months based on a 12 month year. * A month is considered to be complete if the end day-of-month is greater * than or equal to the start day-of-month. * For example, from {@code 2010-01-15} to {@code 2011-03-18} is "1 year, 2 months and 3 days". * <p> * There are two equivalent ways of using this method. * The first is to invoke this method. * The second is to use {@link Period#between(LocalDate, LocalDate)}: * <pre> * // these two lines are equivalent * period = start.until(end); * period = Period.between(start, end); * </pre> * The choice should be made based on which makes the code more readable. * * @param ChronoLocalDate $endDateExclusive the end date, exclusive, which may be in any chronology, not null * @return Period the period between this date and the end date, not null */ public function untilDate(ChronoLocalDate $endDateExclusive) { $end = LocalDate::from($endDateExclusive); $totalMonths = $end->getProlepticMonth() - $this->getProlepticMonth(); // safe $days = $end->day - $this->day; if ($totalMonths > 0 && $days < 0) { $totalMonths--; $calcDate = $this->plusMonths($totalMonths); $days = (int) ($end->toEpochDay() - $calcDate->toEpochDay()); // safe } else { if ($totalMonths < 0 && $days > 0) { $totalMonths++; $days -= $end->lengthOfMonth(); } } $years = Math::div($totalMonths, 12); // safe $months = (int) ($totalMonths % 12); // safe return Period::of(Math::toIntExact($years), $months, $days); }
/** * Obtains an ISO local date from another date-time object. * <p> * This is equivalent to {@link LocalDate#from(TemporalAccessor)}. * * @param TemporalAccessor $temporal the date-time object to convert, not null * @return LocalDate the ISO local date, not null * @throws DateTimeException if unable to create the date */ public function dateFrom(TemporalAccessor $temporal) { return LocalDate::from($temporal); }
/** * Obtains an instance of {@code Year} from a temporal object. * <p> * This obtains a year based on the specified temporal. * A {@code TemporalAccessor} represents an arbitrary set of date and time information, * which this factory converts to an instance of {@code Year}. * <p> * The conversion extracts the {@link ChronoField#YEAR year} field. * The extraction is only permitted if the temporal object has an ISO * chronology, or can be converted to a {@code LocalDate}. * <p> * This method matches the signature of the functional interface {@link TemporalQuery} * allowing it to be used as a query via method reference, {@code Year::from}. * * @param TemporalAccessor $temporal the temporal object to convert, not null * @return Year the year, not null * @throws DateTimeException if unable to convert to a {@code Year} */ public static function from(TemporalAccessor $temporal) { if ($temporal instanceof Year) { return $temporal; } try { if (IsoChronology::INSTANCE()->equals(AbstractChronology::from($temporal)) == false) { $temporal = LocalDate::from($temporal); } return self::of($temporal->get(ChronoField::YEAR())); } catch (DateTimeException $ex) { throw new DateTimeException("Unable to obtain Year from TemporalAccessor: " . $temporal . " of type " . get_class($temporal), $ex); } }
public function test_from_TemporalAccessor_null() { TestHelper::assertNullException($this, function () { LocalDate::from(null); }); }
public function dateFrom(TemporalAccessor $temporal) { if ($temporal instanceof MinguoDate) { return $temporal; } return MinguoDate::ofIsoDate(LocalDate::from($temporal)); }
public function query(TemporalQuery $query) { if ($query == TemporalQueries::zoneId()) { return $this->zone; } else { if ($query == TemporalQueries::chronology()) { return $this->chrono; } else { if ($query == TemporalQueries::localDate()) { return $this->date != null ? LocalDate::from($this->date) : null; } else { if ($query == TemporalQueries::localTime()) { return $this->time; } else { if ($query == TemporalQueries::zone() || $query == TemporalQueries::offset()) { return $query->queryFrom($this); } else { if ($query == TemporalQueries::precision()) { return null; // not a complete date/time } } } } } } // inline TemporalAccessor.super.query(query) as an optimization // non-JDK classes are not permitted to make this optimization return $query->queryFrom($this); }
/** * Obtains an instance of {@code LocalDateTime} from a temporal object. * <p> * This obtains a local date-time based on the specified temporal. * A {@code TemporalAccessor} represents an arbitrary set of date and time information, * which this factory converts to an instance of {@code LocalDateTime}. * <p> * The conversion extracts and combines the {@code LocalDate} and the * {@code LocalTime} from the temporal object. * Implementations are permitted to perform optimizations such as accessing * those fields that are equivalent to the relevant objects. * <p> * This method matches the signature of the functional interface {@link TemporalQuery} * allowing it to be used as a query via method reference, {@code LocalDateTime::from}. * * @param TemporalAccessor $temporal the temporal object to convert, not null * @return LocalDateTime the local date-time, not null * @throws DateTimeException if unable to convert to a {@code LocalDateTime} */ public static function from(TemporalAccessor $temporal) { if ($temporal instanceof LocalDateTime) { return $temporal; } else { if ($temporal instanceof ZonedDateTime) { return $temporal->toLocalDateTime(); } else { if ($temporal instanceof OffsetDateTime) { return $temporal->toLocalDateTime(); } } } try { $date = LocalDate::from($temporal); $time = LocalTime::from($temporal); return new LocalDateTime($date, $time); } catch (DateTimeException $ex) { throw new DateTimeException("Unable to obtain LocalDateTime from TemporalAccessor: " . $temporal . " of type " . get_class($temporal), $ex); } }
public function test_fieldResolvesToChronoZonedDateTime_overrideChrono_matches() { $mdt = MinguoDate::of(100, 6, 30); $mzdt = $mdt->atTime(LocalTime::NOON())->atZone(self::EUROPE_PARIS()); $f = (new DateTimeFormatterBuilder())->appendValue(new ResolvingField($mzdt))->toFormatter(); $f = $f->withChronology(MinguoChronology::INSTANCE()); $accessor = $f->parse("1234567890"); $this->assertEquals($accessor->query(TemporalQueries::localDate()), LocalDate::from($mdt)); $this->assertEquals($accessor->query(TemporalQueries::localTime()), LocalTime::NOON()); $this->assertEquals($accessor->query(TemporalQueries::chronology()), MinguoChronology::INSTANCE()); $this->assertEquals($accessor->query(TemporalQueries::zoneId()), self::EUROPE_PARIS()); }
/** * Obtains a {@code TemporalAdjuster} that wraps a date adjuster. * <p> * The {@code TemporalAdjuster} is based on the low level {@code Temporal} interface. * This method allows an adjustment from {@code LocalDate} to {@code LocalDate} * to be wrapped to match the temporal-based interface. * This is provided for convenience to make user-written adjusters simpler. * <p> * In general, user-written adjusters should be static constants: * <pre>{@code * static TemporalAdjuster TWO_DAYS_LATER = * TemporalAdjusters.ofDateAdjuster(date -> date.plusDays(2)); * }</pre> * * @param $dateBasedAdjuster callable the date-based adjuster, not null * @return TemporalAdjuster the temporal adjuster wrapping on the date adjuster, not null */ public static function ofDateAdjuster(callable $dateBasedAdjuster) { return TemporalAdjusters::fromCallable(function (Temporal $temporal) use($dateBasedAdjuster) { $input = LocalDate::from($temporal); $output = $dateBasedAdjuster($input); return $temporal->adjust($output); }); }
/** * Obtains an instance of {@code ZonedDateTime} from a temporal object. * <p> * This obtains a zoned date-time based on the specified temporal. * A {@code TemporalAccessor} represents an arbitrary set of date and time information, * which this factory converts to an instance of {@code ZonedDateTime}. * <p> * The conversion will first obtain a {@code ZoneId} from the temporal object, * falling back to a {@code ZoneOffset} if necessary. It will then try to obtain * an {@code Instant}, falling back to a {@code LocalDateTime} if necessary. * The result will be either the combination of {@code ZoneId} or {@code ZoneOffset} * with {@code Instant} or {@code LocalDateTime}. * Implementations are permitted to perform optimizations such as accessing * those fields that are equivalent to the relevant objects. * <p> * This method matches the signature of the functional interface {@link TemporalQuery} * allowing it to be used as a query via method reference, {@code ZonedDateTime::from}. * * @param TemporalAccessor $temporal the temporal object to convert, not null * @return ZonedDateTime the zoned date-time, not null * @throws DateTimeException if unable to convert to an {@code ZonedDateTime} */ public static function from(TemporalAccessor $temporal) { if ($temporal instanceof ZonedDateTime) { return $temporal; } try { $zone = ZoneId::from($temporal); if ($temporal->isSupported(ChronoField::INSTANT_SECONDS())) { $epochSecond = $temporal->getLong(ChronoField::INSTANT_SECONDS()); $nanoOfSecond = $temporal->get(ChronoField::NANO_OF_SECOND()); return self::create($epochSecond, $nanoOfSecond, $zone); } else { $date = LocalDate::from($temporal); $time = LocalTime::from($temporal); return self::ofDateAndTime($date, $time, $zone); } } catch (DateTimeException $ex) { throw new DateTimeException("Unable to obtain ZonedDateTime from TemporalAccessor: " . $temporal . " of type " . get_class($temporal), $ex); } }
/** * @dataProvider data_date */ public function test_date_parse(LocalDate $date, FormatStyle $dateStyle, $dateStyleOld, Locale $locale) { $old = \IntlDateFormatter::create($locale->getLocale(), $dateStyleOld, \IntlDateFormatter::NONE, new \DateTimeZone('UTC')); $oldDate = new \DateTime($date->getYear() . '-' . $date->getMonthValue() . '-' . $date->getDayOfMonth(), new \DateTimeZone('UTC')); $text = $old->format($oldDate); $f = $this->builder->appendLocalized($dateStyle, null)->toFormatter2($locale); $parsed = $f->parsePos($text, $this->pos); $this->assertEquals($this->pos->getIndex(), strlen($text)); $this->assertEquals($this->pos->getErrorIndex(), -1); $this->assertEquals(LocalDate::from($parsed), $date); }