Skip to content

Commit

Permalink
update dayjs locale mapping
Browse files Browse the repository at this point in the history
  • Loading branch information
iamkun committed Apr 13, 2022
1 parent 7a06d66 commit 5eeedb3
Showing 1 changed file with 75 additions and 28 deletions.
103 changes: 75 additions & 28 deletions src/generate/dayjs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,70 @@ dayjs.extend((o, c) => {

type IlocaleMapObject = Record<string, string>;
const localeMap: IlocaleMapObject = {
// ar_EG:
// az_AZ:
// bg_BG:
bn_BD: 'bn-bd',
by_BY: 'be',
// ca_ES:
// cs_CZ:
// da_DK:
// de_DE:
// el_GR:
en_GB: 'en-gb',
en_US: 'en',
// es_ES:
// et_EE:
// fa_IR:
// fi_FI:
fr_BE: 'fr', // todo: dayjs has no fr_BE locale, use fr at present
fr_CA: 'fr-ca',
// fr_FR:
// ga_IE:
// gl_ES:
// he_IL:
// hi_IN:
// hr_HR:
// hu_HU:
hy_AM: 'hy-am',
// id_ID:
// is_IS:
// it_IT:
// ja_JP:
// ka_GE:
// kk_KZ:
// km_KH:
kmr_IQ: 'ku',
// kn_IN:
// ko_KR:
// ku_IQ: // previous ku in antd
// lt_LT:
// lv_LV:
// mk_MK:
// ml_IN:
// mn_MN:
// ms_MY:
// nb_NO:
// ne_NP:
nl_BE: 'nl-be',
// nl_NL:
// pl_PL:
pt_BR: 'pt-br',
// pt_PT:
// ro_RO:
// ru_RU:
// sk_SK:
// sl_SI:
// sr_RS:
// sv_SE:
// ta_IN:
// th_TH:
// tr_TR:
// uk_UA:
// ur_PK:
// vi_VN:
zh_CN: 'zh-cn',
zh_HK: 'zh-hk',
zh_TW: 'zh-tw',
};

Expand All @@ -47,18 +108,18 @@ const parseNoMatchNotice = () => {
const generateConfig: GenerateConfig<Dayjs> = {
// get
getNow: () => dayjs(),
getFixedDate: string => dayjs(string, ['YYYY-M-DD', 'YYYY-MM-DD']),
getEndDate: date => date.endOf('month'),
getWeekDay: date => {
getFixedDate: (string) => dayjs(string, ['YYYY-M-DD', 'YYYY-MM-DD']),
getEndDate: (date) => date.endOf('month'),
getWeekDay: (date) => {
const clone = date.locale('en');
return clone.weekday() + clone.localeData().firstDayOfWeek();
},
getYear: date => date.year(),
getMonth: date => date.month(),
getDate: date => date.date(),
getHour: date => date.hour(),
getMinute: date => date.minute(),
getSecond: date => date.second(),
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'),
Expand All @@ -73,26 +134,14 @@ const generateConfig: GenerateConfig<Dayjs> = {

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

locale: {
getWeekFirstDay: locale =>
dayjs()
.locale(parseLocale(locale))
.localeData()
.firstDayOfWeek(),
getWeekFirstDay: (locale) => dayjs().locale(parseLocale(locale)).localeData().firstDayOfWeek(),
getWeekFirstDate: (locale, date) => date.locale(parseLocale(locale)).weekday(0),
getWeek: (locale, date) => date.locale(parseLocale(locale)).week(),
getShortWeekDays: locale =>
dayjs()
.locale(parseLocale(locale))
.localeData()
.weekdaysMin(),
getShortMonths: locale =>
dayjs()
.locale(parseLocale(locale))
.localeData()
.monthsShort(),
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);
Expand All @@ -103,9 +152,7 @@ const generateConfig: GenerateConfig<Dayjs> = {
// parse Wo
const year = formatText.split('-')[0];
const weekStr = formatText.split('-')[1];
const firstWeek = dayjs(year, 'YYYY')
.startOf('year')
.locale(localeStr);
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) {
Expand Down

0 comments on commit 5eeedb3

Please sign in to comment.