mirror of
https://github.com/DIYgod/RSSHub.git
synced 2025-12-03 18:48:12 +08:00
test: utils/date
This commit is contained in:
@@ -51,7 +51,8 @@ module.exports = async (ctx) => {
|
||||
.contents()
|
||||
.eq(0)
|
||||
.text()
|
||||
.trim()
|
||||
.trim(),
|
||||
8
|
||||
),
|
||||
link: `${item.find('.feed-block-title a').attr('href')}`,
|
||||
};
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
// 格式化 类型这个的时间 , 几分钟前 | 几小时前 | 几天前 | 几月前 | 几年前 | 具体的格式不对的时间
|
||||
module.exports = (html, timeZone) => {
|
||||
const serverOffset = new Date().getTimezoneOffset() / 60;
|
||||
|
||||
module.exports = (html, timeZone = -serverOffset) => {
|
||||
let math;
|
||||
let date = new Date();
|
||||
if (/(\d+)分钟前/.exec(html)) {
|
||||
@@ -46,8 +48,7 @@ module.exports = (html, timeZone) => {
|
||||
date = new Date(date.getFullYear(), date.getMonth(), date.getDate(), math[1], math[2]);
|
||||
}
|
||||
|
||||
if (date && timeZone) {
|
||||
const serverOffset = new Date().getTimezoneOffset() / 60;
|
||||
if (date) {
|
||||
return new Date(date.getTime() - 60 * 60 * 1000 * (timeZone + serverOffset)).toUTCString();
|
||||
}
|
||||
return html;
|
||||
|
||||
@@ -34,6 +34,7 @@
|
||||
"eslint-plugin-prettier": "3.0.1",
|
||||
"jest": "23.6.0",
|
||||
"lint-staged": "8.1.0",
|
||||
"mockdate": "^2.0.2",
|
||||
"nodemon": "1.18.9",
|
||||
"prettier": "1.16.0",
|
||||
"prettier-check": "2.0.0",
|
||||
|
||||
64
test/date.js
Normal file
64
test/date.js
Normal file
@@ -0,0 +1,64 @@
|
||||
const parseDate = require('../lib/utils/date');
|
||||
const MockDate = require('mockdate');
|
||||
|
||||
describe('date', () => {
|
||||
MockDate.set(new Date('2019-01-01'));
|
||||
|
||||
const date = new Date();
|
||||
|
||||
it(`m分钟前`, async () => {
|
||||
expect(+new Date(parseDate('10分钟前'))).toBe(+date - 10 * 60 * 1000);
|
||||
});
|
||||
|
||||
it(`H小时前`, async () => {
|
||||
expect(+new Date(parseDate('10小时前'))).toBe(+date - 10 * 60 * 60 * 1000);
|
||||
});
|
||||
|
||||
it(`D天前`, async () => {
|
||||
expect(+new Date(parseDate('10天前'))).toBe(+date - 10 * 24 * 60 * 60 * 1000);
|
||||
});
|
||||
|
||||
it(`M月前`, async () => {
|
||||
expect(+new Date(parseDate('1月前'))).toBe(+date - 1 * 31 * 24 * 60 * 60 * 1000);
|
||||
});
|
||||
|
||||
it(`Y年前`, async () => {
|
||||
expect(+new Date(parseDate('1年前'))).toBe(+date - 1 * 365 * 24 * 60 * 60 * 1000);
|
||||
});
|
||||
|
||||
it(`今天 H:m`, async () => {
|
||||
expect(+new Date(parseDate('今天 08:00'))).toBe(+new Date('2019-1-1 08:00'));
|
||||
});
|
||||
|
||||
it(`昨天 H:m`, async () => {
|
||||
expect(+new Date(parseDate('昨天 20:00'))).toBe(+new Date('2018-12-31 20:00'));
|
||||
});
|
||||
|
||||
it(`Y年M月D日H时`, async () => {
|
||||
expect(+new Date(parseDate('2018年4月2日1时'))).toBe(+new Date('2018-4-2 01:00'));
|
||||
});
|
||||
|
||||
it(`Y-M-D H:m`, async () => {
|
||||
expect(+new Date(parseDate('2018-4-2 02:03'))).toBe(+new Date('2018-4-2 02:03'));
|
||||
});
|
||||
|
||||
it(`M-D H:m`, async () => {
|
||||
expect(+new Date(parseDate('2-3 02:03'))).toBe(+new Date('2019-2-3 02:03'));
|
||||
});
|
||||
|
||||
it(`M月D日 H:m`, async () => {
|
||||
expect(+new Date(parseDate('2月3日 02:03'))).toBe(+new Date('2019-2-3 02:03'));
|
||||
});
|
||||
|
||||
it(`Y-M-D`, async () => {
|
||||
expect(+new Date(parseDate('2018-4-2'))).toBe(+new Date('2018-4-2'));
|
||||
});
|
||||
|
||||
it(`M-D`, async () => {
|
||||
expect(+new Date(parseDate('4-2'))).toBe(+new Date('2019-4-2'));
|
||||
});
|
||||
|
||||
it(`H:m`, async () => {
|
||||
expect(+new Date(parseDate('02:03'))).toBe(+new Date('2019-1-1 02:03'));
|
||||
});
|
||||
});
|
||||
@@ -6612,6 +6612,11 @@ mkdirp@0.5.1, mkdirp@0.5.x, mkdirp@^0.5.0, mkdirp@^0.5.1, mkdirp@~0.5.0, mkdirp@
|
||||
dependencies:
|
||||
minimist "0.0.8"
|
||||
|
||||
mockdate@^2.0.2:
|
||||
version "2.0.2"
|
||||
resolved "https://registry.yarnpkg.com/mockdate/-/mockdate-2.0.2.tgz#5ae0c0eaf8fe23e009cd01f9889b42c4f634af12"
|
||||
integrity sha1-WuDA6vj+I+AJzQH5iJtCxPY0rxI=
|
||||
|
||||
move-concurrently@^1.0.1:
|
||||
version "1.0.1"
|
||||
resolved "https://registry.yarnpkg.com/move-concurrently/-/move-concurrently-1.0.1.tgz#be2c005fda32e0b29af1f05d7c4b33214c701f92"
|
||||
|
||||
Reference in New Issue
Block a user