From 55ca8d3260a79f9b60c073313f75ec7400996c46 Mon Sep 17 00:00:00 2001 From: Face <69168154+face-hh@users.noreply.github.com> Date: Tue, 10 Jun 2025 16:30:10 +0300 Subject: [PATCH] fix charts to use local time --- website/src/lib/utils.ts | 5 +++++ website/src/routes/api/coin/[coinSymbol]/+server.ts | 5 +++-- .../src/routes/api/hopium/questions/[id]/+server.ts | 7 ++++--- website/src/routes/coin/[coinSymbol]/+page.svelte | 11 ++++++----- 4 files changed, 18 insertions(+), 10 deletions(-) diff --git a/website/src/lib/utils.ts b/website/src/lib/utils.ts index 0da9ac9..398a99a 100644 --- a/website/src/lib/utils.ts +++ b/website/src/lib/utils.ts @@ -334,3 +334,8 @@ export function showSchoolPrideCannons(confetti: any) { } export const formatMarketCap = formatValue; + +export function timeToLocal(originalTime: number): number { + const d = new Date(originalTime * 1000); + return Math.floor(Date.UTC(d.getFullYear(), d.getMonth(), d.getDate(), d.getHours(), d.getMinutes(), d.getSeconds(), d.getMilliseconds()) / 1000); +} \ No newline at end of file diff --git a/website/src/routes/api/coin/[coinSymbol]/+server.ts b/website/src/routes/api/coin/[coinSymbol]/+server.ts index 818bca8..1f5be80 100644 --- a/website/src/routes/api/coin/[coinSymbol]/+server.ts +++ b/website/src/routes/api/coin/[coinSymbol]/+server.ts @@ -2,6 +2,7 @@ import { error, json } from '@sveltejs/kit'; import { db } from '$lib/server/db'; import { coin, user, priceHistory, transaction } from '$lib/server/db/schema'; import { eq, desc } from 'drizzle-orm'; +import { timeToLocal } from '$lib/utils'; function aggregatePriceHistory(priceData: any[], intervalMinutes: number = 60) { if (priceData.length === 0) return []; @@ -19,7 +20,7 @@ function aggregatePriceHistory(priceData: any[], intervalMinutes: number = 60) { if (!candlesticks.has(intervalStart)) { candlesticks.set(intervalStart, { - time: Math.floor(intervalStart / 1000), + time: timeToLocal(Math.floor(intervalStart / 1000)), open: point.price, high: point.price, low: point.price, @@ -87,7 +88,7 @@ function aggregateVolumeData(transactionData: any[], intervalMinutes: number = 6 if (!volumeMap.has(intervalStart)) { volumeMap.set(intervalStart, { - time: Math.floor(intervalStart / 1000), + time: timeToLocal(Math.floor(intervalStart / 1000)), volume: 0 }); } diff --git a/website/src/routes/api/hopium/questions/[id]/+server.ts b/website/src/routes/api/hopium/questions/[id]/+server.ts index 2a7e13b..19633d0 100644 --- a/website/src/routes/api/hopium/questions/[id]/+server.ts +++ b/website/src/routes/api/hopium/questions/[id]/+server.ts @@ -4,6 +4,7 @@ import { db } from '$lib/server/db'; import { predictionQuestion, user, predictionBet } from '$lib/server/db/schema'; import { eq, desc, sum, and, asc } from 'drizzle-orm'; import type { RequestHandler } from './$types'; +import { timeToLocal } from '$lib/utils'; export const GET: RequestHandler = async ({ params, request }) => { const questionId = parseInt(params.id!); @@ -84,7 +85,7 @@ export const GET: RequestHandler = async ({ params, request }) => { if (probabilityHistory.length > 0) { const firstBetTime = Math.floor(new Date(probabilityHistory[0].createdAt).getTime() / 1000); probabilityData.push({ - time: firstBetTime - 3600, // 1 hour before first bet + time: timeToLocal(firstBetTime - 3600), value: 50 }); } @@ -100,7 +101,7 @@ export const GET: RequestHandler = async ({ params, request }) => { const yesPercentage = total > 0 ? (runningYesTotal / total) * 100 : 50; probabilityData.push({ - time: Math.floor(new Date(bet.createdAt).getTime() / 1000), + time: timeToLocal(Math.floor(new Date(bet.createdAt).getTime() / 1000)), value: Number(yesPercentage.toFixed(1)) }); } @@ -108,7 +109,7 @@ export const GET: RequestHandler = async ({ params, request }) => { // Add current point if no recent bets if (probabilityData.length > 0) { const lastPoint = probabilityData[probabilityData.length - 1]; - const currentTime = Math.floor(Date.now() / 1000); + const currentTime = timeToLocal(Math.floor(Date.now() / 1000)); // Only add current point if last bet was more than 1 hour ago if (currentTime - lastPoint.time > 3600) { diff --git a/website/src/routes/coin/[coinSymbol]/+page.svelte b/website/src/routes/coin/[coinSymbol]/+page.svelte index 4ac75d8..cd12688 100644 --- a/website/src/routes/coin/[coinSymbol]/+page.svelte +++ b/website/src/routes/coin/[coinSymbol]/+page.svelte @@ -23,7 +23,7 @@ import CoinIcon from '$lib/components/self/CoinIcon.svelte'; import { USER_DATA } from '$lib/stores/user-data'; import { fetchPortfolioData } from '$lib/stores/portfolio-data'; - import { getPublicUrl, getTimeframeInSeconds } from '$lib/utils.js'; + import { getPublicUrl, getTimeframeInSeconds, timeToLocal } from '$lib/utils.js'; import { websocketController, type PriceUpdate, isConnectedStore } from '$lib/stores/websocket'; import SEO from '$lib/components/self/SEO.svelte'; @@ -159,12 +159,13 @@ const currentTime = Math.floor(Date.now() / 1000); const currentCandleTime = Math.floor(currentTime / timeframeSeconds) * timeframeSeconds; + const localCandleTime = timeToLocal(currentCandleTime); const lastCandle = chartData[chartData.length - 1]; - if (lastCandle && lastCandle.time === currentCandleTime) { + if (lastCandle && lastCandle.time === localCandleTime) { const updatedCandle = { - time: currentCandleTime, + time: localCandleTime, open: lastCandle.open, high: Math.max(lastCandle.high, newPrice), low: Math.min(lastCandle.low, newPrice), @@ -173,9 +174,9 @@ candlestickSeries.update(updatedCandle); chartData[chartData.length - 1] = updatedCandle; - } else if (currentCandleTime > (lastCandle?.time || 0)) { + } else if (localCandleTime > (lastCandle?.time || 0)) { const newCandle = { - time: currentCandleTime, + time: localCandleTime, open: newPrice, high: newPrice, low: newPrice,