mirror of
https://github.com/DIYgod/RSSHub.git
synced 2025-12-04 02:58:08 +08:00
refactor: timezone conversion in lib/utils/date.js (#7438)
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
const got = require('@/utils/got');
|
const got = require('@/utils/got');
|
||||||
const cheerio = require('cheerio');
|
const cheerio = require('cheerio');
|
||||||
const date = require('@/utils/date');
|
const parseDate = require('@/utils/parse-date');
|
||||||
|
|
||||||
module.exports = async (ctx) => {
|
module.exports = async (ctx) => {
|
||||||
const response = await got({
|
const response = await got({
|
||||||
@@ -30,7 +30,7 @@ module.exports = async (ctx) => {
|
|||||||
title: item.text(),
|
title: item.text(),
|
||||||
description,
|
description,
|
||||||
link: `https://diygod.me/animal-crossing/#${item.attr('id')}`,
|
link: `https://diygod.me/animal-crossing/#${item.attr('id')}`,
|
||||||
pubDate: date(item.text().split(' ')[1]),
|
pubDate: parseDate(item.text().split(' ')[1], 'M月D日'),
|
||||||
guid: item.text().split(' ')[0],
|
guid: item.text().split(' ')[0],
|
||||||
};
|
};
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
const date = require('@/utils/date');
|
const parseDate = require('@/utils/parse-date');
|
||||||
const got = require('@/utils/got');
|
const got = require('@/utils/got');
|
||||||
const cheerio = require('cheerio');
|
const cheerio = require('cheerio');
|
||||||
|
|
||||||
@@ -12,14 +12,14 @@ module.exports = async (ctx) => {
|
|||||||
const element = $(e);
|
const element = $(e);
|
||||||
const title = element.find('a').text();
|
const title = element.find('a').text();
|
||||||
const link = url + element.find('a').attr('href');
|
const link = url + element.find('a').attr('href');
|
||||||
const dateraw = /(\d+)\/(\d+)\/(\d+)/.exec(link);
|
const dateraw = /\d{4}\/\d{2}\/\d{2}/.exec(link)[0];
|
||||||
|
|
||||||
return {
|
return {
|
||||||
title: title,
|
title: title,
|
||||||
description: '',
|
description: '',
|
||||||
link: link,
|
link: link,
|
||||||
author: '王垠',
|
author: '王垠',
|
||||||
pubDate: date(`${dateraw[1]}-${dateraw[2]}-${dateraw[3]}`),
|
pubDate: parseDate(dateraw, 'YYYY/MM/DD'),
|
||||||
};
|
};
|
||||||
})
|
})
|
||||||
.get();
|
.get();
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
const got = require('@/utils/got');
|
const got = require('@/utils/got');
|
||||||
const cheerio = require('cheerio');
|
const cheerio = require('cheerio');
|
||||||
const iconv = require('iconv-lite');
|
const iconv = require('iconv-lite');
|
||||||
const date = require('@/utils/date');
|
const parseDate = require('@/utils/parse-date');
|
||||||
|
|
||||||
module.exports = async (ctx) => {
|
module.exports = async (ctx) => {
|
||||||
const { bo_table } = ctx.params;
|
const { bo_table } = ctx.params;
|
||||||
@@ -38,7 +38,7 @@ module.exports = async (ctx) => {
|
|||||||
title: title,
|
title: title,
|
||||||
description: description,
|
description: description,
|
||||||
link: link,
|
link: link,
|
||||||
pubDate: date(item.find('td.mw_basic_list_datetime').text()),
|
pubDate: parseDate(item.find('td.mw_basic_list_datetime').text(), 'HH:mm'),
|
||||||
};
|
};
|
||||||
})
|
})
|
||||||
.get(),
|
.get(),
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
const got = require('@/utils/got');
|
const got = require('@/utils/got');
|
||||||
const weiboUtils = require('./utils');
|
const weiboUtils = require('./utils');
|
||||||
const date = require('@/utils/date');
|
const timezone = require('@/utils/timezone');
|
||||||
const queryString = require('query-string');
|
const queryString = require('query-string');
|
||||||
|
|
||||||
module.exports = async (ctx) => {
|
module.exports = async (ctx) => {
|
||||||
@@ -37,7 +37,7 @@ module.exports = async (ctx) => {
|
|||||||
description: desc,
|
description: desc,
|
||||||
author: mblog.user.screen_name,
|
author: mblog.user.screen_name,
|
||||||
link: `https://weibo.com/${mblog.user.id}/${mblog.bid}`,
|
link: `https://weibo.com/${mblog.user.id}/${mblog.bid}`,
|
||||||
pubDate: date(mblog.created_at, 8),
|
pubDate: timezone(mblog.created_at, +8),
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
4
lib/utils/parse-date.js
Normal file
4
lib/utils/parse-date.js
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
const dayjs = require("dayjs");
|
||||||
|
dayjs.extend(require('dayjs/plugin/customParseFormat'));
|
||||||
|
|
||||||
|
module.exports = (date, ...options) => dayjs(date, ...options).toDate();
|
||||||
14
lib/utils/timezone.js
Normal file
14
lib/utils/timezone.js
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
const assert = require("assert").strict;
|
||||||
|
|
||||||
|
const millisInAnHour = 60 * 60 * 1000;
|
||||||
|
const serverTimezone = -new Date().getTimezoneOffset() / 60;
|
||||||
|
|
||||||
|
module.exports = (date, timezone = serverTimezone) => {
|
||||||
|
if (typeof (date) === "string" || date instanceof String) {
|
||||||
|
date = new Date(date);
|
||||||
|
}
|
||||||
|
|
||||||
|
assert(date instanceof Date);
|
||||||
|
|
||||||
|
return new Date(date.getTime() - millisInAnHour * (timezone - serverTimezone));
|
||||||
|
};
|
||||||
Reference in New Issue
Block a user