aboutsummaryrefslogtreecommitdiff
path: root/php/src/Google/Protobuf/Internal/GPBUtil.php
diff options
context:
space:
mode:
Diffstat (limited to 'php/src/Google/Protobuf/Internal/GPBUtil.php')
-rw-r--r--php/src/Google/Protobuf/Internal/GPBUtil.php26
1 files changed, 19 insertions, 7 deletions
diff --git a/php/src/Google/Protobuf/Internal/GPBUtil.php b/php/src/Google/Protobuf/Internal/GPBUtil.php
index 0beedba33..7ec3ca229 100644
--- a/php/src/Google/Protobuf/Internal/GPBUtil.php
+++ b/php/src/Google/Protobuf/Internal/GPBUtil.php
@@ -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)
{