https://github.com/raisely/tiny-timestamp
Unique small timestamps
https://github.com/raisely/tiny-timestamp
Last synced: about 1 year ago
JSON representation
Unique small timestamps
- Host: GitHub
- URL: https://github.com/raisely/tiny-timestamp
- Owner: raisely
- License: other
- Created: 2020-12-03T08:31:35.000Z (over 5 years ago)
- Default Branch: main
- Last Pushed: 2020-12-03T08:51:41.000Z (over 5 years ago)
- Last Synced: 2025-03-02T22:41:37.115Z (over 1 year ago)
- Language: JavaScript
- Size: 14.6 KB
- Stars: 1
- Watchers: 6
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# 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
```
```javascript
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]];
```