<commit_before>#
#
#            Nim's Runtime Library
#        (c) Copyright 2019 Nim contributors
#
#    See the file "copying.txt", included in this
#    distribution, for details about the copyright.
#

##[
  The ``std/monotimes`` module implements monotonic timestamps. A monotonic
  timestamp represents the time that has passed since some system defined
  point in time. The monotonic timestamps are guaranteed to always increase,
  meaning that that the following is guaranteed to work:

  .. code-block:: nim
    let a = getMonoTime()
    # ... do some work
    let b = getMonoTime()
    assert a <= b

  This is not guaranteed for the `times.Time` type! This means that the
  `MonoTime` should be used when measuring durations of time with
  high precision.

  However, since `MonoTime` represents the time that has passed since some
  unknown time origin, it cannot be converted to a human readable timestamp.
  If this is required, the `times.Time` type should be used instead.

  The `MonoTime` type stores the timestamp in nanosecond resolution, but note
  that the actual supported time resolution differs for different systems.

  See also
  ========
  * `times module <times.html>`_
]##

import times

type
  MonoTime* = object ## Represents a monotonic timestamp.
    ticks: int64

when defined(macosx):
  type
    MachTimebaseInfoData {.pure, final, importc: "mach_timebase_info_data_t",
        header: "<mach/mach_time.h>".} = object
      numer, denom: int32

  proc mach_absolute_time(): int64 {.importc, header: "<mach/mach.h>".}
  proc mach_timebase_info(info: var MachTimebaseInfoData) {.importc,
    header: "<mach/mach_time.h>".}

  let machAbsoluteTimeFreq = block:
    var freq: MachTimebaseInfoData
    mach_timebase_info(freq)
    freq

when defined(js):
  proc getJsTicks: float =
    {.emit: """
      var isNode = typeof module !== 'undefined' && module.exports

      if (isNode) {
        var process = require('process');
        var time = process.hrtime()
        return time[0] + time[1] / 1000000000;
      } else {
        return window.performance.now() * 1000000;
      }
    """.}

  # Workaround for #6752.
  {.push overflowChecks: off.}
  proc `-`(a, b: int64): int64 =
    system.`-`(a, b)
  proc `+`(a, b: int64): int64 =
    system.`+`(a, b)
  {.pop.}
<commit_msg>Fix JS bug in std/monotimes (#12499) [backport]<commit_after>#
#
#            Nim's Runtime Library
#        (c) Copyright 2019 Nim contributors
#
#    See the file "copying.txt", included in this
#    distribution, for details about the copyright.
#

##[
  The ``std/monotimes`` module implements monotonic timestamps. A monotonic
  timestamp represents the time that has passed since some system defined
  point in time. The monotonic timestamps are guaranteed to always increase,
  meaning that that the following is guaranteed to work:

  .. code-block:: nim
    let a = getMonoTime()
    # ... do some work
    let b = getMonoTime()
    assert a <= b

  This is not guaranteed for the `times.Time` type! This means that the
  `MonoTime` should be used when measuring durations of time with
  high precision.

  However, since `MonoTime` represents the time that has passed since some
  unknown time origin, it cannot be converted to a human readable timestamp.
  If this is required, the `times.Time` type should be used instead.

  The `MonoTime` type stores the timestamp in nanosecond resolution, but note
  that the actual supported time resolution differs for different systems.

  See also
  ========
  * `times module <times.html>`_
]##

import times

type
  MonoTime* = object ## Represents a monotonic timestamp.
    ticks: int64

when defined(macosx):
  type
    MachTimebaseInfoData {.pure, final, importc: "mach_timebase_info_data_t",
        header: "<mach/mach_time.h>".} = object
      numer, denom: int32

  proc mach_absolute_time(): int64 {.importc, header: "<mach/mach.h>".}
  proc mach_timebase_info(info: var MachTimebaseInfoData) {.importc,
    header: "<mach/mach_time.h>".}

  let machAbsoluteTimeFreq = block:
    var freq: MachTimebaseInfoData
    mach_timebase_info(freq)
    freq

when defined(js):
  proc getJsTicks: float =
    ## Returns ticks in the unit seconds
    {.emit: """
      var isNode = typeof module !== 'undefined' && module.exports

      if (isNode) {
        var process = require('process');
        var time = process.hrtime()
        return time[0] + time[1] / 1000000000;
      } else {
        return window.performance.now() / 1000;
      }
    """.}

  # Workaround for #6752.
  {.push overflowChecks: off.}
  proc `-`(a, b: int64): int64 =
    system.`-`(a, b)
  proc `+`(a, b: int64): int64 =
    system.`+`(a, b)
  {.pop.}

{'fraction_non_alphanumeric': 0.1076595744680851, 'fraction_numerical': 0.017872340425531916, 'mean_word_length': 3.1382042253521125, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6235156', 'n_tokens_mistral': 1550, 'n_tokens_neox': 1441, 'n_words': 522}