1 | import * as C from './constant';
|
2 | import en from './locale/en';
|
3 | import U from './utils';
|
4 | var L = 'en';
|
5 |
|
6 | var Ls = {};
|
7 |
|
8 | Ls[L] = en;
|
9 | var IS_DAYJS = '$isDayjsObject';
|
10 |
|
11 | var isDayjs = function isDayjs(d) {
|
12 | return d instanceof Dayjs || !!(d && d[IS_DAYJS]);
|
13 | };
|
14 |
|
15 | var parseLocale = function parseLocale(preset, object, isLocal) {
|
16 | var l;
|
17 | if (!preset) return L;
|
18 |
|
19 | if (typeof preset === 'string') {
|
20 | var presetLower = preset.toLowerCase();
|
21 |
|
22 | if (Ls[presetLower]) {
|
23 | l = presetLower;
|
24 | }
|
25 |
|
26 | if (object) {
|
27 | Ls[presetLower] = object;
|
28 | l = presetLower;
|
29 | }
|
30 |
|
31 | var presetSplit = preset.split('-');
|
32 |
|
33 | if (!l && presetSplit.length > 1) {
|
34 | return parseLocale(presetSplit[0]);
|
35 | }
|
36 | } else {
|
37 | var name = preset.name;
|
38 | Ls[name] = preset;
|
39 | l = name;
|
40 | }
|
41 |
|
42 | if (!isLocal && l) L = l;
|
43 | return l || !isLocal && L;
|
44 | };
|
45 |
|
46 | var dayjs = function dayjs(date, c) {
|
47 | if (isDayjs(date)) {
|
48 | return date.clone();
|
49 | }
|
50 |
|
51 |
|
52 | var cfg = typeof c === 'object' ? c : {};
|
53 | cfg.date = date;
|
54 | cfg.args = arguments;
|
55 |
|
56 | return new Dayjs(cfg);
|
57 | };
|
58 |
|
59 | var wrapper = function wrapper(date, instance) {
|
60 | return dayjs(date, {
|
61 | locale: instance.$L,
|
62 | utc: instance.$u,
|
63 | x: instance.$x,
|
64 | $offset: instance.$offset
|
65 |
|
66 | });
|
67 | };
|
68 |
|
69 | var Utils = U;
|
70 |
|
71 | Utils.l = parseLocale;
|
72 | Utils.i = isDayjs;
|
73 | Utils.w = wrapper;
|
74 |
|
75 | var parseDate = function parseDate(cfg) {
|
76 | var date = cfg.date,
|
77 | utc = cfg.utc;
|
78 | if (date === null) return new Date(NaN);
|
79 |
|
80 | if (Utils.u(date)) return new Date();
|
81 |
|
82 | if (date instanceof Date) return new Date(date);
|
83 |
|
84 | if (typeof date === 'string' && !/Z$/i.test(date)) {
|
85 | var d = date.match(C.REGEX_PARSE);
|
86 |
|
87 | if (d) {
|
88 | var m = d[2] - 1 || 0;
|
89 | var ms = (d[7] || '0').substring(0, 3);
|
90 |
|
91 | if (utc) {
|
92 | return new Date(Date.UTC(d[1], m, d[3] || 1, d[4] || 0, d[5] || 0, d[6] || 0, ms));
|
93 | }
|
94 |
|
95 | return new Date(d[1], m, d[3] || 1, d[4] || 0, d[5] || 0, d[6] || 0, ms);
|
96 | }
|
97 | }
|
98 |
|
99 | return new Date(date);
|
100 | };
|
101 |
|
102 | var Dayjs = function () {
|
103 | function Dayjs(cfg) {
|
104 | this.$L = parseLocale(cfg.locale, null, true);
|
105 | this.parse(cfg);
|
106 |
|
107 | this.$x = this.$x || cfg.x || {};
|
108 | this[IS_DAYJS] = true;
|
109 | }
|
110 |
|
111 | var _proto = Dayjs.prototype;
|
112 |
|
113 | _proto.parse = function parse(cfg) {
|
114 | this.$d = parseDate(cfg);
|
115 | this.init();
|
116 | };
|
117 |
|
118 | _proto.init = function init() {
|
119 | var $d = this.$d;
|
120 | this.$y = $d.getFullYear();
|
121 | this.$M = $d.getMonth();
|
122 | this.$D = $d.getDate();
|
123 | this.$W = $d.getDay();
|
124 | this.$H = $d.getHours();
|
125 | this.$m = $d.getMinutes();
|
126 | this.$s = $d.getSeconds();
|
127 | this.$ms = $d.getMilliseconds();
|
128 | }
|
129 | ;
|
130 |
|
131 | _proto.$utils = function $utils() {
|
132 | return Utils;
|
133 | };
|
134 |
|
135 | _proto.isValid = function isValid() {
|
136 | return !(this.$d.toString() === C.INVALID_DATE_STRING);
|
137 | };
|
138 |
|
139 | _proto.isSame = function isSame(that, units) {
|
140 | var other = dayjs(that);
|
141 | return this.startOf(units) <= other && other <= this.endOf(units);
|
142 | };
|
143 |
|
144 | _proto.isAfter = function isAfter(that, units) {
|
145 | return dayjs(that) < this.startOf(units);
|
146 | };
|
147 |
|
148 | _proto.isBefore = function isBefore(that, units) {
|
149 | return this.endOf(units) < dayjs(that);
|
150 | };
|
151 |
|
152 | _proto.$g = function $g(input, get, set) {
|
153 | if (Utils.u(input)) return this[get];
|
154 | return this.set(set, input);
|
155 | };
|
156 |
|
157 | _proto.unix = function unix() {
|
158 | return Math.floor(this.valueOf() / 1000);
|
159 | };
|
160 |
|
161 | _proto.valueOf = function valueOf() {
|
162 |
|
163 | return this.$d.getTime();
|
164 | };
|
165 |
|
166 | _proto.startOf = function startOf(units, _startOf) {
|
167 | var _this = this;
|
168 |
|
169 |
|
170 | var isStartOf = !Utils.u(_startOf) ? _startOf : true;
|
171 | var unit = Utils.p(units);
|
172 |
|
173 | var instanceFactory = function instanceFactory(d, m) {
|
174 | var ins = Utils.w(_this.$u ? Date.UTC(_this.$y, m, d) : new Date(_this.$y, m, d), _this);
|
175 | return isStartOf ? ins : ins.endOf(C.D);
|
176 | };
|
177 |
|
178 | var instanceFactorySet = function instanceFactorySet(method, slice) {
|
179 | var argumentStart = [0, 0, 0, 0];
|
180 | var argumentEnd = [23, 59, 59, 999];
|
181 | return Utils.w(_this.toDate()[method].apply(
|
182 | _this.toDate('s'), (isStartOf ? argumentStart : argumentEnd).slice(slice)), _this);
|
183 | };
|
184 |
|
185 | var $W = this.$W,
|
186 | $M = this.$M,
|
187 | $D = this.$D;
|
188 | var utcPad = "set" + (this.$u ? 'UTC' : '');
|
189 |
|
190 | switch (unit) {
|
191 | case C.Y:
|
192 | return isStartOf ? instanceFactory(1, 0) : instanceFactory(31, 11);
|
193 |
|
194 | case C.M:
|
195 | return isStartOf ? instanceFactory(1, $M) : instanceFactory(0, $M + 1);
|
196 |
|
197 | case C.W:
|
198 | {
|
199 | var weekStart = this.$locale().weekStart || 0;
|
200 | var gap = ($W < weekStart ? $W + 7 : $W) - weekStart;
|
201 | return instanceFactory(isStartOf ? $D - gap : $D + (6 - gap), $M);
|
202 | }
|
203 |
|
204 | case C.D:
|
205 | case C.DATE:
|
206 | return instanceFactorySet(utcPad + "Hours", 0);
|
207 |
|
208 | case C.H:
|
209 | return instanceFactorySet(utcPad + "Minutes", 1);
|
210 |
|
211 | case C.MIN:
|
212 | return instanceFactorySet(utcPad + "Seconds", 2);
|
213 |
|
214 | case C.S:
|
215 | return instanceFactorySet(utcPad + "Milliseconds", 3);
|
216 |
|
217 | default:
|
218 | return this.clone();
|
219 | }
|
220 | };
|
221 |
|
222 | _proto.endOf = function endOf(arg) {
|
223 | return this.startOf(arg, false);
|
224 | };
|
225 |
|
226 | _proto.$set = function $set(units, _int) {
|
227 | var _C$D$C$DATE$C$M$C$Y$C;
|
228 |
|
229 |
|
230 | var unit = Utils.p(units);
|
231 | var utcPad = "set" + (this.$u ? 'UTC' : '');
|
232 | var name = (_C$D$C$DATE$C$M$C$Y$C = {}, _C$D$C$DATE$C$M$C$Y$C[C.D] = utcPad + "Date", _C$D$C$DATE$C$M$C$Y$C[C.DATE] = utcPad + "Date", _C$D$C$DATE$C$M$C$Y$C[C.M] = utcPad + "Month", _C$D$C$DATE$C$M$C$Y$C[C.Y] = utcPad + "FullYear", _C$D$C$DATE$C$M$C$Y$C[C.H] = utcPad + "Hours", _C$D$C$DATE$C$M$C$Y$C[C.MIN] = utcPad + "Minutes", _C$D$C$DATE$C$M$C$Y$C[C.S] = utcPad + "Seconds", _C$D$C$DATE$C$M$C$Y$C[C.MS] = utcPad + "Milliseconds", _C$D$C$DATE$C$M$C$Y$C)[unit];
|
233 | var arg = unit === C.D ? this.$D + (_int - this.$W) : _int;
|
234 |
|
235 | if (unit === C.M || unit === C.Y) {
|
236 |
|
237 | var date = this.clone().set(C.DATE, 1);
|
238 | date.$d[name](arg);
|
239 | date.init();
|
240 | this.$d = date.set(C.DATE, Math.min(this.$D, date.daysInMonth())).$d;
|
241 | } else if (name) this.$d[name](arg);
|
242 |
|
243 | this.init();
|
244 | return this;
|
245 | };
|
246 |
|
247 | _proto.set = function set(string, _int2) {
|
248 | return this.clone().$set(string, _int2);
|
249 | };
|
250 |
|
251 | _proto.get = function get(unit) {
|
252 | return this[Utils.p(unit)]();
|
253 | };
|
254 |
|
255 | _proto.add = function add(number, units) {
|
256 | var _this2 = this,
|
257 | _C$MIN$C$H$C$S$unit;
|
258 |
|
259 | number = Number(number);
|
260 |
|
261 | var unit = Utils.p(units);
|
262 |
|
263 | var instanceFactorySet = function instanceFactorySet(n) {
|
264 | var d = dayjs(_this2);
|
265 | return Utils.w(d.date(d.date() + Math.round(n * number)), _this2);
|
266 | };
|
267 |
|
268 | if (unit === C.M) {
|
269 | return this.set(C.M, this.$M + number);
|
270 | }
|
271 |
|
272 | if (unit === C.Y) {
|
273 | return this.set(C.Y, this.$y + number);
|
274 | }
|
275 |
|
276 | if (unit === C.D) {
|
277 | return instanceFactorySet(1);
|
278 | }
|
279 |
|
280 | if (unit === C.W) {
|
281 | return instanceFactorySet(7);
|
282 | }
|
283 |
|
284 | var step = (_C$MIN$C$H$C$S$unit = {}, _C$MIN$C$H$C$S$unit[C.MIN] = C.MILLISECONDS_A_MINUTE, _C$MIN$C$H$C$S$unit[C.H] = C.MILLISECONDS_A_HOUR, _C$MIN$C$H$C$S$unit[C.S] = C.MILLISECONDS_A_SECOND, _C$MIN$C$H$C$S$unit)[unit] || 1;
|
285 |
|
286 | var nextTimeStamp = this.$d.getTime() + number * step;
|
287 | return Utils.w(nextTimeStamp, this);
|
288 | };
|
289 |
|
290 | _proto.subtract = function subtract(number, string) {
|
291 | return this.add(number * -1, string);
|
292 | };
|
293 |
|
294 | _proto.format = function format(formatStr) {
|
295 | var _this3 = this;
|
296 |
|
297 | var locale = this.$locale();
|
298 | if (!this.isValid()) return locale.invalidDate || C.INVALID_DATE_STRING;
|
299 | var str = formatStr || C.FORMAT_DEFAULT;
|
300 | var zoneStr = Utils.z(this);
|
301 | var $H = this.$H,
|
302 | $m = this.$m,
|
303 | $M = this.$M;
|
304 | var weekdays = locale.weekdays,
|
305 | months = locale.months,
|
306 | meridiem = locale.meridiem;
|
307 |
|
308 | var getShort = function getShort(arr, index, full, length) {
|
309 | return arr && (arr[index] || arr(_this3, str)) || full[index].slice(0, length);
|
310 | };
|
311 |
|
312 | var get$H = function get$H(num) {
|
313 | return Utils.s($H % 12 || 12, num, '0');
|
314 | };
|
315 |
|
316 | var meridiemFunc = meridiem || function (hour, minute, isLowercase) {
|
317 | var m = hour < 12 ? 'AM' : 'PM';
|
318 | return isLowercase ? m.toLowerCase() : m;
|
319 | };
|
320 |
|
321 | var matches = function matches(match) {
|
322 | switch (match) {
|
323 | case 'YY':
|
324 | return String(_this3.$y).slice(-2);
|
325 |
|
326 | case 'YYYY':
|
327 | return Utils.s(_this3.$y, 4, '0');
|
328 |
|
329 | case 'M':
|
330 | return $M + 1;
|
331 |
|
332 | case 'MM':
|
333 | return Utils.s($M + 1, 2, '0');
|
334 |
|
335 | case 'MMM':
|
336 | return getShort(locale.monthsShort, $M, months, 3);
|
337 |
|
338 | case 'MMMM':
|
339 | return getShort(months, $M);
|
340 |
|
341 | case 'D':
|
342 | return _this3.$D;
|
343 |
|
344 | case 'DD':
|
345 | return Utils.s(_this3.$D, 2, '0');
|
346 |
|
347 | case 'd':
|
348 | return String(_this3.$W);
|
349 |
|
350 | case 'dd':
|
351 | return getShort(locale.weekdaysMin, _this3.$W, weekdays, 2);
|
352 |
|
353 | case 'ddd':
|
354 | return getShort(locale.weekdaysShort, _this3.$W, weekdays, 3);
|
355 |
|
356 | case 'dddd':
|
357 | return weekdays[_this3.$W];
|
358 |
|
359 | case 'H':
|
360 | return String($H);
|
361 |
|
362 | case 'HH':
|
363 | return Utils.s($H, 2, '0');
|
364 |
|
365 | case 'h':
|
366 | return get$H(1);
|
367 |
|
368 | case 'hh':
|
369 | return get$H(2);
|
370 |
|
371 | case 'a':
|
372 | return meridiemFunc($H, $m, true);
|
373 |
|
374 | case 'A':
|
375 | return meridiemFunc($H, $m, false);
|
376 |
|
377 | case 'm':
|
378 | return String($m);
|
379 |
|
380 | case 'mm':
|
381 | return Utils.s($m, 2, '0');
|
382 |
|
383 | case 's':
|
384 | return String(_this3.$s);
|
385 |
|
386 | case 'ss':
|
387 | return Utils.s(_this3.$s, 2, '0');
|
388 |
|
389 | case 'SSS':
|
390 | return Utils.s(_this3.$ms, 3, '0');
|
391 |
|
392 | case 'Z':
|
393 | return zoneStr;
|
394 | // 'ZZ' logic below
|
395 |
|
396 | default:
|
397 | break;
|
398 | }
|
399 |
|
400 | return null;
|
401 | };
|
402 |
|
403 | return str.replace(C.REGEX_FORMAT, function (match, $1) {
|
404 | return $1 || matches(match) || zoneStr.replace(':', '');
|
405 | });
|
406 | };
|
407 |
|
408 | _proto.utcOffset = function utcOffset() {
|
409 |
|
410 |
|
411 | return -Math.round(this.$d.getTimezoneOffset() / 15) * 15;
|
412 | };
|
413 |
|
414 | _proto.diff = function diff(input, units, _float) {
|
415 | var _this4 = this;
|
416 |
|
417 | var unit = Utils.p(units);
|
418 | var that = dayjs(input);
|
419 | var zoneDelta = (that.utcOffset() - this.utcOffset()) * C.MILLISECONDS_A_MINUTE;
|
420 | var diff = this - that;
|
421 |
|
422 | var getMonth = function getMonth() {
|
423 | return Utils.m(_this4, that);
|
424 | };
|
425 |
|
426 | var result;
|
427 |
|
428 | switch (unit) {
|
429 | case C.Y:
|
430 | result = getMonth() / 12;
|
431 | break;
|
432 |
|
433 | case C.M:
|
434 | result = getMonth();
|
435 | break;
|
436 |
|
437 | case C.Q:
|
438 | result = getMonth() / 3;
|
439 | break;
|
440 |
|
441 | case C.W:
|
442 | result = (diff - zoneDelta) / C.MILLISECONDS_A_WEEK;
|
443 | break;
|
444 |
|
445 | case C.D:
|
446 | result = (diff - zoneDelta) / C.MILLISECONDS_A_DAY;
|
447 | break;
|
448 |
|
449 | case C.H:
|
450 | result = diff / C.MILLISECONDS_A_HOUR;
|
451 | break;
|
452 |
|
453 | case C.MIN:
|
454 | result = diff / C.MILLISECONDS_A_MINUTE;
|
455 | break;
|
456 |
|
457 | case C.S:
|
458 | result = diff / C.MILLISECONDS_A_SECOND;
|
459 | break;
|
460 |
|
461 | default:
|
462 | result = diff;
|
463 |
|
464 | break;
|
465 | }
|
466 |
|
467 | return _float ? result : Utils.a(result);
|
468 | };
|
469 |
|
470 | _proto.daysInMonth = function daysInMonth() {
|
471 | return this.endOf(C.M).$D;
|
472 | };
|
473 |
|
474 | _proto.$locale = function $locale() {
|
475 |
|
476 | return Ls[this.$L];
|
477 | };
|
478 |
|
479 | _proto.locale = function locale(preset, object) {
|
480 | if (!preset) return this.$L;
|
481 | var that = this.clone();
|
482 | var nextLocaleName = parseLocale(preset, object, true);
|
483 | if (nextLocaleName) that.$L = nextLocaleName;
|
484 | return that;
|
485 | };
|
486 |
|
487 | _proto.clone = function clone() {
|
488 | return Utils.w(this.$d, this);
|
489 | };
|
490 |
|
491 | _proto.toDate = function toDate() {
|
492 | return new Date(this.valueOf());
|
493 | };
|
494 |
|
495 | _proto.toJSON = function toJSON() {
|
496 | return this.isValid() ? this.toISOString() : null;
|
497 | };
|
498 |
|
499 | _proto.toISOString = function toISOString() {
|
500 |
|
501 |
|
502 |
|
503 | return this.$d.toISOString();
|
504 | };
|
505 |
|
506 | _proto.toString = function toString() {
|
507 | return this.$d.toUTCString();
|
508 | };
|
509 |
|
510 | return Dayjs;
|
511 | }();
|
512 |
|
513 | var proto = Dayjs.prototype;
|
514 | dayjs.prototype = proto;
|
515 | [['$ms', C.MS], ['$s', C.S], ['$m', C.MIN], ['$H', C.H], ['$W', C.D], ['$M', C.M], ['$y', C.Y], ['$D', C.DATE]].forEach(function (g) {
|
516 | proto[g[1]] = function (input) {
|
517 | return this.$g(input, g[0], g[1]);
|
518 | };
|
519 | });
|
520 |
|
521 | dayjs.extend = function (plugin, option) {
|
522 | if (!plugin.$i) {
|
523 |
|
524 | plugin(option, Dayjs, dayjs);
|
525 | plugin.$i = true;
|
526 | }
|
527 |
|
528 | return dayjs;
|
529 | };
|
530 |
|
531 | dayjs.locale = parseLocale;
|
532 | dayjs.isDayjs = isDayjs;
|
533 |
|
534 | dayjs.unix = function (timestamp) {
|
535 | return dayjs(timestamp * 1e3);
|
536 | };
|
537 |
|
538 | dayjs.en = Ls[L];
|
539 | dayjs.Ls = Ls;
|
540 | dayjs.p = {};
|
541 | export default dayjs; |
\ | No newline at end of file |