Skip to content

Commit 0ab7c8f

Browse files
committed
refactor: simplify timezone handling
1 parent 48e58d4 commit 0ab7c8f

File tree

6 files changed

+22
-75
lines changed

6 files changed

+22
-75
lines changed

app/pages/index.vue

Lines changed: 9 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,27 @@
11
<script setup lang="ts">
2-
import { differenceInCalendarDays } from 'date-fns'
32
import { toZonedTime } from 'date-fns-tz'
43
import OpeningHours from 'opening_hours'
54
65
function getOpeningHoursStatus(
76
expression: string | null | undefined,
87
timezone: string | null | undefined,
9-
latitude?: number,
10-
longitude?: number,
118
reference: Date = new Date(),
12-
): { isOpen: boolean, message: string, nextChange: Date | null, isTomorrow: boolean } {
9+
): { isOpen: boolean, message: string, nextChange: Date | null } {
1310
if (!expression || !timezone)
14-
return { isOpen: false, message: 'Hours unavailable', nextChange: null, isTomorrow: false }
11+
return { isOpen: false, message: 'Hours unavailable', nextChange: null }
1512
1613
try {
17-
const zoned = toZonedTime(reference, timezone)
18-
19-
const nominatiomData: any = { timezone }
20-
if (Number.isFinite(latitude))
21-
nominatiomData.lat = latitude
22-
if (Number.isFinite(longitude))
23-
nominatiomData.lon = longitude
24-
25-
const schedule = new OpeningHours(expression.trim(), nominatiomData)
26-
27-
const isOpen = schedule.getState(zoned)
28-
const nextChange = schedule.getNextChange(zoned) || null
29-
const nextIsTomorrow = nextChange
30-
? differenceInCalendarDays(toZonedTime(nextChange, timezone), toZonedTime(reference, timezone)) === 1
31-
: false
14+
const localDate = toZonedTime(reference, timezone)
15+
const schedule = new OpeningHours(expression.trim())
3216
17+
const isOpen = schedule.getState(localDate)
18+
const nextChange = schedule.getNextChange(localDate) || null
3319
const message = isOpen ? 'Open now' : 'Closed'
3420
35-
return { isOpen, message, nextChange, isTomorrow: nextIsTomorrow }
21+
return { isOpen, message, nextChange }
3622
}
3723
catch {
38-
return { isOpen: false, message: 'Hours unavailable', nextChange: null, isTomorrow: false }
24+
return { isOpen: false, message: 'Hours unavailable', nextChange: null }
3925
}
4026
}
4127
@@ -62,8 +48,6 @@ const { data: locations } = await useFetch('/api/search', {
6248
hoursStatus: getOpeningHoursStatus(
6349
location.openingHours,
6450
location.timezone,
65-
location.latitude,
66-
location.longitude,
6751
),
6852
})) ?? []
6953
},
@@ -201,11 +185,7 @@ function removeCategory(categoryId: string) {
201185
font-medium mb-0 mt-6
202186
>
203187
<template v-if="location.hoursStatus.nextChange">
204-
{{ location.hoursStatus.message }} · {{ location.hoursStatus.isOpen ? 'Closes' : 'Opens' }}
205-
<template v-if="location.hoursStatus.isTomorrow && !location.hoursStatus.isOpen">
206-
tomorrow
207-
</template>
208-
at
188+
{{ location.hoursStatus.message }} · {{ location.hoursStatus.isOpen ? 'Closes' : 'Opens' }} at
209189
<NuxtTime
210190
:datetime="location.hoursStatus.nextChange"
211191
hour="numeric"

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
"@nuxthub/core": "catalog:",
2626
"@vueuse/nuxt": "catalog:",
2727
"consola": "catalog:",
28-
"date-fns": "catalog:",
2928
"date-fns-tz": "catalog:",
3029
"drizzle-orm": "catalog:",
3130
"nimiq-icons": "catalog:",

pnpm-lock.yaml

Lines changed: 0 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pnpm-workspace.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ catalog:
1010
'@unocss/preset-attributify': ^66.5.2
1111
'@vueuse/nuxt': ^13.9.0
1212
consola: ^3.4.2
13-
date-fns: ^4.1.0
1413
date-fns-tz: ^3.2.0
1514
drizzle-kit: latest
1615
drizzle-orm: latest

server/api/search.get.ts

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import type { Location } from '../../shared/types'
22
import { consola } from 'consola'
3+
import { toZonedTime } from 'date-fns-tz'
34
import { eq, sql } from 'drizzle-orm'
5+
import OpeningHours from 'opening_hours'
46
import * as v from 'valibot'
5-
import { isOpenNow } from '../utils/opening-hours'
67

78
const querySchema = v.object({
89
lat: v.optional(v.pipe(
@@ -72,15 +73,19 @@ export default defineEventHandler(async (event): Promise<LocationResponse[]> =>
7273
}>(locations: T[]) => {
7374
if (!openNow)
7475
return locations
75-
const toNumber = (value: unknown): number | undefined => {
76-
const num = typeof value === 'number' ? value : Number(value)
77-
return Number.isFinite(num) ? num : undefined
78-
}
7976

8077
return locations.filter((loc) => {
81-
const lat = toNumber(loc.latitude)
82-
const lon = toNumber(loc.longitude)
83-
return isOpenNow(loc.openingHours, loc.timezone, lat, lon, referenceTime)
78+
if (!loc.openingHours || !loc.timezone)
79+
return false
80+
81+
try {
82+
const localDate = toZonedTime(referenceTime, loc.timezone)
83+
const schedule = new OpeningHours(loc.openingHours.trim())
84+
return schedule.getState(localDate)
85+
}
86+
catch {
87+
return false
88+
}
8489
})
8590
}
8691

server/utils/opening-hours.ts

Lines changed: 0 additions & 30 deletions
This file was deleted.

0 commit comments

Comments
 (0)