-
Notifications
You must be signed in to change notification settings - Fork 629
/
dayjs.ts
311 lines (276 loc) · 9.17 KB
/
dayjs.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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
137
138
139
140
141
142
143
144
145
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
import originDayjs, { Dayjs, OpUnitType, UnitType } from 'dayjs';
import customParseFormat from 'dayjs/plugin/customParseFormat';
import isBetween from 'dayjs/plugin/isBetween';
import weekOfYear from 'dayjs/plugin/weekOfYear';
import AdvancedFormat from 'dayjs/plugin/advancedFormat';
import weekYear from 'dayjs/plugin/weekYear';
import QuarterOfYear from 'dayjs/plugin/quarterOfYear';
import { isDayjs, isArray, isUndefined } from '../_util/is';
const isMoment = (originDayjs() as any)._isAMomentObject;
if (isMoment) {
(originDayjs as any).extend = () => {};
}
const overwriteIsDayjs = (_, Dayjs, dayjs) => {
dayjs = function (date, c) {
if (isDayjs(date)) {
return date.clone();
}
const cfg = typeof c === 'object' ? c : {};
cfg.date = date;
cfg.args = arguments; // eslint-disable-line prefer-rest-params
return new Dayjs(cfg);
};
const proto = Dayjs.prototype;
const old$Utils = proto.$utils;
proto.$utils = function () {
const newUtils = old$Utils();
newUtils.i = isDayjs;
return newUtils;
};
dayjs.isDayjs = isDayjs;
};
originDayjs.extend(overwriteIsDayjs);
originDayjs.extend(customParseFormat);
originDayjs.extend(isBetween);
originDayjs.extend(weekOfYear);
originDayjs.extend(AdvancedFormat);
originDayjs.extend(weekYear);
originDayjs.extend(QuarterOfYear);
export const dayjs = originDayjs;
function startOfWeekTimestamp(date, weekStart: number) {
// 计算 date 与前一个 weekStart 日期的间隔
const diff = (date.day() - weekStart + 7) % 7;
const startOfWeek = date.clone().startOf('day').subtract(diff, 'day');
return startOfWeek.valueOf();
}
function isSameWeekMoment(date1, date2, weekStart: number) {
return startOfWeekTimestamp(date1, weekStart) === startOfWeekTimestamp(date2, weekStart);
}
// 兼容 moment
export const methods = {
add(time, value: number, unit: UnitType) {
return isMoment ? time.clone().add(value, unit) : time.add(value, unit);
},
subtract(time, value: number, unit: UnitType) {
return isMoment ? time.clone().subtract(value, unit) : time.subtract(value, unit);
},
startOf(time, unit?: OpUnitType) {
return isMoment ? time.clone().startOf(unit) : time.startOf(unit);
},
endOf(time, unit?: OpUnitType) {
return isMoment ? time.clone().endOf(unit) : time.endOf(unit);
},
set(time, unit: UnitType, value: number) {
return isMoment ? time.clone().set(unit, value) : time.set(unit, value);
},
isSameWeek(date1, date2, weekStart: number, localeName) {
return isMoment
? isSameWeekMoment(date1, date2, weekStart)
: date1.locale({ ...dayjs.Ls[localeName], weekStart }).isSame(date2, 'week');
},
};
// convert timezone to utcOffset
// https://github.com/iamkun/dayjs/blob/dev/src/plugin/timezone/index.js#L3
const typeToPos = {
year: 0,
month: 1,
day: 2,
hour: 3,
minute: 4,
second: 5,
};
// Cache time-zone lookups from Intl.DateTimeFormat,
// as it is a *very* slow method.
const dtfCache = {};
const getDateTimeFormat = (timezone, timeZoneName?: string) => {
const key = `${timezone}|${timeZoneName || 'short'}`;
let dtf = dtfCache[key];
if (!dtf) {
dtf = new Intl.DateTimeFormat('en-US', {
hour12: false,
timeZone: timezone,
year: 'numeric',
month: '2-digit',
day: '2-digit',
hour: '2-digit',
minute: '2-digit',
second: '2-digit',
});
dtfCache[key] = dtf;
}
return dtf;
};
const makeFormatParts = (timestamp, timezone) => {
const date = new Date(timestamp);
const dtf = getDateTimeFormat(timezone);
return dtf.formatToParts(date);
};
const tzOffset = (timestamp, timezone) => {
const formatResult = makeFormatParts(timestamp, timezone);
const filled = [];
for (let i = 0; i < formatResult.length; i += 1) {
const { type, value } = formatResult[i];
const pos = typeToPos[type];
if (pos >= 0) {
filled[pos] = parseInt(value, 10);
}
}
const hour = filled[3];
// Workaround for the same behavior in different node version
// https://github.com/nodejs/node/issues/33027
/* istanbul ignore next */
const fixedHour = hour === 24 ? 0 : hour;
const utcTs = Date.UTC(filled[0], filled[1] - 1, filled[2], fixedHour, filled[4], filled[5], 0);
let asTS = +timestamp;
const over = asTS % 1000;
asTS -= over;
return (utcTs - asTS) / (60 * 1000);
};
// find the right offset a given local time. The o input is our guess, which determines which
// offset we'll pick in ambiguous cases (e.g. there are two 3 AMs b/c Fallback DST)
// https://github.com/moment/luxon/blob/master/src/datetime.js#L76
const fixOffset = (localTS, o0, tz) => {
// Our UTC time is just a guess because our offset is just a guess
let utcGuess = localTS - o0 * 60 * 1000;
// Test whether the zone matches the offset for this ts
const o2 = tzOffset(utcGuess, tz);
// If so, offset didn't change and we're done
if (o0 === o2) {
return [utcGuess, o0];
}
// If not, change the ts by the difference in the offset
utcGuess -= (o2 - o0) * 60 * 1000;
// If that gives us the local time we want, we're done
const o3 = tzOffset(utcGuess, tz);
if (o2 === o3) {
return [utcGuess, o2];
}
// If it's different, we're in a hole time.
// The offset has changed, but the we don't adjust the time
return [localTS - Math.min(o2, o3) * 60 * 1000, Math.max(o2, o3)];
};
export function timezoneToOffset(inputTs: number, timezone: string) {
return fixOffset(inputTs, tzOffset(new Date().getTime(), timezone), timezone)[1];
}
// get local now time
export function getNow(utcOffset?: number, timezone?: string) {
return isUndefined(utcOffset) && !timezone ? dayjs() : toTimezone(dayjs(), utcOffset, timezone);
}
// convert local date to specify timezone date
export function toTimezone(
time: Dayjs,
utcOffset?: number,
timezone?: string,
local?: boolean
): Dayjs {
if (!time || (isUndefined(utcOffset) && !timezone)) {
return time;
}
const localOffset = -time.toDate().getTimezoneOffset();
const uOffset = isUndefined(utcOffset)
? !timezone
? localOffset
: timezoneToOffset(time.valueOf(), timezone)
: utcOffset;
const timezoneOffset = Math.abs(uOffset) <= 16 ? uOffset * 60 : uOffset;
const diffOffset = local ? localOffset - timezoneOffset : timezoneOffset - localOffset;
return dayjs(dayjs(time).valueOf() + diffOffset * 60 * 1000);
}
// convert specify timezone date to local date
export function toLocal(time: Dayjs, utcOffset?: number, timezone?: string): Dayjs {
return toTimezone(time, utcOffset, timezone, true);
}
export function getTimeFormat(format) {
const units = ['H', 'h', 'm', 's', 'A', 'a'];
let timeFormat = '';
units.some((unit) => {
if (format.indexOf(unit) !== -1) {
timeFormat = `${unit}${format.split(` ${unit}`)[1]}`;
return true;
}
return false;
});
return timeFormat || 'HH:mm:ss';
}
export function getDayjsValue(
time,
format: string | string[],
utcOffset?: number,
timezone?: string
) {
if (!time) {
return undefined;
}
const formatValue = (value, i) => {
if (isDayjs(value)) {
return dayjs(value.valueOf());
}
if (typeof value === 'string') {
const dv = dayjs(value, isArray(format) ? format[i] : format);
return dv.isValid() ? dv : dayjs(value, 'YYYY-MM-DD');
}
return dayjs(value);
};
// if set a timezone, convert to timezone date
const getRealTime = (t, i) =>
utcOffset !== undefined || timezone
? toTimezone(formatValue(t, i), utcOffset, timezone)
: formatValue(t, i);
if (isArray(time)) {
return time.map((t, i) => (t ? getRealTime(t, i) : undefined));
}
return getRealTime(time, 0);
}
export function getValueWithTime(date: Dayjs, time?: Dayjs): Dayjs {
const y = date.year();
const m = date.month();
const d = date.date();
if (time) {
let returnTime = time;
returnTime = methods.set(returnTime, 'year', y);
returnTime = methods.set(returnTime, 'month', m);
returnTime = methods.set(returnTime, 'date', d);
return returnTime;
}
return date;
}
export function getSortedDayjsArray(values?: Dayjs[]) {
if (!values || !values[0] || !values[1]) {
return values;
}
const newValues = [...values];
newValues.sort((a, b) => a.valueOf() - b.valueOf());
return newValues;
}
export function isDayjsChange(prevValue: Dayjs | undefined, currentValue: Dayjs | undefined) {
if (currentValue === undefined && prevValue === undefined) {
return false;
}
return (
(currentValue && !prevValue) ||
(!currentValue && prevValue) ||
dayjs(currentValue).valueOf() !== dayjs(prevValue).valueOf()
);
}
export function isDayjsArrayChange(
prevValue: Dayjs[] | undefined,
currentValue: Dayjs[] | undefined
) {
if (currentValue === undefined && prevValue === undefined) {
return false;
}
return (
(currentValue && !prevValue) ||
(!currentValue && prevValue) ||
(isArray(currentValue) &&
isArray(prevValue) &&
dayjs(currentValue[0]).valueOf() !== dayjs(prevValue[0]).valueOf()) ||
dayjs(currentValue[1]).valueOf() !== dayjs(prevValue[1]).valueOf()
);
}
export function isValidTimeString(str: string, format: string | string[], index?: number) {
return (
typeof str === 'string' &&
dayjs(str, format).format(isArray(format) ? format[index] : format) === str
);
}