Skip to content
This repository has been archived by the owner on Nov 4, 2024. It is now read-only.

COR-725 unix date correction #4255

Merged
merged 4 commits into from
May 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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
);

Expand All @@ -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 }) => (
Expand Down
6 changes: 3 additions & 3 deletions packages/app/src/utils/current-date-context.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { assert, middleOfDayInSeconds } from '@corona-dashboard/common';
import { assert, endOfDayInSeconds } from '@corona-dashboard/common';
import {
createContext,
ReactNode,
Expand Down Expand Up @@ -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)),
[]
);

Expand Down
20 changes: 14 additions & 6 deletions packages/common/src/utils/timeframe/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
DateValue,
isDateSeries,
isDateSpanSeries,
startOfDayInSeconds,
TimestampedValue,
} from '../../data-sorting';
import { DAY_IN_SECONDS } from '../../time';
Expand Down Expand Up @@ -101,22 +102,29 @@ export function getValuesInTimeframe<T extends TimestampedValue>(
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;
}