Coder Social home page Coder Social logo

tiny-timestamp's Introduction

TinyTimstamp

Unique small timestamps.

Handy for cross referencing logs or error reports, especially if your users tend to send you screenshots and you don't want to be transcribing long codes.

It encodes a timestamp based on process.hrtime() to create unique identifiers. See below for how this is handled

npm install tiny-timestamp
const { decodeStamp, tinyStamp, toISOString } = require('tiny-timestamp');

const value = tinyStamp();
const originalTime = decodeStamp(value);

console.log(value); // '2tLJTc:s9V'
console.log(originalTime); // [ 1606876750, 123451 ]
console.log(toISOString(value)); // '2020-12-02T02:39:10.000123451'

// You can shorten identifiers by setting a repeat interval
// and any precision above that period will be discarded

const month = 60 * 60 * 24 * 30; // in seconds
const shorterValue = tinyStamp({
	repeatableInterval: month,
	time: originalTime,
});
console.log(shorterValue); // -eXc:s9V

// You won't be able to get back the exact original time
console.log(decodeStamp(shorterValue)); // [ 2428750, 123451 ]
console.log(toISOString(shorterValue)); // '1970-01-29T02:39:10.000123451'

Under the hood

hrtime() produces a time in nanoseconds, though it's not guaranteed to have nanosecond precision, the precision should be good enough to give a low likelihood of collision between two identifiers.

However, because hrtime() is calculated in seconds from some arbitrary time, on it's own it can't produce a good unique identifier. tinyStamp replaces the seconds value from hrtime with the seconds since the UNIX epoch.

In other words

const arbitraryTime = process.hrtime();
const fixedTime = [parseInt(new Date() / 1000), arbitraryTime[1]];

tiny-timestamp's People

Contributors

chrisjensen avatar

Stargazers

Flynn avatar

Watchers

James Cloos avatar Murray Bunton avatar Paul Day avatar Tom Maitland avatar Flynn avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.