Skip to content

Commit

Permalink
Fix for backend time being incorrect
Browse files Browse the repository at this point in the history
  • Loading branch information
BrianCheung1 committed Oct 18, 2023
1 parent f74d9b9 commit 21c547f
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 8 deletions.
20 changes: 12 additions & 8 deletions app/api/events/route.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import axios from "axios"
import { NextResponse } from "next/server"
import moment from "moment-timezone"

const eventsData =
"https://raw.githubusercontent.com/ccev/pogoinfo/v2/active/events.json"
Expand All @@ -12,12 +13,14 @@ export async function GET(req: Request) {
axios.get(eventsData),
axios.get(leekData),
])
const currentDate = new Date()
const userTimezone = Intl.DateTimeFormat().resolvedOptions().timeZone;
const currentDate = moment.tz(new Date().toLocaleString(), userTimezone);


// Function to convert date strings to Date objects
const parseDate = (dateString: string) => {
const date = new Date(dateString)
date.setSeconds(0)
const date = moment.tz(dateString)
date.seconds(0)
return date
}

Expand All @@ -38,7 +41,7 @@ export async function GET(req: Request) {
},
})

events.data.forEach((obj:any) => {
events.data.forEach((obj: any) => {
const key = createKey(obj)
if (combinedObjectsMap.has(key)) {
const combinedObject = combineObjects(combinedObjectsMap.get(key), obj)
Expand All @@ -48,7 +51,7 @@ export async function GET(req: Request) {
}
})

leekEvents.data.forEach((obj:any) => {
leekEvents.data.forEach((obj: any) => {
const key = createKey(obj)
if (combinedObjectsMap.has(key)) {
const combinedObject = combineObjects(combinedObjectsMap.get(key), obj)
Expand All @@ -63,8 +66,8 @@ export async function GET(req: Request) {

const activeEvents = combinedObjectsArray
.filter((event: { start: string; end: string }) => {
const eventStartDate = new Date(event.start)
const eventEndDate = new Date(event.end)
const eventStartDate = moment.tz(event.start)
const eventEndDate = moment.tz(event.end)

// Check if the current date is within the event's start and end dates
return currentDate >= eventStartDate && currentDate <= eventEndDate
Expand All @@ -76,7 +79,7 @@ export async function GET(req: Request) {

const upcomingEvents = combinedObjectsArray
.filter((event: { start: string }) => {
const eventStartDate = new Date(event.start)
const eventStartDate = moment.tz(event.start)

// Check if the current date is before the event's start date
return currentDate < eventStartDate
Expand All @@ -90,6 +93,7 @@ export async function GET(req: Request) {
{
msg: "Success",
time: currentDate,
timeZone: userTimezone,
active_events: activeEvents,
upcoming_events: upcomingEvents,
},
Expand Down
20 changes: 20 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"@tabler/icons-react": "^2.39.0",
"axios": "^1.5.1",
"axios-cache-interceptor": "^1.3.1",
"moment-timezone": "^0.5.43",
"next": "13.5.4",
"react": "^18",
"react-dom": "^18",
Expand Down

0 comments on commit 21c547f

Please sign in to comment.