Skip to content

Commit

Permalink
fix offset once again
Browse files Browse the repository at this point in the history
  • Loading branch information
ari-party committed Jun 5, 2024
1 parent 9034e01 commit 397fce5
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 5 deletions.
10 changes: 5 additions & 5 deletions src/lib/umami/getViews.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import ky from 'ky';

import getNearestMidnight from '../../utils/getNearestMidnight';

export interface XY {
x: string;
y: number;
Expand All @@ -10,16 +12,14 @@ export interface PageViews {
sessions: XY[];
}

const oneDay = 24 * 60 * 60 * 1000;

export default async function getViews(
websiteAPI: string,
websiteId: string,
token: string,
): Promise<PageViews | null> {
const oneDay = 24 * 60 * 60 * 1000;

let today: Date | number = new Date();
today.setHours(24, 0, 0, 0);
today = today.getTime();
const today = getNearestMidnight();

const monthAgo = today - 30 * oneDay;

Expand Down
20 changes: 20 additions & 0 deletions src/utils/getNearestMidnight.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
export default function getNearestMidnight() {
const now = Date.now();

let currentMidnight: Date | number = new Date(now);
currentMidnight.setHours(0, 0, 0, 0);
currentMidnight = currentMidnight.getTime();

let nextMidnight: Date | number = new Date(currentMidnight);
nextMidnight.setHours(24, 0, 0, 0);
nextMidnight = nextMidnight.getTime();

// Calculate the difference in milliseconds
const diffToCurrentMidnight = Math.abs(now - currentMidnight);
const diffToNextMidnight = Math.abs(nextMidnight - now);

// Return the nearest midnight
return diffToCurrentMidnight <= diffToNextMidnight
? currentMidnight
: nextMidnight;
}

0 comments on commit 397fce5

Please sign in to comment.