chronos-date 1.0.2 → 1.1.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/dist/basic-DqKyujoj.mjs +382 -0
- package/dist/basic-DsQqC5nZ.cjs +466 -0
- package/dist/constants.cjs +43 -1
- package/dist/constants.d.cts +17 -1
- package/dist/constants.d.mts +17 -1
- package/dist/constants.mjs +21 -1
- package/dist/convert-DbgntRVW.cjs +49 -0
- package/dist/convert-mMaI4YP0.mjs +38 -0
- package/dist/greet-D07t2_e6.mjs +51 -0
- package/dist/greet-hjVsd2dt.cjs +56 -0
- package/dist/guards.cjs +140 -1
- package/dist/guards.d.cts +18 -2
- package/dist/guards.d.mts +18 -2
- package/dist/guards.mjs +131 -1
- package/dist/helpers-CQzi908i.cjs +278 -0
- package/dist/helpers-DEf4sevD.mjs +201 -0
- package/dist/index.cjs +1477 -1
- package/dist/index.d.cts +18 -2
- package/dist/index.d.mts +18 -2
- package/dist/index.mjs +1474 -1
- package/dist/non-primitives-D9zxCwX8.cjs +68 -0
- package/dist/non-primitives-DBtomDty.mjs +38 -0
- package/dist/plugins/banglaPlugin.cjs +137 -1
- package/dist/plugins/banglaPlugin.d.cts +19 -3
- package/dist/plugins/banglaPlugin.d.mts +19 -3
- package/dist/plugins/banglaPlugin.mjs +136 -1
- package/dist/plugins/businessPlugin.cjs +134 -1
- package/dist/plugins/businessPlugin.d.cts +17 -1
- package/dist/plugins/businessPlugin.d.mts +17 -1
- package/dist/plugins/businessPlugin.mjs +133 -1
- package/dist/plugins/dateRangePlugin.cjs +62 -1
- package/dist/plugins/dateRangePlugin.d.cts +19 -3
- package/dist/plugins/dateRangePlugin.d.mts +19 -3
- package/dist/plugins/dateRangePlugin.mjs +61 -1
- package/dist/plugins/dayPartPlugin.cjs +42 -1
- package/dist/plugins/dayPartPlugin.d.cts +17 -1
- package/dist/plugins/dayPartPlugin.d.mts +17 -1
- package/dist/plugins/dayPartPlugin.mjs +41 -1
- package/dist/plugins/durationPlugin.cjs +107 -1
- package/dist/plugins/durationPlugin.d.cts +17 -1
- package/dist/plugins/durationPlugin.d.mts +17 -1
- package/dist/plugins/durationPlugin.mjs +106 -1
- package/dist/plugins/fromNowPlugin.cjs +96 -1
- package/dist/plugins/fromNowPlugin.d.cts +17 -1
- package/dist/plugins/fromNowPlugin.d.mts +17 -1
- package/dist/plugins/fromNowPlugin.mjs +95 -1
- package/dist/plugins/greetingPlugin.cjs +35 -1
- package/dist/plugins/greetingPlugin.d.cts +17 -1
- package/dist/plugins/greetingPlugin.d.mts +17 -1
- package/dist/plugins/greetingPlugin.mjs +34 -1
- package/dist/plugins/palindromePlugin.cjs +38 -1
- package/dist/plugins/palindromePlugin.d.cts +17 -1
- package/dist/plugins/palindromePlugin.d.mts +17 -1
- package/dist/plugins/palindromePlugin.mjs +37 -1
- package/dist/plugins/relativeTimePlugin.cjs +90 -1
- package/dist/plugins/relativeTimePlugin.d.cts +17 -1
- package/dist/plugins/relativeTimePlugin.d.mts +17 -1
- package/dist/plugins/relativeTimePlugin.mjs +89 -1
- package/dist/plugins/roundPlugin.cjs +85 -1
- package/dist/plugins/roundPlugin.d.cts +17 -1
- package/dist/plugins/roundPlugin.d.mts +17 -1
- package/dist/plugins/roundPlugin.mjs +84 -1
- package/dist/plugins/seasonPlugin.cjs +47 -1
- package/dist/plugins/seasonPlugin.d.cts +17 -1
- package/dist/plugins/seasonPlugin.d.mts +17 -1
- package/dist/plugins/seasonPlugin.mjs +46 -1
- package/dist/plugins/timeZonePlugin.cjs +155 -1
- package/dist/plugins/timeZonePlugin.d.cts +17 -1
- package/dist/plugins/timeZonePlugin.d.mts +17 -1
- package/dist/plugins/timeZonePlugin.mjs +154 -1
- package/dist/plugins/zodiacPlugin.cjs +67 -1
- package/dist/plugins/zodiacPlugin.d.cts +17 -1
- package/dist/plugins/zodiacPlugin.d.mts +17 -1
- package/dist/plugins/zodiacPlugin.mjs +66 -1
- package/dist/primitives-CtuSNmV7.cjs +65 -0
- package/dist/primitives-dXzXlzJw.mjs +35 -0
- package/dist/seasons-B7kK88zq.cjs +434 -0
- package/dist/{seasons-CioTH5Dm.d.mts → seasons-B_kjFWIX.d.mts} +16 -0
- package/dist/{seasons-CsVeJ27s.d.cts → seasons-CpUn45n6.d.cts} +16 -0
- package/dist/seasons-Cq3ah3pV.mjs +368 -0
- package/dist/timezone-BNnHFgki.cjs +5625 -0
- package/dist/timezone-Db2CeL32.mjs +5589 -0
- package/dist/{types-B2fgrJ86.d.mts → types-Cb4gxrgJ.d.mts} +29 -15
- package/dist/{types-DPTQ_yLx.d.cts → types-CdmsXbU_.d.cts} +29 -15
- package/dist/types.cjs +16 -0
- package/dist/types.d.cts +18 -2
- package/dist/types.d.mts +18 -2
- package/dist/types.mjs +17 -1
- package/dist/utilities-BGX8dviZ.mjs +257 -0
- package/dist/utilities-BKtVo78T.mjs +59 -0
- package/dist/utilities-CpF3uys3.cjs +88 -0
- package/dist/utilities-D47SN9EZ.cjs +334 -0
- package/dist/utils.cjs +92 -1
- package/dist/utils.d.cts +20 -4
- package/dist/utils.d.mts +20 -4
- package/dist/utils.mjs +60 -1
- package/package.json +10 -4
- 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
|
@@ -0,0 +1,278 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright 2026 - present Nazmul Hassan
|
|
3
|
+
*
|
|
4
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
* you may not use this file except in compliance with the License.
|
|
6
|
+
* You may obtain a copy of the License at
|
|
7
|
+
*
|
|
8
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
*
|
|
10
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
* See the License for the specific language governing permissions and
|
|
14
|
+
* limitations under the License.
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
const require_basic = require('./basic-DsQqC5nZ.cjs');
|
|
18
|
+
const require_primitives = require('./primitives-CtuSNmV7.cjs');
|
|
19
|
+
const require_non_primitives = require('./non-primitives-D9zxCwX8.cjs');
|
|
20
|
+
const require_utilities = require('./utilities-CpF3uys3.cjs');
|
|
21
|
+
const require_guards = require('./guards.cjs');
|
|
22
|
+
|
|
23
|
+
//#region src/utils/helpers.ts
|
|
24
|
+
/** Core formatting logic shared by `formatDate` and `Chronos`, `BanglaCalendar` classes */
|
|
25
|
+
function _formatDateCore(format, dateComponents) {
|
|
26
|
+
const tokenRegex = new RegExp(`^(${require_basic.SORTED_TIME_FORMATS.join("|")})`);
|
|
27
|
+
let result = "";
|
|
28
|
+
let i = 0;
|
|
29
|
+
while (i < format.length) {
|
|
30
|
+
if (format[i] === "[") {
|
|
31
|
+
const end = format.indexOf("]", i);
|
|
32
|
+
if (end !== -1) {
|
|
33
|
+
result += format.slice(i + 1, end);
|
|
34
|
+
i = end + 1;
|
|
35
|
+
continue;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
const match = tokenRegex.exec(format.slice(i));
|
|
39
|
+
if (match) {
|
|
40
|
+
result += dateComponents[match[0]] ?? match[0];
|
|
41
|
+
i += match[0].length;
|
|
42
|
+
} else {
|
|
43
|
+
result += format[i];
|
|
44
|
+
i++;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
return result;
|
|
48
|
+
}
|
|
49
|
+
/** Core formatting logic shared by `formatDate` and `Chronos` class */
|
|
50
|
+
function _formatDate(format, year, month, day, date, hours, minutes, seconds, milliseconds, offset) {
|
|
51
|
+
const paddedYear = _padZero(year, 4);
|
|
52
|
+
return _formatDateCore(format, {
|
|
53
|
+
YYYY: paddedYear,
|
|
54
|
+
YY: paddedYear.slice(-2),
|
|
55
|
+
yyyy: paddedYear,
|
|
56
|
+
yy: paddedYear.slice(-2),
|
|
57
|
+
M: String(month + 1),
|
|
58
|
+
MM: _padZero(month + 1),
|
|
59
|
+
mmm: require_basic.MONTHS[month].slice(0, 3),
|
|
60
|
+
mmmm: require_basic.MONTHS[month],
|
|
61
|
+
d: require_basic.DAYS[day].slice(0, 2),
|
|
62
|
+
dd: require_basic.DAYS[day].slice(0, 3),
|
|
63
|
+
ddd: require_basic.DAYS[day],
|
|
64
|
+
D: String(date),
|
|
65
|
+
DD: _padZero(date),
|
|
66
|
+
Do: require_utilities.getOrdinal(date),
|
|
67
|
+
H: String(hours),
|
|
68
|
+
HH: _padZero(hours),
|
|
69
|
+
h: String(hours % 12 || 12),
|
|
70
|
+
hh: _padZero(hours % 12 || 12),
|
|
71
|
+
m: String(minutes),
|
|
72
|
+
mm: _padZero(minutes),
|
|
73
|
+
s: String(seconds),
|
|
74
|
+
ss: _padZero(seconds),
|
|
75
|
+
ms: String(milliseconds),
|
|
76
|
+
mss: _padZero(milliseconds, 3),
|
|
77
|
+
a: hours < 12 ? "am" : "pm",
|
|
78
|
+
A: hours < 12 ? "AM" : "PM",
|
|
79
|
+
Z: offset,
|
|
80
|
+
ZZ: offset
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
/** Normalize a time string by adding offset at the end */
|
|
84
|
+
function _normalizeOffset(timeStr) {
|
|
85
|
+
return timeStr.replace(/([+-]\d{2})(?!:)/, "$1:00");
|
|
86
|
+
}
|
|
87
|
+
/** Resolve `timeZoneName` value from `Intl.DateTimeFormat` */
|
|
88
|
+
function _resolveNativeTzName(tzId, type, date) {
|
|
89
|
+
try {
|
|
90
|
+
return new Intl.DateTimeFormat("en", {
|
|
91
|
+
timeZone: tzId,
|
|
92
|
+
timeZoneName: type
|
|
93
|
+
}).formatToParts(date).find((p) => p.type === "timeZoneName")?.value;
|
|
94
|
+
} catch {
|
|
95
|
+
return;
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
/** Convert `GMT±HH:mm` string to `UTC±HH:mm` format*/
|
|
99
|
+
function _gmtToUtcOffset(gmt) {
|
|
100
|
+
return gmt === "GMT" ? "UTC+00:00" : gmt?.replace(/^GMT/, "UTC");
|
|
101
|
+
}
|
|
102
|
+
/** Get Bangla season name by month index (`0-11`) */
|
|
103
|
+
function _getBnSeason(month, locale) {
|
|
104
|
+
const season = require_basic.BN_SEASONS[Math.floor(month / 2)];
|
|
105
|
+
return locale === "en" ? season.en : season.bn;
|
|
106
|
+
}
|
|
107
|
+
/** Check whether a Bangla year is leap by Gregorian and Bangla years and calendar variant */
|
|
108
|
+
function _isBnLeapYear(by, gy, v) {
|
|
109
|
+
return v === "revised-1966" ? by % 4 === 2 : require_guards.isLeapYear(gy);
|
|
110
|
+
}
|
|
111
|
+
/** Extract selective unit values from {@link Date} object */
|
|
112
|
+
function _extractDateUnits(date) {
|
|
113
|
+
const month = date.getMonth();
|
|
114
|
+
return {
|
|
115
|
+
gy: date.getFullYear(),
|
|
116
|
+
$gm: month,
|
|
117
|
+
gm: month + 1,
|
|
118
|
+
gd: date.getDate(),
|
|
119
|
+
wd: date.getDay()
|
|
120
|
+
};
|
|
121
|
+
}
|
|
122
|
+
/** Get Gregorian base year from {@link Date} object for Bangla year */
|
|
123
|
+
function _getGregBaseYear(date) {
|
|
124
|
+
const { gy, gm, gd } = _extractDateUnits(date);
|
|
125
|
+
return gm < 4 || gm === 4 && gd < 14 ? gy - 1 : gy;
|
|
126
|
+
}
|
|
127
|
+
/** Get Bangla year from {@link Date} object */
|
|
128
|
+
function _getBnYear(date) {
|
|
129
|
+
return _getGregBaseYear(date) - 593;
|
|
130
|
+
}
|
|
131
|
+
/** Get timestamp in milliseconds between midnight, January 1, 1970 (UTC) and the specified {@link Date} object */
|
|
132
|
+
function _getUtcTs(date) {
|
|
133
|
+
const { gy, $gm, gd } = _extractDateUnits(date);
|
|
134
|
+
return Date.UTC(gy, $gm, gd);
|
|
135
|
+
}
|
|
136
|
+
/** Get number of days elapsed since midnight April 14, 1970 (UTC) for specific {@link Date} */
|
|
137
|
+
function _getElapsedDays(date) {
|
|
138
|
+
return Math.floor((_getUtcTs(date) - Date.UTC(_getGregBaseYear(date), 3, 14)) / require_basic.MS_PER_DAY);
|
|
139
|
+
}
|
|
140
|
+
/** Get number of days elapsed since midnight April 14, 1970 (UTC) and month index for specific `Date` and Bangla calendar variant */
|
|
141
|
+
function _bnDaysMonthIdx(date, variant) {
|
|
142
|
+
const v = variant ?? "revised-2019";
|
|
143
|
+
const table = _isBnLeapYear(_getBnYear(date), date.getFullYear(), v) ? require_basic.BN_MONTH_TABLES?.[v].leap : require_basic.BN_MONTH_TABLES?.[v].normal;
|
|
144
|
+
let days = _getElapsedDays(date);
|
|
145
|
+
let monthIdx = 0;
|
|
146
|
+
while (days >= table[monthIdx]) {
|
|
147
|
+
days -= table[monthIdx];
|
|
148
|
+
monthIdx++;
|
|
149
|
+
}
|
|
150
|
+
return {
|
|
151
|
+
days,
|
|
152
|
+
monthIdx
|
|
153
|
+
};
|
|
154
|
+
}
|
|
155
|
+
/**
|
|
156
|
+
* Convert number to string and pad at the start with zero (`'0'`)
|
|
157
|
+
* @param value Value to convert and pad with
|
|
158
|
+
* @param length Maximum length to pad, default is `2`
|
|
159
|
+
* @returns The padded string
|
|
160
|
+
*/
|
|
161
|
+
function _padZero(value, length = 2) {
|
|
162
|
+
return String(value).padStart(length, "0");
|
|
163
|
+
}
|
|
164
|
+
/**
|
|
165
|
+
* Pad at the start of a string with Bangla zero (`'০'`)
|
|
166
|
+
* @param str String to pad with
|
|
167
|
+
* @param length Maximum length to pad, default is `2`
|
|
168
|
+
* @returns The padded string
|
|
169
|
+
*/
|
|
170
|
+
function _padShunno(str, length = 2) {
|
|
171
|
+
return str.padStart(length, "০");
|
|
172
|
+
}
|
|
173
|
+
/**
|
|
174
|
+
* Convert a string, number, or `Date` object to a `Date` object.
|
|
175
|
+
* - If the input is already a `Date`, it is returned as is.
|
|
176
|
+
* - If it's a string, it is parsed into a `Date`.
|
|
177
|
+
* - If it's a number or undefined, it is treated as a timestamp and converted to a `Date`.
|
|
178
|
+
* @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).
|
|
179
|
+
* @returns A `Date` object representing the input date.
|
|
180
|
+
*/
|
|
181
|
+
function _dateArgsToDate(value) {
|
|
182
|
+
return require_guards.isDate(value) ? value : new Date(require_primitives.isString(value) ? value.replace(/['"]/g, "") : value ?? Date.now());
|
|
183
|
+
}
|
|
184
|
+
/**
|
|
185
|
+
* Type guard to check if a value has the necessary properties to be reconstructed into a `Chronos` instance.
|
|
186
|
+
* - 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.
|
|
187
|
+
* @param value The value to check for reconstructability.
|
|
188
|
+
* @returns `true` if the value has the required properties for reconstruction, otherwise `false`.
|
|
189
|
+
*/
|
|
190
|
+
function _hasChronosProperties(value) {
|
|
191
|
+
return require_non_primitives.isObjectWithKeys(value, [
|
|
192
|
+
"origin",
|
|
193
|
+
"native",
|
|
194
|
+
"utcOffset",
|
|
195
|
+
"timeZoneName",
|
|
196
|
+
"timeZoneId"
|
|
197
|
+
]) && 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));
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
//#endregion
|
|
201
|
+
Object.defineProperty(exports, '_bnDaysMonthIdx', {
|
|
202
|
+
enumerable: true,
|
|
203
|
+
get: function () {
|
|
204
|
+
return _bnDaysMonthIdx;
|
|
205
|
+
}
|
|
206
|
+
});
|
|
207
|
+
Object.defineProperty(exports, '_dateArgsToDate', {
|
|
208
|
+
enumerable: true,
|
|
209
|
+
get: function () {
|
|
210
|
+
return _dateArgsToDate;
|
|
211
|
+
}
|
|
212
|
+
});
|
|
213
|
+
Object.defineProperty(exports, '_formatDate', {
|
|
214
|
+
enumerable: true,
|
|
215
|
+
get: function () {
|
|
216
|
+
return _formatDate;
|
|
217
|
+
}
|
|
218
|
+
});
|
|
219
|
+
Object.defineProperty(exports, '_formatDateCore', {
|
|
220
|
+
enumerable: true,
|
|
221
|
+
get: function () {
|
|
222
|
+
return _formatDateCore;
|
|
223
|
+
}
|
|
224
|
+
});
|
|
225
|
+
Object.defineProperty(exports, '_getBnSeason', {
|
|
226
|
+
enumerable: true,
|
|
227
|
+
get: function () {
|
|
228
|
+
return _getBnSeason;
|
|
229
|
+
}
|
|
230
|
+
});
|
|
231
|
+
Object.defineProperty(exports, '_getBnYear', {
|
|
232
|
+
enumerable: true,
|
|
233
|
+
get: function () {
|
|
234
|
+
return _getBnYear;
|
|
235
|
+
}
|
|
236
|
+
});
|
|
237
|
+
Object.defineProperty(exports, '_gmtToUtcOffset', {
|
|
238
|
+
enumerable: true,
|
|
239
|
+
get: function () {
|
|
240
|
+
return _gmtToUtcOffset;
|
|
241
|
+
}
|
|
242
|
+
});
|
|
243
|
+
Object.defineProperty(exports, '_hasChronosProperties', {
|
|
244
|
+
enumerable: true,
|
|
245
|
+
get: function () {
|
|
246
|
+
return _hasChronosProperties;
|
|
247
|
+
}
|
|
248
|
+
});
|
|
249
|
+
Object.defineProperty(exports, '_isBnLeapYear', {
|
|
250
|
+
enumerable: true,
|
|
251
|
+
get: function () {
|
|
252
|
+
return _isBnLeapYear;
|
|
253
|
+
}
|
|
254
|
+
});
|
|
255
|
+
Object.defineProperty(exports, '_normalizeOffset', {
|
|
256
|
+
enumerable: true,
|
|
257
|
+
get: function () {
|
|
258
|
+
return _normalizeOffset;
|
|
259
|
+
}
|
|
260
|
+
});
|
|
261
|
+
Object.defineProperty(exports, '_padShunno', {
|
|
262
|
+
enumerable: true,
|
|
263
|
+
get: function () {
|
|
264
|
+
return _padShunno;
|
|
265
|
+
}
|
|
266
|
+
});
|
|
267
|
+
Object.defineProperty(exports, '_padZero', {
|
|
268
|
+
enumerable: true,
|
|
269
|
+
get: function () {
|
|
270
|
+
return _padZero;
|
|
271
|
+
}
|
|
272
|
+
});
|
|
273
|
+
Object.defineProperty(exports, '_resolveNativeTzName', {
|
|
274
|
+
enumerable: true,
|
|
275
|
+
get: function () {
|
|
276
|
+
return _resolveNativeTzName;
|
|
277
|
+
}
|
|
278
|
+
});
|
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright 2026 - present Nazmul Hassan
|
|
3
|
+
*
|
|
4
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
* you may not use this file except in compliance with the License.
|
|
6
|
+
* You may obtain a copy of the License at
|
|
7
|
+
*
|
|
8
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
*
|
|
10
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
* See the License for the specific language governing permissions and
|
|
14
|
+
* limitations under the License.
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
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-DqKyujoj.mjs";
|
|
18
|
+
import { i as isString, n as isNonEmptyString } from "./primitives-dXzXlzJw.mjs";
|
|
19
|
+
import { a as isValidArray, i as isObjectWithKeys } from "./non-primitives-DBtomDty.mjs";
|
|
20
|
+
import { t as getOrdinal } from "./utilities-BKtVo78T.mjs";
|
|
21
|
+
import { isDate, isDateString, isLeapYear, isValidUTCOffset } from "./guards.mjs";
|
|
22
|
+
|
|
23
|
+
//#region src/utils/helpers.ts
|
|
24
|
+
/** Core formatting logic shared by `formatDate` and `Chronos`, `BanglaCalendar` classes */
|
|
25
|
+
function _formatDateCore(format, dateComponents) {
|
|
26
|
+
const tokenRegex = new RegExp(`^(${SORTED_TIME_FORMATS.join("|")})`);
|
|
27
|
+
let result = "";
|
|
28
|
+
let i = 0;
|
|
29
|
+
while (i < format.length) {
|
|
30
|
+
if (format[i] === "[") {
|
|
31
|
+
const end = format.indexOf("]", i);
|
|
32
|
+
if (end !== -1) {
|
|
33
|
+
result += format.slice(i + 1, end);
|
|
34
|
+
i = end + 1;
|
|
35
|
+
continue;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
const match = tokenRegex.exec(format.slice(i));
|
|
39
|
+
if (match) {
|
|
40
|
+
result += dateComponents[match[0]] ?? match[0];
|
|
41
|
+
i += match[0].length;
|
|
42
|
+
} else {
|
|
43
|
+
result += format[i];
|
|
44
|
+
i++;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
return result;
|
|
48
|
+
}
|
|
49
|
+
/** Core formatting logic shared by `formatDate` and `Chronos` class */
|
|
50
|
+
function _formatDate(format, year, month, day, date, hours, minutes, seconds, milliseconds, offset) {
|
|
51
|
+
const paddedYear = _padZero(year, 4);
|
|
52
|
+
return _formatDateCore(format, {
|
|
53
|
+
YYYY: paddedYear,
|
|
54
|
+
YY: paddedYear.slice(-2),
|
|
55
|
+
yyyy: paddedYear,
|
|
56
|
+
yy: paddedYear.slice(-2),
|
|
57
|
+
M: String(month + 1),
|
|
58
|
+
MM: _padZero(month + 1),
|
|
59
|
+
mmm: MONTHS[month].slice(0, 3),
|
|
60
|
+
mmmm: MONTHS[month],
|
|
61
|
+
d: DAYS[day].slice(0, 2),
|
|
62
|
+
dd: DAYS[day].slice(0, 3),
|
|
63
|
+
ddd: DAYS[day],
|
|
64
|
+
D: String(date),
|
|
65
|
+
DD: _padZero(date),
|
|
66
|
+
Do: getOrdinal(date),
|
|
67
|
+
H: String(hours),
|
|
68
|
+
HH: _padZero(hours),
|
|
69
|
+
h: String(hours % 12 || 12),
|
|
70
|
+
hh: _padZero(hours % 12 || 12),
|
|
71
|
+
m: String(minutes),
|
|
72
|
+
mm: _padZero(minutes),
|
|
73
|
+
s: String(seconds),
|
|
74
|
+
ss: _padZero(seconds),
|
|
75
|
+
ms: String(milliseconds),
|
|
76
|
+
mss: _padZero(milliseconds, 3),
|
|
77
|
+
a: hours < 12 ? "am" : "pm",
|
|
78
|
+
A: hours < 12 ? "AM" : "PM",
|
|
79
|
+
Z: offset,
|
|
80
|
+
ZZ: offset
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
/** Normalize a time string by adding offset at the end */
|
|
84
|
+
function _normalizeOffset(timeStr) {
|
|
85
|
+
return timeStr.replace(/([+-]\d{2})(?!:)/, "$1:00");
|
|
86
|
+
}
|
|
87
|
+
/** Resolve `timeZoneName` value from `Intl.DateTimeFormat` */
|
|
88
|
+
function _resolveNativeTzName(tzId, type, date) {
|
|
89
|
+
try {
|
|
90
|
+
return new Intl.DateTimeFormat("en", {
|
|
91
|
+
timeZone: tzId,
|
|
92
|
+
timeZoneName: type
|
|
93
|
+
}).formatToParts(date).find((p) => p.type === "timeZoneName")?.value;
|
|
94
|
+
} catch {
|
|
95
|
+
return;
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
/** Convert `GMT±HH:mm` string to `UTC±HH:mm` format*/
|
|
99
|
+
function _gmtToUtcOffset(gmt) {
|
|
100
|
+
return gmt === "GMT" ? "UTC+00:00" : gmt?.replace(/^GMT/, "UTC");
|
|
101
|
+
}
|
|
102
|
+
/** Get Bangla season name by month index (`0-11`) */
|
|
103
|
+
function _getBnSeason(month, locale) {
|
|
104
|
+
const season = BN_SEASONS[Math.floor(month / 2)];
|
|
105
|
+
return locale === "en" ? season.en : season.bn;
|
|
106
|
+
}
|
|
107
|
+
/** Check whether a Bangla year is leap by Gregorian and Bangla years and calendar variant */
|
|
108
|
+
function _isBnLeapYear(by, gy, v) {
|
|
109
|
+
return v === "revised-1966" ? by % 4 === 2 : isLeapYear(gy);
|
|
110
|
+
}
|
|
111
|
+
/** Extract selective unit values from {@link Date} object */
|
|
112
|
+
function _extractDateUnits(date) {
|
|
113
|
+
const month = date.getMonth();
|
|
114
|
+
return {
|
|
115
|
+
gy: date.getFullYear(),
|
|
116
|
+
$gm: month,
|
|
117
|
+
gm: month + 1,
|
|
118
|
+
gd: date.getDate(),
|
|
119
|
+
wd: date.getDay()
|
|
120
|
+
};
|
|
121
|
+
}
|
|
122
|
+
/** Get Gregorian base year from {@link Date} object for Bangla year */
|
|
123
|
+
function _getGregBaseYear(date) {
|
|
124
|
+
const { gy, gm, gd } = _extractDateUnits(date);
|
|
125
|
+
return gm < 4 || gm === 4 && gd < 14 ? gy - 1 : gy;
|
|
126
|
+
}
|
|
127
|
+
/** Get Bangla year from {@link Date} object */
|
|
128
|
+
function _getBnYear(date) {
|
|
129
|
+
return _getGregBaseYear(date) - 593;
|
|
130
|
+
}
|
|
131
|
+
/** Get timestamp in milliseconds between midnight, January 1, 1970 (UTC) and the specified {@link Date} object */
|
|
132
|
+
function _getUtcTs(date) {
|
|
133
|
+
const { gy, $gm, gd } = _extractDateUnits(date);
|
|
134
|
+
return Date.UTC(gy, $gm, gd);
|
|
135
|
+
}
|
|
136
|
+
/** Get number of days elapsed since midnight April 14, 1970 (UTC) for specific {@link Date} */
|
|
137
|
+
function _getElapsedDays(date) {
|
|
138
|
+
return Math.floor((_getUtcTs(date) - Date.UTC(_getGregBaseYear(date), 3, 14)) / MS_PER_DAY);
|
|
139
|
+
}
|
|
140
|
+
/** Get number of days elapsed since midnight April 14, 1970 (UTC) and month index for specific `Date` and Bangla calendar variant */
|
|
141
|
+
function _bnDaysMonthIdx(date, variant) {
|
|
142
|
+
const v = variant ?? "revised-2019";
|
|
143
|
+
const table = _isBnLeapYear(_getBnYear(date), date.getFullYear(), v) ? BN_MONTH_TABLES?.[v].leap : BN_MONTH_TABLES?.[v].normal;
|
|
144
|
+
let days = _getElapsedDays(date);
|
|
145
|
+
let monthIdx = 0;
|
|
146
|
+
while (days >= table[monthIdx]) {
|
|
147
|
+
days -= table[monthIdx];
|
|
148
|
+
monthIdx++;
|
|
149
|
+
}
|
|
150
|
+
return {
|
|
151
|
+
days,
|
|
152
|
+
monthIdx
|
|
153
|
+
};
|
|
154
|
+
}
|
|
155
|
+
/**
|
|
156
|
+
* Convert number to string and pad at the start with zero (`'0'`)
|
|
157
|
+
* @param value Value to convert and pad with
|
|
158
|
+
* @param length Maximum length to pad, default is `2`
|
|
159
|
+
* @returns The padded string
|
|
160
|
+
*/
|
|
161
|
+
function _padZero(value, length = 2) {
|
|
162
|
+
return String(value).padStart(length, "0");
|
|
163
|
+
}
|
|
164
|
+
/**
|
|
165
|
+
* Pad at the start of a string with Bangla zero (`'০'`)
|
|
166
|
+
* @param str String to pad with
|
|
167
|
+
* @param length Maximum length to pad, default is `2`
|
|
168
|
+
* @returns The padded string
|
|
169
|
+
*/
|
|
170
|
+
function _padShunno(str, length = 2) {
|
|
171
|
+
return str.padStart(length, "০");
|
|
172
|
+
}
|
|
173
|
+
/**
|
|
174
|
+
* Convert a string, number, or `Date` object to a `Date` object.
|
|
175
|
+
* - If the input is already a `Date`, it is returned as is.
|
|
176
|
+
* - If it's a string, it is parsed into a `Date`.
|
|
177
|
+
* - If it's a number or undefined, it is treated as a timestamp and converted to a `Date`.
|
|
178
|
+
* @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).
|
|
179
|
+
* @returns A `Date` object representing the input date.
|
|
180
|
+
*/
|
|
181
|
+
function _dateArgsToDate(value) {
|
|
182
|
+
return isDate(value) ? value : new Date(isString(value) ? value.replace(/['"]/g, "") : value ?? Date.now());
|
|
183
|
+
}
|
|
184
|
+
/**
|
|
185
|
+
* Type guard to check if a value has the necessary properties to be reconstructed into a `Chronos` instance.
|
|
186
|
+
* - 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.
|
|
187
|
+
* @param value The value to check for reconstructability.
|
|
188
|
+
* @returns `true` if the value has the required properties for reconstruction, otherwise `false`.
|
|
189
|
+
*/
|
|
190
|
+
function _hasChronosProperties(value) {
|
|
191
|
+
return isObjectWithKeys(value, [
|
|
192
|
+
"origin",
|
|
193
|
+
"native",
|
|
194
|
+
"utcOffset",
|
|
195
|
+
"timeZoneName",
|
|
196
|
+
"timeZoneId"
|
|
197
|
+
]) && isNonEmptyString(value.origin) && (isDate(value.native) || isDateString(value.native)) && isValidUTCOffset(value.utcOffset) && isNonEmptyString(value.timeZoneName) && (isNonEmptyString(value.timeZoneId) || isValidArray(value.timeZoneId));
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
//#endregion
|
|
201
|
+
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 };
|