mdtg 0.0.3 → 1.0.0
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/README.md +3 -6
- package/index.js +108 -33
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -2,10 +2,6 @@
|
|
|
2
2
|
|
|
3
3
|
This is a package that helps you to convert `Date` objects to [military date time](https://en.wikipedia.org/wiki/Date-time_group) and vice versa.
|
|
4
4
|
|
|
5
|
-
#### Limitations
|
|
6
|
-
|
|
7
|
-
Currently there is no support for different timezones.
|
|
8
|
-
|
|
9
5
|
### Install
|
|
10
6
|
|
|
11
7
|
```bash
|
|
@@ -24,6 +20,7 @@ console.log(mdtg.toMDT({ form: "shortened" }));
|
|
|
24
20
|
console.log(mdtg.toMDT({ form: "long" }));
|
|
25
21
|
|
|
26
22
|
console.log(MDTG.parse("12135530Zaug24"));
|
|
27
|
-
console.log(MDTG.parse("
|
|
28
|
-
console.log(MDTG.parse("
|
|
23
|
+
console.log(MDTG.parse("12135530Asep24"));
|
|
24
|
+
console.log(MDTG.parse("121355LOCT24"));
|
|
25
|
+
console.log(MDTG.parse("121355M"));
|
|
29
26
|
```
|
package/index.js
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
const normalizeNumber = (number) => String(number).padStart(2, "0");
|
|
2
|
+
|
|
1
3
|
export class MDTG {
|
|
2
4
|
#currentDate = null;
|
|
3
5
|
static months = [
|
|
@@ -15,32 +17,77 @@ export class MDTG {
|
|
|
15
17
|
"dec",
|
|
16
18
|
];
|
|
17
19
|
|
|
20
|
+
static offset = {
|
|
21
|
+
Y: 12,
|
|
22
|
+
X: 11,
|
|
23
|
+
W: 10,
|
|
24
|
+
V: 9,
|
|
25
|
+
U: 8,
|
|
26
|
+
T: 7,
|
|
27
|
+
S: 6,
|
|
28
|
+
R: 5,
|
|
29
|
+
Q: 4,
|
|
30
|
+
P: 3,
|
|
31
|
+
O: 2,
|
|
32
|
+
N: 1,
|
|
33
|
+
Z: 0,
|
|
34
|
+
A: -1,
|
|
35
|
+
B: -2,
|
|
36
|
+
C: -3,
|
|
37
|
+
D: -4,
|
|
38
|
+
E: -5,
|
|
39
|
+
F: -6,
|
|
40
|
+
G: -7,
|
|
41
|
+
H: -8,
|
|
42
|
+
I: -9,
|
|
43
|
+
J: -10,
|
|
44
|
+
K: -11,
|
|
45
|
+
L: -12,
|
|
46
|
+
M: -13,
|
|
47
|
+
};
|
|
48
|
+
|
|
18
49
|
constructor(date) {
|
|
19
|
-
|
|
50
|
+
const now = new Date();
|
|
51
|
+
this.#currentDate =
|
|
52
|
+
date ??
|
|
53
|
+
new Date(
|
|
54
|
+
Date.UTC(
|
|
55
|
+
now.getUTCFullYear(),
|
|
56
|
+
now.getUTCMonth(),
|
|
57
|
+
now.getUTCDate(),
|
|
58
|
+
now.getUTCHours(),
|
|
59
|
+
now.getUTCMinutes(),
|
|
60
|
+
now.getUTCSeconds(),
|
|
61
|
+
now.getUTCMilliseconds(),
|
|
62
|
+
),
|
|
63
|
+
);
|
|
20
64
|
}
|
|
21
65
|
|
|
22
66
|
static isShortFormat(str) {
|
|
23
|
-
return /^[0-9]{6}Z$/.test(str);
|
|
67
|
+
return /^[0-9]{6}[A-Z]$/.test(str);
|
|
24
68
|
}
|
|
25
69
|
|
|
26
70
|
static isShortenedFormat(str) {
|
|
27
|
-
return /^[0-9]{6}Z(jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec)[0-9]{2}$/i.test(
|
|
71
|
+
return /^[0-9]{6}[A-Z](jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec)[0-9]{2}$/i.test(
|
|
28
72
|
str,
|
|
29
73
|
);
|
|
30
74
|
}
|
|
31
75
|
|
|
32
76
|
static isLongFormat(str) {
|
|
33
|
-
return /^[0-9]{8}Z(jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec)[0-9]{2}$/i.test(
|
|
77
|
+
return /^[0-9]{8}[A-Z](jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec)[0-9]{2}$/i.test(
|
|
34
78
|
str,
|
|
35
79
|
);
|
|
36
80
|
}
|
|
37
81
|
|
|
38
82
|
#toShortMDT() {
|
|
39
|
-
const shortMDT =
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
83
|
+
const shortMDT = [
|
|
84
|
+
this.#currentDate.getUTCDate(),
|
|
85
|
+
this.#currentDate.getUTCHours(),
|
|
86
|
+
this.#currentDate.getUTCMinutes(),
|
|
87
|
+
]
|
|
88
|
+
.map(normalizeNumber)
|
|
89
|
+
.concat("Z")
|
|
90
|
+
.join("");
|
|
44
91
|
if (!MDTG.isShortFormat(shortMDT)) {
|
|
45
92
|
throw new Error("There was an error while building short MDT");
|
|
46
93
|
}
|
|
@@ -48,14 +95,19 @@ export class MDTG {
|
|
|
48
95
|
}
|
|
49
96
|
|
|
50
97
|
#toLongMDT() {
|
|
51
|
-
const longMDT =
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
98
|
+
const longMDT = [
|
|
99
|
+
this.#currentDate.getUTCDate(),
|
|
100
|
+
this.#currentDate.getUTCHours(),
|
|
101
|
+
this.#currentDate.getUTCMinutes(),
|
|
102
|
+
this.#currentDate.getUTCSeconds(),
|
|
103
|
+
]
|
|
104
|
+
.map(normalizeNumber)
|
|
105
|
+
.concat(
|
|
106
|
+
"Z",
|
|
107
|
+
MDTG.months[this.#currentDate.getUTCMonth()],
|
|
108
|
+
normalizeNumber(this.#currentDate.getUTCFullYear() % 2000),
|
|
109
|
+
)
|
|
110
|
+
.join("");
|
|
59
111
|
if (!MDTG.isLongFormat(longMDT)) {
|
|
60
112
|
throw new Error("There was an error while building long MDT");
|
|
61
113
|
}
|
|
@@ -63,10 +115,11 @@ export class MDTG {
|
|
|
63
115
|
}
|
|
64
116
|
|
|
65
117
|
#toShortenedMDT() {
|
|
66
|
-
const shortenedMDT =
|
|
67
|
-
this.#toShortMDT()
|
|
68
|
-
MDTG.months[this.#currentDate.
|
|
69
|
-
|
|
118
|
+
const shortenedMDT = [
|
|
119
|
+
this.#toShortMDT(),
|
|
120
|
+
MDTG.months[this.#currentDate.getUTCMonth()],
|
|
121
|
+
normalizeNumber(this.#currentDate.getUTCFullYear() % 2000),
|
|
122
|
+
].join("");
|
|
70
123
|
if (!MDTG.isShortenedFormat(shortenedMDT)) {
|
|
71
124
|
throw new Error("There was an error while building shortened MDT");
|
|
72
125
|
}
|
|
@@ -88,37 +141,59 @@ export class MDTG {
|
|
|
88
141
|
const day = Number.parseInt(str.slice(0, 2));
|
|
89
142
|
const hours = Number.parseInt(str.slice(2, 4));
|
|
90
143
|
const minutes = Number.parseInt(str.slice(4, 6));
|
|
91
|
-
const
|
|
144
|
+
const timezoneOffset = str.slice(-1).toUpperCase();
|
|
92
145
|
|
|
93
146
|
const today = new Date();
|
|
94
147
|
const date = new Date(
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
148
|
+
Date.UTC(
|
|
149
|
+
today.getUTCFullYear(),
|
|
150
|
+
today.getUTCMonth(),
|
|
151
|
+
day,
|
|
152
|
+
hours + this.offset[timezoneOffset],
|
|
153
|
+
minutes,
|
|
154
|
+
0,
|
|
155
|
+
0,
|
|
156
|
+
),
|
|
102
157
|
);
|
|
103
158
|
return date;
|
|
104
159
|
} else if (MDTG.isShortenedFormat(str)) {
|
|
105
160
|
const day = Number.parseInt(str.slice(0, 2));
|
|
106
161
|
const hours = Number.parseInt(str.slice(2, 4));
|
|
107
162
|
const minutes = Number.parseInt(str.slice(4, 6));
|
|
108
|
-
const
|
|
163
|
+
const timezoneOffset = str.slice(6, 7).toUpperCase();
|
|
109
164
|
const month = MDTG.months.indexOf(str.slice(7, 10).toLowerCase());
|
|
110
165
|
const year = 2000 + Number.parseInt(str.slice(10, 12));
|
|
111
|
-
const date = new Date(
|
|
166
|
+
const date = new Date(
|
|
167
|
+
Date.UTC(
|
|
168
|
+
year,
|
|
169
|
+
month,
|
|
170
|
+
day,
|
|
171
|
+
hours + this.offset[timezoneOffset],
|
|
172
|
+
minutes,
|
|
173
|
+
0,
|
|
174
|
+
0,
|
|
175
|
+
),
|
|
176
|
+
);
|
|
112
177
|
return date;
|
|
113
178
|
} else if (MDTG.isLongFormat(str)) {
|
|
114
179
|
const day = Number.parseInt(str.slice(0, 2));
|
|
115
180
|
const hours = Number.parseInt(str.slice(2, 4));
|
|
116
181
|
const minutes = Number.parseInt(str.slice(4, 6));
|
|
117
182
|
const seconds = Number.parseInt(str.slice(6, 8));
|
|
118
|
-
const
|
|
183
|
+
const timezoneOffset = str.slice(8, 9).toUpperCase();
|
|
119
184
|
const month = MDTG.months.indexOf(str.slice(9, 12).toLowerCase());
|
|
120
185
|
const year = 2000 + Number.parseInt(str.slice(12, 14));
|
|
121
|
-
const date = new Date(
|
|
186
|
+
const date = new Date(
|
|
187
|
+
Date.UTC(
|
|
188
|
+
year,
|
|
189
|
+
month,
|
|
190
|
+
day,
|
|
191
|
+
hours + this.offset[timezoneOffset],
|
|
192
|
+
minutes,
|
|
193
|
+
seconds,
|
|
194
|
+
0,
|
|
195
|
+
),
|
|
196
|
+
);
|
|
122
197
|
return date;
|
|
123
198
|
} else {
|
|
124
199
|
throw Error("There was an error while parsing the string " + str);
|