Browse Source

Fix start date of date ranges for instances in time zones with negative UTC offset

pull/8099/head
Thomas Kaul 5 days ago
parent
commit
4193e8814a
  1. 13
      apps/api/src/app/portfolio/calculator/roai/portfolio-calculator.spec.ts
  2. 10
      apps/api/src/app/portfolio/portfolio.service.ts
  3. 6
      libs/common/src/lib/calculation-helper.spec.ts

13
apps/api/src/app/portfolio/calculator/roai/portfolio-calculator.spec.ts

@ -190,6 +190,10 @@ describe('PortfolioCalculator', () => {
}); });
describe('get performance', () => { describe('get performance', () => {
afterEach(() => {
jest.useRealTimers();
});
it('with a calendar year date range', async () => { it('with a calendar year date range', async () => {
const snapshot = await portfolioCalculator.getSnapshot(); const snapshot = await portfolioCalculator.getSnapshot();
@ -242,13 +246,14 @@ describe('PortfolioCalculator', () => {
netPerformance: 10 netPerformance: 10
}), }),
getHistoricalDataItem({ getHistoricalDataItem({
date: '2017-01-03', date: '2017-01-01',
investment: 100, investment: 100,
netPerformance: 20 netPerformance: 20
}) })
]; ];
jest.useFakeTimers().setSystemTime(new Date('2017-01-04T02:30:00.000Z')); // 1 January 2017, 10:00 in New York
jest.useFakeTimers().setSystemTime(new Date('2017-01-01T15:00:00.000Z'));
const { endDate, startDate } = getIntervalFromDateRange({ const { endDate, startDate } = getIntervalFromDateRange({
dateRange: 'ytd' dateRange: 'ytd'
@ -259,13 +264,11 @@ describe('PortfolioCalculator', () => {
start: startDate start: startDate
}); });
jest.useRealTimers();
expect( expect(
chart.map(({ date }) => { chart.map(({ date }) => {
return date; return date;
}) })
).toEqual(['2016-12-31', '2017-01-03']); ).toEqual(['2016-12-31', '2017-01-01']);
}); });
}); });
}); });

10
apps/api/src/app/portfolio/portfolio.service.ts

@ -47,6 +47,7 @@ import {
isAccountExcluded, isAccountExcluded,
isDraftActivity, isDraftActivity,
parseDate, parseDate,
resetHours,
resolveUserSettings resolveUserSettings
} from '@ghostfolio/common/helper'; } from '@ghostfolio/common/helper';
import { import {
@ -559,8 +560,13 @@ export class PortfolioService {
const { historicalData } = await portfolioCalculator.getSnapshot(); const { historicalData } = await portfolioCalculator.getSnapshot();
const items = historicalData.filter(({ date }) => { const items = historicalData.filter((historicalDataItem) => {
return !isBefore(date, startDate) && !isAfter(date, endDate); const date = resetHours(parseDate(historicalDataItem.date));
return (
!isBefore(date, resetHours(startDate)) &&
!isAfter(date, resetHours(endDate))
);
}); });
let investments: InvestmentItem[]; let investments: InvestmentItem[];

6
libs/common/src/lib/calculation-helper.spec.ts

@ -10,6 +10,10 @@ import { DateRange } from './types';
describe('CalculationHelper', () => { describe('CalculationHelper', () => {
describe('interval from date range', () => { describe('interval from date range', () => {
afterEach(() => {
jest.useRealTimers();
});
it('Get interval of a calendar year', async () => { it('Get interval of a calendar year', async () => {
const { endDate, startDate } = getIntervalFromDateRange({ const { endDate, startDate } = getIntervalFromDateRange({
dateRange: '2024' dateRange: '2024'
@ -42,8 +46,6 @@ describe('CalculationHelper', () => {
new Date(2023, 9, 10), new Date(2023, 9, 10),
new Date(2019, 9, 10) new Date(2019, 9, 10)
]); ]);
jest.useRealTimers();
}); });
}); });

Loading…
Cancel
Save