diff --git a/docs/joinus/pub-date.md b/docs/joinus/pub-date.md index cf4ac5dba59fe2..b46d35bf360754 100644 --- a/docs/joinus/pub-date.md +++ b/docs/joinus/pub-date.md @@ -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 @@ -39,4 +47,4 @@ const pubDate = parseDate('2020/12/30', 'YYYY/MM/DD') const timezone = require('@/utils/timezone'); const pubDate = timezone(new Date(), +8) -``` \ No newline at end of file +``` diff --git a/lib/routes/blogs/diygod/animal-crossing.js b/lib/routes/blogs/diygod/animal-crossing.js index 68b48f54dc5f2c..1c4cd565d055d3 100644 --- a/lib/routes/blogs/diygod/animal-crossing.js +++ b/lib/routes/blogs/diygod/animal-crossing.js @@ -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({ diff --git a/lib/routes/blogs/wangyin.js b/lib/routes/blogs/wangyin.js index f6a41bc58257e2..28646754b5786d 100644 --- a/lib/routes/blogs/wangyin.js +++ b/lib/routes/blogs/wangyin.js @@ -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'); diff --git a/lib/routes/etoland/board.js b/lib/routes/etoland/board.js index e5c5ec34735a12..44b2e9c8c3f525 100644 --- a/lib/routes/etoland/board.js +++ b/lib/routes/etoland/board.js @@ -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; diff --git a/lib/routes/gov/guangdong/eea.js b/lib/routes/gov/guangdong/eea.js index 92afc69544f859..f31b1823dc3758 100644 --- a/lib/routes/gov/guangdong/eea.js +++ b/lib/routes/gov/guangdong/eea.js @@ -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 = { diff --git a/lib/routes/lfsyd/tag.js b/lib/routes/lfsyd/tag.js index 9ae2aeca507901..784daa72a8a5e5 100644 --- a/lib/routes/lfsyd/tag.js +++ b/lib/routes/lfsyd/tag.js @@ -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; diff --git a/lib/routes/smzdm/article.js b/lib/routes/smzdm/article.js index 4cbde98a3b08c8..4a1ac1d748a73e 100644 --- a/lib/routes/smzdm/article.js +++ b/lib/routes/smzdm/article.js @@ -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) => { diff --git a/lib/routes/smzdm/haowen.js b/lib/routes/smzdm/haowen.js index dd842ad9a9b939..c8b42ad54dc0d8 100644 --- a/lib/routes/smzdm/haowen.js +++ b/lib/routes/smzdm/haowen.js @@ -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) => { diff --git a/lib/routes/smzdm/haowen_fenlei.js b/lib/routes/smzdm/haowen_fenlei.js index ebcb880803cc06..d3801b31daf3aa 100644 --- a/lib/routes/smzdm/haowen_fenlei.js +++ b/lib/routes/smzdm/haowen_fenlei.js @@ -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'; diff --git a/lib/routes/smzdm/keyword.js b/lib/routes/smzdm/keyword.js index b72cfc8eb1d8fc..e548a037c3fbea 100644 --- a/lib/routes/smzdm/keyword.js +++ b/lib/routes/smzdm/keyword.js @@ -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) => { diff --git a/lib/routes/zaker/focusread.js b/lib/routes/zaker/focusread.js index 495d1bfb43fec2..677f9424cf88c7 100644 --- a/lib/routes/zaker/focusread.js +++ b/lib/routes/zaker/focusread.js @@ -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`; @@ -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); diff --git a/lib/routes/zaker/source.js b/lib/routes/zaker/source.js index ddcea6758e35b0..440a9da70f9eff 100644 --- a/lib/routes/zaker/source.js +++ b/lib/routes/zaker/source.js @@ -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'; @@ -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); diff --git a/lib/utils/parse-date.js b/lib/utils/parse-date.js index 51b4eba17e8654..183cff0b5ef7aa 100644 --- a/lib/utils/parse-date.js +++ b/lib/utils/parse-date.js @@ -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; + }, +};