mirror of
https://github.com/DIYgod/RSSHub.git
synced 2025-12-05 04:11:26 +08:00
fix(utils): parse relative date with meridiem (#9775)
* fix(utils): parse relative date with meridiem * fix: use regex
This commit is contained in:
@@ -129,7 +129,7 @@ module.exports = {
|
|||||||
|
|
||||||
// 将 `\d+年\d+月...\d+秒前` 分割成 `['\d+年', ..., '\d+秒前']`
|
// 将 `\d+年\d+月...\d+秒前` 分割成 `['\d+年', ..., '\d+秒前']`
|
||||||
|
|
||||||
const matches = theDate.match(/(?:\D+)?\d+(?!:|-|\/)\D+/g);
|
const matches = theDate.match(/(?:\D+)?\d+(?!:|-|\/|(a|p)m)\D+/g);
|
||||||
|
|
||||||
if (matches) {
|
if (matches) {
|
||||||
// 获得最后的时间单元,如 `\d+秒前`
|
// 获得最后的时间单元,如 `\d+秒前`
|
||||||
@@ -187,7 +187,9 @@ module.exports = {
|
|||||||
for (const w of words) {
|
for (const w of words) {
|
||||||
const wordMatches = w.regExp.exec(theDate);
|
const wordMatches = w.regExp.exec(theDate);
|
||||||
if (wordMatches) {
|
if (wordMatches) {
|
||||||
return dayjs(`${w.startAt.format('YYYY-MM-DD')} ${wordMatches[1]}`).toDate();
|
// The default parser of dayjs() can parse '8:00 pm' but not '8:00pm'
|
||||||
|
// so we need to insert a space in between
|
||||||
|
return dayjs(`${w.startAt.format('YYYY-MM-DD')} ${/a|pm$/.test(wordMatches[1]) ? wordMatches[1].replace(/a|pm/, ' $&') : wordMatches[1]}`).toDate();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -112,6 +112,10 @@ describe('parseRelativeDate', () => {
|
|||||||
expect(+new Date(parseRelativeDate('Today 08:00'))).toBe(+date + 8 * hour);
|
expect(+new Date(parseRelativeDate('Today 08:00'))).toBe(+date + 8 * hour);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('Today, h:m a', () => {
|
||||||
|
expect(+new Date(parseRelativeDate('Today, 8:00 pm'))).toBe(+date + 20 * hour);
|
||||||
|
});
|
||||||
|
|
||||||
it('TDA H:m:s', () => {
|
it('TDA H:m:s', () => {
|
||||||
expect(+new Date(parseRelativeDate('TDA 08:00:00'))).toBe(+date + 8 * hour);
|
expect(+new Date(parseRelativeDate('TDA 08:00:00'))).toBe(+date + 8 * hour);
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user