Skip to content
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

chore(utils): parse-date supports relative time & fix routes #7530

Merged
merged 2 commits into from
May 15, 2021
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
16 changes: 12 additions & 4 deletions docs/joinus/pub-date.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,24 @@

### Parse Date

这个是一个工具类用于使用[dayjs](https://github.com/iamkun/dayjs)。大部分情况下,应当可以直接使用他获取到正确的`Date Object`
这个是一个工具类用于使用[dayjs](https://github.com/iamkun/dayjs)。大部分情况下,应当可以直接使用他获取到正确的`Date Object`

具体解析参数请参考dayjs github说明

```javascript
const parseDate = require('@/utils/parse-date');
const { parseDate } = require('@/utils/parse-date');

const pubDate = parseDate('2020/12/30', 'YYYY/MM/DD')
const pubDate = parseDate('2020/12/30', 'YYYY/MM/DD');
```

如果你需要解析相对日期,请使用 `parseRelativeDate`。

```javascript
const { parseRelativeDate } = require('@/utils/parse-date');

const pubDate = parseRelativeDate('2天前');
const pubDate = parseRelativeDate('前天 15:36');
```

### Timezone

Expand All @@ -39,4 +47,4 @@ const pubDate = parseDate('2020/12/30', 'YYYY/MM/DD')
const timezone = require('@/utils/timezone');

const pubDate = timezone(new Date(), +8)
```
```
2 changes: 1 addition & 1 deletion lib/routes/blogs/diygod/animal-crossing.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const got = require('@/utils/got');
const cheerio = require('cheerio');
const parseDate = require('@/utils/parse-date');
const { parseDate } = require('@/utils/parse-date');

module.exports = async (ctx) => {
const response = await got({
Expand Down
2 changes: 1 addition & 1 deletion lib/routes/blogs/wangyin.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const parseDate = require('@/utils/parse-date');
const { parseDate } = require('@/utils/parse-date');
const got = require('@/utils/got');
const cheerio = require('cheerio');

Expand Down
2 changes: 1 addition & 1 deletion lib/routes/etoland/board.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const got = require('@/utils/got');
const cheerio = require('cheerio');
const iconv = require('iconv-lite');
const parseDate = require('@/utils/parse-date');
const { parseDate } = require('@/utils/parse-date');

module.exports = async (ctx) => {
const { bo_table } = ctx.params;
Expand Down
2 changes: 1 addition & 1 deletion lib/routes/gov/guangdong/eea.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const url = require('url');
const got = require('@/utils/got');
const cheerio = require('cheerio');
const timezone = require('@/utils/timezone');
const parseDate = require('@/utils/parse-date');
const { parseDate } = require('@/utils/parse-date');
const rootUrl = 'http://eea.gd.gov.cn/';

const config = {
Expand Down
2 changes: 1 addition & 1 deletion lib/routes/lfsyd/tag.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const got = require('@/utils/got');
const util = require('./utils');
const timezone = require('@/utils/timezone');
const parseDate = require('@/utils/parse-date');
const { parseDate } = require('@/utils/parse-date');

module.exports = async (ctx) => {
const tag_id = ctx.params.tag;
Expand Down
2 changes: 1 addition & 1 deletion lib/routes/smzdm/article.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const got = require('@/utils/got');
const cheerio = require('cheerio');
const parseDate = require('@/utils/parse-date');
const { parseDate } = require('@/utils/parse-date');
const timezone = require('@/utils/timezone');

module.exports = async (ctx) => {
Expand Down
2 changes: 1 addition & 1 deletion lib/routes/smzdm/haowen.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const got = require('@/utils/got');
const cheerio = require('cheerio');
const parseDate = require('@/utils/parse-date');
const { parseDate } = require('@/utils/parse-date');
const timezone = require('@/utils/timezone');

module.exports = async (ctx) => {
Expand Down
2 changes: 1 addition & 1 deletion lib/routes/smzdm/haowen_fenlei.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const got = require('@/utils/got');
const cheerio = require('cheerio');
const timezone = require('@/utils/timezone');
const parseDate = require('@/utils/parse-date');
const { parseDate } = require('@/utils/parse-date');
module.exports = async (ctx) => {
const name = ctx.params.name;
const sort = ctx.params.sort || '0';
Expand Down
2 changes: 1 addition & 1 deletion lib/routes/smzdm/keyword.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const got = require('@/utils/got');
const cheerio = require('cheerio');
const parseDate = require('@/utils/parse-date');
const { parseDate } = require('@/utils/parse-date');
const timezone = require('@/utils/timezone');

module.exports = async (ctx) => {
Expand Down
5 changes: 3 additions & 2 deletions lib/routes/zaker/focusread.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const got = require('@/utils/got');
const cheerio = require('cheerio');
const date_util = require('@/utils/date');
const { parseRelativeDate } = require('@/utils/parse-date');
const timezone = require('@/utils/timezone');

module.exports = async (ctx) => {
const link = `http://www.myzaker.com/?pos=selected_article`;
Expand Down Expand Up @@ -48,7 +49,7 @@ module.exports = async (ctx) => {
title: title,
link: itemUrl,
description: description,
pubDate: date_util(date, 8),
pubDate: timezone(parseRelativeDate(date), +8),
};
ctx.cache.set(itemUrl, JSON.stringify(single));
return Promise.resolve(single);
Expand Down
5 changes: 3 additions & 2 deletions lib/routes/zaker/source.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const got = require('@/utils/got');
const cheerio = require('cheerio');
const date_util = require('@/utils/date');
const { parseRelativeDate } = require('@/utils/parse-date');
const timezone = require('@/utils/timezone');

module.exports = async (ctx) => {
const type = ctx.params.type || 'source';
Expand Down Expand Up @@ -50,7 +51,7 @@ module.exports = async (ctx) => {
title: title,
link: itemUrl,
description: description,
pubDate: date_util(date, 8),
pubDate: timezone(parseRelativeDate(date), +8),
};
ctx.cache.set(itemUrl, JSON.stringify(single));
return Promise.resolve(single);
Expand Down
40 changes: 39 additions & 1 deletion lib/utils/parse-date.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,42 @@
const dayjs = require('dayjs');
dayjs.extend(require('dayjs/plugin/customParseFormat'));

module.exports = (date, ...options) => dayjs(date, ...options).toDate();
const patterns = [
{
regexp: /^(\d+)分钟前$/,
handler: (minute) => dayjs().subtract(minute, 'minutes'),
},
{
regexp: /^(\d+)小时前$/,
handler: (hour) => dayjs().subtract(hour, 'hours'),
},
{
regexp: /^(\d+)天前$/,
handler: (day) => dayjs().subtract(day, 'days'),
},
{
regexp: /^今天\s*((\d+:\d+)?)$/,
handler: (hm) => dayjs(hm || '0:0', ['HH:m', 'HH:mm', 'H:m', 'H:mm']),
},
{
regexp: /^昨天\s*((\d+:\d+)?)$/,
handler: (hm) => dayjs(hm || '0:0', ['HH:m', 'HH:mm', 'H:m', 'H:mm']).subtract(1, 'day'),
},
{
regexp: /^前天\s*((\d+:\d+)?)$/,
handler: (hm) => dayjs(hm || '0:0', ['HH:m', 'HH:mm', 'H:m', 'H:mm']).subtract(2, 'day'),
},
];

module.exports = {
parseDate: (date, ...options) => dayjs(date, ...options).toDate(),
parseRelativeDate: (date) => {
for (const pattern of patterns) {
const match = pattern.regexp.exec(date);
if (match !== null) {
return pattern.handler(match[1]).toDate();
}
}
return null;
},
};