chronos-date 1.0.2 → 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/dist/basic-DIp_pxR2.cjs +450 -0
- package/dist/basic-Dputob9z.mjs +366 -0
- package/dist/constants.cjs +27 -1
- package/dist/constants.mjs +5 -1
- package/dist/convert-CNVL-mop.mjs +22 -0
- package/dist/convert-dQuGWhVm.cjs +33 -0
- package/dist/greet-B8ZtypgG.cjs +40 -0
- package/dist/greet-B9j4afzD.mjs +35 -0
- package/dist/guards.cjs +119 -1
- package/dist/guards.mjs +110 -1
- package/dist/helpers-CrMc9iJZ.mjs +185 -0
- package/dist/helpers-M_SfAbAv.cjs +262 -0
- package/dist/index.cjs +1461 -1
- package/dist/index.mjs +1458 -1
- package/dist/non-primitives-CT4vszrX.cjs +52 -0
- package/dist/non-primitives-DrQfPj1r.mjs +22 -0
- package/dist/plugins/banglaPlugin.cjs +121 -1
- package/dist/plugins/banglaPlugin.mjs +120 -1
- package/dist/plugins/businessPlugin.cjs +118 -1
- package/dist/plugins/businessPlugin.mjs +117 -1
- package/dist/plugins/dateRangePlugin.cjs +46 -1
- package/dist/plugins/dateRangePlugin.mjs +45 -1
- package/dist/plugins/dayPartPlugin.cjs +26 -1
- package/dist/plugins/dayPartPlugin.mjs +25 -1
- package/dist/plugins/durationPlugin.cjs +91 -1
- package/dist/plugins/durationPlugin.mjs +90 -1
- package/dist/plugins/fromNowPlugin.cjs +80 -1
- package/dist/plugins/fromNowPlugin.mjs +79 -1
- package/dist/plugins/greetingPlugin.cjs +19 -1
- package/dist/plugins/greetingPlugin.mjs +18 -1
- package/dist/plugins/palindromePlugin.cjs +22 -1
- package/dist/plugins/palindromePlugin.mjs +21 -1
- package/dist/plugins/relativeTimePlugin.cjs +74 -1
- package/dist/plugins/relativeTimePlugin.mjs +73 -1
- package/dist/plugins/roundPlugin.cjs +69 -1
- package/dist/plugins/roundPlugin.mjs +68 -1
- package/dist/plugins/seasonPlugin.cjs +31 -1
- package/dist/plugins/seasonPlugin.mjs +30 -1
- package/dist/plugins/timeZonePlugin.cjs +139 -1
- package/dist/plugins/timeZonePlugin.mjs +138 -1
- package/dist/plugins/zodiacPlugin.cjs +51 -1
- package/dist/plugins/zodiacPlugin.mjs +50 -1
- package/dist/primitives-BME3_QrA.cjs +49 -0
- package/dist/primitives-DEt6I7Sy.mjs +19 -0
- package/dist/seasons-CHnjrIKK.mjs +352 -0
- package/dist/seasons-CcZicoe1.cjs +418 -0
- package/dist/timezone-BfBXeMK_.mjs +5573 -0
- package/dist/timezone-DYVJJYKz.cjs +5609 -0
- package/dist/types.mjs +1 -1
- package/dist/utilities-BU4MSYS7.mjs +43 -0
- package/dist/utilities-CGL7F6R9.cjs +72 -0
- package/dist/utilities-CtOZhIYv.mjs +241 -0
- package/dist/utilities-bXwTPJaD.cjs +318 -0
- package/dist/utils.cjs +76 -1
- package/dist/utils.mjs +44 -1
- package/package.json +1 -1
- package/dist/basic-CKxaRSHQ.cjs +0 -1
- package/dist/basic-e46DaNAi.mjs +0 -1
- package/dist/convert-Bmp63ats.mjs +0 -1
- package/dist/convert-DrLrcgqz.cjs +0 -1
- package/dist/greet-BBsrvmkn.mjs +0 -1
- package/dist/greet-C-6mruI9.cjs +0 -1
- package/dist/helpers-DGzYnP81.cjs +0 -1
- package/dist/helpers-N1X_Rj_V.mjs +0 -1
- package/dist/non-primitives-B2EE6D6s.mjs +0 -1
- package/dist/non-primitives-Bu3a4WL4.cjs +0 -1
- package/dist/primitives-Cxss_JVF.mjs +0 -1
- package/dist/primitives-Db2FUp4e.cjs +0 -1
- package/dist/seasons-ChAIVphi.mjs +0 -1
- package/dist/seasons-oABOhHcX.cjs +0 -1
- package/dist/timezone-B10UItNO.mjs +0 -1
- package/dist/timezone-CWxbK_7I.cjs +0 -1
- package/dist/utilities-B8dOAQVD.cjs +0 -1
- package/dist/utilities-BJE06bms.mjs +0 -1
- package/dist/utilities-D2-p26DX.cjs +0 -1
- package/dist/utilities-DV_ohS37.mjs +0 -1
package/dist/guards.mjs
CHANGED
|
@@ -1 +1,110 @@
|
|
|
1
|
-
import{n as
|
|
1
|
+
import { n as NATIVE_TZ_IDS, t as IANA_TZ_IDS } from "./timezone-BfBXeMK_.mjs";
|
|
2
|
+
import { n as isNonEmptyString, t as isBoolean } from "./primitives-DEt6I7Sy.mjs";
|
|
3
|
+
import { r as isObject, t as isFunction } from "./non-primitives-DrQfPj1r.mjs";
|
|
4
|
+
import { a as isNumericString, n as normalizeNumber } from "./utilities-BU4MSYS7.mjs";
|
|
5
|
+
|
|
6
|
+
//#region src/guards.ts
|
|
7
|
+
/**
|
|
8
|
+
* * Type guard to check if a value is a Date object.
|
|
9
|
+
* @param value - The value to check.
|
|
10
|
+
* @returns `true` if the value is a Date object, otherwise `false`.
|
|
11
|
+
*/
|
|
12
|
+
function isDate(value) {
|
|
13
|
+
return value instanceof Date;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* * Type guard to check if a value is a valid date string.
|
|
17
|
+
* @param value - The value to check.
|
|
18
|
+
* @returns `true` if the value is a valid date string, otherwise `false`.
|
|
19
|
+
*/
|
|
20
|
+
function isDateString(value) {
|
|
21
|
+
return isNonEmptyString(value) && !isNaN(Date.parse(value));
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* * Checks if the provided value is a valid time string in "HH:MM" format.
|
|
25
|
+
*
|
|
26
|
+
* @param value - The value to check.
|
|
27
|
+
* @returns `true` if the value is a valid time string, `false` otherwise.
|
|
28
|
+
*/
|
|
29
|
+
function isValidTime(value) {
|
|
30
|
+
if (!isNonEmptyString(value)) return false;
|
|
31
|
+
const [hourStr, minuteStr] = value.split(":");
|
|
32
|
+
if (!isNumericString(hourStr) || !isNumericString(minuteStr)) return false;
|
|
33
|
+
const hour = Number(hourStr);
|
|
34
|
+
const minute = Number(minuteStr);
|
|
35
|
+
return hour >= 0 && hour <= 23 && minute >= 0 && minute <= 59;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* * Checks if the provided value is a valid `UTCOffset` (e.g. `UTC-01:30`).
|
|
39
|
+
*
|
|
40
|
+
* @param value - The value to check.
|
|
41
|
+
* @returns `true` if the value is a valid utc offset, `false` otherwise.
|
|
42
|
+
*/
|
|
43
|
+
function isValidUTCOffset(value) {
|
|
44
|
+
return isNonEmptyString(value) ? /^UTC[+-]?\d{1,2}:\d{2}$/.test(value) : false;
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* * Validates whether the provided value is a recognized IANA time zone identifier (excluding `"Factory"`), based on the {@link https://en.wikipedia.org/wiki/List_of_tz_database_time_zones IANA TZ Database}.
|
|
48
|
+
*
|
|
49
|
+
* @remarks
|
|
50
|
+
* - Relies on a large constant map of time zone identifiers, which can increase bundle size in browser environments. Matches against `597` identifiers.
|
|
51
|
+
* - Prefer {@link isNativeTimeZoneId} when you want a lightweight, native-only validation approach. Matches against `418` identifiers.
|
|
52
|
+
*
|
|
53
|
+
* @param value Time zone identifier to validate.
|
|
54
|
+
* @returns `true` if the value is a valid IANA time zone identifier, otherwise `false`.
|
|
55
|
+
*/
|
|
56
|
+
function isValidTimeZoneId(value) {
|
|
57
|
+
return isNonEmptyString(value) ? new Set([...IANA_TZ_IDS]).has(value) : false;
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* * Validates whether the provided value is a supported time zone identifier using the native JavaScript API (`Intl.supportedValuesOf('timeZone')`).
|
|
61
|
+
*
|
|
62
|
+
* @remarks
|
|
63
|
+
* - Uses only native {@link Intl} capabilities—minimal code footprint, highly performant. Matches against `418` identifiers.
|
|
64
|
+
* - Prefer {@link isValidTimeZoneId} when validation must align strictly with the full
|
|
65
|
+
* {@link https://en.wikipedia.org/wiki/List_of_tz_database_time_zones IANA TZ Database}. Matches against `597` identifiers.
|
|
66
|
+
*
|
|
67
|
+
* @param value Time zone identifier to validate.
|
|
68
|
+
* @returns `true` if the value is a valid native JS-supported time zone identifier, otherwise `false`.
|
|
69
|
+
*/
|
|
70
|
+
function isNativeTimeZoneId(value) {
|
|
71
|
+
return isNonEmptyString(value) ? new Set(NATIVE_TZ_IDS).has(value) : false;
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* * Checks if the year is a leap year.
|
|
75
|
+
*
|
|
76
|
+
* - A year is a leap year if it is divisible by 4, but not divisible by 100, unless it is also divisible by 400.
|
|
77
|
+
* - For example, 2000 and 2400 are leap years, but 1900 and 2100 are not.
|
|
78
|
+
* @param year The year to check.
|
|
79
|
+
* @returns `true` if the year is a leap year, `false` otherwise.
|
|
80
|
+
*/
|
|
81
|
+
function isLeapYear(year) {
|
|
82
|
+
const $year = normalizeNumber(year);
|
|
83
|
+
return $year ? $year % 4 === 0 && $year % 100 !== 0 || $year % 400 === 0 : false;
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* * Checks if a value is a date-like object from `Date`, `Chronos`, `Moment.js`, `Day.js`, `Luxon`, `JS-Joda`, or `Temporal`
|
|
87
|
+
* @param value Value to check if it is date-like object.
|
|
88
|
+
* @returns `true` if the value is date-like object, otherwise `false`.
|
|
89
|
+
*/
|
|
90
|
+
function isDateLike(value) {
|
|
91
|
+
if (value instanceof Date) return true;
|
|
92
|
+
if (isObject(value)) {
|
|
93
|
+
if (isFunction(value.format) && isFunction(value.toJSON) && isFunction(value.toISOString)) return true;
|
|
94
|
+
if (isFunction(value.toISO) && isFunction(value.toFormat) && isBoolean(value.isValid)) return true;
|
|
95
|
+
if (isFunction(value.plus) && isFunction(value.minus) && isFunction(value.equals) && isFunction(value.getClass)) return true;
|
|
96
|
+
if (isFunction(value.toJSON) && isFunction(value.toString) && [
|
|
97
|
+
"PlainDate",
|
|
98
|
+
"ZonedDateTime",
|
|
99
|
+
"Instant"
|
|
100
|
+
].includes(value.constructor?.name ?? "")) return true;
|
|
101
|
+
}
|
|
102
|
+
return false;
|
|
103
|
+
}
|
|
104
|
+
/** Checks if a value represents time value (number) with different forms of {@link TimeWithUnit units} */
|
|
105
|
+
function isTimeWithUnit(value) {
|
|
106
|
+
return isNonEmptyString(value) && /^-?\d*\.?\d+ *(milliseconds?|msecs?|ms|seconds?|secs?|s|minutes?|mins?|m|hours?|hrs?|h|days?|d|weeks?|w|months?|mo|years?|yrs?|y)?$/i.test(value);
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
//#endregion
|
|
110
|
+
export { isDate, isDateLike, isDateString, isLeapYear, isNativeTimeZoneId, isTimeWithUnit, isValidTime, isValidTimeZoneId, isValidUTCOffset };
|
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
import { c as MONTHS, d as SORTED_TIME_FORMATS, i as BN_SEASONS, o as DAYS, r as BN_MONTH_TABLES, u as MS_PER_DAY } from "./basic-Dputob9z.mjs";
|
|
2
|
+
import { i as isString, n as isNonEmptyString } from "./primitives-DEt6I7Sy.mjs";
|
|
3
|
+
import { a as isValidArray, i as isObjectWithKeys } from "./non-primitives-DrQfPj1r.mjs";
|
|
4
|
+
import { t as getOrdinal } from "./utilities-BU4MSYS7.mjs";
|
|
5
|
+
import { isDate, isDateString, isLeapYear, isValidUTCOffset } from "./guards.mjs";
|
|
6
|
+
|
|
7
|
+
//#region src/utils/helpers.ts
|
|
8
|
+
/** Core formatting logic shared by `formatDate` and `Chronos`, `BanglaCalendar` classes */
|
|
9
|
+
function _formatDateCore(format, dateComponents) {
|
|
10
|
+
const tokenRegex = new RegExp(`^(${SORTED_TIME_FORMATS.join("|")})`);
|
|
11
|
+
let result = "";
|
|
12
|
+
let i = 0;
|
|
13
|
+
while (i < format.length) {
|
|
14
|
+
if (format[i] === "[") {
|
|
15
|
+
const end = format.indexOf("]", i);
|
|
16
|
+
if (end !== -1) {
|
|
17
|
+
result += format.slice(i + 1, end);
|
|
18
|
+
i = end + 1;
|
|
19
|
+
continue;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
const match = tokenRegex.exec(format.slice(i));
|
|
23
|
+
if (match) {
|
|
24
|
+
result += dateComponents[match[0]] ?? match[0];
|
|
25
|
+
i += match[0].length;
|
|
26
|
+
} else {
|
|
27
|
+
result += format[i];
|
|
28
|
+
i++;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
return result;
|
|
32
|
+
}
|
|
33
|
+
/** Core formatting logic shared by `formatDate` and `Chronos` class */
|
|
34
|
+
function _formatDate(format, year, month, day, date, hours, minutes, seconds, milliseconds, offset) {
|
|
35
|
+
const paddedYear = _padZero(year, 4);
|
|
36
|
+
return _formatDateCore(format, {
|
|
37
|
+
YYYY: paddedYear,
|
|
38
|
+
YY: paddedYear.slice(-2),
|
|
39
|
+
yyyy: paddedYear,
|
|
40
|
+
yy: paddedYear.slice(-2),
|
|
41
|
+
M: String(month + 1),
|
|
42
|
+
MM: _padZero(month + 1),
|
|
43
|
+
mmm: MONTHS[month].slice(0, 3),
|
|
44
|
+
mmmm: MONTHS[month],
|
|
45
|
+
d: DAYS[day].slice(0, 2),
|
|
46
|
+
dd: DAYS[day].slice(0, 3),
|
|
47
|
+
ddd: DAYS[day],
|
|
48
|
+
D: String(date),
|
|
49
|
+
DD: _padZero(date),
|
|
50
|
+
Do: getOrdinal(date),
|
|
51
|
+
H: String(hours),
|
|
52
|
+
HH: _padZero(hours),
|
|
53
|
+
h: String(hours % 12 || 12),
|
|
54
|
+
hh: _padZero(hours % 12 || 12),
|
|
55
|
+
m: String(minutes),
|
|
56
|
+
mm: _padZero(minutes),
|
|
57
|
+
s: String(seconds),
|
|
58
|
+
ss: _padZero(seconds),
|
|
59
|
+
ms: String(milliseconds),
|
|
60
|
+
mss: _padZero(milliseconds, 3),
|
|
61
|
+
a: hours < 12 ? "am" : "pm",
|
|
62
|
+
A: hours < 12 ? "AM" : "PM",
|
|
63
|
+
Z: offset,
|
|
64
|
+
ZZ: offset
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
/** Normalize a time string by adding offset at the end */
|
|
68
|
+
function _normalizeOffset(timeStr) {
|
|
69
|
+
return timeStr.replace(/([+-]\d{2})(?!:)/, "$1:00");
|
|
70
|
+
}
|
|
71
|
+
/** Resolve `timeZoneName` value from `Intl.DateTimeFormat` */
|
|
72
|
+
function _resolveNativeTzName(tzId, type, date) {
|
|
73
|
+
try {
|
|
74
|
+
return new Intl.DateTimeFormat("en", {
|
|
75
|
+
timeZone: tzId,
|
|
76
|
+
timeZoneName: type
|
|
77
|
+
}).formatToParts(date).find((p) => p.type === "timeZoneName")?.value;
|
|
78
|
+
} catch {
|
|
79
|
+
return;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
/** Convert `GMT±HH:mm` string to `UTC±HH:mm` format*/
|
|
83
|
+
function _gmtToUtcOffset(gmt) {
|
|
84
|
+
return gmt === "GMT" ? "UTC+00:00" : gmt?.replace(/^GMT/, "UTC");
|
|
85
|
+
}
|
|
86
|
+
/** Get Bangla season name by month index (`0-11`) */
|
|
87
|
+
function _getBnSeason(month, locale) {
|
|
88
|
+
const season = BN_SEASONS[Math.floor(month / 2)];
|
|
89
|
+
return locale === "en" ? season.en : season.bn;
|
|
90
|
+
}
|
|
91
|
+
/** Check whether a Bangla year is leap by Gregorian and Bangla years and calendar variant */
|
|
92
|
+
function _isBnLeapYear(by, gy, v) {
|
|
93
|
+
return v === "revised-1966" ? by % 4 === 2 : isLeapYear(gy);
|
|
94
|
+
}
|
|
95
|
+
/** Extract selective unit values from {@link Date} object */
|
|
96
|
+
function _extractDateUnits(date) {
|
|
97
|
+
const month = date.getMonth();
|
|
98
|
+
return {
|
|
99
|
+
gy: date.getFullYear(),
|
|
100
|
+
$gm: month,
|
|
101
|
+
gm: month + 1,
|
|
102
|
+
gd: date.getDate(),
|
|
103
|
+
wd: date.getDay()
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
/** Get Gregorian base year from {@link Date} object for Bangla year */
|
|
107
|
+
function _getGregBaseYear(date) {
|
|
108
|
+
const { gy, gm, gd } = _extractDateUnits(date);
|
|
109
|
+
return gm < 4 || gm === 4 && gd < 14 ? gy - 1 : gy;
|
|
110
|
+
}
|
|
111
|
+
/** Get Bangla year from {@link Date} object */
|
|
112
|
+
function _getBnYear(date) {
|
|
113
|
+
return _getGregBaseYear(date) - 593;
|
|
114
|
+
}
|
|
115
|
+
/** Get timestamp in milliseconds between midnight, January 1, 1970 (UTC) and the specified {@link Date} object */
|
|
116
|
+
function _getUtcTs(date) {
|
|
117
|
+
const { gy, $gm, gd } = _extractDateUnits(date);
|
|
118
|
+
return Date.UTC(gy, $gm, gd);
|
|
119
|
+
}
|
|
120
|
+
/** Get number of days elapsed since midnight April 14, 1970 (UTC) for specific {@link Date} */
|
|
121
|
+
function _getElapsedDays(date) {
|
|
122
|
+
return Math.floor((_getUtcTs(date) - Date.UTC(_getGregBaseYear(date), 3, 14)) / MS_PER_DAY);
|
|
123
|
+
}
|
|
124
|
+
/** Get number of days elapsed since midnight April 14, 1970 (UTC) and month index for specific `Date` and Bangla calendar variant */
|
|
125
|
+
function _bnDaysMonthIdx(date, variant) {
|
|
126
|
+
const v = variant ?? "revised-2019";
|
|
127
|
+
const table = _isBnLeapYear(_getBnYear(date), date.getFullYear(), v) ? BN_MONTH_TABLES?.[v].leap : BN_MONTH_TABLES?.[v].normal;
|
|
128
|
+
let days = _getElapsedDays(date);
|
|
129
|
+
let monthIdx = 0;
|
|
130
|
+
while (days >= table[monthIdx]) {
|
|
131
|
+
days -= table[monthIdx];
|
|
132
|
+
monthIdx++;
|
|
133
|
+
}
|
|
134
|
+
return {
|
|
135
|
+
days,
|
|
136
|
+
monthIdx
|
|
137
|
+
};
|
|
138
|
+
}
|
|
139
|
+
/**
|
|
140
|
+
* Convert number to string and pad at the start with zero (`'0'`)
|
|
141
|
+
* @param value Value to convert and pad with
|
|
142
|
+
* @param length Maximum length to pad, default is `2`
|
|
143
|
+
* @returns The padded string
|
|
144
|
+
*/
|
|
145
|
+
function _padZero(value, length = 2) {
|
|
146
|
+
return String(value).padStart(length, "0");
|
|
147
|
+
}
|
|
148
|
+
/**
|
|
149
|
+
* Pad at the start of a string with Bangla zero (`'০'`)
|
|
150
|
+
* @param str String to pad with
|
|
151
|
+
* @param length Maximum length to pad, default is `2`
|
|
152
|
+
* @returns The padded string
|
|
153
|
+
*/
|
|
154
|
+
function _padShunno(str, length = 2) {
|
|
155
|
+
return str.padStart(length, "০");
|
|
156
|
+
}
|
|
157
|
+
/**
|
|
158
|
+
* Convert a string, number, or `Date` object to a `Date` object.
|
|
159
|
+
* - If the input is already a `Date`, it is returned as is.
|
|
160
|
+
* - If it's a string, it is parsed into a `Date`.
|
|
161
|
+
* - If it's a number or undefined, it is treated as a timestamp and converted to a `Date`.
|
|
162
|
+
* @param value The date input to convert, which can be a `Date` object, a date string, a timestamp number, or undefined (which defaults to the current date and time).
|
|
163
|
+
* @returns A `Date` object representing the input date.
|
|
164
|
+
*/
|
|
165
|
+
function _dateArgsToDate(value) {
|
|
166
|
+
return isDate(value) ? value : new Date(isString(value) ? value.replace(/['"]/g, "") : value ?? Date.now());
|
|
167
|
+
}
|
|
168
|
+
/**
|
|
169
|
+
* Type guard to check if a value has the necessary properties to be reconstructed into a `Chronos` instance.
|
|
170
|
+
* - Validates that the value is an object with the required keys and that the `native` property is a valid date or date string, and that the `utcOffset` is valid.
|
|
171
|
+
* @param value The value to check for reconstructability.
|
|
172
|
+
* @returns `true` if the value has the required properties for reconstruction, otherwise `false`.
|
|
173
|
+
*/
|
|
174
|
+
function _hasChronosProperties(value) {
|
|
175
|
+
return isObjectWithKeys(value, [
|
|
176
|
+
"origin",
|
|
177
|
+
"native",
|
|
178
|
+
"utcOffset",
|
|
179
|
+
"timeZoneName",
|
|
180
|
+
"timeZoneId"
|
|
181
|
+
]) && isNonEmptyString(value.origin) && (isDate(value.native) || isDateString(value.native)) && isValidUTCOffset(value.utcOffset) && isNonEmptyString(value.timeZoneName) && (isNonEmptyString(value.timeZoneId) || isValidArray(value.timeZoneId));
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
//#endregion
|
|
185
|
+
export { _getBnSeason as a, _hasChronosProperties as c, _padShunno as d, _padZero as f, _formatDateCore as i, _isBnLeapYear as l, _dateArgsToDate as n, _getBnYear as o, _resolveNativeTzName as p, _formatDate as r, _gmtToUtcOffset as s, _bnDaysMonthIdx as t, _normalizeOffset as u };
|
|
@@ -0,0 +1,262 @@
|
|
|
1
|
+
const require_basic = require('./basic-DIp_pxR2.cjs');
|
|
2
|
+
const require_primitives = require('./primitives-BME3_QrA.cjs');
|
|
3
|
+
const require_non_primitives = require('./non-primitives-CT4vszrX.cjs');
|
|
4
|
+
const require_utilities = require('./utilities-CGL7F6R9.cjs');
|
|
5
|
+
const require_guards = require('./guards.cjs');
|
|
6
|
+
|
|
7
|
+
//#region src/utils/helpers.ts
|
|
8
|
+
/** Core formatting logic shared by `formatDate` and `Chronos`, `BanglaCalendar` classes */
|
|
9
|
+
function _formatDateCore(format, dateComponents) {
|
|
10
|
+
const tokenRegex = new RegExp(`^(${require_basic.SORTED_TIME_FORMATS.join("|")})`);
|
|
11
|
+
let result = "";
|
|
12
|
+
let i = 0;
|
|
13
|
+
while (i < format.length) {
|
|
14
|
+
if (format[i] === "[") {
|
|
15
|
+
const end = format.indexOf("]", i);
|
|
16
|
+
if (end !== -1) {
|
|
17
|
+
result += format.slice(i + 1, end);
|
|
18
|
+
i = end + 1;
|
|
19
|
+
continue;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
const match = tokenRegex.exec(format.slice(i));
|
|
23
|
+
if (match) {
|
|
24
|
+
result += dateComponents[match[0]] ?? match[0];
|
|
25
|
+
i += match[0].length;
|
|
26
|
+
} else {
|
|
27
|
+
result += format[i];
|
|
28
|
+
i++;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
return result;
|
|
32
|
+
}
|
|
33
|
+
/** Core formatting logic shared by `formatDate` and `Chronos` class */
|
|
34
|
+
function _formatDate(format, year, month, day, date, hours, minutes, seconds, milliseconds, offset) {
|
|
35
|
+
const paddedYear = _padZero(year, 4);
|
|
36
|
+
return _formatDateCore(format, {
|
|
37
|
+
YYYY: paddedYear,
|
|
38
|
+
YY: paddedYear.slice(-2),
|
|
39
|
+
yyyy: paddedYear,
|
|
40
|
+
yy: paddedYear.slice(-2),
|
|
41
|
+
M: String(month + 1),
|
|
42
|
+
MM: _padZero(month + 1),
|
|
43
|
+
mmm: require_basic.MONTHS[month].slice(0, 3),
|
|
44
|
+
mmmm: require_basic.MONTHS[month],
|
|
45
|
+
d: require_basic.DAYS[day].slice(0, 2),
|
|
46
|
+
dd: require_basic.DAYS[day].slice(0, 3),
|
|
47
|
+
ddd: require_basic.DAYS[day],
|
|
48
|
+
D: String(date),
|
|
49
|
+
DD: _padZero(date),
|
|
50
|
+
Do: require_utilities.getOrdinal(date),
|
|
51
|
+
H: String(hours),
|
|
52
|
+
HH: _padZero(hours),
|
|
53
|
+
h: String(hours % 12 || 12),
|
|
54
|
+
hh: _padZero(hours % 12 || 12),
|
|
55
|
+
m: String(minutes),
|
|
56
|
+
mm: _padZero(minutes),
|
|
57
|
+
s: String(seconds),
|
|
58
|
+
ss: _padZero(seconds),
|
|
59
|
+
ms: String(milliseconds),
|
|
60
|
+
mss: _padZero(milliseconds, 3),
|
|
61
|
+
a: hours < 12 ? "am" : "pm",
|
|
62
|
+
A: hours < 12 ? "AM" : "PM",
|
|
63
|
+
Z: offset,
|
|
64
|
+
ZZ: offset
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
/** Normalize a time string by adding offset at the end */
|
|
68
|
+
function _normalizeOffset(timeStr) {
|
|
69
|
+
return timeStr.replace(/([+-]\d{2})(?!:)/, "$1:00");
|
|
70
|
+
}
|
|
71
|
+
/** Resolve `timeZoneName` value from `Intl.DateTimeFormat` */
|
|
72
|
+
function _resolveNativeTzName(tzId, type, date) {
|
|
73
|
+
try {
|
|
74
|
+
return new Intl.DateTimeFormat("en", {
|
|
75
|
+
timeZone: tzId,
|
|
76
|
+
timeZoneName: type
|
|
77
|
+
}).formatToParts(date).find((p) => p.type === "timeZoneName")?.value;
|
|
78
|
+
} catch {
|
|
79
|
+
return;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
/** Convert `GMT±HH:mm` string to `UTC±HH:mm` format*/
|
|
83
|
+
function _gmtToUtcOffset(gmt) {
|
|
84
|
+
return gmt === "GMT" ? "UTC+00:00" : gmt?.replace(/^GMT/, "UTC");
|
|
85
|
+
}
|
|
86
|
+
/** Get Bangla season name by month index (`0-11`) */
|
|
87
|
+
function _getBnSeason(month, locale) {
|
|
88
|
+
const season = require_basic.BN_SEASONS[Math.floor(month / 2)];
|
|
89
|
+
return locale === "en" ? season.en : season.bn;
|
|
90
|
+
}
|
|
91
|
+
/** Check whether a Bangla year is leap by Gregorian and Bangla years and calendar variant */
|
|
92
|
+
function _isBnLeapYear(by, gy, v) {
|
|
93
|
+
return v === "revised-1966" ? by % 4 === 2 : require_guards.isLeapYear(gy);
|
|
94
|
+
}
|
|
95
|
+
/** Extract selective unit values from {@link Date} object */
|
|
96
|
+
function _extractDateUnits(date) {
|
|
97
|
+
const month = date.getMonth();
|
|
98
|
+
return {
|
|
99
|
+
gy: date.getFullYear(),
|
|
100
|
+
$gm: month,
|
|
101
|
+
gm: month + 1,
|
|
102
|
+
gd: date.getDate(),
|
|
103
|
+
wd: date.getDay()
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
/** Get Gregorian base year from {@link Date} object for Bangla year */
|
|
107
|
+
function _getGregBaseYear(date) {
|
|
108
|
+
const { gy, gm, gd } = _extractDateUnits(date);
|
|
109
|
+
return gm < 4 || gm === 4 && gd < 14 ? gy - 1 : gy;
|
|
110
|
+
}
|
|
111
|
+
/** Get Bangla year from {@link Date} object */
|
|
112
|
+
function _getBnYear(date) {
|
|
113
|
+
return _getGregBaseYear(date) - 593;
|
|
114
|
+
}
|
|
115
|
+
/** Get timestamp in milliseconds between midnight, January 1, 1970 (UTC) and the specified {@link Date} object */
|
|
116
|
+
function _getUtcTs(date) {
|
|
117
|
+
const { gy, $gm, gd } = _extractDateUnits(date);
|
|
118
|
+
return Date.UTC(gy, $gm, gd);
|
|
119
|
+
}
|
|
120
|
+
/** Get number of days elapsed since midnight April 14, 1970 (UTC) for specific {@link Date} */
|
|
121
|
+
function _getElapsedDays(date) {
|
|
122
|
+
return Math.floor((_getUtcTs(date) - Date.UTC(_getGregBaseYear(date), 3, 14)) / require_basic.MS_PER_DAY);
|
|
123
|
+
}
|
|
124
|
+
/** Get number of days elapsed since midnight April 14, 1970 (UTC) and month index for specific `Date` and Bangla calendar variant */
|
|
125
|
+
function _bnDaysMonthIdx(date, variant) {
|
|
126
|
+
const v = variant ?? "revised-2019";
|
|
127
|
+
const table = _isBnLeapYear(_getBnYear(date), date.getFullYear(), v) ? require_basic.BN_MONTH_TABLES?.[v].leap : require_basic.BN_MONTH_TABLES?.[v].normal;
|
|
128
|
+
let days = _getElapsedDays(date);
|
|
129
|
+
let monthIdx = 0;
|
|
130
|
+
while (days >= table[monthIdx]) {
|
|
131
|
+
days -= table[monthIdx];
|
|
132
|
+
monthIdx++;
|
|
133
|
+
}
|
|
134
|
+
return {
|
|
135
|
+
days,
|
|
136
|
+
monthIdx
|
|
137
|
+
};
|
|
138
|
+
}
|
|
139
|
+
/**
|
|
140
|
+
* Convert number to string and pad at the start with zero (`'0'`)
|
|
141
|
+
* @param value Value to convert and pad with
|
|
142
|
+
* @param length Maximum length to pad, default is `2`
|
|
143
|
+
* @returns The padded string
|
|
144
|
+
*/
|
|
145
|
+
function _padZero(value, length = 2) {
|
|
146
|
+
return String(value).padStart(length, "0");
|
|
147
|
+
}
|
|
148
|
+
/**
|
|
149
|
+
* Pad at the start of a string with Bangla zero (`'০'`)
|
|
150
|
+
* @param str String to pad with
|
|
151
|
+
* @param length Maximum length to pad, default is `2`
|
|
152
|
+
* @returns The padded string
|
|
153
|
+
*/
|
|
154
|
+
function _padShunno(str, length = 2) {
|
|
155
|
+
return str.padStart(length, "০");
|
|
156
|
+
}
|
|
157
|
+
/**
|
|
158
|
+
* Convert a string, number, or `Date` object to a `Date` object.
|
|
159
|
+
* - If the input is already a `Date`, it is returned as is.
|
|
160
|
+
* - If it's a string, it is parsed into a `Date`.
|
|
161
|
+
* - If it's a number or undefined, it is treated as a timestamp and converted to a `Date`.
|
|
162
|
+
* @param value The date input to convert, which can be a `Date` object, a date string, a timestamp number, or undefined (which defaults to the current date and time).
|
|
163
|
+
* @returns A `Date` object representing the input date.
|
|
164
|
+
*/
|
|
165
|
+
function _dateArgsToDate(value) {
|
|
166
|
+
return require_guards.isDate(value) ? value : new Date(require_primitives.isString(value) ? value.replace(/['"]/g, "") : value ?? Date.now());
|
|
167
|
+
}
|
|
168
|
+
/**
|
|
169
|
+
* Type guard to check if a value has the necessary properties to be reconstructed into a `Chronos` instance.
|
|
170
|
+
* - Validates that the value is an object with the required keys and that the `native` property is a valid date or date string, and that the `utcOffset` is valid.
|
|
171
|
+
* @param value The value to check for reconstructability.
|
|
172
|
+
* @returns `true` if the value has the required properties for reconstruction, otherwise `false`.
|
|
173
|
+
*/
|
|
174
|
+
function _hasChronosProperties(value) {
|
|
175
|
+
return require_non_primitives.isObjectWithKeys(value, [
|
|
176
|
+
"origin",
|
|
177
|
+
"native",
|
|
178
|
+
"utcOffset",
|
|
179
|
+
"timeZoneName",
|
|
180
|
+
"timeZoneId"
|
|
181
|
+
]) && require_primitives.isNonEmptyString(value.origin) && (require_guards.isDate(value.native) || require_guards.isDateString(value.native)) && require_guards.isValidUTCOffset(value.utcOffset) && require_primitives.isNonEmptyString(value.timeZoneName) && (require_primitives.isNonEmptyString(value.timeZoneId) || require_non_primitives.isValidArray(value.timeZoneId));
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
//#endregion
|
|
185
|
+
Object.defineProperty(exports, '_bnDaysMonthIdx', {
|
|
186
|
+
enumerable: true,
|
|
187
|
+
get: function () {
|
|
188
|
+
return _bnDaysMonthIdx;
|
|
189
|
+
}
|
|
190
|
+
});
|
|
191
|
+
Object.defineProperty(exports, '_dateArgsToDate', {
|
|
192
|
+
enumerable: true,
|
|
193
|
+
get: function () {
|
|
194
|
+
return _dateArgsToDate;
|
|
195
|
+
}
|
|
196
|
+
});
|
|
197
|
+
Object.defineProperty(exports, '_formatDate', {
|
|
198
|
+
enumerable: true,
|
|
199
|
+
get: function () {
|
|
200
|
+
return _formatDate;
|
|
201
|
+
}
|
|
202
|
+
});
|
|
203
|
+
Object.defineProperty(exports, '_formatDateCore', {
|
|
204
|
+
enumerable: true,
|
|
205
|
+
get: function () {
|
|
206
|
+
return _formatDateCore;
|
|
207
|
+
}
|
|
208
|
+
});
|
|
209
|
+
Object.defineProperty(exports, '_getBnSeason', {
|
|
210
|
+
enumerable: true,
|
|
211
|
+
get: function () {
|
|
212
|
+
return _getBnSeason;
|
|
213
|
+
}
|
|
214
|
+
});
|
|
215
|
+
Object.defineProperty(exports, '_getBnYear', {
|
|
216
|
+
enumerable: true,
|
|
217
|
+
get: function () {
|
|
218
|
+
return _getBnYear;
|
|
219
|
+
}
|
|
220
|
+
});
|
|
221
|
+
Object.defineProperty(exports, '_gmtToUtcOffset', {
|
|
222
|
+
enumerable: true,
|
|
223
|
+
get: function () {
|
|
224
|
+
return _gmtToUtcOffset;
|
|
225
|
+
}
|
|
226
|
+
});
|
|
227
|
+
Object.defineProperty(exports, '_hasChronosProperties', {
|
|
228
|
+
enumerable: true,
|
|
229
|
+
get: function () {
|
|
230
|
+
return _hasChronosProperties;
|
|
231
|
+
}
|
|
232
|
+
});
|
|
233
|
+
Object.defineProperty(exports, '_isBnLeapYear', {
|
|
234
|
+
enumerable: true,
|
|
235
|
+
get: function () {
|
|
236
|
+
return _isBnLeapYear;
|
|
237
|
+
}
|
|
238
|
+
});
|
|
239
|
+
Object.defineProperty(exports, '_normalizeOffset', {
|
|
240
|
+
enumerable: true,
|
|
241
|
+
get: function () {
|
|
242
|
+
return _normalizeOffset;
|
|
243
|
+
}
|
|
244
|
+
});
|
|
245
|
+
Object.defineProperty(exports, '_padShunno', {
|
|
246
|
+
enumerable: true,
|
|
247
|
+
get: function () {
|
|
248
|
+
return _padShunno;
|
|
249
|
+
}
|
|
250
|
+
});
|
|
251
|
+
Object.defineProperty(exports, '_padZero', {
|
|
252
|
+
enumerable: true,
|
|
253
|
+
get: function () {
|
|
254
|
+
return _padZero;
|
|
255
|
+
}
|
|
256
|
+
});
|
|
257
|
+
Object.defineProperty(exports, '_resolveNativeTzName', {
|
|
258
|
+
enumerable: true,
|
|
259
|
+
get: function () {
|
|
260
|
+
return _resolveNativeTzName;
|
|
261
|
+
}
|
|
262
|
+
});
|