Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
},
"dependencies": {
"classnames": "^2.2.1",
"dayjs": "^1.8.18",
"moment": "^2.24.0",
"rc-trigger": "^4.0.0-alpha.6",
"rc-util": "^4.15.7"
Expand Down
124 changes: 124 additions & 0 deletions src/generate/dayjs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
import dayjs, { Dayjs } from 'dayjs';
import { noteOnce } from 'rc-util/lib/warning';
import weekday from 'dayjs/plugin/weekday';
import localeData from 'dayjs/plugin/localeData';
import weekOfYear from 'dayjs/plugin/weekOfYear';
import weekYear from 'dayjs/plugin/weekYear';
import advancedFormat from 'dayjs/plugin/advancedFormat';
import customParseFormat from 'dayjs/plugin/customParseFormat';
import { GenerateConfig } from '.';

dayjs.extend(customParseFormat);
dayjs.extend(advancedFormat);
dayjs.extend(weekday);
dayjs.extend(localeData);
dayjs.extend(weekOfYear);
dayjs.extend(weekYear);

dayjs.extend((o, c) => {
// todo support Wo (ISO week)
const proto = c.prototype;
const oldFormat = proto.format;
proto.format = function f(formatStr: string) {
const str = (formatStr || '').replace('Wo', 'wo');
return oldFormat.bind(this)(str);
};
});

type IlocaleMapObject = { [key: string]: string };
const localeMap: IlocaleMapObject = {
en_GB: 'en-gb',
en_US: 'en',
zh_CN: 'zh-cn',
zh_TW: 'zh-tw',
};

const parseLocale = (locale: string) => {
const mapLocale = localeMap[locale];
return mapLocale || locale.split('_')[0];
};

const parseNoMatchNotice = () => {
/* istanbul ignore next */
noteOnce(
false,
'Not match any format. Please help to fire a issue about this.',
);
}

const generateConfig: GenerateConfig<Dayjs> = {
// get
getNow: () => dayjs(),
getWeekDay: date => date.weekday(),
getYear: date => date.year(),
getMonth: date => date.month(),
getDate: date => date.date(),
getHour: date => date.hour(),
getMinute: date => date.minute(),
getSecond: date => date.second(),

// set
addYear: (date, diff) => date.add(diff, 'year'),
addMonth: (date, diff) => date.add(diff, 'month'),
addDate: (date, diff) => date.add(diff, 'day'),
setYear: (date, year) => date.year(year),
setMonth: (date, month) => date.month(month),
setDate: (date, num) => date.date(num),
setHour: (date, hour) => date.hour(hour),
setMinute: (date, minute) => date.minute(minute),
setSecond: (date, second) => date.second(second),

// Compare
isAfter: (date1, date2) => date1.isAfter(date2),
isValidate: date => date.isValid(),

locale: {
getWeekFirstDay: locale =>
dayjs()
.locale(parseLocale(locale))
.localeData()
.firstDayOfWeek(),
getWeek: (locale, date) => date.locale(parseLocale(locale)).week(),
getShortWeekDays: locale =>
dayjs()
.locale(parseLocale(locale))
.localeData()
.weekdaysMin(),
getShortMonths: locale =>
dayjs()
.locale(parseLocale(locale))
.localeData()
.monthsShort(),
format: (locale, date, format) =>
date.locale(parseLocale(locale)).format(format),
parse: (locale, text, formats) => {
const localeStr = parseLocale(locale)
for (let i = 0; i < formats.length; i += 1) {
const format = formats[i];
const formatText = text;
if (format.includes('wo') || format.includes('Wo')) { // parse Wo
const year = formatText.split('-')[0]
const weekStr = formatText.split('-')[1]
const firstWeek = dayjs(year, 'YYYY').startOf('year').locale(localeStr)
for (let j = 0; j <= 52; j += 1) {
const nextWeek = firstWeek.add(j, 'week')
if (nextWeek.format('Wo') === weekStr) {
return nextWeek
}
}
parseNoMatchNotice()
return null;
}
const date = dayjs(formatText, format).locale(localeStr);
if (date.isValid()) {
return date;
}
}

parseNoMatchNotice()
return null;
},
},
};

export default generateConfig;
Loading