mdtg 0.0.4 → 1.1.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/.github/workflows/test.yml +39 -0
- package/README.md +3 -6
- package/index.js +138 -36
- package/package.json +1 -1
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
name: Test
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
push:
|
|
5
|
+
branches:
|
|
6
|
+
- main
|
|
7
|
+
pull_request:
|
|
8
|
+
branches:
|
|
9
|
+
- main
|
|
10
|
+
|
|
11
|
+
jobs:
|
|
12
|
+
lint:
|
|
13
|
+
name: Linting
|
|
14
|
+
runs-on: ubuntu-latest
|
|
15
|
+
steps:
|
|
16
|
+
- uses: actions/checkout@v4
|
|
17
|
+
- name: Setup Node.js
|
|
18
|
+
uses: actions/setup-node@v4
|
|
19
|
+
with:
|
|
20
|
+
node-version: 22
|
|
21
|
+
- run: npm install
|
|
22
|
+
- name: Check format
|
|
23
|
+
run: npm run lint
|
|
24
|
+
build:
|
|
25
|
+
name: Testing
|
|
26
|
+
needs: lint
|
|
27
|
+
strategy:
|
|
28
|
+
matrix:
|
|
29
|
+
os: [ubuntu-latest, ubuntu-24.04-arm]
|
|
30
|
+
node-version: [18, 20, 22]
|
|
31
|
+
runs-on: ${{ matrix.os }}
|
|
32
|
+
steps:
|
|
33
|
+
- uses: actions/checkout@v4
|
|
34
|
+
- name: Setup Node.js ${{ matrix.node-version }}
|
|
35
|
+
uses: actions/setup-node@v4
|
|
36
|
+
with:
|
|
37
|
+
node-version: ${{ matrix.node-version }}
|
|
38
|
+
- run: npm install
|
|
39
|
+
- run: npm test
|
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,34 +17,88 @@ 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
|
-
#toShortMDT() {
|
|
82
|
+
#toShortMDT(timezone = "Z") {
|
|
83
|
+
if (!/^[A-Z]$/.test(timezone.toUpperCase())) {
|
|
84
|
+
timezone = "Z";
|
|
85
|
+
}
|
|
86
|
+
const newDate = new Date(
|
|
87
|
+
Date.UTC(
|
|
88
|
+
this.#currentDate.getUTCFullYear(),
|
|
89
|
+
this.#currentDate.getUTCMonth(),
|
|
90
|
+
this.#currentDate.getUTCDate(),
|
|
91
|
+
this.#currentDate.getUTCHours() - MDTG.offset[timezone.toUpperCase()],
|
|
92
|
+
this.#currentDate.getUTCMinutes(),
|
|
93
|
+
),
|
|
94
|
+
);
|
|
41
95
|
const shortMDT = [
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
96
|
+
newDate.getUTCDate(),
|
|
97
|
+
newDate.getUTCHours(),
|
|
98
|
+
newDate.getUTCMinutes(),
|
|
45
99
|
]
|
|
46
100
|
.map(normalizeNumber)
|
|
47
|
-
.concat(
|
|
101
|
+
.concat(timezone.toUpperCase())
|
|
48
102
|
.join("");
|
|
49
103
|
if (!MDTG.isShortFormat(shortMDT)) {
|
|
50
104
|
throw new Error("There was an error while building short MDT");
|
|
@@ -52,18 +106,31 @@ export class MDTG {
|
|
|
52
106
|
return shortMDT;
|
|
53
107
|
}
|
|
54
108
|
|
|
55
|
-
#toLongMDT() {
|
|
109
|
+
#toLongMDT(timezone = "Z") {
|
|
110
|
+
if (!/^[A-Z]$/.test(timezone.toUpperCase())) {
|
|
111
|
+
timezone = "Z";
|
|
112
|
+
}
|
|
113
|
+
const newDate = new Date(
|
|
114
|
+
Date.UTC(
|
|
115
|
+
this.#currentDate.getUTCFullYear(),
|
|
116
|
+
this.#currentDate.getUTCMonth(),
|
|
117
|
+
this.#currentDate.getUTCDate(),
|
|
118
|
+
this.#currentDate.getUTCHours() - MDTG.offset[timezone.toUpperCase()],
|
|
119
|
+
this.#currentDate.getUTCMinutes(),
|
|
120
|
+
this.#currentDate.getUTCSeconds(),
|
|
121
|
+
),
|
|
122
|
+
);
|
|
56
123
|
const longMDT = [
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
124
|
+
newDate.getUTCDate(),
|
|
125
|
+
newDate.getUTCHours(),
|
|
126
|
+
newDate.getUTCMinutes(),
|
|
127
|
+
newDate.getUTCSeconds(),
|
|
61
128
|
]
|
|
62
129
|
.map(normalizeNumber)
|
|
63
130
|
.concat(
|
|
64
|
-
|
|
65
|
-
MDTG.months[
|
|
66
|
-
normalizeNumber(
|
|
131
|
+
timezone.toUpperCase(),
|
|
132
|
+
MDTG.months[newDate.getUTCMonth()],
|
|
133
|
+
normalizeNumber(newDate.getUTCFullYear() % 2000),
|
|
67
134
|
)
|
|
68
135
|
.join("");
|
|
69
136
|
if (!MDTG.isLongFormat(longMDT)) {
|
|
@@ -72,11 +139,24 @@ export class MDTG {
|
|
|
72
139
|
return longMDT;
|
|
73
140
|
}
|
|
74
141
|
|
|
75
|
-
#toShortenedMDT() {
|
|
142
|
+
#toShortenedMDT(timezone = "Z") {
|
|
143
|
+
if (!/^[A-Z]$/.test(timezone.toUpperCase())) {
|
|
144
|
+
timezone = "Z";
|
|
145
|
+
}
|
|
146
|
+
const newDate = new Date(
|
|
147
|
+
Date.UTC(
|
|
148
|
+
this.#currentDate.getUTCFullYear(),
|
|
149
|
+
this.#currentDate.getUTCMonth(),
|
|
150
|
+
this.#currentDate.getUTCDate(),
|
|
151
|
+
this.#currentDate.getUTCHours() - MDTG.offset[timezone.toUpperCase()],
|
|
152
|
+
this.#currentDate.getUTCMinutes(),
|
|
153
|
+
this.#currentDate.getUTCSeconds(),
|
|
154
|
+
),
|
|
155
|
+
);
|
|
76
156
|
const shortenedMDT = [
|
|
77
|
-
this.#toShortMDT(),
|
|
78
|
-
MDTG.months[
|
|
79
|
-
normalizeNumber(
|
|
157
|
+
this.#toShortMDT(timezone.toUpperCase()),
|
|
158
|
+
MDTG.months[newDate.getUTCMonth()],
|
|
159
|
+
normalizeNumber(newDate.getUTCFullYear() % 2000),
|
|
80
160
|
].join("");
|
|
81
161
|
if (!MDTG.isShortenedFormat(shortenedMDT)) {
|
|
82
162
|
throw new Error("There was an error while building shortened MDT");
|
|
@@ -86,11 +166,11 @@ export class MDTG {
|
|
|
86
166
|
|
|
87
167
|
toMDT(options) {
|
|
88
168
|
if (options?.form === "short") {
|
|
89
|
-
return this.#toShortMDT();
|
|
169
|
+
return this.#toShortMDT(options?.timezone);
|
|
90
170
|
} else if (options?.form === "shortened") {
|
|
91
|
-
return this.#toShortenedMDT();
|
|
171
|
+
return this.#toShortenedMDT(options?.timezone);
|
|
92
172
|
} else {
|
|
93
|
-
return this.#toLongMDT();
|
|
173
|
+
return this.#toLongMDT(options?.timezone);
|
|
94
174
|
}
|
|
95
175
|
}
|
|
96
176
|
|
|
@@ -99,37 +179,59 @@ export class MDTG {
|
|
|
99
179
|
const day = Number.parseInt(str.slice(0, 2));
|
|
100
180
|
const hours = Number.parseInt(str.slice(2, 4));
|
|
101
181
|
const minutes = Number.parseInt(str.slice(4, 6));
|
|
102
|
-
const
|
|
182
|
+
const timezoneOffset = str.slice(-1).toUpperCase();
|
|
103
183
|
|
|
104
184
|
const today = new Date();
|
|
105
185
|
const date = new Date(
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
186
|
+
Date.UTC(
|
|
187
|
+
today.getUTCFullYear(),
|
|
188
|
+
today.getUTCMonth(),
|
|
189
|
+
day,
|
|
190
|
+
hours + this.offset[timezoneOffset],
|
|
191
|
+
minutes,
|
|
192
|
+
0,
|
|
193
|
+
0,
|
|
194
|
+
),
|
|
113
195
|
);
|
|
114
196
|
return date;
|
|
115
197
|
} else if (MDTG.isShortenedFormat(str)) {
|
|
116
198
|
const day = Number.parseInt(str.slice(0, 2));
|
|
117
199
|
const hours = Number.parseInt(str.slice(2, 4));
|
|
118
200
|
const minutes = Number.parseInt(str.slice(4, 6));
|
|
119
|
-
const
|
|
201
|
+
const timezoneOffset = str.slice(6, 7).toUpperCase();
|
|
120
202
|
const month = MDTG.months.indexOf(str.slice(7, 10).toLowerCase());
|
|
121
203
|
const year = 2000 + Number.parseInt(str.slice(10, 12));
|
|
122
|
-
const date = new Date(
|
|
204
|
+
const date = new Date(
|
|
205
|
+
Date.UTC(
|
|
206
|
+
year,
|
|
207
|
+
month,
|
|
208
|
+
day,
|
|
209
|
+
hours + this.offset[timezoneOffset],
|
|
210
|
+
minutes,
|
|
211
|
+
0,
|
|
212
|
+
0,
|
|
213
|
+
),
|
|
214
|
+
);
|
|
123
215
|
return date;
|
|
124
216
|
} else if (MDTG.isLongFormat(str)) {
|
|
125
217
|
const day = Number.parseInt(str.slice(0, 2));
|
|
126
218
|
const hours = Number.parseInt(str.slice(2, 4));
|
|
127
219
|
const minutes = Number.parseInt(str.slice(4, 6));
|
|
128
220
|
const seconds = Number.parseInt(str.slice(6, 8));
|
|
129
|
-
const
|
|
221
|
+
const timezoneOffset = str.slice(8, 9).toUpperCase();
|
|
130
222
|
const month = MDTG.months.indexOf(str.slice(9, 12).toLowerCase());
|
|
131
223
|
const year = 2000 + Number.parseInt(str.slice(12, 14));
|
|
132
|
-
const date = new Date(
|
|
224
|
+
const date = new Date(
|
|
225
|
+
Date.UTC(
|
|
226
|
+
year,
|
|
227
|
+
month,
|
|
228
|
+
day,
|
|
229
|
+
hours + this.offset[timezoneOffset],
|
|
230
|
+
minutes,
|
|
231
|
+
seconds,
|
|
232
|
+
0,
|
|
233
|
+
),
|
|
234
|
+
);
|
|
133
235
|
return date;
|
|
134
236
|
} else {
|
|
135
237
|
throw Error("There was an error while parsing the string " + str);
|