nv-date-y7 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.js +89 -0
- package/package.json +11 -0
package/index.js
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
const DAY = 86400000;
|
|
2
|
+
|
|
3
|
+
const MIN4 = -DAY * 719528;
|
|
4
|
+
const MAX4 = DAY * 2932897;
|
|
5
|
+
|
|
6
|
+
const MIN6 = -DAY * 100000000;
|
|
7
|
+
const MAX6 = DAY * 100000000 + 1;
|
|
8
|
+
|
|
9
|
+
const pad = (n, w) => String(Math.abs(n)).padStart(w, "0");
|
|
10
|
+
|
|
11
|
+
const mts_to_y7 = (mts=Date.now()) => {
|
|
12
|
+
if (mts < MIN6 || mts >= MAX6)
|
|
13
|
+
throw new RangeError("Out of supported range");
|
|
14
|
+
|
|
15
|
+
const d = new Date(mts);
|
|
16
|
+
const y = d.getUTCFullYear();
|
|
17
|
+
|
|
18
|
+
let ypart;
|
|
19
|
+
|
|
20
|
+
if (mts >= MIN4 && mts < MAX4) {
|
|
21
|
+
// 四位年
|
|
22
|
+
ypart = " " + pad(y, 4);
|
|
23
|
+
} else if (mts >= MAX4) {
|
|
24
|
+
// 正六位
|
|
25
|
+
ypart = "+" + pad(y, 6);
|
|
26
|
+
} else {
|
|
27
|
+
// 负六位
|
|
28
|
+
ypart = "-" + pad(-y, 6);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const MM = pad(d.getUTCMonth() + 1, 2);
|
|
32
|
+
const DD = pad(d.getUTCDate(), 2);
|
|
33
|
+
const hh = pad(d.getUTCHours(), 2);
|
|
34
|
+
const mm = pad(d.getUTCMinutes(), 2);
|
|
35
|
+
const ss = pad(d.getUTCSeconds(), 2);
|
|
36
|
+
const sss = pad(d.getUTCMilliseconds(), 3);
|
|
37
|
+
|
|
38
|
+
return ypart + MM + DD + hh + mm + ss + sss;
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
const y7_to_mts = (y7) => {
|
|
43
|
+
var len = y7.length;
|
|
44
|
+
if(len >20) {
|
|
45
|
+
throw new Error("Invalid length");
|
|
46
|
+
} else {
|
|
47
|
+
y7 = " ".repeat(20-len) +y7;
|
|
48
|
+
}
|
|
49
|
+
let year;
|
|
50
|
+
if (y7.startsWith(" ")) {
|
|
51
|
+
year = parseInt(y7.slice(3, 7));
|
|
52
|
+
y7 = y7.slice(7);
|
|
53
|
+
} else if (y7[0] === "+") {
|
|
54
|
+
year = parseInt(y7.slice(1, 7));
|
|
55
|
+
y7 = y7.slice(7);
|
|
56
|
+
} else if (y7[0] === "-") {
|
|
57
|
+
year = -parseInt(y7.slice(1, 7));
|
|
58
|
+
y7 = y7.slice(7);
|
|
59
|
+
} else {
|
|
60
|
+
throw new Error("Invalid year format");
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
const MM = parseInt(y7.slice(0,2));
|
|
64
|
+
const DD = parseInt(y7.slice(2,4));
|
|
65
|
+
const hh = parseInt(y7.slice(4,6));
|
|
66
|
+
const mm = parseInt(y7.slice(6,8));
|
|
67
|
+
const ss = parseInt(y7.slice(8,10));
|
|
68
|
+
const sss = parseInt(y7.slice(10,13));
|
|
69
|
+
|
|
70
|
+
return Date.UTC(year, MM-1, DD, hh, mm, ss, sss);
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
const dt_to_y7 = (dt)=>mts_to_y7(dt.getTime());
|
|
74
|
+
const y7_to_dt = (y7)=>new Date(y7_to_mts(y7));
|
|
75
|
+
|
|
76
|
+
module.exports = (any=Date.now())=> mts_to_y7((new Date(any)).getTime());
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
module.exports.mts_to_y7 = mts_to_y7;
|
|
80
|
+
module.exports.y7_to_mts = y7_to_mts;
|
|
81
|
+
module.exports.dt_to_y7 = dt_to_y7;
|
|
82
|
+
module.exports.y7_to_dt = y7_to_dt;
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
|