generated from react-component/footer
-
-
Notifications
You must be signed in to change notification settings - Fork 334
feat: add support for luxon #230
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
import { DateTime, Info } from 'luxon'; | ||
|
||
import type { GenerateConfig } from '.'; | ||
|
||
const weekDayFormatMap = { | ||
zh_CN: 'narrow', | ||
zh_TW: 'narrow', | ||
}; | ||
|
||
const weekDayLengthMap = { | ||
en_US: 2, | ||
en_GB: 2, | ||
}; | ||
|
||
/** | ||
* Normalizes part of a moment format string that should | ||
* not be escaped to a luxon compatible format string. | ||
* | ||
* @param part string | ||
* @returns string | ||
*/ | ||
const normalizeFormatPart = (part: string): string => | ||
part | ||
.replace(/Y/g, 'y') | ||
.replace(/D/g, 'd') | ||
.replace(/gg/g, 'kk') | ||
.replace(/Q/g, 'q') | ||
.replace(/([Ww])o/g, 'WW'); | ||
|
||
/** | ||
* Normalizes a moment compatible format string to a luxon compatible format string | ||
* | ||
* @param format string | ||
* @returns string | ||
*/ | ||
const normalizeFormat = (format: string): string => | ||
format | ||
// moment escapes strings contained in brackets | ||
.split(/[[\]]/) | ||
.map((part, index) => { | ||
const shouldEscape = index % 2 > 0; | ||
|
||
return shouldEscape ? part : normalizeFormatPart(part); | ||
}) | ||
// luxon escapes strings contained in single quotes | ||
.join("'"); | ||
|
||
/** | ||
* Normalizes language tags used to luxon compatible | ||
* language tags by replacing underscores with hyphen-minus. | ||
* | ||
* @param locale string | ||
* @returns string | ||
*/ | ||
const normalizeLocale = (locale: string): string => locale.replace(/_/g, '-'); | ||
|
||
const generateConfig: GenerateConfig<DateTime> = { | ||
// get | ||
getNow: () => DateTime.local(), | ||
getFixedDate: string => DateTime.fromFormat(string, 'yyyy-MM-dd'), | ||
getEndDate: date => date.endOf('month'), | ||
getWeekDay: date => date.weekday, | ||
getYear: date => date.year, | ||
getMonth: date => date.month - 1, // getMonth should return 0-11, luxon month returns 1-12 | ||
getDate: date => date.day, | ||
getHour: date => date.hour, | ||
getMinute: date => date.minute, | ||
getSecond: date => date.second, | ||
|
||
// set | ||
addYear: (date, diff) => date.plus({ year: diff }), | ||
addMonth: (date, diff) => date.plus({ month: diff }), | ||
addDate: (date, diff) => date.plus({ day: diff }), | ||
setYear: (date, year) => date.set({ year }), | ||
setMonth: (date, month) => date.set({ month: month + 1 }), // setMonth month argument is 0-11, luxon months are 1-12 | ||
setDate: (date, day) => date.set({ day }), | ||
setHour: (date, hour) => date.set({ hour }), | ||
setMinute: (date, minute) => date.set({ minute }), | ||
setSecond: (date, second) => date.set({ second }), | ||
|
||
// Compare | ||
isAfter: (date1, date2) => date1 > date2, | ||
isValidate: date => date.isValid, | ||
|
||
locale: { | ||
getWeekFirstDate: (locale, date) => date.setLocale(normalizeLocale(locale)).startOf('week'), | ||
getWeekFirstDay: locale => | ||
DateTime.local().setLocale(normalizeLocale(locale)).startOf('week').weekday, | ||
getWeek: (locale, date) => date.setLocale(normalizeLocale(locale)).weekNumber, | ||
getShortWeekDays: locale => { | ||
const weekdays = Info.weekdays(weekDayFormatMap[locale] || 'short', { | ||
locale: normalizeLocale(locale), | ||
}); | ||
|
||
const shifted = weekdays.map(weekday => weekday.slice(0, weekDayLengthMap[locale])); | ||
|
||
// getShortWeekDays should return weekday labels starting from Sunday. | ||
// luxon returns them starting from Monday, so we have to shift the results. | ||
shifted.unshift(shifted.pop() as string); | ||
|
||
return shifted; | ||
}, | ||
getShortMonths: locale => Info.months('short', { locale: normalizeLocale(locale) }), | ||
format: (locale, date, format) => { | ||
if (!date || !date.isValid) { | ||
return null; | ||
} | ||
|
||
return date.setLocale(normalizeLocale(locale)).toFormat(normalizeFormat(format)); | ||
}, | ||
parse: (locale, text, formats) => { | ||
for (let i = 0; i < formats.length; i += 1) { | ||
const normalizedFormat = normalizeFormat(formats[i]); | ||
|
||
const date = DateTime.fromFormat(text, normalizedFormat, { | ||
locale: normalizeLocale(locale), | ||
}); | ||
|
||
if (date.isValid) { | ||
return date; | ||
} | ||
} | ||
|
||
return null; | ||
}, | ||
}, | ||
}; | ||
|
||
export default generateConfig; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As mentioned in the PR description, luxon (through Intl API) formatting for week days does not match moment's 1:1.
I made some static adaptations so that the main antd locales are formatted the same, but I didn't think it was proper to bloat the implementation with formatting for all locales.
I have indicated in the PR description how we could provide easier customization for users, or simply document how to do it with the current implementation.