UNPKG

3.49 kBJavaScriptView Raw
1import { MILLISECONDS_A_MINUTE, MIN } from '../../constant';
2export default (function (option, Dayjs, dayjs) {
3 var proto = Dayjs.prototype;
4
5 dayjs.utc = function (date) {
6 var cfg = {
7 date: date,
8 utc: true,
9 args: arguments
10 }; // eslint-disable-line prefer-rest-params
11
12 return new Dayjs(cfg); // eslint-disable-line no-use-before-define
13 };
14
15 proto.utc = function (keepLocalTime) {
16 var ins = dayjs(this.toDate(), {
17 locale: this.$L,
18 utc: true
19 });
20
21 if (keepLocalTime) {
22 return ins.add(this.utcOffset(), MIN);
23 }
24
25 return ins;
26 };
27
28 proto.local = function () {
29 return dayjs(this.toDate(), {
30 locale: this.$L,
31 utc: false
32 });
33 };
34
35 var oldParse = proto.parse;
36
37 proto.parse = function (cfg) {
38 if (cfg.utc) {
39 this.$u = true;
40 }
41
42 if (!this.$utils().u(cfg.$offset)) {
43 this.$offset = cfg.$offset;
44 }
45
46 oldParse.call(this, cfg);
47 };
48
49 var oldInit = proto.init;
50
51 proto.init = function () {
52 if (this.$u) {
53 var $d = this.$d;
54 this.$y = $d.getUTCFullYear();
55 this.$M = $d.getUTCMonth();
56 this.$D = $d.getUTCDate();
57 this.$W = $d.getUTCDay();
58 this.$H = $d.getUTCHours();
59 this.$m = $d.getUTCMinutes();
60 this.$s = $d.getUTCSeconds();
61 this.$ms = $d.getUTCMilliseconds();
62 } else {
63 oldInit.call(this);
64 }
65 };
66
67 var oldUtcOffset = proto.utcOffset;
68
69 proto.utcOffset = function (input, keepLocalTime) {
70 var _this$$utils = this.$utils(),
71 u = _this$$utils.u;
72
73 if (u(input)) {
74 if (this.$u) {
75 return 0;
76 }
77
78 if (!u(this.$offset)) {
79 return this.$offset;
80 }
81
82 return oldUtcOffset.call(this);
83 }
84
85 var offset = Math.abs(input) <= 16 ? input * 60 : input;
86 var ins = this;
87
88 if (keepLocalTime) {
89 ins.$offset = offset;
90 ins.$u = input === 0;
91 return ins;
92 }
93
94 if (input !== 0) {
95 var localTimezoneOffset = this.$u ? this.toDate().getTimezoneOffset() : -1 * this.utcOffset();
96 ins = this.local().add(offset + localTimezoneOffset, MIN);
97 ins.$offset = offset;
98 ins.$x.$localOffset = localTimezoneOffset;
99 } else {
100 ins = this.utc();
101 }
102
103 return ins;
104 };
105
106 var oldFormat = proto.format;
107 var UTC_FORMAT_DEFAULT = 'YYYY-MM-DDTHH:mm:ss[Z]';
108
109 proto.format = function (formatStr) {
110 var str = formatStr || (this.$u ? UTC_FORMAT_DEFAULT : '');
111 return oldFormat.call(this, str);
112 };
113
114 proto.valueOf = function () {
115 var addedOffset = !this.$utils().u(this.$offset) ? this.$offset + (this.$x.$localOffset || new Date().getTimezoneOffset()) : 0;
116 return this.$d.valueOf() - addedOffset * MILLISECONDS_A_MINUTE;
117 };
118
119 proto.isUTC = function () {
120 return !!this.$u;
121 };
122
123 proto.toISOString = function () {
124 return this.toDate().toISOString();
125 };
126
127 proto.toString = function () {
128 return this.toDate().toUTCString();
129 };
130
131 var oldToDate = proto.toDate;
132
133 proto.toDate = function (type) {
134 if (type === 's' && this.$offset) {
135 return dayjs(this.format('YYYY-MM-DD HH:mm:ss:SSS')).toDate();
136 }
137
138 return oldToDate.call(this);
139 };
140
141 var oldDiff = proto.diff;
142
143 proto.diff = function (input, units, _float) {
144 if (input && this.$u === input.$u) {
145 return oldDiff.call(this, input, units, _float);
146 }
147
148 var localThis = this.local();
149 var localInput = dayjs(input).local();
150 return oldDiff.call(localThis, localInput, units, _float);
151 };
152});
\No newline at end of file