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; }
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) { $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; }
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; }
private function doTestOffset(ZoneOffset $offset, $hours, $minutes, $seconds) { $this->assertEquals($offset->getTotalSeconds(), $hours * 60 * 60 + $minutes * 60 + $seconds); if ($hours == 0 && $minutes == 0 && $seconds == 0) { $id = "Z"; } else { $str = $hours < 0 || $minutes < 0 || $seconds < 0 ? "-" : "+"; $str .= substr(Math::abs($hours) + 100, 1); $str .= ":"; $str .= substr(Math::abs($minutes) + 100, 1); if ($seconds != 0) { $str .= ":"; $str .= substr(Math::abs($seconds) + 100, 1); } $id = $str; } $this->assertEquals($offset->getId(), $id); $this->assertEquals($offset, ZoneOffset::ofHoursMinutesSeconds($hours, $minutes, $seconds)); if ($seconds == 0) { $this->assertEquals($offset, ZoneOffset::ofHoursMinutes($hours, $minutes)); if ($minutes == 0) { $this->assertEquals($offset, ZoneOffset::ofHours($hours)); } } $this->assertEquals(ZoneOffset::of($id), $offset); $this->assertEquals($offset->__toString(), $id); }
public function test_now() { $expected = ZonedDateTime::nowOf(Clock::systemDefaultZone()); $test = ZonedDateTime::now(); $diff = Math::abs($test->toLocalTime()->toNanoOfDay() - $expected->toLocalTime()->toNanoOfDay()); if ($diff >= 100000000) { // may be date change $expected = ZonedDateTime::nowOf(Clock::systemDefaultZone()); $test = ZonedDateTime::now(); $diff = Math::abs($test->toLocalTime()->toNanoOfDay() - $expected->toLocalTime()->toNanoOfDay()); } $this->assertTrue($diff < 100000000); // less than 0.1 secs }
private static function buildId($totalSeconds) { if ($totalSeconds === 0) { return "Z"; } else { $absTotalSeconds = Math::abs($totalSeconds); $buf = ''; $absHours = Math::div($absTotalSeconds, LocalTime::SECONDS_PER_HOUR); $absMinutes = Math::div($absTotalSeconds, LocalTime::SECONDS_PER_MINUTE) % LocalTime::MINUTES_PER_HOUR; $buf .= ($totalSeconds < 0 ? "-" : "+") . ($absHours < 10 ? "0" : "") . $absHours . ($absMinutes < 10 ? ":0" : ":") . $absMinutes; $absSeconds = $absTotalSeconds % LocalTime::SECONDS_PER_MINUTE; if ($absSeconds !== 0) { $buf .= ($absSeconds < 10 ? ":0" : ":") . $absSeconds; } return $buf; } }
/** * Outputs this year-month as a {@code String}, such as {@code 2007-12}. * <p> * The output will be in the format {@code uuuu-MM}: * * @return string a string representation of this year-month, not null */ public function __toString() { $absYear = Math::abs($this->year); $buf = ''; if ($absYear < 1000) { if ($this->year < 0) { $y = (string) ($this->year - 10000); $buf .= substr($y, 0, 1) . substr($y, 2); } else { $buf .= substr($this->year + 10000, 1); } } else { $buf .= $this->year; } return $buf . ($this->month < 10 ? "-0" : "-") . $this->month; }
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; }
public function test_now() { $nowDT = ZonedDateTime::now(); $expected = OffsetTime::nowOf(Clock::systemDefaultZone()); $test = OffsetTime::now(); $diff = Math::abs($test->toLocalTime()->toNanoOfDay() - $expected->toLocalTime()->toNanoOfDay()); $this->assertTrue($diff < 100000000); // less than 0.1 secs $this->assertEquals($test->getOffset(), $nowDT->getOffset()); }
public function test_now() { $expected = Instant::nowOf(Clock::systemUTC()); $test = Instant::now(); $diff = Math::abs($test->toEpochMilli() - $expected->toEpochMilli()); $this->assertTrue($diff < 100); // less than 0.1 secs }