mdtg 0.0.4 → 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 +91 -27
- 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
|
@@ -17,31 +17,73 @@ export class MDTG {
|
|
|
17
17
|
"dec",
|
|
18
18
|
];
|
|
19
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
|
+
|
|
20
49
|
constructor(date) {
|
|
21
|
-
|
|
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
|
+
);
|
|
22
64
|
}
|
|
23
65
|
|
|
24
66
|
static isShortFormat(str) {
|
|
25
|
-
return /^[0-9]{6}Z$/.test(str);
|
|
67
|
+
return /^[0-9]{6}[A-Z]$/.test(str);
|
|
26
68
|
}
|
|
27
69
|
|
|
28
70
|
static isShortenedFormat(str) {
|
|
29
|
-
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(
|
|
30
72
|
str,
|
|
31
73
|
);
|
|
32
74
|
}
|
|
33
75
|
|
|
34
76
|
static isLongFormat(str) {
|
|
35
|
-
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(
|
|
36
78
|
str,
|
|
37
79
|
);
|
|
38
80
|
}
|
|
39
81
|
|
|
40
82
|
#toShortMDT() {
|
|
41
83
|
const shortMDT = [
|
|
42
|
-
this.#currentDate.
|
|
43
|
-
this.#currentDate.
|
|
44
|
-
this.#currentDate.
|
|
84
|
+
this.#currentDate.getUTCDate(),
|
|
85
|
+
this.#currentDate.getUTCHours(),
|
|
86
|
+
this.#currentDate.getUTCMinutes(),
|
|
45
87
|
]
|
|
46
88
|
.map(normalizeNumber)
|
|
47
89
|
.concat("Z")
|
|
@@ -54,16 +96,16 @@ export class MDTG {
|
|
|
54
96
|
|
|
55
97
|
#toLongMDT() {
|
|
56
98
|
const longMDT = [
|
|
57
|
-
this.#currentDate.
|
|
58
|
-
this.#currentDate.
|
|
59
|
-
this.#currentDate.
|
|
60
|
-
this.#currentDate.
|
|
99
|
+
this.#currentDate.getUTCDate(),
|
|
100
|
+
this.#currentDate.getUTCHours(),
|
|
101
|
+
this.#currentDate.getUTCMinutes(),
|
|
102
|
+
this.#currentDate.getUTCSeconds(),
|
|
61
103
|
]
|
|
62
104
|
.map(normalizeNumber)
|
|
63
105
|
.concat(
|
|
64
106
|
"Z",
|
|
65
|
-
MDTG.months[this.#currentDate.
|
|
66
|
-
normalizeNumber(this.#currentDate.
|
|
107
|
+
MDTG.months[this.#currentDate.getUTCMonth()],
|
|
108
|
+
normalizeNumber(this.#currentDate.getUTCFullYear() % 2000),
|
|
67
109
|
)
|
|
68
110
|
.join("");
|
|
69
111
|
if (!MDTG.isLongFormat(longMDT)) {
|
|
@@ -75,8 +117,8 @@ export class MDTG {
|
|
|
75
117
|
#toShortenedMDT() {
|
|
76
118
|
const shortenedMDT = [
|
|
77
119
|
this.#toShortMDT(),
|
|
78
|
-
MDTG.months[this.#currentDate.
|
|
79
|
-
normalizeNumber(this.#currentDate.
|
|
120
|
+
MDTG.months[this.#currentDate.getUTCMonth()],
|
|
121
|
+
normalizeNumber(this.#currentDate.getUTCFullYear() % 2000),
|
|
80
122
|
].join("");
|
|
81
123
|
if (!MDTG.isShortenedFormat(shortenedMDT)) {
|
|
82
124
|
throw new Error("There was an error while building shortened MDT");
|
|
@@ -99,37 +141,59 @@ export class MDTG {
|
|
|
99
141
|
const day = Number.parseInt(str.slice(0, 2));
|
|
100
142
|
const hours = Number.parseInt(str.slice(2, 4));
|
|
101
143
|
const minutes = Number.parseInt(str.slice(4, 6));
|
|
102
|
-
const
|
|
144
|
+
const timezoneOffset = str.slice(-1).toUpperCase();
|
|
103
145
|
|
|
104
146
|
const today = new Date();
|
|
105
147
|
const date = new Date(
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
148
|
+
Date.UTC(
|
|
149
|
+
today.getUTCFullYear(),
|
|
150
|
+
today.getUTCMonth(),
|
|
151
|
+
day,
|
|
152
|
+
hours + this.offset[timezoneOffset],
|
|
153
|
+
minutes,
|
|
154
|
+
0,
|
|
155
|
+
0,
|
|
156
|
+
),
|
|
113
157
|
);
|
|
114
158
|
return date;
|
|
115
159
|
} else if (MDTG.isShortenedFormat(str)) {
|
|
116
160
|
const day = Number.parseInt(str.slice(0, 2));
|
|
117
161
|
const hours = Number.parseInt(str.slice(2, 4));
|
|
118
162
|
const minutes = Number.parseInt(str.slice(4, 6));
|
|
119
|
-
const
|
|
163
|
+
const timezoneOffset = str.slice(6, 7).toUpperCase();
|
|
120
164
|
const month = MDTG.months.indexOf(str.slice(7, 10).toLowerCase());
|
|
121
165
|
const year = 2000 + Number.parseInt(str.slice(10, 12));
|
|
122
|
-
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
|
+
);
|
|
123
177
|
return date;
|
|
124
178
|
} else if (MDTG.isLongFormat(str)) {
|
|
125
179
|
const day = Number.parseInt(str.slice(0, 2));
|
|
126
180
|
const hours = Number.parseInt(str.slice(2, 4));
|
|
127
181
|
const minutes = Number.parseInt(str.slice(4, 6));
|
|
128
182
|
const seconds = Number.parseInt(str.slice(6, 8));
|
|
129
|
-
const
|
|
183
|
+
const timezoneOffset = str.slice(8, 9).toUpperCase();
|
|
130
184
|
const month = MDTG.months.indexOf(str.slice(9, 12).toLowerCase());
|
|
131
185
|
const year = 2000 + Number.parseInt(str.slice(12, 14));
|
|
132
|
-
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
|
+
);
|
|
133
197
|
return date;
|
|
134
198
|
} else {
|
|
135
199
|
throw Error("There was an error while parsing the string " + str);
|