Skip to content

Commit caf70c4

Browse files
committed
2 parents c3fd750 + 0e6f389 commit caf70c4

File tree

3 files changed

+347
-108
lines changed

3 files changed

+347
-108
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
},
4040
"dependencies": {
4141
"classnames": "^2.2.1",
42+
"dayjs": "^1.8.18",
4243
"moment": "^2.24.0",
4344
"rc-trigger": "^4.0.0-alpha.6",
4445
"rc-util": "^4.15.7"

src/generate/dayjs.ts

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
import dayjs, { Dayjs } from 'dayjs';
2+
import { noteOnce } from 'rc-util/lib/warning';
3+
import weekday from 'dayjs/plugin/weekday';
4+
import localeData from 'dayjs/plugin/localeData';
5+
import weekOfYear from 'dayjs/plugin/weekOfYear';
6+
import weekYear from 'dayjs/plugin/weekYear';
7+
import advancedFormat from 'dayjs/plugin/advancedFormat';
8+
import customParseFormat from 'dayjs/plugin/customParseFormat';
9+
import { GenerateConfig } from '.';
10+
11+
dayjs.extend(customParseFormat);
12+
dayjs.extend(advancedFormat);
13+
dayjs.extend(weekday);
14+
dayjs.extend(localeData);
15+
dayjs.extend(weekOfYear);
16+
dayjs.extend(weekYear);
17+
18+
dayjs.extend((o, c) => {
19+
// todo support Wo (ISO week)
20+
const proto = c.prototype;
21+
const oldFormat = proto.format;
22+
proto.format = function f(formatStr: string) {
23+
const str = (formatStr || '').replace('Wo', 'wo');
24+
return oldFormat.bind(this)(str);
25+
};
26+
});
27+
28+
type IlocaleMapObject = { [key: string]: string };
29+
const localeMap: IlocaleMapObject = {
30+
en_GB: 'en-gb',
31+
en_US: 'en',
32+
zh_CN: 'zh-cn',
33+
zh_TW: 'zh-tw',
34+
};
35+
36+
const parseLocale = (locale: string) => {
37+
const mapLocale = localeMap[locale];
38+
return mapLocale || locale.split('_')[0];
39+
};
40+
41+
const parseNoMatchNotice = () => {
42+
/* istanbul ignore next */
43+
noteOnce(
44+
false,
45+
'Not match any format. Please help to fire a issue about this.',
46+
);
47+
}
48+
49+
const generateConfig: GenerateConfig<Dayjs> = {
50+
// get
51+
getNow: () => dayjs(),
52+
getWeekDay: date => date.weekday(),
53+
getYear: date => date.year(),
54+
getMonth: date => date.month(),
55+
getDate: date => date.date(),
56+
getHour: date => date.hour(),
57+
getMinute: date => date.minute(),
58+
getSecond: date => date.second(),
59+
60+
// set
61+
addYear: (date, diff) => date.add(diff, 'year'),
62+
addMonth: (date, diff) => date.add(diff, 'month'),
63+
addDate: (date, diff) => date.add(diff, 'day'),
64+
setYear: (date, year) => date.year(year),
65+
setMonth: (date, month) => date.month(month),
66+
setDate: (date, num) => date.date(num),
67+
setHour: (date, hour) => date.hour(hour),
68+
setMinute: (date, minute) => date.minute(minute),
69+
setSecond: (date, second) => date.second(second),
70+
71+
// Compare
72+
isAfter: (date1, date2) => date1.isAfter(date2),
73+
isValidate: date => date.isValid(),
74+
75+
locale: {
76+
getWeekFirstDay: locale =>
77+
dayjs()
78+
.locale(parseLocale(locale))
79+
.localeData()
80+
.firstDayOfWeek(),
81+
getWeek: (locale, date) => date.locale(parseLocale(locale)).week(),
82+
getShortWeekDays: locale =>
83+
dayjs()
84+
.locale(parseLocale(locale))
85+
.localeData()
86+
.weekdaysMin(),
87+
getShortMonths: locale =>
88+
dayjs()
89+
.locale(parseLocale(locale))
90+
.localeData()
91+
.monthsShort(),
92+
format: (locale, date, format) =>
93+
date.locale(parseLocale(locale)).format(format),
94+
parse: (locale, text, formats) => {
95+
const localeStr = parseLocale(locale)
96+
for (let i = 0; i < formats.length; i += 1) {
97+
const format = formats[i];
98+
const formatText = text;
99+
if (format.includes('wo') || format.includes('Wo')) { // parse Wo
100+
const year = formatText.split('-')[0]
101+
const weekStr = formatText.split('-')[1]
102+
const firstWeek = dayjs(year, 'YYYY').startOf('year').locale(localeStr)
103+
for (let j = 0; j <= 52; j += 1) {
104+
const nextWeek = firstWeek.add(j, 'week')
105+
if (nextWeek.format('Wo') === weekStr) {
106+
return nextWeek
107+
}
108+
}
109+
parseNoMatchNotice()
110+
return null;
111+
}
112+
const date = dayjs(formatText, format).locale(localeStr);
113+
if (date.isValid()) {
114+
return date;
115+
}
116+
}
117+
118+
parseNoMatchNotice()
119+
return null;
120+
},
121+
},
122+
};
123+
124+
export default generateConfig;

0 commit comments

Comments
 (0)