pax_global_header00006660000000000000000000000064142753305710014521gustar00rootroot0000000000000052 comment=c6ce571579d5c3da2b346e53e91722e5763bfebc node-microtime-3.1.1/000077500000000000000000000000001427533057100144365ustar00rootroot00000000000000node-microtime-3.1.1/.github/000077500000000000000000000000001427533057100157765ustar00rootroot00000000000000node-microtime-3.1.1/.github/workflows/000077500000000000000000000000001427533057100200335ustar00rootroot00000000000000node-microtime-3.1.1/.github/workflows/release.yml000066400000000000000000000037341427533057100222050ustar00rootroot00000000000000name: Release on: push: tags: ['*'] jobs: build: permissions: contents: read strategy: matrix: include: - os: ubuntu-latest arch: x64 build-group: linux-x64 - os: ubuntu-latest arch: x64 build-group: linux-arm - os: ubuntu-latest arch: x64 build-group: linux-arm64 - os: macos-latest arch: x64 build-group: darwin-x64+arm64 - os: windows-2019 arch: x86 build-group: win32-x86 - os: windows-2019 arch: x64 build-group: win32-x64 runs-on: ${{ matrix.os }} name: Build ${{ matrix.build-group }} env: BUILD_GROUP: ${{ matrix.build-group }} steps: - name: Checkout uses: actions/checkout@v3 with: submodules: recursive - name: Set up node uses: actions/setup-node@v3 with: node-version: 14 architecture: ${{ matrix.arch }} - name: Install run: npm install --ignore-scripts - name: Prebuild run: npm run prebuild-$BUILD_GROUP shell: bash - name: Prepare artifact run: tar -zcvf $BUILD_GROUP.tar.gz -C prebuilds . shell: bash - name: Upload artifact uses: actions/upload-artifact@v3 with: name: ${{ env.BUILD_GROUP }} path: ${{ env.BUILD_GROUP }}.tar.gz retention-days: 1 release: needs: build permissions: contents: write runs-on: ubuntu-latest name: Release steps: - name: Checkout uses: actions/checkout@v3 - name: Download artifacts uses: actions/download-artifact@v3 with: path: artifacts - name: Create GitHub release uses: docker://antonyurchenko/git-release:v4 env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} with: args: artifacts/*/*.tar.gz node-microtime-3.1.1/.github/workflows/test.yml000066400000000000000000000022371427533057100215410ustar00rootroot00000000000000name: Test on: [push, pull_request] permissions: contents: read jobs: lint: runs-on: ubuntu-latest name: Lint steps: - name: Checkout uses: actions/checkout@v3 with: submodules: recursive - name: Use node latest uses: actions/setup-node@v3 with: node-version: latest - name: Install run: npm install - name: Lint Test run: npm run lint-test test: strategy: matrix: os: [ubuntu-latest, macos-latest, windows-latest] node: [12, 14, 16] arch: [x86, x64] exclude: - { os: ubuntu-latest, arch: x86 } - { os: macos-latest, arch: x86 } runs-on: ${{ matrix.os }} name: ${{ matrix.os }} / Node ${{ matrix.node }} ${{ matrix.arch }} steps: - name: Checkout uses: actions/checkout@v3 with: submodules: recursive - name: Use node ${{ matrix.node }} uses: actions/setup-node@v3 with: node-version: ${{ matrix.node }} architecture: ${{ matrix.arch }} - name: Install run: npm install - name: Test run: npm test node-microtime-3.1.1/.gitignore000066400000000000000000000000401427533057100164200ustar00rootroot00000000000000/node_modules /build /prebuilds node-microtime-3.1.1/.npmignore000066400000000000000000000000071427533057100164320ustar00rootroot00000000000000build/ node-microtime-3.1.1/LICENSE000066400000000000000000000020601427533057100154410ustar00rootroot00000000000000Copyright (c) 2011 Wade Simmons Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. node-microtime-3.1.1/README.md000066400000000000000000000041241427533057100157160ustar00rootroot00000000000000# node-microtime [![npm](https://img.shields.io/npm/dm/microtime.svg)](https://www.npmjs.com/package/microtime) [![npm](https://img.shields.io/npm/v/microtime.svg)](https://www.npmjs.com/package/microtime) Date.now() will only give you accuracy in milliseconds. This module calls `gettimeofday(2)` to get the time in microseconds and provides it in a few different formats. The same warning from that function applies: _The resolution of the system clock is hardware dependent, and the time may be updated continuously or in ``ticks.''_ ## N-API Support As of version `3.0.0`, this library is built using the [N-API](https://nodejs.org/api/n-api.html) library. This should allow you to upgrade to newer versions of Node.js without having to reinstall / rebuild this library. Version `3.0.0` and later also bundle prebuilt binaries for common systems in the npm package itself. These will be used if the version of Node you are using supports N-API ([>= v6.14.2](https://nodejs.org/api/n-api.html#n_api_n_api_version_matrix)), otherwise the binary will be recompiled on install. # Installation npm install microtime # Usage ### microtime.now() Get the current time in microseconds as an integer. ### microtime.nowDouble() Get the current time in seconds as a floating point number with microsecond accuracy (similar to `time.time()` in Python and `Time.now.to_f` in Ruby). ### microtime.nowStruct() Get the current time and return as a list with seconds and microseconds (matching the return value of `gettimeofday(2)`). # Example > var microtime = require('microtime') > microtime.now() 1297448895297028 > microtime.nowDouble() 1297448897.600976 > microtime.nowStruct() [ 1297448902, 753875 ] ## Estimating clock resolution Starting with version 0.1.3, there is a test script that tries to guess the clock resolution. You can run it with `npm test microtime`. Example output: microtime.now() = 1298960083489806 microtime.nowDouble() = 1298960083.511521 microtime.nowStruct() = [ 1298960083, 511587 ] Guessing clock resolution... Clock resolution observed: 1us node-microtime-3.1.1/binding.gyp000066400000000000000000000020711427533057100165710ustar00rootroot00000000000000{ 'targets': [ { 'target_name': 'microtime', 'sources': [ 'src/microtime.cc' ], 'cflags!': [ '-fno-exceptions' ], 'cflags_cc!': [ '-fno-exceptions' ], 'include_dirs': [" (http://wades.im/mons)", "homepage": "https://github.com/wadey/node-microtime", "repository": "git://github.com/wadey/node-microtime.git", "main": "./index", "license": "MIT", "keywords": [ "microtime", "microseconds", "gettimeofday" ], "engines": { "node": ">= 14.13.0" }, "scripts": { "install": "node-gyp-build", "rebuild": "node-gyp rebuild", "prebuild": "prebuildify -t 14.13.0 -t electron@18.2.0 --napi --strip", "prebuild-linux-x64": "prebuildify -t 14.13.0 -t electron@18.2.0 --napi --strip", "prebuild-linux-arm": "prebuildify-cross -i linux-armv7 -t 14.13.0 --napi --strip", "prebuild-linux-arm64": "prebuildify-cross -i linux-arm64 -t 14.13.0 --napi --strip", "prebuild-darwin-x64+arm64": "prebuildify -t 14.13.0 -t electron@18.2.0 --arch x64+arm64 --napi --strip", "prebuild-win32-x64": "prebuildify -t 14.13.0 -t electron@18.2.0 --napi --strip", "prebuild-win32-x86": "prebuildify -t 14.13.0 -t electron@18.2.0 --napi --strip", "download-prebuilds": "prebuildify-ci download", "test": "node test.js", "format": "clang-format -i src/microtime.cc", "lint-test": "standard && clang-format -output-replacements-xml src/microtime.cc | (! grep -q ' #include #if defined(_MSC_VER) #include #include // Pick GetSystemTimePreciseAsFileTime or GetSystemTimeAsFileTime depending // on which is available at runtime. typedef VOID(WINAPI *WinGetSystemTime)(LPFILETIME); static WinGetSystemTime getSystemTime = NULL; struct timezone { int tz_minuteswest; int tz_dsttime; }; int gettimeofday(struct timeval *tv, struct timezone *tz) { FILETIME ft; (*getSystemTime)(&ft); unsigned long long t = ft.dwHighDateTime; t <<= 32; t |= ft.dwLowDateTime; t /= 10; t -= 11644473600000000ULL; tv->tv_sec = (long)(t / 1000000UL); tv->tv_usec = (long)(t % 1000000UL); return 0; } #else #include #endif // A very basic version of Node::ErrnoException since Napi doesn't expose it Napi::Error ErrnoException(Napi::Env env, int errorno) { Napi::Error e = Napi::Error::New(env, strerror(errorno)); e.Set("syscall", Napi::String::New(env, "gettimeofday")); e.Set("errno", Napi::Number::New(env, errno)); // NOTE: in Node::ErrnoException this would be the string of the code // like "EFAULT", just simplify with the number here. e.Set("code", Napi::Number::New(env, errno)); return e; } Napi::Value Now(const Napi::CallbackInfo &info) { Napi::Env env = info.Env(); timeval t; int r = gettimeofday(&t, NULL); if (r < 0) { ErrnoException(env, errno).ThrowAsJavaScriptException(); return env.Null(); } return Napi::Number::New(env, ((t.tv_sec * 1000000.0) + t.tv_usec)); } Napi::Value NowDouble(const Napi::CallbackInfo &info) { Napi::Env env = info.Env(); timeval t; int r = gettimeofday(&t, NULL); if (r < 0) { ErrnoException(env, errno).ThrowAsJavaScriptException(); return env.Null(); } return Napi::Number::New(env, t.tv_sec + (t.tv_usec * 0.000001)); } Napi::Value NowStruct(const Napi::CallbackInfo &info) { Napi::Env env = info.Env(); timeval t; int r = gettimeofday(&t, NULL); if (r < 0) { ErrnoException(env, errno).ThrowAsJavaScriptException(); return env.Null(); } Napi::Array array = Napi::Array::New(env, 2); array.Set((uint32_t)0, (double)t.tv_sec); array.Set((uint32_t)1, (double)t.tv_usec); return array; } Napi::Object Init(Napi::Env env, Napi::Object exports) { exports.Set(Napi::String::New(env, "now"), Napi::Function::New(env, Now)); exports.Set(Napi::String::New(env, "nowDouble"), Napi::Function::New(env, NowDouble)); exports.Set(Napi::String::New(env, "nowStruct"), Napi::Function::New(env, NowStruct)); #if defined(_MSC_VER) getSystemTime = (WinGetSystemTime)GetProcAddress( GetModuleHandle(TEXT("kernel32.dll")), "GetSystemTimePreciseAsFileTime"); if (getSystemTime == NULL) { getSystemTime = &GetSystemTimeAsFileTime; } #endif return exports; } NODE_API_MODULE(NODE_GYP_MODULE_NAME, Init); node-microtime-3.1.1/test.js000066400000000000000000000013421427533057100157530ustar00rootroot00000000000000var microtime = require('./index') console.log('microtime.now() =', microtime.now()) console.log('microtime.nowDouble() =', microtime.nowDouble()) console.log('microtime.nowStruct() =', microtime.nowStruct()) console.log('\nGuessing clock resolution...') var start = microtime.now() var minDiff = Infinity var minCycles = 10000 var maxCycles = 100000 var cycles = maxCycles for (var i = 0; i < cycles; ++i) { var a = microtime.now() var b = microtime.now() var diff = (b - a) || (b - start) if (diff > 0 && diff < minDiff) { minDiff = diff cycles = minCycles } } if (minDiff === Infinity) { console.log('Unable to guess clock resolution') } else { console.log('Clock resolution observed: ' + minDiff + 'us') }