From 1c89edc712877bb25d9d51c9c262bc9d83a75f16 Mon Sep 17 00:00:00 2001 From: VWSCoronaDashboard24 Date: Wed, 18 May 2022 10:34:13 +0200 Subject: [PATCH 1/4] fix: use startOfDay for all graph values --- packages/common/src/utils/timeframe/index.ts | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/packages/common/src/utils/timeframe/index.ts b/packages/common/src/utils/timeframe/index.ts index 45f39c0dbe..f6f27914a0 100644 --- a/packages/common/src/utils/timeframe/index.ts +++ b/packages/common/src/utils/timeframe/index.ts @@ -3,6 +3,7 @@ import { DateValue, isDateSeries, isDateSpanSeries, + startOfDayInSeconds, TimestampedValue, } from '../../data-sorting'; import { DAY_IN_SECONDS } from '../../time'; @@ -101,19 +102,26 @@ export function getValuesInTimeframe( const end = Math.ceil(endDate.getTime() / 1000); if (isDateSeries(values)) { - return values.filter((x: DateValue) => x.date_unix >= start && x.date_unix <= end) as T[]; + return values.filter((x: DateValue) => { + const correctedDateUnix = startOfDayInSeconds(x.date_unix); + return correctedDateUnix >= start && correctedDateUnix <= end; + }) as T[]; } if (isDateSpanSeries(values)) { - return values.filter( - (x: DateSpanValue) => x.date_end_unix >= start && x.date_end_unix <= end - ) as T[]; + return values.filter((x: DateSpanValue) => { + const correctedDateUnix = startOfDayInSeconds(x.date_end_unix); + return correctedDateUnix >= start && correctedDateUnix <= end; + }) as T[]; } throw new Error(`Incompatible timestamps are used in value ${values[0]}`); } -function getTimeframeBoundaryUnix(timeframe: TimeframeOption, endDate: Date): number { +function getTimeframeBoundaryUnix( + timeframe: TimeframeOption, + endDate: Date +): number { if (timeframe === TimeframeOption.ALL) { return 0; } From a2b8b82139bb9e9159268e23c51d4d8279f17259 Mon Sep 17 00:00:00 2001 From: VWSCoronaDashboard24 Date: Wed, 18 May 2022 16:17:57 +0200 Subject: [PATCH 2/4] fix: convert dates to startOfDay --- packages/common/src/utils/timeframe/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/common/src/utils/timeframe/index.ts b/packages/common/src/utils/timeframe/index.ts index f6f27914a0..317342f58d 100644 --- a/packages/common/src/utils/timeframe/index.ts +++ b/packages/common/src/utils/timeframe/index.ts @@ -126,5 +126,5 @@ function getTimeframeBoundaryUnix( return 0; } const days = getDaysForTimeframe(timeframe); - return Math.floor(endDate.getTime() / 1000) - days * DAY_IN_SECONDS; + return startOfDayInSeconds(endDate.getTime() / 1000) - days * DAY_IN_SECONDS; } From a1abd0b4a1f5a4201c459d686699c880b71d5e2e Mon Sep 17 00:00:00 2001 From: VWSCoronaDashboard24 Date: Thu, 19 May 2022 09:04:33 +0200 Subject: [PATCH 3/4] fix: use endOfDay instead of middleOfDay for current date hook --- packages/app/src/utils/current-date-context.tsx | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/app/src/utils/current-date-context.tsx b/packages/app/src/utils/current-date-context.tsx index 2983e7aa69..2842ee8c85 100644 --- a/packages/app/src/utils/current-date-context.tsx +++ b/packages/app/src/utils/current-date-context.tsx @@ -1,4 +1,4 @@ -import { assert, middleOfDayInSeconds } from '@corona-dashboard/common'; +import { assert, endOfDayInSeconds } from '@corona-dashboard/common'; import { createContext, ReactNode, @@ -32,10 +32,10 @@ export function CurrentDateProvider({ children: ReactNode; }) { const [date, setDate] = useState( - new Date(middleOfDayInSeconds(dateInSeconds) * 1000) + new Date(endOfDayInSeconds(dateInSeconds) * 1000) ); useEffect( - () => setDate(new Date(middleOfDayInSeconds(Date.now() / 1000) * 1000)), + () => setDate(new Date(endOfDayInSeconds(Date.now() / 1000) * 1000)), [] ); From b6f850106f10b058ab2c96377e6385df1f6bbc10 Mon Sep 17 00:00:00 2001 From: VWSCoronaDashboard24 Date: Thu, 19 May 2022 09:35:23 +0200 Subject: [PATCH 4/4] fix: update tests --- .../app/src/utils/__tests__/current-date-context.spec.tsx | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/app/src/utils/__tests__/current-date-context.spec.tsx b/packages/app/src/utils/__tests__/current-date-context.spec.tsx index bd1faafa25..3c420a5a5c 100644 --- a/packages/app/src/utils/__tests__/current-date-context.spec.tsx +++ b/packages/app/src/utils/__tests__/current-date-context.spec.tsx @@ -2,7 +2,7 @@ import { cleanup, renderHook } from '@testing-library/react-hooks/server'; import { suite } from 'uvu'; import * as assert from 'uvu/assert'; import { CurrentDateProvider, useCurrentDate } from '../current-date-context'; -import { middleOfDayInSeconds } from '@corona-dashboard/common'; +import { endOfDayInSeconds } from '@corona-dashboard/common'; import injectJsDom from 'jsdom-global'; const UseCurrentDate = suite('useCurrentDate'); @@ -20,7 +20,7 @@ UseCurrentDate.after((context) => { }); UseCurrentDate('should return the passed date initially', () => { - const yesterday = middleOfDayInSeconds( + const yesterday = endOfDayInSeconds( new Date().setDate(new Date().getDate() - 1) / 1000 ); @@ -37,11 +37,11 @@ UseCurrentDate('should return the passed date initially', () => { }); UseCurrentDate('should return the current date after hydration', () => { - const yesterday = middleOfDayInSeconds( + const yesterday = endOfDayInSeconds( new Date().setDate(new Date().getDate() - 1) / 1000 ); - const now = middleOfDayInSeconds(Date.now() / 1000); + const now = endOfDayInSeconds(Date.now() / 1000); const { result, hydrate } = renderHook(() => useCurrentDate(), { wrapper: ({ children }) => (