| 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.timestampNow = timestampNow; |
| 17 |
exports.timestampFromDate = timestampFromDate; |
| 18 |
exports.timestampDate = timestampDate; |
| 19 |
exports.timestampFromMs = timestampFromMs; |
| 20 |
exports.timestampMs = timestampMs; |
| 21 |
const timestamp_pb_js_1 = require("./gen/google/protobuf/timestamp_pb.js"); |
| 22 |
const create_js_1 = require("../create.js"); |
| 23 |
const proto_int64_js_1 = require("../proto-int64.js"); |
| 24 |
/** |
| 25 |
* Create a google.protobuf.Timestamp for the current time. |
| 26 |
*/ |
| 27 |
function timestampNow() { |
| 28 |
return timestampFromDate(new Date()); |
| 29 |
} |
| 30 |
/** |
| 31 |
* Create a google.protobuf.Timestamp message from an ECMAScript Date. |
| 32 |
*/ |
| 33 |
function timestampFromDate(date) { |
| 34 |
return timestampFromMs(date.getTime()); |
| 35 |
} |
| 36 |
/** |
| 37 |
* Convert a google.protobuf.Timestamp message to an ECMAScript Date. |
| 38 |
*/ |
| 39 |
function timestampDate(timestamp) { |
| 40 |
return new Date(timestampMs(timestamp)); |
| 41 |
} |
| 42 |
/** |
| 43 |
* Create a google.protobuf.Timestamp message from a Unix timestamp in milliseconds. |
| 44 |
*/ |
| 45 |
function timestampFromMs(timestampMs) { |
| 46 |
const seconds = Math.floor(timestampMs / 1000); |
| 47 |
return (0, create_js_1.create)(timestamp_pb_js_1.TimestampSchema, { |
| 48 |
seconds: proto_int64_js_1.protoInt64.parse(seconds), |
| 49 |
nanos: (timestampMs - seconds * 1000) * 1000000, |
| 50 |
}); |
| 51 |
} |
| 52 |
/** |
| 53 |
* Convert a google.protobuf.Timestamp to a Unix timestamp in milliseconds. |
| 54 |
*/ |
| 55 |
function timestampMs(timestamp) { |
| 56 |
return (Number(timestamp.seconds) * 1000 + Math.round(timestamp.nanos / 1000000)); |
| 57 |
} |