| 1 |
"use strict"; |
| 2 |
// Copyright 2021-2026 Buf Technologies, Inc. |
| 3 |
// |
| 4 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
// you may not use this file except in compliance with the License. |
| 6 |
// You may obtain a copy of the License at |
| 7 |
// |
| 8 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
// |
| 10 |
// Unless required by applicable law or agreed to in writing, software |
| 11 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
// See the License for the specific language governing permissions and |
| 14 |
// limitations under the License. |
| 15 |
Object.defineProperty(exports, "__esModule", { value: true }); |
| 16 |
exports.durationFromMs = durationFromMs; |
| 17 |
exports.durationMs = durationMs; |
| 18 |
const duration_pb_js_1 = require("./gen/google/protobuf/duration_pb.js"); |
| 19 |
const create_js_1 = require("../create.js"); |
| 20 |
const proto_int64_js_1 = require("../proto-int64.js"); |
| 21 |
/** |
| 22 |
* Create a google.protobuf.Duration message from a Unix timestamp in milliseconds. |
| 23 |
*/ |
| 24 |
function durationFromMs(durationMs) { |
| 25 |
const sign = durationMs < 0 ? -1 : 1; |
| 26 |
const absDurationMs = Math.abs(durationMs); |
| 27 |
const absSeconds = Math.floor(absDurationMs / 1000); |
| 28 |
const absNanos = (absDurationMs - absSeconds * 1000) * 1000000; |
| 29 |
return (0, create_js_1.create)(duration_pb_js_1.DurationSchema, { |
| 30 |
seconds: proto_int64_js_1.protoInt64.parse(absSeconds * sign), |
| 31 |
nanos: absNanos === 0 ? 0 : absNanos * sign, // deliberately avoid signed 0 - it does not serialize |
| 32 |
}); |
| 33 |
} |
| 34 |
/** |
| 35 |
* Convert a google.protobuf.Duration to a Unix timestamp in milliseconds. |
| 36 |
*/ |
| 37 |
function durationMs(duration) { |
| 38 |
return Number(duration.seconds) * 1000 + Math.round(duration.nanos / 1000000); |
| 39 |
} |