Skip to content

Commit

Permalink
refactor: timezone conversion in lib/utils/date.js (DIYgod#7438)
Browse files Browse the repository at this point in the history
  • Loading branch information
queensferryme authored May 8, 2021
1 parent ca12701 commit 10f5bb7
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 9 deletions.
4 changes: 2 additions & 2 deletions 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 date = require('@/utils/date');
const parseDate = require('@/utils/parse-date');

module.exports = async (ctx) => {
const response = await got({
Expand Down Expand Up @@ -30,7 +30,7 @@ module.exports = async (ctx) => {
title: item.text(),
description,
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],
};
})
Expand Down
6 changes: 3 additions & 3 deletions lib/routes/blogs/wangyin.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const date = require('@/utils/date');
const parseDate = require('@/utils/parse-date');
const got = require('@/utils/got');
const cheerio = require('cheerio');

Expand All @@ -12,14 +12,14 @@ module.exports = async (ctx) => {
const element = $(e);
const title = element.find('a').text();
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 {
title: title,
description: '',
link: link,
author: '王垠',
pubDate: date(`${dateraw[1]}-${dateraw[2]}-${dateraw[3]}`),
pubDate: parseDate(dateraw, 'YYYY/MM/DD'),
};
})
.get();
Expand Down
4 changes: 2 additions & 2 deletions 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 date = require('@/utils/date');
const parseDate = require('@/utils/parse-date');

module.exports = async (ctx) => {
const { bo_table } = ctx.params;
Expand Down Expand Up @@ -38,7 +38,7 @@ module.exports = async (ctx) => {
title: title,
description: description,
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(),
Expand Down
4 changes: 2 additions & 2 deletions lib/routes/weibo/super_index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const got = require('@/utils/got');
const weiboUtils = require('./utils');
const date = require('@/utils/date');
const timezone = require('@/utils/timezone');
const queryString = require('query-string');

module.exports = async (ctx) => {
Expand Down Expand Up @@ -37,7 +37,7 @@ module.exports = async (ctx) => {
description: desc,
author: mblog.user.screen_name,
link: `https://weibo.com/${mblog.user.id}/${mblog.bid}`,
pubDate: date(mblog.created_at, 8),
pubDate: timezone(mblog.created_at, +8),
});
}
}
Expand Down
4 changes: 4 additions & 0 deletions lib/utils/parse-date.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
const dayjs = require("dayjs");
dayjs.extend(require('dayjs/plugin/customParseFormat'));

module.exports = (date, ...options) => dayjs(date, ...options).toDate();
14 changes: 14 additions & 0 deletions lib/utils/timezone.js
Original file line number Diff line number Diff line change
@@ -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));
};

0 comments on commit 10f5bb7

Please sign in to comment.