lupine.api 1.1.58 → 1.1.59
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/README.md +3 -3
- package/admin/admin-about.tsx +12 -16
- package/admin/admin-config.tsx +47 -44
- package/admin/admin-css.tsx +3 -3
- package/admin/admin-db.tsx +75 -75
- package/admin/admin-frame-helper.tsx +364 -364
- package/admin/admin-frame.tsx +164 -164
- package/admin/admin-index.tsx +65 -65
- package/admin/admin-login.tsx +111 -111
- package/admin/admin-menu-edit.tsx +637 -637
- package/admin/admin-menu-list.tsx +87 -87
- package/admin/admin-page-edit.tsx +564 -564
- package/admin/admin-page-list.tsx +83 -83
- package/admin/admin-performance.tsx +28 -28
- package/admin/admin-release.tsx +427 -426
- package/admin/admin-resources.tsx +382 -382
- package/admin/admin-shell.tsx +89 -89
- package/admin/admin-table-data.tsx +146 -146
- package/admin/admin-table-list.tsx +230 -230
- package/admin/admin-test-animations.tsx +395 -395
- package/admin/admin-test-component.tsx +823 -808
- package/admin/admin-test-edit.tsx +319 -319
- package/admin/admin-test-themes.tsx +56 -56
- package/admin/admin-tokens.tsx +338 -338
- package/admin/design/admin-design.tsx +174 -174
- package/admin/design/block-grid.tsx +36 -36
- package/admin/design/block-grid1.tsx +21 -21
- package/admin/design/block-paragraph.tsx +19 -19
- package/admin/design/block-title.tsx +19 -19
- package/admin/design/design-block-box.tsx +140 -140
- package/admin/design/drag-data.tsx +24 -24
- package/admin/index.ts +9 -9
- package/admin/package.json +15 -15
- package/admin/tsconfig.json +127 -127
- package/dev/copy-folder.js +32 -32
- package/dev/cp-index-html.js +69 -69
- package/dev/file-utils.js +12 -12
- package/dev/index.js +18 -19
- package/dev/package.json +12 -12
- package/dev/plugin-ifelse.js +168 -168
- package/dev/plugin-ifelse.test.js +37 -37
- package/dev/run-cmd.js +14 -14
- package/dev/send-request.js +12 -12
- package/package.json +55 -55
- package/src/admin-api/admin-api-helper.ts +210 -205
- package/src/admin-api/admin-api.ts +65 -65
- package/src/admin-api/admin-auth.ts +152 -146
- package/src/admin-api/admin-config.ts +94 -84
- package/src/admin-api/admin-csv.ts +94 -94
- package/src/admin-api/admin-db.ts +269 -269
- package/src/admin-api/admin-menu.ts +135 -135
- package/src/admin-api/admin-page.ts +135 -135
- package/src/admin-api/admin-performance.ts +128 -128
- package/src/admin-api/admin-release.ts +703 -700
- package/src/admin-api/admin-resources.ts +318 -318
- package/src/admin-api/admin-token-helper.ts +82 -79
- package/src/admin-api/admin-tokens.ts +90 -90
- package/src/admin-api/index.ts +2 -2
- package/src/admin-api/web-config-api.ts +19 -19
- package/src/api/api-cache.ts +103 -103
- package/src/api/api-helper.ts +44 -44
- package/src/api/api-module.ts +67 -60
- package/src/api/api-router.ts +177 -177
- package/src/api/api-shared-storage.ts +64 -64
- package/src/api/async-storage.ts +5 -5
- package/src/api/debug-service.ts +56 -56
- package/src/api/encode-html.ts +27 -27
- package/src/api/handle-status.ts +75 -75
- package/src/api/index.ts +15 -16
- package/src/api/mini-web-socket.ts +270 -270
- package/src/api/server-content-type.ts +82 -82
- package/src/api/server-render.ts +235 -215
- package/src/api/shell-service.ts +74 -74
- package/src/api/simple-storage.ts +80 -80
- package/src/api/static-server.ts +128 -125
- package/src/api/to-client-delivery.ts +26 -26
- package/src/app/app-cache.ts +55 -55
- package/src/app/app-helper.ts +62 -62
- package/src/app/app-message.ts +109 -109
- package/src/app/app-shared-storage.ts +363 -363
- package/src/app/app-start.ts +136 -136
- package/src/app/cleanup-exit.ts +16 -16
- package/src/app/host-to-path.ts +38 -38
- package/src/app/index.ts +11 -11
- package/src/app/process-dev-requests.ts +130 -130
- package/src/app/web-listener.ts +294 -294
- package/src/app/web-processor.ts +47 -42
- package/src/app/web-server.ts +100 -100
- package/src/common-js/web-env.js +104 -104
- package/src/index.ts +7 -7
- package/src/lang/api-lang-en.ts +26 -26
- package/src/lang/api-lang-zh-cn.ts +27 -27
- package/src/lang/index.ts +2 -2
- package/src/lang/lang-helper.ts +76 -76
- package/src/lang/lang-props.ts +6 -6
- package/src/lib/db/db-helper.ts +23 -23
- package/src/lib/db/db-mysql.ts +249 -250
- package/src/lib/db/db-sqlite.ts +101 -101
- package/src/lib/db/db.spec.ts +28 -28
- package/src/lib/db/db.ts +325 -325
- package/src/lib/db/index.ts +5 -5
- package/src/lib/index.ts +3 -3
- package/src/lib/logger.spec.ts +214 -214
- package/src/lib/logger.ts +281 -281
- package/src/lib/runtime-require.ts +37 -37
- package/src/lib/utils/cookie-util.ts +34 -34
- package/src/lib/utils/crypto.ts +58 -58
- package/src/lib/utils/date-utils.ts +317 -317
- package/src/lib/utils/deep-merge.ts +37 -37
- package/src/lib/utils/delay.ts +12 -12
- package/src/lib/utils/file-setting.ts +55 -55
- package/src/lib/utils/format-bytes.ts +11 -11
- package/src/lib/utils/fs-utils.ts +158 -158
- package/src/lib/utils/get-env.ts +27 -27
- package/src/lib/utils/index.ts +12 -12
- package/src/lib/utils/is-type.ts +48 -48
- package/src/lib/utils/load-env.ts +14 -14
- package/src/lib/utils/pad.ts +6 -6
- package/src/models/api-base.ts +5 -5
- package/src/models/api-module-props.ts +10 -11
- package/src/models/api-router-props.ts +26 -26
- package/src/models/app-cache-props.ts +33 -33
- package/src/models/app-data-props.ts +10 -10
- package/src/models/app-helper-props.ts +6 -6
- package/src/models/app-shared-storage-props.ts +38 -38
- package/src/models/app-start-props.ts +18 -18
- package/src/models/async-storage-props.ts +13 -13
- package/src/models/db-config.ts +30 -30
- package/src/models/host-to-path-props.ts +12 -12
- package/src/models/index.ts +16 -16
- package/src/models/json-object.ts +8 -8
- package/src/models/locals-props.ts +36 -36
- package/src/models/logger-props.ts +84 -84
- package/src/models/simple-storage-props.ts +13 -14
- package/src/models/to-client-delivery-props.ts +6 -6
- package/tsconfig.json +115 -115
- package/dev/plugin-gen-versions.js +0 -20
|
@@ -1,317 +1,317 @@
|
|
|
1
|
-
export class DateUtils {
|
|
2
|
-
/*
|
|
3
|
-
* returns the number of milliseconds elapsed since January 1, 1970 00:00:00 UTC.
|
|
4
|
-
*/
|
|
5
|
-
static now(): number {
|
|
6
|
-
return Date.now();
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
// Date time string format: YYYY-MM-DDTHH:mm:ss.sssZ
|
|
10
|
-
// The string that you want to parse into a Date should match this format or a portion of this format.
|
|
11
|
-
// The “T” character separates the date from the time portion of the string. The “Z” character is the UTC offset representation.
|
|
12
|
-
static toDate(str: string): Date {
|
|
13
|
-
return new Date(Date.parse(str));
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
static clone(dt: Date): Date {
|
|
17
|
-
return new Date(dt.valueOf());
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
static isLeapYear(year: number): boolean {
|
|
21
|
-
return (year % 4 === 0 && year % 100 !== 0) || year % 400 === 0;
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
static daysInMonth(year: number, month: number) {
|
|
25
|
-
return month === 1 ? (DateUtils.isLeapYear(year) ? 29 : 28) : 31 - ((month % 7) % 2);
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
static createDate(
|
|
29
|
-
year: number,
|
|
30
|
-
monthIndex: number,
|
|
31
|
-
date?: number,
|
|
32
|
-
hours?: number,
|
|
33
|
-
minutes?: number,
|
|
34
|
-
seconds?: number,
|
|
35
|
-
ms?: number
|
|
36
|
-
): Date {
|
|
37
|
-
const dt = new Date(year, monthIndex, date, hours, minutes, seconds, ms);
|
|
38
|
-
return dt;
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
// It returns the number of milliseconds since January 1, 1970, 00:00:00 UTC instead of local (regardless of which time zone you are in).
|
|
42
|
-
static createUTCDate(
|
|
43
|
-
year: number,
|
|
44
|
-
monthIndex?: number,
|
|
45
|
-
date?: number,
|
|
46
|
-
hours?: number,
|
|
47
|
-
minutes?: number,
|
|
48
|
-
seconds?: number,
|
|
49
|
-
ms?: number
|
|
50
|
-
): Date {
|
|
51
|
-
const dt = new Date(Date.UTC.apply(null, [year, monthIndex, date, hours, minutes, seconds, ms]));
|
|
52
|
-
return dt;
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
static set(
|
|
56
|
-
dt: Date,
|
|
57
|
-
year?: number,
|
|
58
|
-
monthIndex?: number,
|
|
59
|
-
date?: number,
|
|
60
|
-
hours?: number,
|
|
61
|
-
minutes?: number,
|
|
62
|
-
seconds?: number,
|
|
63
|
-
ms?: number
|
|
64
|
-
): Date {
|
|
65
|
-
if (!dt) {
|
|
66
|
-
dt = new Date(DateUtils.now());
|
|
67
|
-
}
|
|
68
|
-
if (typeof year === 'number') {
|
|
69
|
-
dt.setFullYear(year);
|
|
70
|
-
}
|
|
71
|
-
if (typeof monthIndex === 'number') {
|
|
72
|
-
dt.setMonth(monthIndex);
|
|
73
|
-
}
|
|
74
|
-
if (typeof date === 'number') {
|
|
75
|
-
dt.setDate(date);
|
|
76
|
-
}
|
|
77
|
-
if (typeof hours === 'number') {
|
|
78
|
-
dt.setHours(hours);
|
|
79
|
-
}
|
|
80
|
-
if (typeof minutes === 'number') {
|
|
81
|
-
dt.setMinutes(minutes);
|
|
82
|
-
}
|
|
83
|
-
if (typeof seconds === 'number') {
|
|
84
|
-
dt.setSeconds(seconds);
|
|
85
|
-
}
|
|
86
|
-
if (typeof ms === 'number') {
|
|
87
|
-
dt.setMilliseconds(ms);
|
|
88
|
-
}
|
|
89
|
-
return dt;
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
static add(
|
|
93
|
-
dt: Date,
|
|
94
|
-
year?: number,
|
|
95
|
-
monthCount?: number,
|
|
96
|
-
date?: number,
|
|
97
|
-
hours?: number,
|
|
98
|
-
minutes?: number,
|
|
99
|
-
seconds?: number,
|
|
100
|
-
ms?: number
|
|
101
|
-
): Date {
|
|
102
|
-
if (!dt) {
|
|
103
|
-
dt = new Date(DateUtils.now());
|
|
104
|
-
}
|
|
105
|
-
if (typeof year === 'number') {
|
|
106
|
-
dt.setFullYear(dt.getFullYear() + year);
|
|
107
|
-
}
|
|
108
|
-
if (typeof monthCount === 'number') {
|
|
109
|
-
dt.setMonth(dt.getMonth() + monthCount);
|
|
110
|
-
}
|
|
111
|
-
if (typeof date === 'number') {
|
|
112
|
-
dt.setDate(dt.getDate() + date);
|
|
113
|
-
}
|
|
114
|
-
if (typeof hours === 'number') {
|
|
115
|
-
dt.setHours(dt.getHours() + hours);
|
|
116
|
-
}
|
|
117
|
-
if (typeof minutes === 'number') {
|
|
118
|
-
dt.setMinutes(dt.getMinutes() + minutes);
|
|
119
|
-
}
|
|
120
|
-
if (typeof seconds === 'number') {
|
|
121
|
-
dt.setSeconds(dt.getSeconds() + seconds);
|
|
122
|
-
}
|
|
123
|
-
if (typeof ms === 'number') {
|
|
124
|
-
dt.setMilliseconds(dt.getMilliseconds() + ms);
|
|
125
|
-
}
|
|
126
|
-
return dt;
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
// returns a difference object from two dates
|
|
130
|
-
static diff(endDate: Date, startDate: Date): DiffDate {
|
|
131
|
-
const startYear = startDate.getFullYear();
|
|
132
|
-
let yearDiff = endDate.getFullYear() - startYear;
|
|
133
|
-
let monthDiff = endDate.getMonth() - startDate.getMonth();
|
|
134
|
-
if (monthDiff < 0) {
|
|
135
|
-
yearDiff--;
|
|
136
|
-
monthDiff += 12;
|
|
137
|
-
}
|
|
138
|
-
let dayDiff = endDate.getDate() - startDate.getDate();
|
|
139
|
-
if (dayDiff < 0) {
|
|
140
|
-
if (monthDiff > 0) {
|
|
141
|
-
monthDiff--;
|
|
142
|
-
} else {
|
|
143
|
-
yearDiff--;
|
|
144
|
-
monthDiff = 11;
|
|
145
|
-
}
|
|
146
|
-
dayDiff += DateUtils.daysInMonth(startYear, startDate.getMonth());
|
|
147
|
-
}
|
|
148
|
-
const msTotal = endDate.valueOf() - startDate.valueOf(); // milliseconds
|
|
149
|
-
const secondTotal = Math.floor(msTotal / 1000); // milliseconds -> seconds
|
|
150
|
-
const hourDiff = Math.floor(secondTotal / (60 * 60)) % 24;
|
|
151
|
-
const minuteDiff = Math.floor(secondTotal / 60) % 60;
|
|
152
|
-
const secondDiff = secondTotal % 60;
|
|
153
|
-
const msDiff = msTotal % 1000;
|
|
154
|
-
return new DiffDate(yearDiff, monthDiff, dayDiff, hourDiff, minuteDiff, secondDiff, msDiff);
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
// returns a time difference string from two dates
|
|
158
|
-
static diffString(endDate: Date, startDate: Date, printMS = false): string {
|
|
159
|
-
const diff = DateUtils.diff(endDate, startDate);
|
|
160
|
-
let ret = '';
|
|
161
|
-
if (diff.years !== 0) {
|
|
162
|
-
ret = ret + diff.years + ' years(s), ';
|
|
163
|
-
}
|
|
164
|
-
if (diff.years !== 0 || diff.months !== 0) {
|
|
165
|
-
ret = ret + diff.months + ' month(s), ';
|
|
166
|
-
}
|
|
167
|
-
if (diff.years !== 0 || diff.months !== 0 || diff.days !== 0) {
|
|
168
|
-
ret = ret + diff.days + ' day(s), ';
|
|
169
|
-
}
|
|
170
|
-
if (diff.years !== 0 || diff.months !== 0 || diff.days !== 0 || diff.hours !== 0) {
|
|
171
|
-
ret = ret + diff.hours + ' hour(s), ';
|
|
172
|
-
}
|
|
173
|
-
if (diff.years !== 0 || diff.months !== 0 || diff.days !== 0 || diff.hours !== 0 || diff.minutes !== 0) {
|
|
174
|
-
ret = ret + diff.minutes + ' minute(s), ';
|
|
175
|
-
}
|
|
176
|
-
if (
|
|
177
|
-
diff.years !== 0 ||
|
|
178
|
-
diff.months !== 0 ||
|
|
179
|
-
diff.days !== 0 ||
|
|
180
|
-
diff.hours !== 0 ||
|
|
181
|
-
diff.minutes !== 0 ||
|
|
182
|
-
diff.seconds !== 0 ||
|
|
183
|
-
!printMS
|
|
184
|
-
) {
|
|
185
|
-
ret = ret + diff.seconds + ' second(s)';
|
|
186
|
-
if (printMS) {
|
|
187
|
-
ret += ', ';
|
|
188
|
-
}
|
|
189
|
-
}
|
|
190
|
-
if (printMS) {
|
|
191
|
-
ret = ret + diff.milliseconds + ' ms';
|
|
192
|
-
}
|
|
193
|
-
return ret;
|
|
194
|
-
}
|
|
195
|
-
|
|
196
|
-
// returns a YYYYMMDD format string
|
|
197
|
-
static toYMD(dt: Date, separator: string) {
|
|
198
|
-
separator = typeof separator === 'undefined' ? '-' : separator;
|
|
199
|
-
return (
|
|
200
|
-
dt.getFullYear() +
|
|
201
|
-
separator +
|
|
202
|
-
('0' + (dt.getMonth() + 1)).toString().slice(-2) +
|
|
203
|
-
separator +
|
|
204
|
-
('0' + dt.getDate()).toString().slice(-2)
|
|
205
|
-
);
|
|
206
|
-
}
|
|
207
|
-
static toYmdHms(dt: Date, separator: string) {
|
|
208
|
-
separator = typeof separator === 'undefined' ? '-' : separator;
|
|
209
|
-
return (
|
|
210
|
-
dt.getFullYear() +
|
|
211
|
-
separator +
|
|
212
|
-
('0' + (dt.getMonth() + 1)).toString().slice(-2) +
|
|
213
|
-
separator +
|
|
214
|
-
('0' + dt.getDate()).toString().slice(-2) +
|
|
215
|
-
' ' +
|
|
216
|
-
('0' + dt.getHours()).toString().slice(-2) +
|
|
217
|
-
':' +
|
|
218
|
-
('0' + dt.getMinutes()).toString().slice(-2) +
|
|
219
|
-
':' +
|
|
220
|
-
('0' + dt.getSeconds()).toString().slice(-2)
|
|
221
|
-
);
|
|
222
|
-
}
|
|
223
|
-
|
|
224
|
-
static toJSONString(dt: Date) {
|
|
225
|
-
// return dt.getUTCFullYear() + '-' + ('0' + (dt.getUTCMonth() + 1)).toString().slice(-2) +
|
|
226
|
-
// '-' + ('0' + dt.getUTCDate()).toString().slice(-2) + ' ' + ('0' + dt.getUTCHours()).toString().slice(-2) +
|
|
227
|
-
// ':' + ('0' + dt.getUTCMinutes()).toString().slice(-2) + ':' + ('0' + dt.getUTCSeconds()).toString().slice(-2) + 'Z';
|
|
228
|
-
return dt.toJSON();
|
|
229
|
-
}
|
|
230
|
-
|
|
231
|
-
static showJSONString(dt: string, separator = '-') {
|
|
232
|
-
return DateUtils.toYmdHms(DateUtils.toDate(dt), separator);
|
|
233
|
-
}
|
|
234
|
-
|
|
235
|
-
static fromJSONString(dt: string) {
|
|
236
|
-
return DateUtils.toDate(dt);
|
|
237
|
-
}
|
|
238
|
-
|
|
239
|
-
static clearTime(dt: Date) {
|
|
240
|
-
dt.setHours(0);
|
|
241
|
-
dt.setMinutes(0);
|
|
242
|
-
dt.setSeconds(0);
|
|
243
|
-
dt.setMilliseconds(0);
|
|
244
|
-
return dt;
|
|
245
|
-
}
|
|
246
|
-
|
|
247
|
-
static clearUTCTime(dt: Date) {
|
|
248
|
-
dt.setUTCHours(0);
|
|
249
|
-
dt.setUTCMinutes(0);
|
|
250
|
-
dt.setUTCSeconds(0);
|
|
251
|
-
dt.setUTCMilliseconds(0);
|
|
252
|
-
return dt;
|
|
253
|
-
}
|
|
254
|
-
|
|
255
|
-
static format(dt: Date, fmt: string) {
|
|
256
|
-
if (!fmt) {
|
|
257
|
-
fmt = 'YYYY-MM-DD';
|
|
258
|
-
}
|
|
259
|
-
if (!dt) {
|
|
260
|
-
dt = new Date();
|
|
261
|
-
}
|
|
262
|
-
|
|
263
|
-
const parts: { [key: string]: string } = {
|
|
264
|
-
YYYY: dt.getFullYear().toString(),
|
|
265
|
-
YY: ('00' + (dt.getFullYear() - 100)).toString().slice(-2),
|
|
266
|
-
MM: ('0' + (dt.getMonth() + 1)).toString().slice(-2),
|
|
267
|
-
M: (dt.getMonth() + 1).toString(),
|
|
268
|
-
DD: ('0' + dt.getDate()).toString().slice(-2),
|
|
269
|
-
D: dt.getDate().toString(),
|
|
270
|
-
hh: ('0' + dt.getHours()).toString().slice(-2),
|
|
271
|
-
h: dt.getHours().toString(),
|
|
272
|
-
mm: ('0' + dt.getMinutes()).toString().slice(-2),
|
|
273
|
-
ss: ('0' + dt.getSeconds()).toString().slice(-2),
|
|
274
|
-
SSS: ('00' + dt.getMilliseconds()).toString().slice(-3),
|
|
275
|
-
S: Math.floor(dt.getMilliseconds() / 100)
|
|
276
|
-
.toString()
|
|
277
|
-
.slice(-1),
|
|
278
|
-
};
|
|
279
|
-
|
|
280
|
-
const array = fmt.match(/(\[[^\[]*\])|(\\)?(YYYY|YY|MM?|DD?|hh?|mm?|ss?|SSS|S|.)/g) as string[];
|
|
281
|
-
for (let i = 0, length = array.length; i < length; i++) {
|
|
282
|
-
if (parts[array[i]]) {
|
|
283
|
-
array[i] = parts[array[i]];
|
|
284
|
-
}
|
|
285
|
-
}
|
|
286
|
-
const ret = array.join('');
|
|
287
|
-
return ret;
|
|
288
|
-
}
|
|
289
|
-
}
|
|
290
|
-
|
|
291
|
-
export class DiffDate {
|
|
292
|
-
years: number;
|
|
293
|
-
months: number;
|
|
294
|
-
days: number;
|
|
295
|
-
hours: number;
|
|
296
|
-
minutes: number;
|
|
297
|
-
seconds: number;
|
|
298
|
-
milliseconds: number;
|
|
299
|
-
|
|
300
|
-
constructor(
|
|
301
|
-
years: number,
|
|
302
|
-
months: number,
|
|
303
|
-
days: number,
|
|
304
|
-
hours: number,
|
|
305
|
-
minutes: number,
|
|
306
|
-
seconds: number,
|
|
307
|
-
milliseconds: number
|
|
308
|
-
) {
|
|
309
|
-
this.years = years;
|
|
310
|
-
this.months = months;
|
|
311
|
-
this.days = days;
|
|
312
|
-
this.hours = hours;
|
|
313
|
-
this.minutes = minutes;
|
|
314
|
-
this.seconds = seconds;
|
|
315
|
-
this.milliseconds = milliseconds;
|
|
316
|
-
}
|
|
317
|
-
}
|
|
1
|
+
export class DateUtils {
|
|
2
|
+
/*
|
|
3
|
+
* returns the number of milliseconds elapsed since January 1, 1970 00:00:00 UTC.
|
|
4
|
+
*/
|
|
5
|
+
static now(): number {
|
|
6
|
+
return Date.now();
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
// Date time string format: YYYY-MM-DDTHH:mm:ss.sssZ
|
|
10
|
+
// The string that you want to parse into a Date should match this format or a portion of this format.
|
|
11
|
+
// The “T” character separates the date from the time portion of the string. The “Z” character is the UTC offset representation.
|
|
12
|
+
static toDate(str: string): Date {
|
|
13
|
+
return new Date(Date.parse(str));
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
static clone(dt: Date): Date {
|
|
17
|
+
return new Date(dt.valueOf());
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
static isLeapYear(year: number): boolean {
|
|
21
|
+
return (year % 4 === 0 && year % 100 !== 0) || year % 400 === 0;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
static daysInMonth(year: number, month: number) {
|
|
25
|
+
return month === 1 ? (DateUtils.isLeapYear(year) ? 29 : 28) : 31 - ((month % 7) % 2);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
static createDate(
|
|
29
|
+
year: number,
|
|
30
|
+
monthIndex: number,
|
|
31
|
+
date?: number,
|
|
32
|
+
hours?: number,
|
|
33
|
+
minutes?: number,
|
|
34
|
+
seconds?: number,
|
|
35
|
+
ms?: number
|
|
36
|
+
): Date {
|
|
37
|
+
const dt = new Date(year, monthIndex, date, hours, minutes, seconds, ms);
|
|
38
|
+
return dt;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
// It returns the number of milliseconds since January 1, 1970, 00:00:00 UTC instead of local (regardless of which time zone you are in).
|
|
42
|
+
static createUTCDate(
|
|
43
|
+
year: number,
|
|
44
|
+
monthIndex?: number,
|
|
45
|
+
date?: number,
|
|
46
|
+
hours?: number,
|
|
47
|
+
minutes?: number,
|
|
48
|
+
seconds?: number,
|
|
49
|
+
ms?: number
|
|
50
|
+
): Date {
|
|
51
|
+
const dt = new Date(Date.UTC.apply(null, [year, monthIndex, date, hours, minutes, seconds, ms]));
|
|
52
|
+
return dt;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
static set(
|
|
56
|
+
dt: Date,
|
|
57
|
+
year?: number,
|
|
58
|
+
monthIndex?: number,
|
|
59
|
+
date?: number,
|
|
60
|
+
hours?: number,
|
|
61
|
+
minutes?: number,
|
|
62
|
+
seconds?: number,
|
|
63
|
+
ms?: number
|
|
64
|
+
): Date {
|
|
65
|
+
if (!dt) {
|
|
66
|
+
dt = new Date(DateUtils.now());
|
|
67
|
+
}
|
|
68
|
+
if (typeof year === 'number') {
|
|
69
|
+
dt.setFullYear(year);
|
|
70
|
+
}
|
|
71
|
+
if (typeof monthIndex === 'number') {
|
|
72
|
+
dt.setMonth(monthIndex);
|
|
73
|
+
}
|
|
74
|
+
if (typeof date === 'number') {
|
|
75
|
+
dt.setDate(date);
|
|
76
|
+
}
|
|
77
|
+
if (typeof hours === 'number') {
|
|
78
|
+
dt.setHours(hours);
|
|
79
|
+
}
|
|
80
|
+
if (typeof minutes === 'number') {
|
|
81
|
+
dt.setMinutes(minutes);
|
|
82
|
+
}
|
|
83
|
+
if (typeof seconds === 'number') {
|
|
84
|
+
dt.setSeconds(seconds);
|
|
85
|
+
}
|
|
86
|
+
if (typeof ms === 'number') {
|
|
87
|
+
dt.setMilliseconds(ms);
|
|
88
|
+
}
|
|
89
|
+
return dt;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
static add(
|
|
93
|
+
dt: Date,
|
|
94
|
+
year?: number,
|
|
95
|
+
monthCount?: number,
|
|
96
|
+
date?: number,
|
|
97
|
+
hours?: number,
|
|
98
|
+
minutes?: number,
|
|
99
|
+
seconds?: number,
|
|
100
|
+
ms?: number
|
|
101
|
+
): Date {
|
|
102
|
+
if (!dt) {
|
|
103
|
+
dt = new Date(DateUtils.now());
|
|
104
|
+
}
|
|
105
|
+
if (typeof year === 'number') {
|
|
106
|
+
dt.setFullYear(dt.getFullYear() + year);
|
|
107
|
+
}
|
|
108
|
+
if (typeof monthCount === 'number') {
|
|
109
|
+
dt.setMonth(dt.getMonth() + monthCount);
|
|
110
|
+
}
|
|
111
|
+
if (typeof date === 'number') {
|
|
112
|
+
dt.setDate(dt.getDate() + date);
|
|
113
|
+
}
|
|
114
|
+
if (typeof hours === 'number') {
|
|
115
|
+
dt.setHours(dt.getHours() + hours);
|
|
116
|
+
}
|
|
117
|
+
if (typeof minutes === 'number') {
|
|
118
|
+
dt.setMinutes(dt.getMinutes() + minutes);
|
|
119
|
+
}
|
|
120
|
+
if (typeof seconds === 'number') {
|
|
121
|
+
dt.setSeconds(dt.getSeconds() + seconds);
|
|
122
|
+
}
|
|
123
|
+
if (typeof ms === 'number') {
|
|
124
|
+
dt.setMilliseconds(dt.getMilliseconds() + ms);
|
|
125
|
+
}
|
|
126
|
+
return dt;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
// returns a difference object from two dates
|
|
130
|
+
static diff(endDate: Date, startDate: Date): DiffDate {
|
|
131
|
+
const startYear = startDate.getFullYear();
|
|
132
|
+
let yearDiff = endDate.getFullYear() - startYear;
|
|
133
|
+
let monthDiff = endDate.getMonth() - startDate.getMonth();
|
|
134
|
+
if (monthDiff < 0) {
|
|
135
|
+
yearDiff--;
|
|
136
|
+
monthDiff += 12;
|
|
137
|
+
}
|
|
138
|
+
let dayDiff = endDate.getDate() - startDate.getDate();
|
|
139
|
+
if (dayDiff < 0) {
|
|
140
|
+
if (monthDiff > 0) {
|
|
141
|
+
monthDiff--;
|
|
142
|
+
} else {
|
|
143
|
+
yearDiff--;
|
|
144
|
+
monthDiff = 11;
|
|
145
|
+
}
|
|
146
|
+
dayDiff += DateUtils.daysInMonth(startYear, startDate.getMonth());
|
|
147
|
+
}
|
|
148
|
+
const msTotal = endDate.valueOf() - startDate.valueOf(); // milliseconds
|
|
149
|
+
const secondTotal = Math.floor(msTotal / 1000); // milliseconds -> seconds
|
|
150
|
+
const hourDiff = Math.floor(secondTotal / (60 * 60)) % 24;
|
|
151
|
+
const minuteDiff = Math.floor(secondTotal / 60) % 60;
|
|
152
|
+
const secondDiff = secondTotal % 60;
|
|
153
|
+
const msDiff = msTotal % 1000;
|
|
154
|
+
return new DiffDate(yearDiff, monthDiff, dayDiff, hourDiff, minuteDiff, secondDiff, msDiff);
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
// returns a time difference string from two dates
|
|
158
|
+
static diffString(endDate: Date, startDate: Date, printMS = false): string {
|
|
159
|
+
const diff = DateUtils.diff(endDate, startDate);
|
|
160
|
+
let ret = '';
|
|
161
|
+
if (diff.years !== 0) {
|
|
162
|
+
ret = ret + diff.years + ' years(s), ';
|
|
163
|
+
}
|
|
164
|
+
if (diff.years !== 0 || diff.months !== 0) {
|
|
165
|
+
ret = ret + diff.months + ' month(s), ';
|
|
166
|
+
}
|
|
167
|
+
if (diff.years !== 0 || diff.months !== 0 || diff.days !== 0) {
|
|
168
|
+
ret = ret + diff.days + ' day(s), ';
|
|
169
|
+
}
|
|
170
|
+
if (diff.years !== 0 || diff.months !== 0 || diff.days !== 0 || diff.hours !== 0) {
|
|
171
|
+
ret = ret + diff.hours + ' hour(s), ';
|
|
172
|
+
}
|
|
173
|
+
if (diff.years !== 0 || diff.months !== 0 || diff.days !== 0 || diff.hours !== 0 || diff.minutes !== 0) {
|
|
174
|
+
ret = ret + diff.minutes + ' minute(s), ';
|
|
175
|
+
}
|
|
176
|
+
if (
|
|
177
|
+
diff.years !== 0 ||
|
|
178
|
+
diff.months !== 0 ||
|
|
179
|
+
diff.days !== 0 ||
|
|
180
|
+
diff.hours !== 0 ||
|
|
181
|
+
diff.minutes !== 0 ||
|
|
182
|
+
diff.seconds !== 0 ||
|
|
183
|
+
!printMS
|
|
184
|
+
) {
|
|
185
|
+
ret = ret + diff.seconds + ' second(s)';
|
|
186
|
+
if (printMS) {
|
|
187
|
+
ret += ', ';
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
if (printMS) {
|
|
191
|
+
ret = ret + diff.milliseconds + ' ms';
|
|
192
|
+
}
|
|
193
|
+
return ret;
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
// returns a YYYYMMDD format string
|
|
197
|
+
static toYMD(dt: Date, separator: string) {
|
|
198
|
+
separator = typeof separator === 'undefined' ? '-' : separator;
|
|
199
|
+
return (
|
|
200
|
+
dt.getFullYear() +
|
|
201
|
+
separator +
|
|
202
|
+
('0' + (dt.getMonth() + 1)).toString().slice(-2) +
|
|
203
|
+
separator +
|
|
204
|
+
('0' + dt.getDate()).toString().slice(-2)
|
|
205
|
+
);
|
|
206
|
+
}
|
|
207
|
+
static toYmdHms(dt: Date, separator: string) {
|
|
208
|
+
separator = typeof separator === 'undefined' ? '-' : separator;
|
|
209
|
+
return (
|
|
210
|
+
dt.getFullYear() +
|
|
211
|
+
separator +
|
|
212
|
+
('0' + (dt.getMonth() + 1)).toString().slice(-2) +
|
|
213
|
+
separator +
|
|
214
|
+
('0' + dt.getDate()).toString().slice(-2) +
|
|
215
|
+
' ' +
|
|
216
|
+
('0' + dt.getHours()).toString().slice(-2) +
|
|
217
|
+
':' +
|
|
218
|
+
('0' + dt.getMinutes()).toString().slice(-2) +
|
|
219
|
+
':' +
|
|
220
|
+
('0' + dt.getSeconds()).toString().slice(-2)
|
|
221
|
+
);
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
static toJSONString(dt: Date) {
|
|
225
|
+
// return dt.getUTCFullYear() + '-' + ('0' + (dt.getUTCMonth() + 1)).toString().slice(-2) +
|
|
226
|
+
// '-' + ('0' + dt.getUTCDate()).toString().slice(-2) + ' ' + ('0' + dt.getUTCHours()).toString().slice(-2) +
|
|
227
|
+
// ':' + ('0' + dt.getUTCMinutes()).toString().slice(-2) + ':' + ('0' + dt.getUTCSeconds()).toString().slice(-2) + 'Z';
|
|
228
|
+
return dt.toJSON();
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
static showJSONString(dt: string, separator = '-') {
|
|
232
|
+
return DateUtils.toYmdHms(DateUtils.toDate(dt), separator);
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
static fromJSONString(dt: string) {
|
|
236
|
+
return DateUtils.toDate(dt);
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
static clearTime(dt: Date) {
|
|
240
|
+
dt.setHours(0);
|
|
241
|
+
dt.setMinutes(0);
|
|
242
|
+
dt.setSeconds(0);
|
|
243
|
+
dt.setMilliseconds(0);
|
|
244
|
+
return dt;
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
static clearUTCTime(dt: Date) {
|
|
248
|
+
dt.setUTCHours(0);
|
|
249
|
+
dt.setUTCMinutes(0);
|
|
250
|
+
dt.setUTCSeconds(0);
|
|
251
|
+
dt.setUTCMilliseconds(0);
|
|
252
|
+
return dt;
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
static format(dt: Date, fmt: string) {
|
|
256
|
+
if (!fmt) {
|
|
257
|
+
fmt = 'YYYY-MM-DD';
|
|
258
|
+
}
|
|
259
|
+
if (!dt) {
|
|
260
|
+
dt = new Date();
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
const parts: { [key: string]: string } = {
|
|
264
|
+
YYYY: dt.getFullYear().toString(),
|
|
265
|
+
YY: ('00' + (dt.getFullYear() - 100)).toString().slice(-2),
|
|
266
|
+
MM: ('0' + (dt.getMonth() + 1)).toString().slice(-2),
|
|
267
|
+
M: (dt.getMonth() + 1).toString(),
|
|
268
|
+
DD: ('0' + dt.getDate()).toString().slice(-2),
|
|
269
|
+
D: dt.getDate().toString(),
|
|
270
|
+
hh: ('0' + dt.getHours()).toString().slice(-2),
|
|
271
|
+
h: dt.getHours().toString(),
|
|
272
|
+
mm: ('0' + dt.getMinutes()).toString().slice(-2),
|
|
273
|
+
ss: ('0' + dt.getSeconds()).toString().slice(-2),
|
|
274
|
+
SSS: ('00' + dt.getMilliseconds()).toString().slice(-3),
|
|
275
|
+
S: Math.floor(dt.getMilliseconds() / 100)
|
|
276
|
+
.toString()
|
|
277
|
+
.slice(-1),
|
|
278
|
+
};
|
|
279
|
+
|
|
280
|
+
const array = fmt.match(/(\[[^\[]*\])|(\\)?(YYYY|YY|MM?|DD?|hh?|mm?|ss?|SSS|S|.)/g) as string[];
|
|
281
|
+
for (let i = 0, length = array.length; i < length; i++) {
|
|
282
|
+
if (parts[array[i]]) {
|
|
283
|
+
array[i] = parts[array[i]];
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
const ret = array.join('');
|
|
287
|
+
return ret;
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
export class DiffDate {
|
|
292
|
+
years: number;
|
|
293
|
+
months: number;
|
|
294
|
+
days: number;
|
|
295
|
+
hours: number;
|
|
296
|
+
minutes: number;
|
|
297
|
+
seconds: number;
|
|
298
|
+
milliseconds: number;
|
|
299
|
+
|
|
300
|
+
constructor(
|
|
301
|
+
years: number,
|
|
302
|
+
months: number,
|
|
303
|
+
days: number,
|
|
304
|
+
hours: number,
|
|
305
|
+
minutes: number,
|
|
306
|
+
seconds: number,
|
|
307
|
+
milliseconds: number
|
|
308
|
+
) {
|
|
309
|
+
this.years = years;
|
|
310
|
+
this.months = months;
|
|
311
|
+
this.days = days;
|
|
312
|
+
this.hours = hours;
|
|
313
|
+
this.minutes = minutes;
|
|
314
|
+
this.seconds = seconds;
|
|
315
|
+
this.milliseconds = milliseconds;
|
|
316
|
+
}
|
|
317
|
+
}
|