Skip to content

Commit

Permalink
Fix parsed date time using a timezone
Browse files Browse the repository at this point in the history
The `Date` does not include timezone information so the date ISO string
was wrong when run in a different timezone.
  • Loading branch information
Siilwyn committed Jul 31, 2024
1 parent 9dcb9f5 commit ac92f8e
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 10 deletions.
21 changes: 21 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 @@ -31,6 +31,7 @@
"@vueuse/core": "^9.13.0",
"@vueuse/nuxt": "^9.13.0",
"csv": "^6.2.7",
"date-fns-tz": "^3.1.3",
"floating-vue": "^2.0.0-beta.20",
"husky": "^8.0.3",
"jiti": "^1.18.2",
Expand Down
26 changes: 16 additions & 10 deletions src/server/api/consume-opening-hours.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import { fromZonedTime } from 'date-fns-tz';
import { Client } from '@microsoft/microsoft-graph-client';
import { ClientSecretCredential } from '@azure/identity';
import { TokenCredentialAuthenticationProvider } from '@microsoft/microsoft-graph-client/authProviders/azureTokenCredentials/index.js';
import { serverSupabaseServiceRole } from '#supabase/server';

const timezone = 'Europe/Amsterdam';

const { internalSecret, datoApiToken, microsoftGraph } = useRuntimeConfig();

const credential = new ClientSecretCredential(
Expand All @@ -18,7 +21,7 @@ const authProvider = new TokenCredentialAuthenticationProvider(credential, {
const graphClient = Client.initWithMiddleware({
authProvider: authProvider,
fetchOptions: {
headers: { 'Prefer': 'outlook.timezone="Europe/Amsterdam"' },
headers: { 'Prefer': `outlook.timezone="${timezone}"` },
},
});

Expand Down Expand Up @@ -83,15 +86,18 @@ export default defineEventHandler(async (event) => {
}&$select=subject,start,end`
)
.get()
.then((calendarView) => ({
number: building.number,
openEvents: calendarView.value
.filter((event: CalendarEvent) => event.subject === 'Open')
.map((event: CalendarEvent) => ({
start: new Date(event.start.dateTime).toISOString(),
end: new Date(event.end.dateTime).toISOString(),
})),
}))
.then((calendarView) => {
console.info(`Fetched opening hours of ${buildingsOpeningDates.length} buildings`);
return {
number: building.number,
openEvents: calendarView.value
.filter((event: CalendarEvent) => event.subject === 'Open')
.map((event: CalendarEvent) => ({
start: fromZonedTime(event.start.dateTime, timezone).toISOString(),
end: fromZonedTime(event.end.dateTime, timezone).toISOString(),
})),
}
})
.catch((error) => {
console.warn(
`Failed to fetch data for building ${building.number}:`,
Expand Down

0 comments on commit ac92f8e

Please sign in to comment.