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