function data_fieldAndAccessor() { return [[CF::YEAR(), LocalDate::of(2000, 2, 29), true, 2000], [CF::YEAR(), LocalDateTime::of(2000, 2, 29, 5, 4, 3, 200), true, 2000], [CF::MONTH_OF_YEAR(), LocalDate::of(2000, 2, 29), true, 2], [CF::MONTH_OF_YEAR(), LocalDateTime::of(2000, 2, 29, 5, 4, 3, 200), true, 2], [CF::DAY_OF_MONTH(), LocalDate::of(2000, 2, 29), true, 29], [CF::DAY_OF_MONTH(), LocalDateTime::of(2000, 2, 29, 5, 4, 3, 200), true, 29], [CF::DAY_OF_YEAR(), LocalDate::of(2000, 2, 29), true, 60], [CF::DAY_OF_YEAR(), LocalDateTime::of(2000, 2, 29, 5, 4, 3, 200), true, 60], [CF::HOUR_OF_DAY(), LocalTime::of(5, 4, 3, 200), true, 5], [CF::HOUR_OF_DAY(), LocalDateTime::of(2000, 2, 29, 5, 4, 3, 200), true, 5], [CF::MINUTE_OF_DAY(), LocalTime::of(5, 4, 3, 200), true, 5 * 60 + 4], [CF::MINUTE_OF_DAY(), LocalDateTime::of(2000, 2, 29, 5, 4, 3, 200), true, 5 * 60 + 4], [CF::MINUTE_OF_HOUR(), LocalTime::of(5, 4, 3, 200), true, 4], [CF::MINUTE_OF_HOUR(), LocalDateTime::of(2000, 2, 29, 5, 4, 3, 200), true, 4], [CF::SECOND_OF_DAY(), LocalTime::of(5, 4, 3, 200), true, 5 * 3600 + 4 * 60 + 3], [CF::SECOND_OF_DAY(), LocalDateTime::of(2000, 2, 29, 5, 4, 3, 200), true, 5 * 3600 + 4 * 60 + 3], [CF::SECOND_OF_MINUTE(), LocalTime::of(5, 4, 3, 200), true, 3], [CF::SECOND_OF_MINUTE(), LocalDateTime::of(2000, 2, 29, 5, 4, 3, 200), true, 3], [CF::NANO_OF_SECOND(), LocalTime::of(5, 4, 3, 200), true, 200], [CF::NANO_OF_SECOND(), LocalDateTime::of(2000, 2, 29, 5, 4, 3, 200), true, 200], [CF::YEAR(), LocalTime::of(5, 4, 3, 200), false, -1], [CF::MONTH_OF_YEAR(), LocalTime::of(5, 4, 3, 200), false, -1], [CF::DAY_OF_MONTH(), LocalTime::of(5, 4, 3, 200), false, -1], [CF::DAY_OF_YEAR(), LocalTime::of(5, 4, 3, 200), false, -1], [CF::HOUR_OF_DAY(), LocalDate::of(2000, 2, 29), false, -1], [CF::MINUTE_OF_DAY(), LocalDate::of(2000, 2, 29), false, -1], [CF::MINUTE_OF_HOUR(), LocalDate::of(2000, 2, 29), false, -1], [CF::SECOND_OF_DAY(), LocalDate::of(2000, 2, 29), false, -1], [CF::SECOND_OF_MINUTE(), LocalDate::of(2000, 2, 29), false, -1], [CF::NANO_OF_SECOND(), LocalDate::of(2000, 2, 29), false, -1]]; }
/** * @dataProvider data_instantNoZone */ public function test_parse_instantNoZone_supported(DateTimeFormatter $formatter, $text, Instant $expected) { $actual = $formatter->parse($text); $this->assertEquals($actual->isSupported(CF::INSTANT_SECONDS()), true); $this->assertEquals($actual->isSupported(CF::EPOCH_DAY()), false); $this->assertEquals($actual->isSupported(CF::SECOND_OF_DAY()), false); $this->assertEquals($actual->isSupported(CF::NANO_OF_SECOND()), true); $this->assertEquals($actual->isSupported(CF::MICRO_OF_SECOND()), true); $this->assertEquals($actual->isSupported(CF::MILLI_OF_SECOND()), true); }
public static function ISO_LOCAL_TIME() { return self::$ISO_LOCAL_TIME = (new DateTimeFormatterBuilder())->appendValue2(ChronoField::HOUR_OF_DAY(), 2)->appendLiteral(':')->appendValue2(ChronoField::MINUTE_OF_HOUR(), 2)->optionalStart()->appendLiteral(':')->appendValue2(ChronoField::SECOND_OF_MINUTE(), 2)->optionalStart()->appendFraction(ChronoField::NANO_OF_SECOND(), 0, 9, true)->toFormatter3(ResolverStyle::STRICT(), null); }
public function test_toString_noDecimalPoint() { $this->assertEquals("Fraction(NanoOfSecond,3,6)", $this->getFormatterFraction(ChronoField::NANO_OF_SECOND(), 3, 6, false)->__toString()); }
public function test_getLong_TemporalField() { $test = ZonedDateTime::ofDateTime(LocalDateTime::of(2008, 6, 30, 12, 30, 40, 987654321), self::ZONE_0100()); $this->assertEquals($test->getLong(CF::YEAR()), 2008); $this->assertEquals($test->getLong(CF::MONTH_OF_YEAR()), 6); $this->assertEquals($test->getLong(CF::DAY_OF_MONTH()), 30); $this->assertEquals($test->getLong(CF::DAY_OF_WEEK()), 1); $this->assertEquals($test->getLong(CF::DAY_OF_YEAR()), 182); $this->assertEquals($test->getLong(CF::HOUR_OF_DAY()), 12); $this->assertEquals($test->getLong(CF::MINUTE_OF_HOUR()), 30); $this->assertEquals($test->getLong(CF::SECOND_OF_MINUTE()), 40); $this->assertEquals($test->getLong(CF::NANO_OF_SECOND()), 987654321); $this->assertEquals($test->getLong(CF::HOUR_OF_AMPM()), 0); $this->assertEquals($test->getLong(CF::AMPM_OF_DAY()), 1); $this->assertEquals($test->getLong(CF::OFFSET_SECONDS()), 3600); $this->assertEquals($test->getLong(CF::INSTANT_SECONDS()), $test->toEpochSecond()); }
function data_with_longTemporalField() { return [[Instant::ofEpochSecond(10, 200), CF::INSTANT_SECONDS(), 100, Instant::ofEpochSecond(100, 200), null], [Instant::ofEpochSecond(10, 200), CF::INSTANT_SECONDS(), 0, Instant::ofEpochSecond(0, 200), null], [Instant::ofEpochSecond(10, 200), CF::INSTANT_SECONDS(), -100, Instant::ofEpochSecond(-100, 200), null], [Instant::ofEpochSecond(10, 200), CF::NANO_OF_SECOND(), 100, Instant::ofEpochSecond(10, 100), null], [Instant::ofEpochSecond(10, 200), CF::NANO_OF_SECOND(), 0, Instant::ofEpochSecond(10), null], [Instant::ofEpochSecond(10, 200), CF::MICRO_OF_SECOND(), 100, Instant::ofEpochSecond(10, 100 * 1000), null], [Instant::ofEpochSecond(10, 200), CF::MICRO_OF_SECOND(), 0, Instant::ofEpochSecond(10), null], [Instant::ofEpochSecond(10, 200), CF::MILLI_OF_SECOND(), 100, Instant::ofEpochSecond(10, 100 * 1000 * 1000), null], [Instant::ofEpochSecond(10, 200), CF::MILLI_OF_SECOND(), 0, Instant::ofEpochSecond(10), null], [Instant::ofEpochSecond(10, 200), CF::NANO_OF_SECOND(), 1000000000, null, DateTimeException::class], [Instant::ofEpochSecond(10, 200), CF::MICRO_OF_SECOND(), 1000000, null, DateTimeException::class], [Instant::ofEpochSecond(10, 200), CF::MILLI_OF_SECOND(), 1000, null, DateTimeException::class], [Instant::ofEpochSecond(10, 200), CF::SECOND_OF_MINUTE(), 1, null, DateTimeException::class], [Instant::ofEpochSecond(10, 200), CF::SECOND_OF_DAY(), 1, null, DateTimeException::class], [Instant::ofEpochSecond(10, 200), CF::OFFSET_SECONDS(), 1, null, DateTimeException::class], [Instant::ofEpochSecond(10, 200), CF::NANO_OF_DAY(), 1, null, DateTimeException::class], [Instant::ofEpochSecond(10, 200), CF::MINUTE_OF_HOUR(), 1, null, DateTimeException::class], [Instant::ofEpochSecond(10, 200), CF::MINUTE_OF_DAY(), 1, null, DateTimeException::class], [Instant::ofEpochSecond(10, 200), CF::MILLI_OF_DAY(), 1, null, DateTimeException::class], [Instant::ofEpochSecond(10, 200), CF::MICRO_OF_DAY(), 1, null, DateTimeException::class]]; }
public function test_parse_fromField_SecondOfMinute_NanoOfSecond() { $fmt = (new DateTimeFormatterBuilder())->appendValue(ChronoField::SECOND_OF_MINUTE())->appendLiteral('.')->appendValue(ChronoField::NANO_OF_SECOND())->toFormatter(); $acc = $fmt->parse("32.123456789"); $this->assertEquals($acc->isSupported(ChronoField::SECOND_OF_MINUTE()), true); $this->assertEquals($acc->isSupported(ChronoField::NANO_OF_SECOND()), true); $this->assertEquals($acc->isSupported(ChronoField::MICRO_OF_SECOND()), true); $this->assertEquals($acc->isSupported(ChronoField::MILLI_OF_SECOND()), true); $this->assertEquals($acc->getLong(ChronoField::SECOND_OF_MINUTE()), 32); $this->assertEquals($acc->getLong(ChronoField::NANO_OF_SECOND()), 123456789); $this->assertEquals($acc->getLong(ChronoField::MICRO_OF_SECOND()), 123456); $this->assertEquals($acc->getLong(ChronoField::MILLI_OF_SECOND()), 123); }
public function test_getLong_TemporalField() { $test = LocalDateTime::of(2008, 6, 30, 12, 30, 40, 987654321); $this->assertEquals($test->getLong(CF::YEAR()), 2008); $this->assertEquals($test->getLong(CF::MONTH_OF_YEAR()), 6); $this->assertEquals($test->getLong(CF::DAY_OF_MONTH()), 30); $this->assertEquals($test->getLong(CF::DAY_OF_WEEK()), 1); $this->assertEquals($test->getLong(CF::DAY_OF_YEAR()), 182); $this->assertEquals($test->getLong(CF::HOUR_OF_DAY()), 12); $this->assertEquals($test->getLong(CF::MINUTE_OF_HOUR()), 30); $this->assertEquals($test->getLong(CF::SECOND_OF_MINUTE()), 40); $this->assertEquals($test->getLong(CF::NANO_OF_SECOND()), 987654321); $this->assertEquals($test->getLong(CF::HOUR_OF_AMPM()), 0); $this->assertEquals($test->getLong(CF::AMPM_OF_DAY()), 1); }
/** * 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); } }
public function parse(DateTimeParseContext $context, $text, $position) { // TODO cache formatter // new context to avoid overwriting fields like year/month/day $minDigits = $this->fractionalDigits < 0 ? 0 : $this->fractionalDigits; $maxDigits = $this->fractionalDigits < 0 ? 9 : $this->fractionalDigits; $parser = (new DateTimeFormatterBuilder())->append(DateTimeFormatter::ISO_LOCAL_DATE())->appendLiteral('T')->appendValue2(ChronoField::HOUR_OF_DAY(), 2)->appendLiteral(':')->appendValue2(ChronoField::MINUTE_OF_HOUR(), 2)->appendLiteral(':')->appendValue2(ChronoField::SECOND_OF_MINUTE(), 2)->appendFraction(ChronoField::NANO_OF_SECOND(), $minDigits, $maxDigits, true)->appendLiteral('Z')->toFormatter()->toPrinterParser(false); $newContext = $context->copy(); $pos = $parser->parse($newContext, $text, $position); if ($pos < 0) { return $pos; } // parser restricts most fields to 2 digits, so definitely int // correctly parsed nano is also guaranteed to be valid $yearParsed = $newContext->getParsed(ChronoField::YEAR()); $month = $newContext->getParsed(ChronoField::MONTH_OF_YEAR()); $day = $newContext->getParsed(ChronoField::DAY_OF_MONTH()); $hour = $newContext->getParsed(ChronoField::HOUR_OF_DAY()); $min = $newContext->getParsed(ChronoField::MINUTE_OF_HOUR()); $secVal = $newContext->getParsed(ChronoField::SECOND_OF_MINUTE()); $nanoVal = $newContext->getParsed(ChronoField::NANO_OF_SECOND()); $sec = $secVal !== null ? $secVal : 0; $nano = $nanoVal !== null ? $nanoVal : 0; $days = 0; if ($hour === 24 && $min === 0 && $sec === 0 && $nano === 0) { $hour = 0; $days = 1; } else { if ($hour === 23 && $min === 59 && $sec === 60) { $context->setParsedLeapSecond(); $sec = 59; } } $year = $yearParsed % 10000; try { $ldt = LocalDateTime::of($year, $month, $day, $hour, $min, $sec, 0)->plusDays($days); $instantSecs = $ldt->toEpochSecond(ZoneOffset::UTC()); $instantSecs += Math::multiplyExact(Math::div($yearParsed, 10000), self::SECONDS_PER_10000_YEARS); } catch (RuntimeException $ex) { // TODO What do we actually catch here and why return ~$position; } $successPos = $pos; $successPos = $context->setParsedField(ChronoField::INSTANT_SECONDS(), $instantSecs, $position, $successPos); return $context->setParsedField(ChronoField::NANO_OF_SECOND(), $nano, $position, $successPos); }
function data_withTemporalField_outOfRange() { return [[CF::NANO_OF_SECOND(), $this->time(0, 0, 0, 0), CF::NANO_OF_SECOND()->range()->getMinimum() - 1], [CF::NANO_OF_SECOND(), $this->time(0, 0, 0, 0), CF::NANO_OF_SECOND()->range()->getMaximum() + 1], [CF::NANO_OF_DAY(), $this->time(0, 0, 0, 0), CF::NANO_OF_DAY()->range()->getMinimum() - 1], [CF::NANO_OF_DAY(), $this->time(0, 0, 0, 0), CF::NANO_OF_DAY()->range()->getMaximum() + 1], [CF::MICRO_OF_SECOND(), $this->time(0, 0, 0, 0), CF::MICRO_OF_SECOND()->range()->getMinimum() - 1], [CF::MICRO_OF_SECOND(), $this->time(0, 0, 0, 0), CF::MICRO_OF_SECOND()->range()->getMaximum() + 1], [CF::MICRO_OF_DAY(), $this->time(0, 0, 0, 0), CF::MICRO_OF_DAY()->range()->getMinimum() - 1], [CF::MICRO_OF_DAY(), $this->time(0, 0, 0, 0), CF::MICRO_OF_DAY()->range()->getMaximum() + 1], [CF::MILLI_OF_SECOND(), $this->time(0, 0, 0, 0), CF::MILLI_OF_SECOND()->range()->getMinimum() - 1], [CF::MILLI_OF_SECOND(), $this->time(0, 0, 0, 0), CF::MILLI_OF_SECOND()->range()->getMaximum() + 1], [CF::MILLI_OF_DAY(), $this->time(0, 0, 0, 0), CF::MILLI_OF_DAY()->range()->getMinimum() - 1], [CF::MILLI_OF_DAY(), $this->time(0, 0, 0, 0), CF::MILLI_OF_DAY()->range()->getMaximum() + 1], [CF::SECOND_OF_MINUTE(), $this->time(0, 0, 0, 0), CF::SECOND_OF_MINUTE()->range()->getMinimum() - 1], [CF::SECOND_OF_MINUTE(), $this->time(0, 0, 0, 0), CF::SECOND_OF_MINUTE()->range()->getMaximum() + 1], [CF::SECOND_OF_DAY(), $this->time(0, 0, 0, 0), CF::SECOND_OF_DAY()->range()->getMinimum() - 1], [CF::SECOND_OF_DAY(), $this->time(0, 0, 0, 0), CF::SECOND_OF_DAY()->range()->getMaximum() + 1], [CF::MINUTE_OF_HOUR(), $this->time(0, 0, 0, 0), CF::MINUTE_OF_HOUR()->range()->getMinimum() - 1], [CF::MINUTE_OF_HOUR(), $this->time(0, 0, 0, 0), CF::MINUTE_OF_HOUR()->range()->getMaximum() + 1], [CF::MINUTE_OF_DAY(), $this->time(0, 0, 0, 0), CF::MINUTE_OF_DAY()->range()->getMinimum() - 1], [CF::MINUTE_OF_DAY(), $this->time(0, 0, 0, 0), CF::MINUTE_OF_DAY()->range()->getMaximum() + 1], [CF::HOUR_OF_AMPM(), $this->time(0, 0, 0, 0), CF::HOUR_OF_AMPM()->range()->getMinimum() - 1], [CF::HOUR_OF_AMPM(), $this->time(0, 0, 0, 0), CF::HOUR_OF_AMPM()->range()->getMaximum() + 1], [CF::CLOCK_HOUR_OF_AMPM(), $this->time(0, 0, 0, 0), CF::CLOCK_HOUR_OF_AMPM()->range()->getMinimum() - 1], [CF::CLOCK_HOUR_OF_AMPM(), $this->time(0, 0, 0, 0), CF::CLOCK_HOUR_OF_AMPM()->range()->getMaximum() + 1], [CF::HOUR_OF_DAY(), $this->time(0, 0, 0, 0), CF::HOUR_OF_DAY()->range()->getMinimum() - 1], [CF::HOUR_OF_DAY(), $this->time(0, 0, 0, 0), CF::HOUR_OF_DAY()->range()->getMaximum() + 1], [CF::CLOCK_HOUR_OF_DAY(), $this->time(0, 0, 0, 0), CF::CLOCK_HOUR_OF_DAY()->range()->getMinimum() - 1], [CF::CLOCK_HOUR_OF_DAY(), $this->time(0, 0, 0, 0), CF::CLOCK_HOUR_OF_DAY()->range()->getMaximum() + 1], [CF::AMPM_OF_DAY(), $this->time(0, 0, 0, 0), CF::AMPM_OF_DAY()->range()->getMinimum() - 1], [CF::AMPM_OF_DAY(), $this->time(0, 0, 0, 0), CF::AMPM_OF_DAY()->range()->getMaximum() + 1]]; }
private function buildAccessorInstant($instantSecs, $nano) { $mock = new MockAccessor(); $mock->fields->put(ChronoField::INSTANT_SECONDS(), $instantSecs); if ($nano !== null) { $mock->fields->put(ChronoField::NANO_OF_SECOND(), $nano); } return $mock; }
public function test_with_TemporalField() { $test = OffsetTime::of(12, 30, 40, 987654321, self::OFFSET_PONE()); $this->assertEquals($test->with(CF::HOUR_OF_DAY(), 15), OffsetTime::of(15, 30, 40, 987654321, self::OFFSET_PONE())); $this->assertEquals($test->with(CF::MINUTE_OF_HOUR(), 50), OffsetTime::of(12, 50, 40, 987654321, self::OFFSET_PONE())); $this->assertEquals($test->with(CF::SECOND_OF_MINUTE(), 50), OffsetTime::of(12, 30, 50, 987654321, self::OFFSET_PONE())); $this->assertEquals($test->with(CF::NANO_OF_SECOND(), 12345), OffsetTime::of(12, 30, 40, 12345, self::OFFSET_PONE())); $this->assertEquals($test->with(CF::HOUR_OF_AMPM(), 6), OffsetTime::of(18, 30, 40, 987654321, self::OFFSET_PONE())); $this->assertEquals($test->with(CF::AMPM_OF_DAY(), 0), OffsetTime::of(0, 30, 40, 987654321, self::OFFSET_PONE())); $this->assertEquals($test->with(CF::OFFSET_SECONDS(), 7205), OffsetTime::of(12, 30, 40, 987654321, ZoneOffset::ofHoursMinutesSeconds(2, 0, 5))); }
/** * Returns a copy of this duration with the specified nano-of-second. * <p> * This returns a duration with the specified nano-of-second, retaining the * seconds part of this duration. * <p> * This instance is immutable and unaffected by this method call. * * @param int $nanoOfSecond the nano-of-second to represent, from 0 to 999,999,999 * @return Duration a {@code Duration} based on this period with the requested nano-of-second, not null * @throws DateTimeException if the nano-of-second is invalid */ public function withNanos($nanoOfSecond) { ChronoField::NANO_OF_SECOND()->checkValidIntValue($nanoOfSecond); return self::create($this->seconds, $nanoOfSecond); }
public function test_adjacent_lenient_fractionFollows_0digit() { // succeeds because hour/min are fixed width $f = $this->builder->parseLenient()->appendValue2(ChronoField::HOUR_OF_DAY(), 2)->appendValue2(ChronoField::MINUTE_OF_HOUR(), 2)->appendFraction(ChronoField::NANO_OF_SECOND(), 3, 3, false)->toFormatter2(Locale::UK()); $pp = new ParsePosition(0); $parsed = $f->parseUnresolved("1230", $pp); $this->assertEquals($pp->getErrorIndex(), -1); $this->assertEquals($pp->getIndex(), 4); $this->assertEquals($parsed->getLong(ChronoField::HOUR_OF_DAY()), 12); $this->assertEquals($parsed->getLong(ChronoField::MINUTE_OF_HOUR()), 30); }
public function test_isSupported_TemporalField() { // TODO $this->assertEquals(self::TEST_2008_06()->isSupported(null), false); $this->assertEquals(self::TEST_2008_06()->isSupported(CF::NANO_OF_SECOND()), false); $this->assertEquals(self::TEST_2008_06()->isSupported(CF::NANO_OF_DAY()), false); $this->assertEquals(self::TEST_2008_06()->isSupported(CF::MICRO_OF_SECOND()), false); $this->assertEquals(self::TEST_2008_06()->isSupported(CF::MICRO_OF_DAY()), false); $this->assertEquals(self::TEST_2008_06()->isSupported(CF::MILLI_OF_SECOND()), false); $this->assertEquals(self::TEST_2008_06()->isSupported(CF::MILLI_OF_DAY()), false); $this->assertEquals(self::TEST_2008_06()->isSupported(CF::SECOND_OF_MINUTE()), false); $this->assertEquals(self::TEST_2008_06()->isSupported(CF::SECOND_OF_DAY()), false); $this->assertEquals(self::TEST_2008_06()->isSupported(CF::MINUTE_OF_HOUR()), false); $this->assertEquals(self::TEST_2008_06()->isSupported(CF::MINUTE_OF_DAY()), false); $this->assertEquals(self::TEST_2008_06()->isSupported(CF::HOUR_OF_AMPM()), false); $this->assertEquals(self::TEST_2008_06()->isSupported(CF::CLOCK_HOUR_OF_AMPM()), false); $this->assertEquals(self::TEST_2008_06()->isSupported(CF::HOUR_OF_DAY()), false); $this->assertEquals(self::TEST_2008_06()->isSupported(CF::CLOCK_HOUR_OF_DAY()), false); $this->assertEquals(self::TEST_2008_06()->isSupported(CF::AMPM_OF_DAY()), false); $this->assertEquals(self::TEST_2008_06()->isSupported(CF::DAY_OF_WEEK()), false); $this->assertEquals(self::TEST_2008_06()->isSupported(CF::ALIGNED_DAY_OF_WEEK_IN_MONTH()), false); $this->assertEquals(self::TEST_2008_06()->isSupported(CF::ALIGNED_DAY_OF_WEEK_IN_YEAR()), false); $this->assertEquals(self::TEST_2008_06()->isSupported(CF::DAY_OF_MONTH()), false); $this->assertEquals(self::TEST_2008_06()->isSupported(CF::DAY_OF_YEAR()), false); $this->assertEquals(self::TEST_2008_06()->isSupported(CF::EPOCH_DAY()), false); $this->assertEquals(self::TEST_2008_06()->isSupported(CF::ALIGNED_WEEK_OF_MONTH()), false); $this->assertEquals(self::TEST_2008_06()->isSupported(CF::ALIGNED_WEEK_OF_YEAR()), false); $this->assertEquals(self::TEST_2008_06()->isSupported(CF::MONTH_OF_YEAR()), true); $this->assertEquals(self::TEST_2008_06()->isSupported(CF::PROLEPTIC_MONTH()), true); $this->assertEquals(self::TEST_2008_06()->isSupported(CF::YEAR()), true); $this->assertEquals(self::TEST_2008_06()->isSupported(CF::YEAR_OF_ERA()), true); $this->assertEquals(self::TEST_2008_06()->isSupported(CF::ERA()), true); $this->assertEquals(self::TEST_2008_06()->isSupported(CF::INSTANT_SECONDS()), false); $this->assertEquals(self::TEST_2008_06()->isSupported(CF::OFFSET_SECONDS()), false); }
/** * 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); } }
/** * Adjusts the specified temporal object to have this instant. * <p> * This returns a temporal object of the same observable type as the input * with the instant changed to be the same as this. * <p> * The adjustment is equivalent to using {@link Temporal#with(TemporalField, long)} * twice, passing {@link ChronoField#INSTANT_SECONDS} and * {@link ChronoField#NANO_OF_SECOND} as the fields. * <p> * In most cases, it is clearer to reverse the calling pattern by using * {@link Temporal#with(TemporalAdjuster)}: * <pre> * // these two lines are equivalent, but the second approach is recommended * temporal = thisInstant.adjustInto(temporal); * temporal = temporal.with(thisInstant); * </pre> * <p> * This instance is immutable and unaffected by this method call. * * @param Temporal $temporal the target object to be adjusted, not null * @return Temporal the adjusted object, not null * @throws DateTimeException if unable to make the adjustment * @throws ArithmeticException if numeric overflow occurs */ public function adjustInto(Temporal $temporal) { return $temporal->with(ChronoField::INSTANT_SECONDS(), $this->seconds)->with(ChronoField::NANO_OF_SECOND(), $this->nanos); }
private function resolveFractional() { // ensure fractional seconds available as CF requires // resolveTimeLenient() will have merged CF::MICRO_OF_SECOND()/MILLI_OF_SECOND to NANO_OF_SECOND if ($this->time == null && ($this->fieldValues->has(CF::INSTANT_SECONDS()) || $this->fieldValues->has(CF::SECOND_OF_DAY()) || $this->fieldValues->has(CF::SECOND_OF_MINUTE()))) { if ($this->fieldValues->has(CF::NANO_OF_SECOND())) { $nos = $this->fieldValues->get(CF::NANO_OF_SECOND()); $this->fieldValues->put(CF::MICRO_OF_SECOND(), Math::div($nos, 1000)); $this->fieldValues->put(CF::MILLI_OF_SECOND(), Math::div($nos, 1000000)); } else { $this->fieldValues->put(CF::NANO_OF_SECOND(), 0); $this->fieldValues->put(CF::MICRO_OF_SECOND(), 0); $this->fieldValues->put(CF::MILLI_OF_SECOND(), 0); } } }
/** * Returns a copy of this {@code LocalTime} with the nano-of-second altered. * <p> * This instance is immutable and unaffected by this method call. * * @param int $nanoOfSecond the nano-of-second to set in the result, from 0 to 999,999,999 * @return LocalTime a {@code LocalTime} based on this time with the requested nanosecond, not null * @throws DateTimeException if the nanos value is invalid */ public function withNano($nanoOfSecond) { if ($this->nano === $nanoOfSecond) { return $this; } ChronoField::NANO_OF_SECOND()->checkValidValue($nanoOfSecond); return self::create($this->hour, $this->minute, $this->second, $nanoOfSecond); }
public static function init() { self::$FIELD_MAP = ['G' => ChronoField::ERA(), 'y' => ChronoField::YEAR_OF_ERA(), 'u' => ChronoField::YEAR(), 'Q' => IsoFields::QUARTER_OF_YEAR(), 'q' => IsoFields::QUARTER_OF_YEAR(), 'M' => ChronoField::MONTH_OF_YEAR(), 'L' => ChronoField::MONTH_OF_YEAR(), 'D' => ChronoField::DAY_OF_YEAR(), 'd' => ChronoField::DAY_OF_MONTH(), 'F' => ChronoField::ALIGNED_DAY_OF_WEEK_IN_MONTH(), 'E' => ChronoField::DAY_OF_WEEK(), 'c' => ChronoField::DAY_OF_WEEK(), 'e' => ChronoField::DAY_OF_WEEK(), 'a' => ChronoField::AMPM_OF_DAY(), 'H' => ChronoField::HOUR_OF_DAY(), 'k' => ChronoField::CLOCK_HOUR_OF_DAY(), 'K' => ChronoField::HOUR_OF_AMPM(), 'h' => ChronoField::CLOCK_HOUR_OF_AMPM(), 'm' => ChronoField::MINUTE_OF_HOUR(), 's' => ChronoField::SECOND_OF_MINUTE(), 'S' => ChronoField::NANO_OF_SECOND(), 'A' => ChronoField::MILLI_OF_DAY(), 'n' => ChronoField::NANO_OF_SECOND(), 'N' => ChronoField::NANO_OF_DAY()]; }