/** * Obtains a local date in the Thai Buddhist calendar system from the epoch-day. * * @param int $epochDay the epoch day * @return ThaiBuddhistDate the Thai Buddhist local date, not null * @throws DateTimeException if unable to create the date */ public function dateEpochDay($epochDay) { return ThaiBuddhistDate::ofIsoDate(LocalDate::ofEpochDay($epochDay)); }
/** * A query for {@code LocalDate} returning null if not found. * @param TemporalAccessor $temporal * @return null|LocalDate */ public static function _localDate(TemporalAccessor $temporal) { if ($temporal->isSupported(ChronoField::EPOCH_DAY())) { return LocalDate::ofEpochDay($temporal->getLong(ChronoField::EPOCH_DAY())); } return null; }
/** * Returns a copy of this date with the specified field set to a new value. * <p> * This returns a {@code LocalDate}, based on this one, with the value * for the specified field changed. * This can be used to change any supported field, such as the year, month or day-of-month. * If it is not possible to set the value, because the field is not supported or for * some other reason, an exception is thrown. * <p> * In some cases, changing the specified field can cause the resulting date to become invalid, * such as changing the month from 31st January to February would make the day-of-month invalid. * In cases like this, the field is responsible for resolving the date. Typically it will choose * the previous valid date, which would be the last valid day of February in this example. * <p> * If the field is a {@link ChronoField} then the adjustment is implemented here. * The supported fields behave as follows: * <ul> * <li>{@code DAY_OF_WEEK} - * Returns a {@code LocalDate} with the specified day-of-week. * The date is adjusted up to 6 days forward or backward within the boundary * of a Monday to Sunday week. * <li>{@code ALIGNED_DAY_OF_WEEK_IN_MONTH} - * Returns a {@code LocalDate} with the specified aligned-day-of-week. * The date is adjusted to the specified month-based aligned-day-of-week. * Aligned weeks are counted such that the first week of a given month starts * on the first day of that month. * This may cause the date to be moved up to 6 days into the following month. * <li>{@code ALIGNED_DAY_OF_WEEK_IN_YEAR} - * Returns a {@code LocalDate} with the specified aligned-day-of-week. * The date is adjusted to the specified year-based aligned-day-of-week. * Aligned weeks are counted such that the first week of a given year starts * on the first day of that year. * This may cause the date to be moved up to 6 days into the following year. * <li>{@code DAY_OF_MONTH} - * Returns a {@code LocalDate} with the specified day-of-month. * The month and year will be unchanged. If the day-of-month is invalid for the * year and month, then a {@code DateTimeException} is thrown. * <li>{@code DAY_OF_YEAR} - * Returns a {@code LocalDate} with the specified day-of-year. * The year will be unchanged. If the day-of-year is invalid for the * year, then a {@code DateTimeException} is thrown. * <li>{@code EPOCH_DAY} - * Returns a {@code LocalDate} with the specified epoch-day. * This completely replaces the date and is equivalent to {@link #ofEpochDay(long)}. * <li>{@code ALIGNED_WEEK_OF_MONTH} - * Returns a {@code LocalDate} with the specified aligned-week-of-month. * Aligned weeks are counted such that the first week of a given month starts * on the first day of that month. * This adjustment moves the date in whole week chunks to match the specified week. * The result will have the same day-of-week as this date. * This may cause the date to be moved into the following month. * <li>{@code ALIGNED_WEEK_OF_YEAR} - * Returns a {@code LocalDate} with the specified aligned-week-of-year. * Aligned weeks are counted such that the first week of a given year starts * on the first day of that year. * This adjustment moves the date in whole week chunks to match the specified week. * The result will have the same day-of-week as this date. * This may cause the date to be moved into the following year. * <li>{@code MONTH_OF_YEAR} - * Returns a {@code LocalDate} with the specified month-of-year. * The year will be unchanged. The day-of-month will also be unchanged, * unless it would be invalid for the new month and year. In that case, the * day-of-month is adjusted to the maximum valid value for the new month and year. * <li>{@code PROLEPTIC_MONTH} - * Returns a {@code LocalDate} with the specified proleptic-month. * The day-of-month will be unchanged, unless it would be invalid for the new month * and year. In that case, the day-of-month is adjusted to the maximum valid value * for the new month and year. * <li>{@code YEAR_OF_ERA} - * Returns a {@code LocalDate} with the specified year-of-era. * The era and month will be unchanged. The day-of-month will also be unchanged, * unless it would be invalid for the new month and year. In that case, the * day-of-month is adjusted to the maximum valid value for the new month and year. * <li>{@code YEAR} - * Returns a {@code LocalDate} with the specified year. * The month will be unchanged. The day-of-month will also be unchanged, * unless it would be invalid for the new month and year. In that case, the * day-of-month is adjusted to the maximum valid value for the new month and year. * <li>{@code ERA} - * Returns a {@code LocalDate} with the specified era. * The year-of-era and month will be unchanged. The day-of-month will also be unchanged, * unless it would be invalid for the new month and year. In that case, the * day-of-month is adjusted to the maximum valid value for the new month and year. * </ul> * <p> * In all cases, if the new value is outside the valid range of values for the field * then a {@code DateTimeException} will be thrown. * <p> * All other {@code ChronoField} instances will throw an {@code UnsupportedTemporalTypeException}. * <p> * If the field is not a {@code ChronoField}, then the result of this method * is obtained by invoking {@code TemporalField.adjustInto(Temporal, long)} * passing {@code this} as the argument. In this case, the field determines * whether and how to adjust the instant. * <p> * This instance is immutable and unaffected by this method call. * * @param TemporalField $field the field to set in the result, not null * @param int $newValue the new value of the field in the result * @return LocalDate a {@code LocalDate} based on {@code this} with the specified field set, not null * @throws DateTimeException if the field cannot be set * @throws UnsupportedTemporalTypeException if the field is not supported * @throws ArithmeticException if numeric overflow occurs */ public function with(TemporalField $field, $newValue) { if ($field instanceof ChronoField) { /** @var Chronofield $f */ $f = $field; $f->checkValidValue($newValue); switch ($f) { case ChronoField::DAY_OF_WEEK(): return $this->plusDays($newValue - $this->getDayOfWeek()->getValue()); case ChronoField::ALIGNED_DAY_OF_WEEK_IN_MONTH(): return $this->plusDays($newValue - $this->getLong(ChronoField::ALIGNED_DAY_OF_WEEK_IN_MONTH())); case ChronoField::ALIGNED_DAY_OF_WEEK_IN_YEAR(): return $this->plusDays($newValue - $this->getLong(ChronoField::ALIGNED_DAY_OF_WEEK_IN_YEAR())); case ChronoField::DAY_OF_MONTH(): return $this->withDayOfMonth((int) $newValue); case ChronoField::DAY_OF_YEAR(): return $this->withDayOfYear((int) $newValue); case ChronoField::EPOCH_DAY(): return LocalDate::ofEpochDay($newValue); case ChronoField::ALIGNED_WEEK_OF_MONTH(): return $this->plusWeeks($newValue - $this->getLong(ChronoField::ALIGNED_WEEK_OF_MONTH())); case ChronoField::ALIGNED_WEEK_OF_YEAR(): return $this->plusWeeks($newValue - $this->getLong(ChronoField::ALIGNED_WEEK_OF_YEAR())); case ChronoField::MONTH_OF_YEAR(): return $this->withMonth((int) $newValue); case ChronoField::PROLEPTIC_MONTH(): return $this->plusMonths($newValue - $this->getProlepticMonth()); case ChronoField::YEAR_OF_ERA(): return $this->withYear((int) ($this->year >= 1 ? $newValue : 1 - $newValue)); case ChronoField::YEAR(): return $this->withYear((int) $newValue); case ChronoField::ERA(): return $this->getLong(ChronoField::ERA()) == $newValue ? $this : $this->withYear(1 - $this->year); } throw new UnsupportedTemporalTypeException("Unsupported field: " . $field); } return $field->adjustInto($this, $newValue); }
/** * Obtains an ISO local date from the epoch-day. * <p> * This is equivalent to {@link LocalDate#ofEpochDay(long)}. * * @param int $epochDay the epoch day * @return LocalDate the ISO local date, not null * @throws DateTimeException if unable to create the date */ public function dateEpochDay($epochDay) { return LocalDate::ofEpochDay($epochDay); }
/** * @expectedException \Celest\DateTimeException */ public function test_factory_ofEpochDay_belowMin() { LocalDate::ofEpochDay(self::MIN_VALID_EPOCHDAYS() - 1); }
/** * Obtains a local date in the Minguo calendar system from the epoch-day. * * @param int $epochDay the epoch day * @return MinguoDate the Minguo local date, not null * @throws DateTimeException if unable to create the date */ public function dateEpochDay($epochDay) { return MinguoDate::ofIsoDate(LocalDate::ofEpochDay($epochDay)); }
private function findYear($epochSecond, ZoneOffset $offset) { // inline for performance $localSecond = $epochSecond + $offset->getTotalSeconds(); $localEpochDay = Math::floorDiv($localSecond, 86400); return LocalDate::ofEpochDay($localEpochDay)->getYear(); }
/** * Obtains an instance of {@code LocalDateTime} using seconds from the * epoch of 1970-01-01T00:00:00Z. * <p> * This allows the {@link ChronoField#INSTANT_SECONDS epoch-second} field * to be converted to a local date-time. This is primarily intended for * low-level conversions rather than general application usage. * * @param int $epochSecond the number of seconds from the epoch of 1970-01-01T00:00:00Z * @param int $nanoOfSecond the nanosecond within the second, from 0 to 999,999,999 * @param ZoneOffset $offset the zone offset, not null * @return LocalDateTime the local date-time, not null * @throws DateTimeException if the result exceeds the supported range, * or if the nano-of-second is invalid */ public static function ofEpochSecond($epochSecond, $nanoOfSecond, ZoneOffset $offset) { try { ChronoField::NANO_OF_SECOND()->checkValidValue($nanoOfSecond); $localSecond = Math::addExact($epochSecond, $offset->getTotalSeconds()); $localEpochDay = Math::floorDiv($localSecond, LocalTime::SECONDS_PER_DAY); $secsOfDay = Math::floorMod($localSecond, LocalTime::SECONDS_PER_DAY); $date = LocalDate::ofEpochDay($localEpochDay); $time = LocalTime::ofNanoOfDay($secsOfDay * LocalTime::NANOS_PER_SECOND + $nanoOfSecond); return new LocalDateTime($date, $time); } catch (ArithmeticException $ex) { throw new DateTimeException("Value out of bounds", $ex); } }