time.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.monotonicTime = monotonicTime;
  6. /**
  7. * Copyright (c) Microsoft Corporation.
  8. *
  9. * Licensed under the Apache License, Version 2.0 (the "License");
  10. * you may not use this file except in compliance with the License.
  11. * You may obtain a copy of the License at
  12. *
  13. * http://www.apache.org/licenses/LICENSE-2.0
  14. *
  15. * Unless required by applicable law or agreed to in writing, software
  16. * distributed under the License is distributed on an "AS IS" BASIS,
  17. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  18. * See the License for the specific language governing permissions and
  19. * limitations under the License.
  20. */
  21. // The `process.hrtime()` returns a time from some arbitrary
  22. // date in the past; on certain systems, this is the time from the system boot.
  23. // The `monotonicTime()` converts this to milliseconds.
  24. //
  25. // For a Linux server with uptime of 36 days, the `monotonicTime()` value
  26. // will be 36 * 86400 * 1000 = 3_110_400_000, which is larger than
  27. // the maximum value that `setTimeout` accepts as an argument: 2_147_483_647.
  28. //
  29. // To make the `monotonicTime()` a reasonable value, we anchor
  30. // it to the time of the first import of this utility.
  31. const initialTime = process.hrtime();
  32. function monotonicTime() {
  33. const [seconds, nanoseconds] = process.hrtime(initialTime);
  34. return seconds * 1000 + (nanoseconds / 1000 | 0) / 1000;
  35. }