From: xangelo Date: Tue, 30 Jan 2024 14:59:27 +0000 (-0500) Subject: fix: timestamp weirdness by standardizing on non-js timestamps X-Git-Url: https://git.xangelo.ca/?a=commitdiff_plain;h=fc6075cdcad065bfe4576cfdfb37757b0bedd68d;p=river.git fix: timestamp weirdness by standardizing on non-js timestamps --- diff --git a/src/server.ts b/src/server.ts index 06c1b62..3ac2371 100644 --- a/src/server.ts +++ b/src/server.ts @@ -18,6 +18,23 @@ const app = express(); const FEED_REFRESH_RATE = parseInt(process.env.FEED_REFRESH_RATE || '0'); +function timestamp(obj: any): number { + if(obj.getTime) { + return Math.floor(obj.getTime()/1000) + } + else if(obj.toString() === obj) { + return Math.floor(new Date(obj).getTime() / 1000) + } + else if(!isNaN(obj)) { + if(obj.toString().length === 10) { + return obj; + } + else { + return Math.floor(obj/1000); + } + } +} + function contentExtractor(feed: FeedSchema, item): string { let str = ''; switch(feed.title) { @@ -40,14 +57,14 @@ function contentExtractor(feed: FeedSchema, item): string { async function queryFeeds() { const completeFeedList = await db.select('*').from('feeds').orderBy('created_at'); + const now = Date.now(); const feedsToQuery = completeFeedList.filter(feed => { // only update every 5 minutes - return Math.floor(Date.now()/1000) > (feed.updated_at + (1000 * 60 * FEED_REFRESH_RATE)); + return timestamp(now) > (timestamp(feed.updated_at) + (60 * FEED_REFRESH_RATE)); }); console.log(`Querying ${feedsToQuery.length} feeds`); - const now = Date.now(); for(let feed of feedsToQuery) { console.log(`Querying ${feed.title}(${feed.url})`); @@ -60,10 +77,10 @@ async function queryFeeds() { title: item.title, link: item.link, feed_id: feed.id, - pub_date: Math.floor(new Date(item.pubDate).getTime()/1000), + pub_date: timestamp(item.pubDate), author: item.creator || item.author, - created_at: Math.floor(now/1000), - updated_at: Math.floor(now/1000), + created_at: timestamp(now), + updated_at: timestamp(now), meta: { comment_link: item.comments, snippet: contentExtractor(feed, item) @@ -78,7 +95,7 @@ async function queryFeeds() { } await db('feeds').update({ - updated_at: Math.floor(Date.now()/1000) + updated_at: timestamp(now) }).where({ id: feed.id }); @@ -111,7 +128,7 @@ app.get('/river', async (req, res) => { let firstLoad = true; const freshReadInHours = parseInt(process.env.FRESH_READ_IN_HOURS) || 6; - let createdAt = Math.floor(Date.now()/1000 - ((60 * 60) * freshReadInHours)); + let createdAt = Math.floor(timestamp(new Date()) - ((60 * 60) * freshReadInHours)); if(!displayedItems) { const temp = parseInt(header);