-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.js
182 lines (165 loc) · 3.81 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
'use strict';
const currentMs = Date.now();
const startSeconds = Math.floor(currentMs / 1000);
const startNanoseconds = (currentMs % 1000) * 1000 * 1000;
const oneSecond = 1000 * 1000 * 1000;
let nsCount = 0;
/**
* Get nano seconds
*
* @returns {Integer} The value of nano seconds
*/
function nanoseconds() {
if (exports.mode === 'step') {
nsCount = (nsCount + 1) % 1e6;
return nsCount;
}
return Math.floor(Math.random() * 1e6);
}
/**
* The hrtime function for browser
*
* @returns {Array} [Sceonds, NanoSeconds]
*/
function browserHrtime() {
const current = Math.floor(window.performance.now() * 1e6);
const currentSeconds = Math.floor(current / 1e9);
const currentNanoseconds = current % 1e9;
return [
currentSeconds,
currentNanoseconds,
];
}
/**
* Get the hrtime
*
* @returns {Array} [Seconds, NanoSeconds]
*/
function getHrtime() {
/* eslint no-undef:0 */
if (window && window.performance && window.performance.now) {
return browserHrtime();
}
const current = Date.now();
const currentSeconds = Math.floor(current / 1000);
const currentNanoseconds = ((current % 1000) * 1e6) + nanoseconds();
return [
currentSeconds,
currentNanoseconds,
];
}
/**
* Custom function of hrtime
*
* @param {Array} time The start time of hrtime
* @returns {Array} [Seconds, NanoSeconds]
*/
function customHrtime(time) {
const arr = getHrtime();
const currentSeconds = arr[0];
const currentNanoseconds = arr[1];
if (!time) {
return [
currentSeconds,
currentNanoseconds,
];
}
let offsetSeconds = currentSeconds - time[0];
let offsetNanoseconds = currentNanoseconds - time[1];
if (offsetNanoseconds < 0) {
offsetNanoseconds += 1e9;
offsetSeconds -= 1;
}
return [
offsetSeconds,
offsetNanoseconds,
];
}
const hrtime = (process && process.hrtime) || customHrtime;
const start = hrtime();
/**
* Get the current time with nanoseconds
*
* @returns {Array} [Seconds, NanoSeconds]
*/
function now() {
const arr = hrtime(start);
const value = arr[1] + startNanoseconds;
if (value >= oneSecond) {
return [
startSeconds + arr[0] + 1,
value % oneSecond,
];
}
return [
startSeconds + arr[0],
value,
];
}
/**
* Pad ns with '0'
*
* @param {Integer} ns The nano seconds
* @returns {String}
*/
function padNS(ns) {
const str = `${ns}`;
const pad = '000000000'.substring(0, 9 - str.length);
return `${pad}${str}`;
}
/**
* Get the nano seconds string
*
* @param {Array} arr [Seconds, NanoSeconds]
* @returns {String}
*/
function toString(arr) {
const ns = arr || now();
return `${ns[0]}${padNS(ns[1])}`;
}
/**
* Get the nano seconds iso string
*
* @param {Array} arr [Seconds, NanoSeconds]
* @returns {String}
*/
function toISOString(arr) {
const ns = arr || now();
const str = new Date(ns[0] * 1000).toISOString();
const nsStr = `${padNS(ns[1])}`.replace(/0+$/, '');
return str.replace('000Z', `${nsStr}Z`);
}
/**
* Get the nano form iso string
*
* @param {any} str
*/
function fromISOString(str) {
const arr = str.split('.');
const date = new Date(`${arr[0]}.000Z`);
const value = arr[1].substring(0, arr[1].length - 1);
const ns = Number.parseInt(`${value}000000000`.substring(0, 9), 10);
return [
date.getTime() / 1000,
ns,
];
}
/**
* Get the difference of the nano seconds
*
* @param {Array} ns [Seconds, NanoSeconds]
* @param {Array} ns2 [Seconds, NanoSeconds], default is `now()`
* @returns {Integer} The difference nano seconds
*/
function difference(ns, ns2) {
const current = ns2 || now();
const us = current[1] - ns[1];
const s = current[0] - ns[0];
return (s * oneSecond) + us;
}
exports.now = now;
exports.toString = toString;
exports.difference = difference;
exports.toISOString = toISOString;
exports.fromISOString = fromISOString;
exports.mode = 'random';