|
@@ -504,17 +504,29 @@ class GPBUtil
|
|
|
|
|
|
public static function formatDuration($value)
|
|
|
{
|
|
|
- if (bccomp($value->getSeconds(), "315576000001") != -1) {
|
|
|
- throw new GPBDecodeException("Duration number too large.");
|
|
|
+ if (bccomp($value->getSeconds(), '315576000001') != -1) {
|
|
|
+ throw new GPBDecodeException('Duration number too large.');
|
|
|
}
|
|
|
- if (bccomp($value->getSeconds(), "-315576000001") != 1) {
|
|
|
- throw new GPBDecodeException("Duration number too small.");
|
|
|
+ if (bccomp($value->getSeconds(), '-315576000001') != 1) {
|
|
|
+ throw new GPBDecodeException('Duration number too small.');
|
|
|
+ }
|
|
|
+
|
|
|
+ $nanos = $value->getNanos();
|
|
|
+ if ($nanos === 0) {
|
|
|
+ return (string) $value->getSeconds();
|
|
|
}
|
|
|
- return strval(bcadd($value->getSeconds(),
|
|
|
- $value->getNanos() / 1000000000.0, 9));
|
|
|
- }
|
|
|
|
|
|
+ if ($nanos % 1000000 === 0) {
|
|
|
+ $digits = 3;
|
|
|
+ } elseif ($nanos % 1000 === 0) {
|
|
|
+ $digits = 6;
|
|
|
+ } else {
|
|
|
+ $digits = 9;
|
|
|
+ }
|
|
|
|
|
|
+ $nanos = bcdiv($nanos, '1000000000', $digits);
|
|
|
+ return bcadd($value->getSeconds(), $nanos, $digits);
|
|
|
+ }
|
|
|
|
|
|
public static function parseFieldMask($paths_string)
|
|
|
{
|