a2bei4-utils 1.0.2 → 1.0.4
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/LICENSE +21 -21
- package/README.md +2 -2
- package/dist/a2bei4.utils.cjs.js +2080 -1846
- package/dist/a2bei4.utils.cjs.js.map +1 -1
- package/dist/a2bei4.utils.cjs.min.js +1 -1
- package/dist/a2bei4.utils.cjs.min.js.map +1 -1
- package/dist/a2bei4.utils.esm.js +2073 -1843
- package/dist/a2bei4.utils.esm.js.map +1 -1
- package/dist/a2bei4.utils.esm.min.js +1 -1
- package/dist/a2bei4.utils.esm.min.js.map +1 -1
- package/dist/a2bei4.utils.umd.js +2080 -1846
- package/dist/a2bei4.utils.umd.js.map +1 -1
- package/dist/a2bei4.utils.umd.min.js +1 -1
- package/dist/a2bei4.utils.umd.min.js.map +1 -1
- package/dist/arr.cjs +27 -27
- package/dist/arr.cjs.map +1 -1
- package/dist/arr.js +27 -27
- package/dist/arr.js.map +1 -1
- package/dist/audio.cjs +274 -274
- package/dist/audio.cjs.map +1 -1
- package/dist/audio.js +274 -274
- package/dist/audio.js.map +1 -1
- package/dist/browser.cjs +52 -52
- package/dist/browser.cjs.map +1 -1
- package/dist/browser.js +52 -52
- package/dist/browser.js.map +1 -1
- package/dist/common.cjs +369 -369
- package/dist/common.cjs.map +1 -1
- package/dist/common.js +369 -369
- package/dist/common.js.map +1 -1
- package/dist/date.cjs +421 -188
- package/dist/date.cjs.map +1 -1
- package/dist/date.js +414 -185
- package/dist/date.js.map +1 -1
- package/dist/download.cjs +102 -102
- package/dist/download.cjs.map +1 -1
- package/dist/download.js +102 -102
- package/dist/download.js.map +1 -1
- package/dist/evt.cjs +148 -148
- package/dist/evt.cjs.map +1 -1
- package/dist/evt.js +148 -148
- package/dist/evt.js.map +1 -1
- package/dist/id.cjs +68 -68
- package/dist/id.cjs.map +1 -1
- package/dist/id.js +68 -68
- package/dist/id.js.map +1 -1
- package/dist/timer.cjs +51 -50
- package/dist/timer.cjs.map +1 -1
- package/dist/timer.js +51 -50
- package/dist/timer.js.map +1 -1
- package/dist/tree.cjs +165 -165
- package/dist/tree.cjs.map +1 -1
- package/dist/tree.js +165 -165
- package/dist/tree.js.map +1 -1
- package/dist/webSocket.cjs +403 -403
- package/dist/webSocket.cjs.map +1 -1
- package/dist/webSocket.js +403 -403
- package/dist/webSocket.js.map +1 -1
- package/package.json +1 -1
- package/readme.txt +21 -11
- package/types/date.d.ts +243 -45
- package/types/index.d.ts +244 -47
package/dist/date.js
CHANGED
|
@@ -1,188 +1,417 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
if (
|
|
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
|
-
if (
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
const
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
const
|
|
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
|
-
|
|
1
|
+
function getLocales_TimePeriod() {
|
|
2
|
+
return {
|
|
3
|
+
earlyMorning: "凌晨",
|
|
4
|
+
morning: "上午",
|
|
5
|
+
noon: "中午",
|
|
6
|
+
afternoon: "下午",
|
|
7
|
+
evening: "晚上"
|
|
8
|
+
};
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* 将任意值安全转换为 Date 对象。
|
|
13
|
+
* - 数字/数字字符串:视为时间戳
|
|
14
|
+
* - 字符串:尝试按 ISO/RFC 格式解析
|
|
15
|
+
* - 对象:优先 valueOf(),再 toString()
|
|
16
|
+
* - null / undefined / 无效值:返回 null
|
|
17
|
+
*
|
|
18
|
+
* @param {*} val - 待转换值
|
|
19
|
+
* @returns {Date | null} 有效 Date 或 null
|
|
20
|
+
*/
|
|
21
|
+
function toDate(val) {
|
|
22
|
+
if (val == null) return null; // null / undefined
|
|
23
|
+
if (val instanceof Date) return isNaN(val) ? null : val; // 已是 Date,但需排除 Invalid Date
|
|
24
|
+
|
|
25
|
+
// 1. 数字或数字字符串 → 时间戳
|
|
26
|
+
if (typeof val === "number" || (typeof val === "string" && /^-?\d+(\.\d+)?$/.test(val.trim()))) {
|
|
27
|
+
const d = new Date(+val);
|
|
28
|
+
return isNaN(d) ? null : d;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
// 2. 标准 ISO 8601 / RFC 2825 等合法字符串
|
|
32
|
+
if (typeof val === "string") {
|
|
33
|
+
const d = new Date(val);
|
|
34
|
+
return isNaN(d) ? null : d; // 非法格式返回 null
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
// 3. 对象带 valueOf / toString
|
|
38
|
+
if (typeof val === "object") {
|
|
39
|
+
// 优先调用 valueOf(期望返回数字时间戳)
|
|
40
|
+
const prim = val.valueOf ? val.valueOf() : Object.prototype.valueOf.call(val);
|
|
41
|
+
if (typeof prim === "number" && !isNaN(prim)) {
|
|
42
|
+
const d = new Date(prim);
|
|
43
|
+
return isNaN(d) ? null : d;
|
|
44
|
+
}
|
|
45
|
+
// 兜底用字符串
|
|
46
|
+
const str = val.toString ? val.toString() : String(val);
|
|
47
|
+
const d = new Date(str);
|
|
48
|
+
return isNaN(d) ? null : d;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
// 4. 其余情况
|
|
52
|
+
return null;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* 在闭区间 [date1, date2] 内随机生成一个日期(含首尾)。
|
|
57
|
+
* 若顺序相反则自动交换。
|
|
58
|
+
*
|
|
59
|
+
* @param {Date} date1 - 起始日期
|
|
60
|
+
* @param {Date} date2 - 结束日期
|
|
61
|
+
* @returns {Date} 随机日期
|
|
62
|
+
*/
|
|
63
|
+
function randomDateInRange(date1, date2) {
|
|
64
|
+
let v1 = date1.getTime(),
|
|
65
|
+
v2 = date2.getTime();
|
|
66
|
+
if (v1 > v2) [v1, v2] = [v2, v1];
|
|
67
|
+
return new Date(v1 + Math.floor(Math.random() * (v2 - v1 + 1)));
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
//#region 持续时间相关
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* 时间持续对象(完整版本,包含年月日时分秒毫秒)
|
|
74
|
+
* @typedef {Object} DurationObject
|
|
75
|
+
* @property {number} years - 年数
|
|
76
|
+
* @property {number} months - 月数(0-11)
|
|
77
|
+
* @property {number} days - 天数(0-29,取决于 monthDays)
|
|
78
|
+
* @property {number} hours - 小时数(0-23)
|
|
79
|
+
* @property {number} minutes - 分钟数(0-59)
|
|
80
|
+
* @property {number} seconds - 秒数(0-59)
|
|
81
|
+
* @property {number} milliseconds - 毫秒数(0-999)
|
|
82
|
+
*/
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* 时间持续对象(最大单位为天)
|
|
86
|
+
* @typedef {Object} DurationMaxDayObject
|
|
87
|
+
* @property {number} days - 天数
|
|
88
|
+
* @property {number} hours - 小时数(0-23)
|
|
89
|
+
* @property {number} minutes - 分钟数(0-59)
|
|
90
|
+
* @property {number} seconds - 秒数(0-59)
|
|
91
|
+
* @property {number} milliseconds - 毫秒数(0-999)
|
|
92
|
+
*/
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* 时间持续对象(最大单位为小时)
|
|
96
|
+
* @typedef {Object} DurationMaxHourObject
|
|
97
|
+
* @property {number} hours - 小时数
|
|
98
|
+
* @property {number} minutes - 分钟数(0-59)
|
|
99
|
+
* @property {number} seconds - 秒数(0-59)
|
|
100
|
+
* @property {number} milliseconds - 毫秒数(0-999)
|
|
101
|
+
*/
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* 将毫秒转换为时间持续对象。
|
|
105
|
+
*
|
|
106
|
+
* @param {number} milliseconds - 毫秒数(非负整数)
|
|
107
|
+
* @param {Object} [options] - 选项对象。
|
|
108
|
+
* @param {number} [options.yearDays=365] - 一年的天数。
|
|
109
|
+
* @param {number} [options.monthDays=30] - 一月的天数。
|
|
110
|
+
* @returns {DurationObject} 时间持续对象
|
|
111
|
+
* @throws {TypeError} 当 milliseconds 不是有效数字
|
|
112
|
+
* @throws {RangeError} 当 milliseconds 为负数
|
|
113
|
+
* @throws {RangeError} 当 yearDays 或 monthDays 不是正整数
|
|
114
|
+
*
|
|
115
|
+
* @example
|
|
116
|
+
* // 基本用法
|
|
117
|
+
* millisecond2Duration(42070000500);
|
|
118
|
+
* // 返回: { years: 1, months: 4, days: 1, hours: 22, minutes: 6, seconds: 40, milliseconds: 500 }
|
|
119
|
+
*/
|
|
120
|
+
function millisecond2Duration(milliseconds, options = { yearDays: 365, monthDays: 30 }) {
|
|
121
|
+
// 参数验证
|
|
122
|
+
if (typeof milliseconds !== "number" || isNaN(milliseconds)) {
|
|
123
|
+
throw new TypeError("milliseconds must be a valid number");
|
|
124
|
+
}
|
|
125
|
+
if (milliseconds < 0) {
|
|
126
|
+
throw new RangeError("milliseconds must be a non-negative number");
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
// 默认选项
|
|
130
|
+
const { yearDays = 365, monthDays = 30 } = options;
|
|
131
|
+
|
|
132
|
+
// 选项验证
|
|
133
|
+
if (!Number.isInteger(yearDays) || yearDays <= 0) {
|
|
134
|
+
throw new RangeError("yearDays must be a positive integer");
|
|
135
|
+
}
|
|
136
|
+
if (!Number.isInteger(monthDays) || monthDays <= 0) {
|
|
137
|
+
throw new RangeError("monthDays must be a positive integer");
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
const totalMilliseconds = Math.floor(milliseconds);
|
|
141
|
+
const ms = totalMilliseconds % 1000;
|
|
142
|
+
const diffSeconds = Math.floor(totalMilliseconds / 1000);
|
|
143
|
+
|
|
144
|
+
const seconds = diffSeconds % 60;
|
|
145
|
+
const minutes = Math.floor(diffSeconds / 60) % 60;
|
|
146
|
+
const hours = Math.floor(diffSeconds / 3600) % 24;
|
|
147
|
+
|
|
148
|
+
// 计算年、月、日
|
|
149
|
+
const totalDays = Math.floor(diffSeconds / 3600 / 24);
|
|
150
|
+
const years = Math.floor(totalDays / yearDays);
|
|
151
|
+
const remainingDays = totalDays % yearDays;
|
|
152
|
+
const months = Math.floor(remainingDays / monthDays);
|
|
153
|
+
const days = remainingDays % monthDays;
|
|
154
|
+
|
|
155
|
+
return { years, months, days, hours, minutes, seconds, milliseconds: ms };
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* 将毫秒转换为时间持续对象(最大单位为天)。
|
|
160
|
+
* @param {number} milliseconds - 毫秒数(非负整数)
|
|
161
|
+
* @returns {DurationMaxDayObject} 包含天、小时、分钟、秒、毫秒的时间持续对象
|
|
162
|
+
* @throws {TypeError} 当 milliseconds 不是有效数字时抛出
|
|
163
|
+
* @throws {RangeError} 当 milliseconds 为负数时抛出
|
|
164
|
+
* @example
|
|
165
|
+
* // 返回 { days: 486, hours: 22, minutes: 6, seconds: 40, milliseconds: 500 }
|
|
166
|
+
* millisecond2DurationMaxDay(42070000500);
|
|
167
|
+
*/
|
|
168
|
+
function millisecond2DurationMaxDay(milliseconds) {
|
|
169
|
+
if (typeof milliseconds !== "number" || isNaN(milliseconds)) {
|
|
170
|
+
throw new TypeError("milliseconds must be a valid number");
|
|
171
|
+
}
|
|
172
|
+
if (milliseconds < 0) {
|
|
173
|
+
throw new RangeError("milliseconds must be a non-negative number");
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
const totalMilliseconds = Math.floor(milliseconds);
|
|
177
|
+
const ms = totalMilliseconds % 1000;
|
|
178
|
+
const diffSeconds = Math.floor(totalMilliseconds / 1000);
|
|
179
|
+
|
|
180
|
+
const seconds = diffSeconds % 60;
|
|
181
|
+
const minutes = Math.floor(diffSeconds / 60) % 60;
|
|
182
|
+
const hours = Math.floor(diffSeconds / 3600) % 24;
|
|
183
|
+
const days = Math.floor(diffSeconds / 3600 / 24);
|
|
184
|
+
|
|
185
|
+
return { days, hours, minutes, seconds, milliseconds: ms };
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
/**
|
|
189
|
+
* 将毫秒转换为时间持续对象(最大单位为小时)。
|
|
190
|
+
* @param {number} milliseconds - 毫秒数(非负整数)
|
|
191
|
+
* @returns {DurationMaxHourObject} 包含小时、分钟、秒、毫秒的时间持续对象
|
|
192
|
+
* @throws {TypeError} 当 milliseconds 不是有效数字时抛出
|
|
193
|
+
* @throws {RangeError} 当 milliseconds 为负数时抛出
|
|
194
|
+
* @example
|
|
195
|
+
* // 返回 { hours: 11686, minutes: 6, seconds: 40, milliseconds: 500 }
|
|
196
|
+
* millisecond2DurationMaxHour(42070000500);
|
|
197
|
+
*/
|
|
198
|
+
function millisecond2DurationMaxHour(milliseconds) {
|
|
199
|
+
if (typeof milliseconds !== "number" || isNaN(milliseconds)) {
|
|
200
|
+
throw new TypeError("milliseconds must be a valid number");
|
|
201
|
+
}
|
|
202
|
+
if (milliseconds < 0) {
|
|
203
|
+
throw new RangeError("milliseconds must be a non-negative number");
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
const totalMilliseconds = Math.floor(milliseconds);
|
|
207
|
+
const ms = totalMilliseconds % 1000;
|
|
208
|
+
const diffSeconds = Math.floor(totalMilliseconds / 1000);
|
|
209
|
+
|
|
210
|
+
const seconds = diffSeconds % 60;
|
|
211
|
+
const minutes = Math.floor(diffSeconds / 60) % 60;
|
|
212
|
+
const hours = Math.floor(diffSeconds / 3600);
|
|
213
|
+
|
|
214
|
+
return { hours, minutes, seconds, milliseconds: ms };
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
/**
|
|
218
|
+
* 将秒转换为时间持续对象。
|
|
219
|
+
*
|
|
220
|
+
* @param {number} seconds - 秒数(非负整数)
|
|
221
|
+
* @param {Object} [options] - 选项对象。
|
|
222
|
+
* @param {number} [options.yearDays=365] - 一年的天数。
|
|
223
|
+
* @param {number} [options.monthDays=30] - 一月的天数。
|
|
224
|
+
* @returns {DurationObject} 时间持续对象
|
|
225
|
+
* @throws {TypeError} 当 seconds 不是有效数字
|
|
226
|
+
* @throws {RangeError} 当 seconds 为负数
|
|
227
|
+
* @throws {RangeError} 当 yearDays 或 monthDays 不是正整数
|
|
228
|
+
*
|
|
229
|
+
* @example
|
|
230
|
+
* // 基本用法
|
|
231
|
+
* second2Duration(42070000.5);
|
|
232
|
+
* // 返回: { years: 1, months: 4, days: 1, hours: 22, minutes: 6, seconds: 40, milliseconds: 500 }
|
|
233
|
+
*/
|
|
234
|
+
function second2Duration(seconds, options = { yearDays: 365, monthDays: 30 }) {
|
|
235
|
+
if (typeof seconds !== "number" || isNaN(seconds)) {
|
|
236
|
+
throw new TypeError("seconds must be a valid number");
|
|
237
|
+
}
|
|
238
|
+
if (seconds < 0) {
|
|
239
|
+
throw new RangeError("seconds must be a non-negative number");
|
|
240
|
+
}
|
|
241
|
+
return millisecond2Duration(seconds * 1000, options);
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
/**
|
|
245
|
+
* 将秒转换为时间持续对象(最大单位为天)。
|
|
246
|
+
* @param {number} seconds - 秒数(非负整数)
|
|
247
|
+
* @returns {DurationMaxDayObject} 包含天、小时、分钟、秒、毫秒的时间持续对象
|
|
248
|
+
* @throws {TypeError} 当 seconds 不是有效数字时抛出
|
|
249
|
+
* @throws {RangeError} 当 seconds 为负数时抛出
|
|
250
|
+
* @example
|
|
251
|
+
* // 返回 { days: 486, hours: 22, minutes: 6, seconds: 40, milliseconds: 500 }
|
|
252
|
+
* second2DurationMaxDay(42070000.5);
|
|
253
|
+
*/
|
|
254
|
+
function second2DurationMaxDay(seconds) {
|
|
255
|
+
if (typeof seconds !== "number" || isNaN(seconds)) {
|
|
256
|
+
throw new TypeError("seconds must be a valid number");
|
|
257
|
+
}
|
|
258
|
+
if (seconds < 0) {
|
|
259
|
+
throw new RangeError("seconds must be a non-negative number");
|
|
260
|
+
}
|
|
261
|
+
return millisecond2DurationMaxDay(seconds * 1000);
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
/**
|
|
265
|
+
* 将秒转换为时间持续对象(最大单位为小时)。
|
|
266
|
+
* @param {number} seconds - 秒数(非负整数)
|
|
267
|
+
* @returns {DurationMaxHourObject} 包含小时、分钟、秒、毫秒的时间持续对象
|
|
268
|
+
* @throws {TypeError} 当 seconds 不是有效数字时抛出
|
|
269
|
+
* @throws {RangeError} 当 seconds 为负数时抛出
|
|
270
|
+
* @example
|
|
271
|
+
* // 返回 { hours: 11686, minutes: 6, seconds: 40, milliseconds: 500 }
|
|
272
|
+
* second2DurationMaxHour(42070000.5);
|
|
273
|
+
*/
|
|
274
|
+
function second2DurationMaxHour(seconds) {
|
|
275
|
+
if (typeof seconds !== "number" || isNaN(seconds)) {
|
|
276
|
+
throw new TypeError("seconds must be a valid number");
|
|
277
|
+
}
|
|
278
|
+
if (seconds < 0) {
|
|
279
|
+
throw new RangeError("seconds must be a non-negative number");
|
|
280
|
+
}
|
|
281
|
+
return millisecond2DurationMaxHour(seconds * 1000);
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
//#endregion
|
|
285
|
+
|
|
286
|
+
/**
|
|
287
|
+
* 根据小时数返回对应的时间段名称。
|
|
288
|
+
*
|
|
289
|
+
* @param {number} hour - 24 小时制的小时(0-23)
|
|
290
|
+
* @param {object} [locales] - 自定义时段文案
|
|
291
|
+
* @param {string} [locales.earlyMorning="凌晨"] - 00-05
|
|
292
|
+
* @param {string} [locales.morning="上午"] - 06-11
|
|
293
|
+
* @param {string} [locales.noon="中午"] - 12-13
|
|
294
|
+
* @param {string} [locales.afternoon="下午"] - 14-17
|
|
295
|
+
* @param {string} [locales.evening="晚上"] - 18-23
|
|
296
|
+
* @returns {string} 时段名称
|
|
297
|
+
* @throws {RangeError} 当 hour 不在 0-23 范围时抛出
|
|
298
|
+
*/
|
|
299
|
+
function getTimePeriodName(hour, locales = getLocales_TimePeriod()) {
|
|
300
|
+
if (!Number.isInteger(hour) || hour < 0 || hour > 23) {
|
|
301
|
+
throw new RangeError("hour 必须是 0-23 的整数");
|
|
302
|
+
}
|
|
303
|
+
if (hour >= 0 && hour < 6) return locales.earlyMorning;
|
|
304
|
+
if (hour < 12) return locales.morning;
|
|
305
|
+
if (hour < 14) return locales.noon;
|
|
306
|
+
if (hour < 18) return locales.afternoon;
|
|
307
|
+
return locales.evening;
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
/**
|
|
311
|
+
* 格式化时间戳为本地化的时间字符串。
|
|
312
|
+
*
|
|
313
|
+
* @param {number} timestamp - 要格式化的时间戳(毫秒)。
|
|
314
|
+
* @param {Object} [locales] - 本地化配置对象,包含时间相关的本地化字符串。
|
|
315
|
+
* @param {string} [locales.justNow='刚刚'] - 表示刚刚过去的时间。
|
|
316
|
+
* @param {string} [locales.today='今天'] - 表示今天。
|
|
317
|
+
* @param {string} [locales.yesterday='昨天'] - 表示昨天。
|
|
318
|
+
* @param {string} [locales.beforeYesterday='前天'] - 表示前天。
|
|
319
|
+
* @param {string} [locales.year='年'] - 年的单位。
|
|
320
|
+
* @param {string} [locales.month='月'] - 月的单位。
|
|
321
|
+
* @param {string} [locales.day='日'] - 日的单位。
|
|
322
|
+
* @param {Object} [locales.timePeriod] - 一天中不同时间段的本地化字符串。
|
|
323
|
+
* @param {string} [locales.timePeriod.earlyMorning='凌晨'] - 凌晨。
|
|
324
|
+
* @param {string} [locales.timePeriod.morning='上午'] - 上午。
|
|
325
|
+
* @param {string} [locales.timePeriod.noon='中午'] - 中午。
|
|
326
|
+
* @param {string} [locales.timePeriod.afternoon='下午'] - 下午。
|
|
327
|
+
* @param {string} [locales.timePeriod.evening='晚上'] - 晚上。
|
|
328
|
+
* @param {Array<string>} [locales.weekDays=['星期日', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六']] - 星期几的本地化字符串数组。
|
|
329
|
+
*
|
|
330
|
+
* @returns {string} - 格式化后的时间字符串。
|
|
331
|
+
*/
|
|
332
|
+
function formatTimeForLocale(
|
|
333
|
+
timestamp,
|
|
334
|
+
locales = {
|
|
335
|
+
justNow: "刚刚",
|
|
336
|
+
today: "今天",
|
|
337
|
+
yesterday: "昨天",
|
|
338
|
+
beforeYesterday: "前天",
|
|
339
|
+
year: "年",
|
|
340
|
+
month: "月",
|
|
341
|
+
day: "日",
|
|
342
|
+
timePeriod: getLocales_TimePeriod(),
|
|
343
|
+
weekDays: ["星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"]
|
|
344
|
+
}
|
|
345
|
+
) {
|
|
346
|
+
const now = new Date();
|
|
347
|
+
const messageDate = new Date(timestamp);
|
|
348
|
+
|
|
349
|
+
const nowTime = now.getTime();
|
|
350
|
+
const msgTime = messageDate.getTime();
|
|
351
|
+
const diff = nowTime - msgTime;
|
|
352
|
+
const diffMinutes = Math.floor(diff / (1000 * 60));
|
|
353
|
+
|
|
354
|
+
const year = messageDate.getFullYear();
|
|
355
|
+
const month = messageDate.getMonth() + 1;
|
|
356
|
+
const day = messageDate.getDate();
|
|
357
|
+
const hour = messageDate.getHours();
|
|
358
|
+
const minute = messageDate.getMinutes().toString().padStart(2, "0");
|
|
359
|
+
|
|
360
|
+
// 刚刚:1分钟内
|
|
361
|
+
if (diffMinutes < 1) {
|
|
362
|
+
return locales.justNow;
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
// 计算自然天数差(按日期而非时间差)
|
|
366
|
+
const nowDate = new Date(now.getFullYear(), now.getMonth(), now.getDate());
|
|
367
|
+
const msgDate = new Date(year, month - 1, day);
|
|
368
|
+
const diffDays = Math.floor((nowDate.getTime() - msgDate.getTime()) / (1000 * 60 * 60 * 24));
|
|
369
|
+
|
|
370
|
+
const period = getTimePeriodName(hour, locales.timePeriod);
|
|
371
|
+
const timeStr = `${hour}:${minute}`;
|
|
372
|
+
|
|
373
|
+
// 今天
|
|
374
|
+
if (diffDays === 0) {
|
|
375
|
+
return `${locales.today} ${period}${timeStr}`;
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
// 昨天
|
|
379
|
+
if (diffDays === 1) {
|
|
380
|
+
return `${locales.yesterday} ${period}${timeStr}`;
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
// 前天
|
|
384
|
+
if (diffDays === 2) {
|
|
385
|
+
return `${locales.beforeYesterday} ${period}${timeStr}`;
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
// 本周内(周一到周日,且不是今天/昨天/前天)
|
|
389
|
+
// 获取本周一的日期
|
|
390
|
+
const nowDay = now.getDay() || 7; // 周日转为7
|
|
391
|
+
const msgDay = messageDate.getDay() || 7;
|
|
392
|
+
const mondayOfThisWeek = new Date(nowDate.getTime() - (nowDay - 1) * 24 * 60 * 60 * 1000);
|
|
393
|
+
const mondayOfThatWeek = new Date(msgDate.getTime() - (msgDay - 1) * 24 * 60 * 60 * 1000);
|
|
394
|
+
|
|
395
|
+
if (mondayOfThisWeek.getTime() === mondayOfThatWeek.getTime() && diffDays < 7) {
|
|
396
|
+
const weekDayName = locales.weekDays[messageDate.getDay()];
|
|
397
|
+
return `${weekDayName}${period} ${timeStr}`;
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
// 本月内(非本周)
|
|
401
|
+
const nowYear = now.getFullYear();
|
|
402
|
+
const nowMonth = now.getMonth();
|
|
403
|
+
if (year === nowYear && messageDate.getMonth() === nowMonth) {
|
|
404
|
+
return `${month}${locales.month}${day}${locales.day} ${period}${timeStr}`;
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
// 本年内(非本月)
|
|
408
|
+
if (year === nowYear) {
|
|
409
|
+
return `${month}${locales.month}${day}${locales.day} ${period}${timeStr}`;
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
// 其他(非本年)
|
|
413
|
+
return `${year}${locales.year}${month}${locales.month}${day}${locales.day} ${period}${timeStr}`;
|
|
185
414
|
}
|
|
186
415
|
|
|
187
|
-
export {
|
|
416
|
+
export { formatTimeForLocale, getTimePeriodName, millisecond2Duration, millisecond2DurationMaxDay, millisecond2DurationMaxHour, randomDateInRange, second2Duration, second2DurationMaxDay, second2DurationMaxHour, toDate };
|
|
188
417
|
//# sourceMappingURL=date.js.map
|