mdtg 1.3.1 → 2.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 +245 -240
- package/package.json +27 -26
package/index.js
CHANGED
|
@@ -1,244 +1,249 @@
|
|
|
1
1
|
const normalizeNumber = (number) => String(number).padStart(2, "0");
|
|
2
|
+
const parseInteger = (input) => Number.parseInt(input, 10);
|
|
2
3
|
|
|
3
4
|
export class MDTG {
|
|
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
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
5
|
+
#currentDate;
|
|
6
|
+
|
|
7
|
+
static FORM = {
|
|
8
|
+
SHORT: "short",
|
|
9
|
+
SHORTENED: "shortened",
|
|
10
|
+
LONG: "long",
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
static #months = [
|
|
14
|
+
"jan",
|
|
15
|
+
"feb",
|
|
16
|
+
"mar",
|
|
17
|
+
"apr",
|
|
18
|
+
"may",
|
|
19
|
+
"jun",
|
|
20
|
+
"jul",
|
|
21
|
+
"aug",
|
|
22
|
+
"sep",
|
|
23
|
+
"oct",
|
|
24
|
+
"nov",
|
|
25
|
+
"dec",
|
|
26
|
+
];
|
|
27
|
+
|
|
28
|
+
static #shortFormatRegex = /^[0-9]{6}[A-Z]$/;
|
|
29
|
+
static #shortenedFormatRegex =
|
|
30
|
+
/^[0-9]{6}[A-Z](jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec)[0-9]{2}$/i;
|
|
31
|
+
static #longFormatRegex =
|
|
32
|
+
/^[0-9]{8}[A-Z](jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec)[0-9]{2}$/i;
|
|
33
|
+
|
|
34
|
+
static #offset = {
|
|
35
|
+
Y: 12,
|
|
36
|
+
X: 11,
|
|
37
|
+
W: 10,
|
|
38
|
+
V: 9,
|
|
39
|
+
U: 8,
|
|
40
|
+
T: 7,
|
|
41
|
+
S: 6,
|
|
42
|
+
R: 5,
|
|
43
|
+
Q: 4,
|
|
44
|
+
P: 3,
|
|
45
|
+
O: 2,
|
|
46
|
+
N: 1,
|
|
47
|
+
Z: 0,
|
|
48
|
+
A: -1,
|
|
49
|
+
B: -2,
|
|
50
|
+
C: -3,
|
|
51
|
+
D: -4,
|
|
52
|
+
E: -5,
|
|
53
|
+
F: -6,
|
|
54
|
+
G: -7,
|
|
55
|
+
H: -8,
|
|
56
|
+
I: -9,
|
|
57
|
+
J: -10,
|
|
58
|
+
K: -11,
|
|
59
|
+
L: -12,
|
|
60
|
+
M: -13,
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
constructor(date) {
|
|
64
|
+
if (date && !(date instanceof Date)) {
|
|
65
|
+
throw new TypeError("Expected Date object");
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
const now = new Date();
|
|
69
|
+
|
|
70
|
+
this.#currentDate =
|
|
71
|
+
date ??
|
|
72
|
+
new Date(
|
|
73
|
+
Date.UTC(
|
|
74
|
+
now.getUTCFullYear(),
|
|
75
|
+
now.getUTCMonth(),
|
|
76
|
+
now.getUTCDate(),
|
|
77
|
+
now.getUTCHours(),
|
|
78
|
+
now.getUTCMinutes(),
|
|
79
|
+
now.getUTCSeconds(),
|
|
80
|
+
now.getUTCMilliseconds(),
|
|
81
|
+
),
|
|
82
|
+
);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
static #validateTimezone(tz) {
|
|
86
|
+
const key = tz?.toUpperCase();
|
|
87
|
+
return key in MDTG.#offset ? key : "Z";
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
#createDateWithOffset(offset) {
|
|
91
|
+
const d = this.#currentDate;
|
|
92
|
+
|
|
93
|
+
return new Date(
|
|
94
|
+
Date.UTC(
|
|
95
|
+
d.getUTCFullYear(),
|
|
96
|
+
d.getUTCMonth(),
|
|
97
|
+
d.getUTCDate(),
|
|
98
|
+
d.getUTCHours() - offset,
|
|
99
|
+
d.getUTCMinutes(),
|
|
100
|
+
d.getUTCSeconds(),
|
|
101
|
+
),
|
|
102
|
+
);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
static isShortFormat(str) {
|
|
106
|
+
return MDTG.#shortFormatRegex.test(str);
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
static isShortenedFormat(str) {
|
|
110
|
+
return MDTG.#shortenedFormatRegex.test(str);
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
static isLongFormat(str) {
|
|
114
|
+
return MDTG.#longFormatRegex.test(str);
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
#toShortMDT(timezone = "Z") {
|
|
118
|
+
const tz = MDTG.#validateTimezone(timezone);
|
|
119
|
+
const date = this.#createDateWithOffset(MDTG.#offset[tz]);
|
|
120
|
+
|
|
121
|
+
const result = [date.getUTCDate(), date.getUTCHours(), date.getUTCMinutes()]
|
|
122
|
+
.map(normalizeNumber)
|
|
123
|
+
.concat(tz)
|
|
124
|
+
.join("");
|
|
125
|
+
|
|
126
|
+
if (!MDTG.isShortFormat(result)) {
|
|
127
|
+
throw new Error("Failed to build short MDTG");
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
return result;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
#toLongMDT(timezone = "Z") {
|
|
134
|
+
const tz = MDTG.#validateTimezone(timezone);
|
|
135
|
+
const date = this.#createDateWithOffset(MDTG.#offset[tz]);
|
|
136
|
+
|
|
137
|
+
const result = [
|
|
138
|
+
date.getUTCDate(),
|
|
139
|
+
date.getUTCHours(),
|
|
140
|
+
date.getUTCMinutes(),
|
|
141
|
+
date.getUTCSeconds(),
|
|
142
|
+
]
|
|
143
|
+
.map(normalizeNumber)
|
|
144
|
+
.concat(
|
|
145
|
+
tz,
|
|
146
|
+
MDTG.#months[date.getUTCMonth()],
|
|
147
|
+
normalizeNumber(date.getUTCFullYear() % 100),
|
|
148
|
+
)
|
|
149
|
+
.join("");
|
|
150
|
+
|
|
151
|
+
if (!MDTG.isLongFormat(result)) {
|
|
152
|
+
throw new Error("Failed to build long MDTG");
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
return result;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
#toShortenedMDT(timezone = "Z") {
|
|
159
|
+
const tz = MDTG.#validateTimezone(timezone);
|
|
160
|
+
const date = this.#createDateWithOffset(MDTG.#offset[tz]);
|
|
161
|
+
|
|
162
|
+
const result = [
|
|
163
|
+
this.#toShortMDT(tz),
|
|
164
|
+
MDTG.#months[date.getUTCMonth()],
|
|
165
|
+
normalizeNumber(date.getUTCFullYear() % 100),
|
|
166
|
+
].join("");
|
|
167
|
+
|
|
168
|
+
if (!MDTG.isShortenedFormat(result)) {
|
|
169
|
+
throw new Error("Failed to build shortened MDTG");
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
return result;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
toMDT({ form = MDTG.FORM.LONG, timezone = "Z" } = {}) {
|
|
176
|
+
switch (form) {
|
|
177
|
+
case MDTG.FORM.SHORT:
|
|
178
|
+
return this.#toShortMDT(timezone);
|
|
179
|
+
case MDTG.FORM.SHORTENED:
|
|
180
|
+
return this.#toShortenedMDT(timezone);
|
|
181
|
+
default:
|
|
182
|
+
return this.#toLongMDT(timezone);
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
static #parseShort(str) {
|
|
187
|
+
const day = parseInteger(str.slice(0, 2));
|
|
188
|
+
const hours = parseInteger(str.slice(2, 4));
|
|
189
|
+
const minutes = parseInteger(str.slice(4, 6));
|
|
190
|
+
const tz = str.slice(6, 7).toUpperCase();
|
|
191
|
+
|
|
192
|
+
const today = new Date();
|
|
193
|
+
|
|
194
|
+
return new Date(
|
|
195
|
+
Date.UTC(
|
|
196
|
+
today.getUTCFullYear(),
|
|
197
|
+
today.getUTCMonth(),
|
|
198
|
+
day,
|
|
199
|
+
hours + MDTG.#offset[tz],
|
|
200
|
+
minutes,
|
|
201
|
+
),
|
|
202
|
+
);
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
static #parseShortened(str) {
|
|
206
|
+
const day = parseInteger(str.slice(0, 2));
|
|
207
|
+
const hours = parseInteger(str.slice(2, 4));
|
|
208
|
+
const minutes = parseInteger(str.slice(4, 6));
|
|
209
|
+
const tz = str.slice(6, 7).toUpperCase();
|
|
210
|
+
|
|
211
|
+
const month = MDTG.#months.indexOf(str.slice(7, 10).toLowerCase());
|
|
212
|
+
if (month === -1) throw new Error("Invalid month");
|
|
213
|
+
|
|
214
|
+
const year = 2000 + parseInteger(str.slice(10, 12));
|
|
215
|
+
|
|
216
|
+
return new Date(
|
|
217
|
+
Date.UTC(year, month, day, hours + MDTG.#offset[tz], minutes),
|
|
218
|
+
);
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
static #parseLong(str) {
|
|
222
|
+
const day = parseInteger(str.slice(0, 2));
|
|
223
|
+
const hours = parseInteger(str.slice(2, 4));
|
|
224
|
+
const minutes = parseInteger(str.slice(4, 6));
|
|
225
|
+
const seconds = parseInteger(str.slice(6, 8));
|
|
226
|
+
const tz = str.slice(8, 9).toUpperCase();
|
|
227
|
+
|
|
228
|
+
const month = MDTG.#months.indexOf(str.slice(9, 12).toLowerCase());
|
|
229
|
+
if (month === -1) throw new Error("Invalid month");
|
|
230
|
+
|
|
231
|
+
const year = 2000 + parseInteger(str.slice(12, 14));
|
|
232
|
+
|
|
233
|
+
return new Date(
|
|
234
|
+
Date.UTC(year, month, day, hours + MDTG.#offset[tz], minutes, seconds),
|
|
235
|
+
);
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
static parse(str) {
|
|
239
|
+
if (MDTG.isShortFormat(str)) return MDTG.#parseShort(str);
|
|
240
|
+
if (MDTG.isShortenedFormat(str)) return MDTG.#parseShortened(str);
|
|
241
|
+
if (MDTG.isLongFormat(str)) return MDTG.#parseLong(str);
|
|
242
|
+
|
|
243
|
+
throw new Error(`Invalid MDTG string "${str}"`);
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
toDate() {
|
|
247
|
+
return new Date(this.#currentDate);
|
|
248
|
+
}
|
|
244
249
|
}
|
package/package.json
CHANGED
|
@@ -1,28 +1,29 @@
|
|
|
1
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
|
-
|
|
2
|
+
"name": "mdtg",
|
|
3
|
+
"version": "2.0.1",
|
|
4
|
+
"main": "index.js",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"engines": {
|
|
7
|
+
"node": ">=24.0.0"
|
|
8
|
+
},
|
|
9
|
+
"scripts": {
|
|
10
|
+
"coverage": "vitest run --coverage",
|
|
11
|
+
"format": "biome format .",
|
|
12
|
+
"prelint": "npm run format",
|
|
13
|
+
"lint": "biome lint .",
|
|
14
|
+
"test": "vitest run"
|
|
15
|
+
},
|
|
16
|
+
"keywords": [
|
|
17
|
+
"military",
|
|
18
|
+
"date",
|
|
19
|
+
"time"
|
|
20
|
+
],
|
|
21
|
+
"author": "0xflotus",
|
|
22
|
+
"license": "MIT",
|
|
23
|
+
"description": "This package offers you methods to convert Date Objects to military date time and vice versa",
|
|
24
|
+
"devDependencies": {
|
|
25
|
+
"@biomejs/biome": "2.4.7",
|
|
26
|
+
"@vitest/coverage-v8": "4.1.0",
|
|
27
|
+
"vitest": "4.1.0"
|
|
28
|
+
}
|
|
28
29
|
}
|