@zelgadis87/utils-core 5.2.10 → 5.2.12
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/CHANGELOG.md +8 -0
- package/dist/time/TimeInstant.d.ts +9 -9
- package/dist/time/TimeInstantBuilder.d.ts +24 -15
- package/dist/time/types.d.ts +2 -1
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/esbuild/index.cjs +196 -184
- package/esbuild/index.cjs.map +3 -3
- package/esbuild/index.mjs +196 -184
- package/esbuild/index.mjs.map +3 -3
- package/package.json +1 -1
- package/src/time/TimeInstant.ts +275 -204
- package/src/time/TimeInstantBuilder.ts +18 -8
- package/src/time/types.ts +2 -1
package/esbuild/index.cjs
CHANGED
|
@@ -2093,13 +2093,18 @@ var TimeInstant = class _TimeInstant extends TimeBase {
|
|
|
2093
2093
|
return TimeDuration_default.fromMs(Math.max(distance, 0));
|
|
2094
2094
|
}
|
|
2095
2095
|
distanceFromStartOfDay() {
|
|
2096
|
-
|
|
2096
|
+
const params = this.toParameters();
|
|
2097
|
+
const msInDay = params.hours * 36e5 + params.minutes * 6e4 + params.seconds * 1e3 + params.milliseconds;
|
|
2098
|
+
return TimeDuration_default.fromMs(msInDay);
|
|
2097
2099
|
}
|
|
2098
2100
|
atStartOfDay() {
|
|
2099
2101
|
return this.atTime({ hours: 0, minutes: 0, seconds: 0, milliseconds: 0 });
|
|
2100
2102
|
}
|
|
2101
2103
|
distanceFromEndOfDay() {
|
|
2102
|
-
|
|
2104
|
+
const params = this.toParameters();
|
|
2105
|
+
const msInDay = params.hours * 36e5 + params.minutes * 6e4 + params.seconds * 1e3 + params.milliseconds;
|
|
2106
|
+
const msUntilEndOfDay = 86399999 - msInDay;
|
|
2107
|
+
return TimeDuration_default.fromMs(msUntilEndOfDay);
|
|
2103
2108
|
}
|
|
2104
2109
|
atEndOfDay() {
|
|
2105
2110
|
return this.atTime({ hours: 23, minutes: 59, seconds: 59, milliseconds: 999 });
|
|
@@ -2125,7 +2130,7 @@ var TimeInstant = class _TimeInstant extends TimeBase {
|
|
|
2125
2130
|
throw new Error("Instant is not in the past");
|
|
2126
2131
|
}
|
|
2127
2132
|
isToday() {
|
|
2128
|
-
return Math.floor(this.
|
|
2133
|
+
return Math.floor(this.ms / 864e5) === Math.floor(Date.now() / 864e5);
|
|
2129
2134
|
}
|
|
2130
2135
|
/**
|
|
2131
2136
|
* Formats this instant using the given pattern. The pattern can contain the following tokens:
|
|
@@ -2192,9 +2197,12 @@ var TimeInstant = class _TimeInstant extends TimeBase {
|
|
|
2192
2197
|
* @throws Error if the string doesn't match the pattern or contains invalid basic values
|
|
2193
2198
|
* @todo Add calendar-aware validation to reject dates like February 30th, April 31st
|
|
2194
2199
|
*/
|
|
2195
|
-
static
|
|
2200
|
+
static fromString(dateString, pattern, base = _TimeInstant.now(), config = {}) {
|
|
2196
2201
|
return parseTimeInstant(dateString, pattern, base, config);
|
|
2197
2202
|
}
|
|
2203
|
+
static parse(dateString, pattern, config = {}) {
|
|
2204
|
+
return parseTimeInstantComponents(dateString, pattern, config);
|
|
2205
|
+
}
|
|
2198
2206
|
/**
|
|
2199
2207
|
* @returns this instant, in ISO 8601 format (eg, 2024-11-01T15:49:22.024Z). The format is meant to always be realiable.
|
|
2200
2208
|
*/
|
|
@@ -2213,8 +2221,17 @@ var TimeInstant = class _TimeInstant extends TimeBase {
|
|
|
2213
2221
|
toDate() {
|
|
2214
2222
|
return new Date(this.ms);
|
|
2215
2223
|
}
|
|
2216
|
-
|
|
2217
|
-
|
|
2224
|
+
toParameters() {
|
|
2225
|
+
const d = this.toDate();
|
|
2226
|
+
return {
|
|
2227
|
+
year: d.getUTCFullYear(),
|
|
2228
|
+
month: d.getUTCMonth() + 1,
|
|
2229
|
+
date: d.getUTCDate(),
|
|
2230
|
+
hours: d.getUTCHours(),
|
|
2231
|
+
minutes: d.getUTCMinutes(),
|
|
2232
|
+
seconds: d.getUTCSeconds(),
|
|
2233
|
+
milliseconds: d.getUTCMilliseconds()
|
|
2234
|
+
};
|
|
2218
2235
|
}
|
|
2219
2236
|
/**
|
|
2220
2237
|
* @returns true if the instant is in the past, false otherwise
|
|
@@ -2345,30 +2362,21 @@ var TimeInstant = class _TimeInstant extends TimeBase {
|
|
|
2345
2362
|
const timestamp2 = Math.round(this.ms / unitMs) * unitMs;
|
|
2346
2363
|
return _TimeInstant.fromUnixTimestamp(timestamp2);
|
|
2347
2364
|
}
|
|
2348
|
-
get dayOfMonth() {
|
|
2349
|
-
return this.toDate().getUTCDate();
|
|
2350
|
-
}
|
|
2351
|
-
get dayOfWeek() {
|
|
2352
|
-
return this.toDate().getUTCDay() + 1;
|
|
2353
|
-
}
|
|
2354
|
-
get month() {
|
|
2355
|
-
return this.toDate().getUTCMonth() + 1;
|
|
2356
|
-
}
|
|
2357
|
-
get year() {
|
|
2358
|
-
return this.toDate().getUTCFullYear();
|
|
2359
|
-
}
|
|
2360
2365
|
/**
|
|
2361
2366
|
* Returns the week number represented by this instant, according to the ISO 8601 standard.
|
|
2362
|
-
|
|
2367
|
+
* Please note that the instant and the week number could be of two different years, eg the friday 1st january is actually part of week 52 of the previous year.
|
|
2363
2368
|
*/
|
|
2364
2369
|
get weekNumber() {
|
|
2365
2370
|
const date = this.toDate();
|
|
2366
2371
|
const oneDay = 1e3 * 60 * 60 * 24;
|
|
2367
|
-
const
|
|
2368
|
-
const
|
|
2369
|
-
const
|
|
2372
|
+
const dayOfWeek = date.getUTCDay() || 7;
|
|
2373
|
+
const thursdayMs = this.ms + (4 - dayOfWeek) * oneDay;
|
|
2374
|
+
const thursdayDate = new Date(thursdayMs);
|
|
2375
|
+
const thursdayYear = thursdayDate.getUTCFullYear();
|
|
2376
|
+
const firstOfJanMs = Date.UTC(thursdayYear, 0, 1, 12, 0, 0);
|
|
2377
|
+
const dayOfTheYear = Math.round((thursdayMs - firstOfJanMs) / oneDay);
|
|
2370
2378
|
const weekNumber = Math.floor(dayOfTheYear / 7) + 1;
|
|
2371
|
-
return { weekNumber, year:
|
|
2379
|
+
return { weekNumber, year: thursdayYear };
|
|
2372
2380
|
}
|
|
2373
2381
|
/**
|
|
2374
2382
|
* This method is used to provide a better DX when inspecting a TimeInstant object in DevTools.
|
|
@@ -2409,111 +2417,118 @@ function isValidSecond(num) {
|
|
|
2409
2417
|
function isValidMillisecond(num) {
|
|
2410
2418
|
return num >= 0 && num <= 999;
|
|
2411
2419
|
}
|
|
2420
|
+
var monthNamesCache = /* @__PURE__ */ new Map();
|
|
2421
|
+
function getMonthNames(locale = "en") {
|
|
2422
|
+
const cached = monthNamesCache.get(locale);
|
|
2423
|
+
if (cached) {
|
|
2424
|
+
return cached;
|
|
2425
|
+
}
|
|
2426
|
+
const shortFormatter = new Intl.DateTimeFormat(locale, { month: "short" });
|
|
2427
|
+
const longFormatter = new Intl.DateTimeFormat(locale, { month: "long" });
|
|
2428
|
+
const short = [];
|
|
2429
|
+
const long = [];
|
|
2430
|
+
for (let month = 0; month < 12; month++) {
|
|
2431
|
+
const date = new Date(2e3, month, 1);
|
|
2432
|
+
short.push(normalizeMonthName(shortFormatter.format(date)));
|
|
2433
|
+
long.push(normalizeMonthName(longFormatter.format(date)));
|
|
2434
|
+
}
|
|
2435
|
+
const result = { short, long };
|
|
2436
|
+
monthNamesCache.set(locale, result);
|
|
2437
|
+
return result;
|
|
2438
|
+
}
|
|
2439
|
+
function normalizeMonthName(name) {
|
|
2440
|
+
return name.replace(/[.,;:!?]/g, "").normalize("NFD").replace(/[\u0300-\u036f]/g, "").trim();
|
|
2441
|
+
}
|
|
2412
2442
|
var PATTERN_REGEX = /(M|y|d|D|h|H|m|s|S|G|Z|P|a)+/g;
|
|
2413
2443
|
var ESCAPE_REGEX = /\\"|"((?:\\"|[^"])*)"/g;
|
|
2414
|
-
var
|
|
2415
|
-
|
|
2416
|
-
|
|
2417
|
-
|
|
2418
|
-
|
|
2419
|
-
|
|
2420
|
-
|
|
2421
|
-
|
|
2422
|
-
|
|
2423
|
-
|
|
2424
|
-
|
|
2425
|
-
|
|
2426
|
-
|
|
2427
|
-
|
|
2428
|
-
}
|
|
2429
|
-
|
|
2430
|
-
|
|
2431
|
-
|
|
2432
|
-
|
|
2433
|
-
|
|
2434
|
-
|
|
2435
|
-
|
|
2436
|
-
|
|
2437
|
-
|
|
2438
|
-
|
|
2439
|
-
|
|
2440
|
-
|
|
2441
|
-
|
|
2442
|
-
|
|
2444
|
+
var formatterConfigs = {
|
|
2445
|
+
// Year
|
|
2446
|
+
"y": { options: { year: "numeric" } },
|
|
2447
|
+
"yy": { options: { year: "2-digit" } },
|
|
2448
|
+
"yyyy": { options: { year: "numeric" } },
|
|
2449
|
+
// Month
|
|
2450
|
+
"M": { options: { month: "numeric" } },
|
|
2451
|
+
"MM": { options: { month: "2-digit" } },
|
|
2452
|
+
"MMM": { options: { month: "short" } },
|
|
2453
|
+
"MMMM": { options: { month: "long" } },
|
|
2454
|
+
// Day
|
|
2455
|
+
"d": { options: { day: "numeric" } },
|
|
2456
|
+
"dd": { options: { day: "2-digit" } },
|
|
2457
|
+
// Hours (24-hour) - extract and pad manually due to Intl quirk
|
|
2458
|
+
"H": { options: { hour: "numeric", hour12: false }, extract: { partType: "hour" } },
|
|
2459
|
+
"HH": { options: { hour: "2-digit", hour12: false }, extract: { partType: "hour", transform: (v) => v.padStart(2, "0") } },
|
|
2460
|
+
// Hours (12-hour) - extract and pad manually due to Intl quirk
|
|
2461
|
+
"h": { options: { hour: "numeric", hour12: true }, extract: { partType: "hour" } },
|
|
2462
|
+
"hh": { options: { hour: "2-digit", hour12: true }, extract: { partType: "hour", transform: (v) => v.padStart(2, "0") } },
|
|
2463
|
+
// Minutes - extract and pad manually due to Intl quirk
|
|
2464
|
+
"m": { options: { minute: "numeric" }, extract: { partType: "minute" } },
|
|
2465
|
+
"mm": { options: { minute: "2-digit" }, extract: { partType: "minute", transform: (v) => v.padStart(2, "0") } },
|
|
2466
|
+
// Seconds - extract and pad manually due to Intl quirk
|
|
2467
|
+
"s": { options: { second: "numeric" }, extract: { partType: "second" } },
|
|
2468
|
+
"ss": { options: { second: "2-digit" }, extract: { partType: "second", transform: (v) => v.padStart(2, "0") } },
|
|
2469
|
+
// Milliseconds - extract manually
|
|
2470
|
+
"S": { options: { fractionalSecondDigits: 1 }, extract: { partType: "fractionalSecond" } },
|
|
2471
|
+
"SS": { options: { fractionalSecondDigits: 2 }, extract: { partType: "fractionalSecond" } },
|
|
2472
|
+
"SSS": { options: { fractionalSecondDigits: 3 }, extract: { partType: "fractionalSecond" } },
|
|
2473
|
+
// Weekday
|
|
2474
|
+
"D": { options: { weekday: "narrow" } },
|
|
2475
|
+
"DD": { options: { weekday: "short" } },
|
|
2476
|
+
"DDD": { options: { weekday: "long" } },
|
|
2477
|
+
// Era
|
|
2478
|
+
"G": { options: { era: "narrow" } },
|
|
2479
|
+
"GG": { options: { era: "short" } },
|
|
2480
|
+
"GGG": { options: { era: "long" } },
|
|
2481
|
+
// Timezone
|
|
2482
|
+
"Z": { options: { timeZoneName: "short" } },
|
|
2483
|
+
"ZZ": { options: { timeZoneName: "long" } },
|
|
2484
|
+
// Day period
|
|
2485
|
+
"P": { options: { dayPeriod: "narrow" } },
|
|
2486
|
+
"PP": { options: { dayPeriod: "short" } },
|
|
2487
|
+
"PPP": { options: { dayPeriod: "long" } },
|
|
2488
|
+
// Meridiem - extract dayPeriod and lowercase it
|
|
2489
|
+
"a": { options: { hour: "numeric", hour12: true }, extract: { partType: "dayPeriod", transform: (v) => v.toLowerCase() } }
|
|
2443
2490
|
};
|
|
2444
|
-
function formatType(
|
|
2445
|
-
|
|
2446
|
-
|
|
2447
|
-
|
|
2448
|
-
|
|
2449
|
-
|
|
2450
|
-
|
|
2451
|
-
|
|
2452
|
-
|
|
2453
|
-
const
|
|
2454
|
-
|
|
2455
|
-
|
|
2456
|
-
|
|
2457
|
-
|
|
2458
|
-
return length === 2 && day < 10 ? "0" + day : String(day);
|
|
2459
|
-
}
|
|
2460
|
-
if (type === "H" && (length === 1 || length === 2)) {
|
|
2461
|
-
const hours = instant.toDate().getUTCHours();
|
|
2462
|
-
return length === 2 && hours < 10 ? "0" + hours : String(hours);
|
|
2463
|
-
}
|
|
2464
|
-
if (type === "h" && (length === 1 || length === 2)) {
|
|
2465
|
-
let hours = instant.toDate().getUTCHours();
|
|
2466
|
-
hours = hours === 0 ? 12 : hours > 12 ? hours - 12 : hours;
|
|
2467
|
-
return length === 2 && hours < 10 ? "0" + hours : String(hours);
|
|
2468
|
-
}
|
|
2469
|
-
if (type === "m" && (length === 1 || length === 2)) {
|
|
2470
|
-
const minutes = instant.toDate().getUTCMinutes();
|
|
2471
|
-
return length === 2 && minutes < 10 ? "0" + minutes : String(minutes);
|
|
2472
|
-
}
|
|
2473
|
-
if (type === "s" && (length === 1 || length === 2)) {
|
|
2474
|
-
const seconds = instant.toDate().getUTCSeconds();
|
|
2475
|
-
return length === 2 && seconds < 10 ? "0" + seconds : String(seconds);
|
|
2476
|
-
}
|
|
2477
|
-
if (type === "S") {
|
|
2478
|
-
const ms = instant.toDate().getUTCMilliseconds();
|
|
2479
|
-
if (length === 1) return String(Math.floor(ms / 100));
|
|
2480
|
-
if (length === 2) return String(Math.floor(ms / 10)).padStart(2, "0");
|
|
2481
|
-
if (length === 3) return String(ms).padStart(3, "0");
|
|
2482
|
-
return String(ms);
|
|
2483
|
-
}
|
|
2484
|
-
const date = instant.toDate();
|
|
2485
|
-
const option = optionNames[type];
|
|
2486
|
-
const value = values[type][length - 1];
|
|
2487
|
-
if (!value) {
|
|
2488
|
-
return;
|
|
2489
|
-
}
|
|
2490
|
-
const options = {
|
|
2491
|
-
[option]: value,
|
|
2492
|
-
timeZone
|
|
2493
|
-
};
|
|
2494
|
-
if (type === "a") {
|
|
2495
|
-
return Intl.DateTimeFormat(locale, {
|
|
2496
|
-
...options,
|
|
2497
|
-
hour: "numeric"
|
|
2498
|
-
}).formatToParts(date).pop()?.value;
|
|
2499
|
-
}
|
|
2500
|
-
if (type === "G" || type === "Z") {
|
|
2501
|
-
return Intl.DateTimeFormat(locale, options).formatToParts(date).pop()?.value;
|
|
2491
|
+
function formatType(type, length, date, locale = "en", timeZone = "UTC") {
|
|
2492
|
+
const tokenKey = type.repeat(length);
|
|
2493
|
+
const config = formatterConfigs[tokenKey];
|
|
2494
|
+
if (!config) {
|
|
2495
|
+
return void 0;
|
|
2496
|
+
}
|
|
2497
|
+
const formatter = new Intl.DateTimeFormat(locale, { ...config.options, timeZone });
|
|
2498
|
+
if (config.extract) {
|
|
2499
|
+
const part = formatter.formatToParts(date).find((p) => p.type === config.extract.partType);
|
|
2500
|
+
const value = part?.value;
|
|
2501
|
+
if (!value) {
|
|
2502
|
+
return void 0;
|
|
2503
|
+
}
|
|
2504
|
+
return config.extract.transform ? config.extract.transform(value) : value;
|
|
2502
2505
|
}
|
|
2503
|
-
return
|
|
2506
|
+
return formatter.format(date);
|
|
2504
2507
|
}
|
|
2505
2508
|
function formatTimeInstant(instant, pattern, config = {}) {
|
|
2509
|
+
const date = instant.toDate();
|
|
2510
|
+
const locale = config.locale || "en";
|
|
2511
|
+
const timeZone = config.timeZone || "UTC";
|
|
2506
2512
|
return pattern.split(ESCAPE_REGEX).filter((sub) => sub !== void 0).map((sub, index) => {
|
|
2507
2513
|
if (index % 2 !== 0) {
|
|
2508
2514
|
return sub;
|
|
2509
2515
|
}
|
|
2510
2516
|
return sub.replace(PATTERN_REGEX, (match) => {
|
|
2511
2517
|
const type = match.charAt(0);
|
|
2512
|
-
|
|
2518
|
+
const length = match.length;
|
|
2519
|
+
return formatType(type, length, date, locale, timeZone) || match;
|
|
2513
2520
|
});
|
|
2514
2521
|
}).join("");
|
|
2515
2522
|
}
|
|
2516
2523
|
function parseTimeInstant(dateString, pattern, base, config = {}) {
|
|
2524
|
+
const parsed = parseTimeInstantComponents(dateString, pattern, config);
|
|
2525
|
+
const params = {
|
|
2526
|
+
...base.toParameters(),
|
|
2527
|
+
...parsed
|
|
2528
|
+
};
|
|
2529
|
+
return TimeInstant.fromParameters(params);
|
|
2530
|
+
}
|
|
2531
|
+
function parseTimeInstantComponents(dateString, pattern, config = {}) {
|
|
2517
2532
|
let regexPattern = pattern;
|
|
2518
2533
|
const tokens = [];
|
|
2519
2534
|
let position = 0;
|
|
@@ -2531,7 +2546,7 @@ function parseTimeInstant(dateString, pattern, base, config = {}) {
|
|
|
2531
2546
|
case "M":
|
|
2532
2547
|
if (match.length === 1) return "(\\d{1,2})";
|
|
2533
2548
|
if (match.length === 2) return "(\\d{2})";
|
|
2534
|
-
if (match.length === 3) return "([A-Za-z]{
|
|
2549
|
+
if (match.length === 3) return "([A-Za-z.]{1,7})";
|
|
2535
2550
|
return "([A-Za-z]+)";
|
|
2536
2551
|
case "d":
|
|
2537
2552
|
return match.length === 1 ? "(\\d{1,2})" : "(\\d{2})";
|
|
@@ -2567,113 +2582,110 @@ function parseTimeInstant(dateString, pattern, base, config = {}) {
|
|
|
2567
2582
|
if (!matches) {
|
|
2568
2583
|
throw new Error(`Date string "${dateString}" does not match pattern "${pattern}"`);
|
|
2569
2584
|
}
|
|
2570
|
-
|
|
2571
|
-
|
|
2572
|
-
let day = base.dayOfMonth;
|
|
2573
|
-
let hour = base.toDate().getUTCHours();
|
|
2574
|
-
let minute = base.toDate().getUTCMinutes();
|
|
2575
|
-
let second = base.toDate().getUTCSeconds();
|
|
2576
|
-
let millisecond = base.toDate().getUTCMilliseconds();
|
|
2585
|
+
const result = {};
|
|
2586
|
+
const locale = config.locale || "en";
|
|
2577
2587
|
let isPM = false;
|
|
2578
2588
|
let is12Hour = false;
|
|
2589
|
+
let hourValue;
|
|
2579
2590
|
tokens.forEach((token, index) => {
|
|
2580
2591
|
const value = matches[index + 1];
|
|
2581
2592
|
switch (token.type) {
|
|
2582
2593
|
case "y":
|
|
2583
2594
|
if (token.length === 2) {
|
|
2584
2595
|
const shortYear = parseInt(value, 10);
|
|
2585
|
-
year = shortYear < 50 ? 2e3 + shortYear : 1900 + shortYear;
|
|
2596
|
+
result.year = shortYear < 50 ? 2e3 + shortYear : 1900 + shortYear;
|
|
2586
2597
|
} else {
|
|
2587
|
-
year = parseInt(value, 10);
|
|
2598
|
+
result.year = parseInt(value, 10);
|
|
2588
2599
|
}
|
|
2589
2600
|
break;
|
|
2590
2601
|
case "M":
|
|
2591
|
-
|
|
2592
|
-
|
|
2593
|
-
|
|
2594
|
-
|
|
2595
|
-
|
|
2596
|
-
|
|
2597
|
-
|
|
2598
|
-
|
|
2599
|
-
|
|
2600
|
-
|
|
2601
|
-
|
|
2602
|
-
|
|
2603
|
-
|
|
2604
|
-
|
|
2605
|
-
|
|
2606
|
-
|
|
2607
|
-
|
|
2608
|
-
|
|
2609
|
-
|
|
2610
|
-
|
|
2611
|
-
|
|
2612
|
-
|
|
2613
|
-
|
|
2614
|
-
|
|
2615
|
-
"July",
|
|
2616
|
-
"August",
|
|
2617
|
-
"September",
|
|
2618
|
-
"October",
|
|
2619
|
-
"November",
|
|
2620
|
-
"December"
|
|
2621
|
-
];
|
|
2622
|
-
let monthIndex = monthNames2.findIndex((name) => name.toLowerCase() === value.toLowerCase());
|
|
2623
|
-
if (monthIndex === -1) {
|
|
2624
|
-
monthIndex = longMonthNames.findIndex((name) => name.toLowerCase() === value.toLowerCase());
|
|
2602
|
+
switch (token.length) {
|
|
2603
|
+
case 1:
|
|
2604
|
+
case 2:
|
|
2605
|
+
result.month = parseInt(value, 10);
|
|
2606
|
+
break;
|
|
2607
|
+
case 3: {
|
|
2608
|
+
const normalizedValue = normalizeMonthName(value);
|
|
2609
|
+
const monthIndex = getMonthNames(locale).short.findIndex(
|
|
2610
|
+
(name) => name.toLowerCase() === normalizedValue.toLowerCase()
|
|
2611
|
+
);
|
|
2612
|
+
if (monthIndex === -1)
|
|
2613
|
+
throw new Error(`Invalid short month name in date string: ${dateString}`);
|
|
2614
|
+
result.month = monthIndex + 1;
|
|
2615
|
+
break;
|
|
2616
|
+
}
|
|
2617
|
+
case 4: {
|
|
2618
|
+
const normalizedValue = normalizeMonthName(value);
|
|
2619
|
+
const monthIndex = getMonthNames(locale).long.findIndex(
|
|
2620
|
+
(name) => name.toLowerCase() === normalizedValue.toLowerCase()
|
|
2621
|
+
);
|
|
2622
|
+
if (monthIndex === -1)
|
|
2623
|
+
throw new Error(`Invalid full month name in date string: ${dateString}`);
|
|
2624
|
+
result.month = monthIndex + 1;
|
|
2625
|
+
break;
|
|
2625
2626
|
}
|
|
2626
|
-
|
|
2627
|
+
default:
|
|
2628
|
+
throw new Error(`Invalid month pattern: ${token}`);
|
|
2627
2629
|
}
|
|
2628
2630
|
break;
|
|
2629
2631
|
case "d":
|
|
2630
|
-
|
|
2632
|
+
result.date = parseInt(value, 10);
|
|
2631
2633
|
break;
|
|
2632
2634
|
case "H":
|
|
2633
|
-
|
|
2635
|
+
result.hours = parseInt(value, 10);
|
|
2634
2636
|
break;
|
|
2635
2637
|
case "h":
|
|
2636
|
-
|
|
2638
|
+
hourValue = parseInt(value, 10);
|
|
2637
2639
|
is12Hour = true;
|
|
2638
2640
|
break;
|
|
2639
2641
|
case "m":
|
|
2640
|
-
|
|
2642
|
+
result.minutes = parseInt(value, 10);
|
|
2641
2643
|
break;
|
|
2642
2644
|
case "s":
|
|
2643
|
-
|
|
2645
|
+
result.seconds = parseInt(value, 10);
|
|
2644
2646
|
break;
|
|
2645
2647
|
case "S":
|
|
2646
2648
|
let ms = parseInt(value, 10);
|
|
2647
2649
|
if (token.length === 1) ms *= 100;
|
|
2648
2650
|
else if (token.length === 2) ms *= 10;
|
|
2649
|
-
|
|
2651
|
+
result.milliseconds = ms;
|
|
2650
2652
|
break;
|
|
2651
2653
|
case "a":
|
|
2652
2654
|
isPM = value.toLowerCase().includes("p");
|
|
2653
2655
|
break;
|
|
2654
2656
|
}
|
|
2655
2657
|
});
|
|
2656
|
-
if (is12Hour &&
|
|
2657
|
-
|
|
2658
|
-
|
|
2659
|
-
|
|
2660
|
-
|
|
2661
|
-
|
|
2662
|
-
|
|
2663
|
-
|
|
2664
|
-
|
|
2665
|
-
if (
|
|
2666
|
-
|
|
2667
|
-
|
|
2668
|
-
|
|
2669
|
-
|
|
2670
|
-
|
|
2671
|
-
|
|
2672
|
-
|
|
2673
|
-
|
|
2674
|
-
|
|
2675
|
-
|
|
2676
|
-
}
|
|
2658
|
+
if (is12Hour && hourValue !== void 0) {
|
|
2659
|
+
if (isPM && hourValue < 12) {
|
|
2660
|
+
result.hours = hourValue + 12;
|
|
2661
|
+
} else if (!isPM && hourValue === 12) {
|
|
2662
|
+
result.hours = 0;
|
|
2663
|
+
} else {
|
|
2664
|
+
result.hours = hourValue;
|
|
2665
|
+
}
|
|
2666
|
+
}
|
|
2667
|
+
if (typeof result.year === "number" && !isValidYear(result.year)) {
|
|
2668
|
+
throw new Error(`Invalid year in date string: ${dateString}`);
|
|
2669
|
+
}
|
|
2670
|
+
if (typeof result.month === "number" && !isValidMonth(result.month)) {
|
|
2671
|
+
throw new Error(`Invalid month in date string: ${dateString}`);
|
|
2672
|
+
}
|
|
2673
|
+
if (typeof result.date === "number" && !isValidDayOfMonth(result.date)) {
|
|
2674
|
+
throw new Error(`Invalid day in date string: ${dateString}`);
|
|
2675
|
+
}
|
|
2676
|
+
if (typeof result.hours === "number" && !isValidHour(result.hours)) {
|
|
2677
|
+
throw new Error(`Invalid hour in date string: ${dateString}`);
|
|
2678
|
+
}
|
|
2679
|
+
if (typeof result.minutes === "number" && !isValidMinute(result.minutes)) {
|
|
2680
|
+
throw new Error(`Invalid minute in date string: ${dateString}`);
|
|
2681
|
+
}
|
|
2682
|
+
if (typeof result.seconds === "number" && !isValidSecond(result.seconds)) {
|
|
2683
|
+
throw new Error(`Invalid second in date string: ${dateString}`);
|
|
2684
|
+
}
|
|
2685
|
+
if (typeof result.milliseconds === "number" && !isValidMillisecond(result.milliseconds)) {
|
|
2686
|
+
throw new Error(`Invalid millisecond in date string: ${dateString}`);
|
|
2687
|
+
}
|
|
2688
|
+
return result;
|
|
2677
2689
|
}
|
|
2678
2690
|
|
|
2679
2691
|
// src/Logger.ts
|