public function getValue(DateTimePrintContext $context, $value) { $absValue = Math::abs($value); $baseValue = $this->baseValue; if ($this->baseDate != null) { $chrono = AbstractChronology::from($context->getTemporal()); $baseValue = $chrono->dateFrom($this->baseDate)->get($this->field); } if ($value >= $baseValue && $value < $baseValue + self::$EXCEED_POINTS[$this->minWidth]) { // Use the reduced value if it fits in minWidth return $absValue % self::$EXCEED_POINTS[$this->minWidth]; } // Otherwise truncate to fit in maxWidth return $absValue % self::$EXCEED_POINTS[$this->maxWidth]; }
public function format(DateTimePrintContext $context, &$buf) { /** @var ZoneID $zone */ $zone = $context->getValue(TemporalQueries::zoneId()); if ($zone === null) { return false; } $zname = $zone->getId(); if (!$zone instanceof ZoneOffset) { $dt = $context->getTemporal(); $name = $this->getDisplayName($zname, $dt->isSupported(ChronoField::INSTANT_SECONDS()) ? $zone->getRules()->isDaylightSavings(Instant::from($dt)) ? self::$DST : self::$STD : self::$GENERIC, $context->getLocale()); if ($name !== null) { $zname = $name; } } $buf .= $zname; return true; }
public function format(DateTimePrintContext $context, &$buf) { $value = $context->getValueField($this->field); if ($value === null) { return false; } $text = null; $chrono = $context->getTemporal()->query(TemporalQueries::chronology()); if ($chrono === null || $chrono == IsoChronology::INSTANCE()) { $text = $this->provider->getText($this->field, $value, $this->textStyle, $context->getLocale()); } else { $text = $this->provider->getText2($chrono, $this->field, $value, $this->textStyle, $context->getLocale()); } if ($text === null) { return $this->numberPrinterParser()->format($context, $buf); } $buf .= $text; return true; }
public function format(DateTimePrintContext $context, &$buf) { $chrono = AbstractChronology::from($context->getTemporal()); return $this->formatter($context->getLocale(), $chrono)->toPrinterParser(false)->format($context, $buf); }
public function format(DateTimePrintContext $context, &$buf) { // use INSTANT_SECONDS, thus this code is not bound by Instant.MAX $inSecs = $context->getValueField(ChronoField::INSTANT_SECONDS()); $inNanos = null; if ($context->getTemporal()->isSupported(ChronoField::NANO_OF_SECOND())) { $inNanos = $context->getTemporal()->getLong(ChronoField::NANO_OF_SECOND()); } if ($inSecs === null) { return false; } $inSec = $inSecs; $inNano = ChronoField::NANO_OF_SECOND()->checkValidIntValue($inNanos !== null ? $inNanos : 0); // format mostly using LocalDateTime.toString if ($inSec >= -self::SECONDS_0000_TO_1970) { // current era $zeroSecs = $inSec - self::SECONDS_PER_10000_YEARS + self::SECONDS_0000_TO_1970; $hi = Math::floorDiv($zeroSecs, self::SECONDS_PER_10000_YEARS) + 1; $lo = Math::floorMod($zeroSecs, self::SECONDS_PER_10000_YEARS); $ldt = LocalDateTime::ofEpochSecond($lo - self::SECONDS_0000_TO_1970, 0, ZoneOffset::UTC()); if ($hi > 0) { $buf .= '+' . $hi; } $buf .= $ldt; if ($ldt->getSecond() === 0) { $buf .= ":00"; } } else { // before current era $zeroSecs = $inSec + self::SECONDS_0000_TO_1970; $hi = Math::div($zeroSecs, self::SECONDS_PER_10000_YEARS); $lo = $zeroSecs % self::SECONDS_PER_10000_YEARS; $ldt = LocalDateTime::ofEpochSecond($lo - self::SECONDS_0000_TO_1970, 0, ZoneOffset::UTC()); $pos = strlen($buf); $buf .= $ldt; if ($ldt->getSecond() === 0) { $buf .= ":00"; } if ($hi < 0) { if ($ldt->getYear() === -10000) { $buf = substr_replace($buf, $hi - 1, $pos, 2); } else { if ($lo === 0) { $buf = substr_replace($buf, $hi, $pos, 0); } else { $buf = substr_replace($buf, Math::abs($hi), $pos + 1, 0); } } } } // add fraction if ($this->fractionalDigits < 0 && $inNano > 0 || $this->fractionalDigits > 0) { $buf .= '.'; $div = 100000000; for ($i = 0; $this->fractionalDigits === -1 && $inNano > 0 || $this->fractionalDigits === -2 && ($inNano > 0 || $i % 3 !== 0) || $i < $this->fractionalDigits; $i++) { $digit = Math::div($inNano, $div); $buf .= $digit; $inNano = $inNano - $digit * $div; $div = Math::div($div, 10); } } $buf .= 'Z'; return true; }