public function format(DateTimePrintContext $context, &$buf)
 {
     $offsetSecs = $context->getValueField(ChronoField::OFFSET_SECONDS());
     if ($offsetSecs === null) {
         return false;
     }
     $totalSecs = $offsetSecs;
     if ($totalSecs === 0) {
         $buf .= $this->noOffsetText;
     } else {
         $absHours = Math::abs($totalSecs / 3600 % 100);
         // anything larger than 99 silently dropped
         $absMinutes = Math::abs($totalSecs / 60 % 60);
         $absSeconds = Math::abs($totalSecs % 60);
         $bufPos = strlen($buf);
         $output = $absHours;
         $buf .= ($totalSecs < 0 ? "-" : "+") . Math::div($absHours, 10) . $absHours % 10;
         if ($this->type >= 3 || $this->type >= 1 && $absMinutes > 0) {
             $buf .= ($this->type % 2 === 0 ? ":" : "") . Math::div($absMinutes, 10) . $absMinutes % 10;
             $output += $absMinutes;
             if ($this->type >= 7 || $this->type >= 5 && $absSeconds > 0) {
                 $buf .= ($this->type % 2 === 0 ? ":" : "") . Math::div($absSeconds, 10) . $absSeconds % 10;
                 $output += $absSeconds;
             }
         }
         if ($output === 0) {
             $buf = substr($buf, 0, $bufPos);
             $buf .= $this->noOffsetText;
         }
     }
     return true;
 }
Ejemplo n.º 2
0
 public function format(DateTimePrintContext $context, &$buf)
 {
     /** @var ZoneID $zone */
     $zone = $context->getValue($this->query);
     if ($zone === null) {
         return false;
     }
     $buf .= $zone->getId();
     return true;
 }
Ejemplo n.º 3
0
 public function format(DateTimePrintContext $context, &$buf)
 {
     $chrono = $context->getValue(TemporalQueries::chronology());
     if ($chrono == null) {
         return false;
     }
     if ($this->textStyle == null) {
         $buf .= $chrono->getId();
     } else {
         $buf .= $this->getChronologyName($chrono, $context->getLocale());
     }
     return true;
 }
Ejemplo n.º 4
0
 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)
 {
     $offsetSecs = $context->getValueField(ChronoField::OFFSET_SECONDS());
     if ($offsetSecs === null) {
         return false;
     }
     $gmtText = "GMT";
     // TODO: get localized version of 'GMT'
     if ($gmtText !== null) {
         $buf .= $gmtText;
     }
     $totalSecs = Math::toIntExact($offsetSecs);
     if ($totalSecs !== 0) {
         $absHours = Math::abs($totalSecs / 3600 % 100);
         // anything larger than 99 silently dropped
         $absMinutes = Math::abs($totalSecs / 60 % 60);
         $absSeconds = Math::abs($totalSecs % 60);
         $buf .= $totalSecs < 0 ? "-" : "+";
         if ($this->style == TextStyle::FULL()) {
             $this->appendHMS($buf, $absHours);
             $buf .= ':';
             $this->appendHMS($buf, $absMinutes);
             if ($absSeconds != 0) {
                 $buf .= ':';
                 $this->appendHMS($buf, $absSeconds);
             }
         } else {
             if ($absHours >= 10) {
                 $buf .= Math::div($absHours, 10);
             }
             $buf .= $absHours % 10;
             if ($absMinutes != 0 || $absSeconds != 0) {
                 $buf .= ':';
                 $this->appendHMS($buf, $absMinutes);
                 if ($absSeconds != 0) {
                     $buf .= ':';
                     self::appendHMS($buf, $absSeconds);
                 }
             }
         }
     }
     return true;
 }
Ejemplo n.º 7
0
 public function format(DateTimePrintContext $context, &$buf)
 {
     $valueLong = $context->getValueField($this->field);
     if ($valueLong === null) {
         return false;
     }
     $value = $this->getValue($context, $valueLong);
     $decimalStyle = $context->getDecimalStyle();
     $str = $value === Long::MIN_VALUE ? "9223372036854775808" : strval(Math::abs($value));
     if (strlen($str) > $this->maxWidth) {
         throw new DateTimeException("Field " . $this->field . " cannot be printed as the value " . $value . " exceeds the maximum print width of " . $this->maxWidth);
     }
     $str = $decimalStyle->convertNumberToI18N($str);
     if ($value >= 0) {
         switch ($this->signStyle) {
             case SignStyle::EXCEEDS_PAD():
                 if ($this->minWidth < 19 && $value >= self::$EXCEED_POINTS[$this->minWidth]) {
                     $buf .= $decimalStyle->getPositiveSign();
                 }
                 break;
             case SignStyle::ALWAYS():
                 $buf .= $decimalStyle->getPositiveSign();
                 break;
         }
     } else {
         switch ($this->signStyle) {
             case SignStyle::NORMAL():
             case SignStyle::EXCEEDS_PAD():
             case SignStyle::ALWAYS():
                 $buf .= $decimalStyle->getNegativeSign();
                 break;
             case SignStyle::NOT_NEGATIVE():
                 throw new DateTimeException("Field " . $this->field . " cannot be printed as the value " . $value . " cannot be negative according to the SignStyle");
         }
     }
     for ($i = 0; $i < $this->minWidth - strlen($str); $i++) {
         $buf .= $decimalStyle->getZeroDigit();
     }
     $buf .= $str;
     return true;
 }
 public function format(DateTimePrintContext $context, &$buf)
 {
     $length = strlen($buf);
     if ($this->optional) {
         $context->startOptional();
     }
     try {
         foreach ($this->printerParsers as $pp) {
             if ($pp->format($context, $buf) === false) {
                 $buf = substr($buf, 0, $length);
                 // reset buffer
                 return true;
             }
         }
     } finally {
         if ($this->optional) {
             $context->endOptional();
         }
     }
     return true;
 }
 public function format(DateTimePrintContext $context, &$buf)
 {
     $value = $context->getValueField($this->field);
     if ($value === null) {
         return false;
     }
     $decimalStyle = $context->getDecimalStyle();
     $fraction = $this->convertToFraction($value);
     if (gmp_cmp($fraction, 0) === 0) {
         // scale is zero if value is zero
         if ($this->minWidth > 0) {
             if ($this->decimalPoint) {
                 $buf .= $decimalStyle->getDecimalSeparator();
             }
             for ($i = 0; $i < $this->minWidth; $i++) {
                 $buf .= $decimalStyle->getZeroDigit();
             }
         }
     } else {
         $outputScale = Math::min(Math::max(9, $this->minWidth), $this->maxWidth);
         if ($outputScale !== 9) {
             $div = 1 . str_repeat('0', 9 - $outputScale);
             $fraction = gmp_div($fraction, $div);
         }
         $str = gmp_strval($fraction);
         $pad = $outputScale - strlen($str);
         $str = str_repeat('0', $pad) . $str;
         // trim trailing zeros
         while (strlen($str) > $this->minWidth && $str[strlen($str) - 1] === '0') {
             $str = substr($str, 0, strlen($str) - 1);
         }
         $str = $decimalStyle->convertNumberToI18N($str);
         if ($this->decimalPoint) {
             $buf .= $decimalStyle->getDecimalSeparator();
         }
         $buf .= $str;
     }
     return true;
 }
Ejemplo n.º 10
0
 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);
 }
Ejemplo n.º 12
0
 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;
 }