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 }) => ( 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)), [] ); diff --git a/packages/common/src/utils/timeframe/index.ts b/packages/common/src/utils/timeframe/index.ts index 45f39c0dbe..317342f58d 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,22 +102,29 @@ 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; } const days = getDaysForTimeframe(timeframe); - return Math.floor(endDate.getTime() / 1000) - days * DAY_IN_SECONDS; + return startOfDayInSeconds(endDate.getTime() / 1000) - days * DAY_IN_SECONDS; }