Skip to content

Colota - Android GPS Location tracker

refactor: extract startOfDaySec/endOfDaySec into geo (#442)

Max Dietriche75d4613a4bd
diff --git a/apps/mobile/src/hooks/__tests__/useTodayTrack.test.ts b/apps/mobile/src/hooks/__tests__/useTodayTrack.test.ts
index 36d64b87..f8db9651 100644
--- a/apps/mobile/src/hooks/__tests__/useTodayTrack.test.ts
+++ b/apps/mobile/src/hooks/__tests__/useTodayTrack.test.ts
@@ -192,7 +192,7 @@ describe("useTodayTrack", () => {
const calls = mockGetLocationsByDateRange.mock.calls
expect(calls).toHaveLength(2)
- // Both calls are full loads (startOfDayUnix) - much larger than a typical "since" value
+ // Both calls are full loads (start-of-day) - much larger than a typical "since" value
expect(calls[0][0]).toBeGreaterThan(1000000)
expect(calls[1][0]).toBeGreaterThan(1000000)
})
diff --git a/apps/mobile/src/hooks/useTodayTrack.ts b/apps/mobile/src/hooks/useTodayTrack.ts
index 9574cd59..3a09f2d9 100644
--- a/apps/mobile/src/hooks/useTodayTrack.ts
+++ b/apps/mobile/src/hooks/useTodayTrack.ts
@@ -9,16 +9,11 @@ import NativeLocationService from "../services/NativeLocationService"
import type { TrackLocation } from "../components/features/map/mapUtils"
import type { LocationCoords } from "../types/global"
import { logger } from "../utils/logger"
+import { startOfDaySec } from "../utils/geo"
/** How often to bump the version counter (ms) to batch GeoJSON rebuilds. */
const FLUSH_INTERVAL_MS = 5000
-function startOfDayUnix(): number {
- const d = new Date()
- d.setHours(0, 0, 0, 0)
- return Math.floor(d.getTime() / 1000)
-}
-
function todayDateStr(): string {
const d = new Date()
const m = d.getMonth() + 1
@@ -57,7 +52,7 @@ export function useTodayTrack(tracking: boolean, coords: LocationCoords | null)
// Load today's locations from DB
const loadFromDb = useCallback(async (since?: number) => {
try {
- const start = since ?? startOfDayUnix()
+ const start = since ?? startOfDaySec(new Date())
const end = Math.floor(Date.now() / 1000)
const rows = await NativeLocationService.getLocationsByDateRange(start, end)
const mapped: TrackLocation[] = (rows || []).map((r: any) => ({
diff --git a/apps/mobile/src/screens/LocationInspectorScreen.tsx b/apps/mobile/src/screens/LocationInspectorScreen.tsx
index 54d6161e..4042bd67 100644
--- a/apps/mobile/src/screens/LocationInspectorScreen.tsx
+++ b/apps/mobile/src/screens/LocationInspectorScreen.tsx
@@ -18,7 +18,7 @@ import { CalendarPicker } from "../components/features/inspector/CalendarPicker"
import { TrackMap } from "../components/features/inspector/TrackMap"
import { TripList } from "../components/features/inspector/TripList"
import { LocationTable } from "../components/features/inspector/LocationTable"
-import { formatDistance } from "../utils/geo"
+import { formatDistance, startOfDaySec, endOfDaySec } from "../utils/geo"
import { pad2 } from "../utils/format"
import { segmentTrips, getTripColor } from "../utils/trips"
import { EXPORT_FORMATS, type ExportFormat } from "../utils/exportConverters"
@@ -119,13 +119,8 @@ export function LocationHistoryScreen({ navigation, route }: RootScreenProps<"Lo
const fetchTrackData = useCallback(async () => {
const id = ++fetchTrackIdRef.current
try {
- const start = new Date(mapDate)
- start.setHours(0, 0, 0, 0)
- const end = new Date(mapDate)
- end.setHours(23, 59, 59, 999)
-
- const startTimestamp = Math.floor(start.getTime() / 1000)
- const endTimestamp = Math.floor(end.getTime() / 1000)
+ const startTimestamp = startOfDaySec(mapDate)
+ const endTimestamp = endOfDaySec(mapDate)
const result = await NativeLocationService.getLocationsByDateRange(startTimestamp, endTimestamp)
if (id === fetchTrackIdRef.current) {
diff --git a/apps/mobile/src/screens/LocationSummaryScreen.tsx b/apps/mobile/src/screens/LocationSummaryScreen.tsx
index 6067c10e..15f60fb9 100644
--- a/apps/mobile/src/screens/LocationSummaryScreen.tsx
+++ b/apps/mobile/src/screens/LocationSummaryScreen.tsx
@@ -22,7 +22,7 @@ import { ChipGroup } from "../components/ui/ChipGroup"
import { useTheme } from "../hooks/useTheme"
import { DailyStat } from "../types/global"
import NativeLocationService from "../services/NativeLocationService"
-import { formatDistance, formatDuration } from "../utils/geo"
+import { formatDistance, formatDuration, startOfDaySec } from "../utils/geo"
import { fonts } from "../styles/typography"
import { logger } from "../utils/logger"
@@ -52,8 +52,7 @@ function getDateRange(period: Period): { start: number; end: number } {
startDate.setDate(startDate.getDate() - 29)
}
- startDate.setHours(0, 0, 0, 0)
- return { start: Math.floor(startDate.getTime() / 1000), end: endTs }
+ return { start: startOfDaySec(startDate), end: endTs }
}
/** Animated number that counts up from 0 to target */
diff --git a/apps/mobile/src/utils/geo.ts b/apps/mobile/src/utils/geo.ts
index be4f9761..56dd957e 100644
--- a/apps/mobile/src/utils/geo.ts
+++ b/apps/mobile/src/utils/geo.ts
@@ -162,3 +162,17 @@ export function inputToMeters(value: number): number {
export function metersToInput(meters: number): number {
return usesMiles() ? Math.round(meters * FEET_PER_METER) : meters
}
+
+/** Start of `date`'s local day, in Unix seconds. */
+export function startOfDaySec(date: Date): number {
+ const d = new Date(date)
+ d.setHours(0, 0, 0, 0)
+ return Math.floor(d.getTime() / 1000)
+}
+
+/** End of `date`'s local day (23:59:59.999), in Unix seconds. */
+export function endOfDaySec(date: Date): number {
+ const d = new Date(date)
+ d.setHours(23, 59, 59, 999)
+ return Math.floor(d.getTime() / 1000)
+}