diff --git a/CHANGELOG.md b/CHANGELOG.md index 1ef9636b45..cdac6f5b33 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,6 +16,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Fixed - Fixed the date shift of the market data in the portfolio calculation in time zones behind UTC +- Fixed the date shift of the historical market data in the asset profile dialog and the holding detail dialog for users in a time zone behind UTC - Fixed the dates of manually entered historical market data in time zones other than UTC ## 3.101.0 - 2026-11-28 diff --git a/libs/common/src/lib/helper.ahead-of-utc.time-zone.spec.ts b/libs/common/src/lib/helper.ahead-of-utc.time-zone.spec.ts new file mode 100644 index 0000000000..974e45d049 --- /dev/null +++ b/libs/common/src/lib/helper.ahead-of-utc.time-zone.spec.ts @@ -0,0 +1,28 @@ +/** + * @jest-environment /../../apps/api/jest-environment-tz.js + * @jest-environment-options {"timeZone": "Pacific/Auckland"} + */ +import { + DATE_FORMAT, + getLocalDateFromUtcDate +} from '@ghostfolio/common/helper'; + +import { format } from 'date-fns'; + +describe('Helper in a time zone ahead of UTC', () => { + describe('Get local date from UTC date', () => { + it.each([ + ['in summer time', '2026-01-01', '2025-12-31T11:00:00.000Z'], + ['at the end of summer time', '2026-04-05', '2026-04-04T11:00:00.000Z'], + ['in standard time', '2026-07-01', '2026-06-30T12:00:00.000Z'], + ['at the start of summer time', '2026-09-27', '2026-09-26T12:00:00.000Z'] + ])('Preserve the calendar date %s', (_, dateString, localMidnight) => { + const localDate = getLocalDateFromUtcDate( + new Date(`${dateString}T00:00:00.000Z`) + ); + + expect(format(localDate, DATE_FORMAT)).toEqual(dateString); + expect(localDate).toEqual(new Date(localMidnight)); + }); + }); +}); diff --git a/libs/common/src/lib/helper.behind-utc.time-zone.spec.ts b/libs/common/src/lib/helper.behind-utc.time-zone.spec.ts new file mode 100644 index 0000000000..0645952ff8 --- /dev/null +++ b/libs/common/src/lib/helper.behind-utc.time-zone.spec.ts @@ -0,0 +1,28 @@ +/** + * @jest-environment /../../apps/api/jest-environment-tz.js + * @jest-environment-options {"timeZone": "America/New_York"} + */ +import { + DATE_FORMAT, + getLocalDateFromUtcDate +} from '@ghostfolio/common/helper'; + +import { format } from 'date-fns'; + +describe('Helper in a time zone behind UTC', () => { + describe('Get local date from UTC date', () => { + it.each([ + ['in standard time', '2026-01-01', '2026-01-01T05:00:00.000Z'], + ['at the start of summer time', '2026-03-08', '2026-03-08T05:00:00.000Z'], + ['in summer time', '2026-07-01', '2026-07-01T04:00:00.000Z'], + ['at the end of summer time', '2026-11-01', '2026-11-01T04:00:00.000Z'] + ])('Preserve the calendar date %s', (_, dateString, localMidnight) => { + const localDate = getLocalDateFromUtcDate( + new Date(`${dateString}T00:00:00.000Z`) + ); + + expect(format(localDate, DATE_FORMAT)).toEqual(dateString); + expect(localDate).toEqual(new Date(localMidnight)); + }); + }); +}); diff --git a/libs/common/src/lib/helper.ts b/libs/common/src/lib/helper.ts index e0e5103a47..8ba69a9c0d 100644 --- a/libs/common/src/lib/helper.ts +++ b/libs/common/src/lib/helper.ts @@ -493,6 +493,14 @@ export function getHoldingName({ return name ?? symbol; } +export function getLocalDateFromUtcDate(aUtcDate: Date) { + return new Date( + aUtcDate.getUTCFullYear(), + aUtcDate.getUTCMonth(), + aUtcDate.getUTCDate() + ); +} + export function getLocale() { return navigator.language ?? DEFAULT_LOCALE; } diff --git a/libs/ui/src/lib/historical-market-data-editor/historical-market-data-editor.component.spec.ts b/libs/ui/src/lib/historical-market-data-editor/historical-market-data-editor.component.spec.ts index 7cb9636f0e..d22634c9ab 100644 --- a/libs/ui/src/lib/historical-market-data-editor/historical-market-data-editor.component.spec.ts +++ b/libs/ui/src/lib/historical-market-data-editor/historical-market-data-editor.component.spec.ts @@ -57,4 +57,40 @@ describe('GfHistoricalMarketDataEditorComponent', () => { expect(component.formatDay(31)).toBe('31'); }); }); + + describe('ngOnChanges', () => { + afterEach(() => { + jest.useRealTimers(); + }); + + it('should keep the market price of today if the last item is in the future', () => { + jest.useFakeTimers().setSystemTime(new Date(2026, 10, 29, 20)); + + fixture.componentRef.setInput('marketData', [ + { date: new Date(2026, 10, 29), marketPrice: 1 }, + { date: new Date(2026, 10, 30), marketPrice: 2 } + ]); + + fixture.detectChanges(); + + expect(component.marketDataByMonth['2026-11']['29'].marketPrice).toBe(1); + }); + + it('should include today if all items are in the future', () => { + jest.useFakeTimers().setSystemTime(new Date(2026, 10, 29, 20)); + + fixture.componentRef.setInput('marketData', [ + { date: new Date(2027, 0, 15), marketPrice: 1 } + ]); + + fixture.detectChanges(); + + expect(Object.keys(component.marketDataByMonth).sort()).toEqual([ + '2026-11', + '2026-12', + '2027-01' + ]); + expect(component.marketDataByMonth['2026-11']['29']).toBeDefined(); + }); + }); }); diff --git a/libs/ui/src/lib/historical-market-data-editor/historical-market-data-editor.component.ts b/libs/ui/src/lib/historical-market-data-editor/historical-market-data-editor.component.ts index 6da916a8ef..bf5d20166b 100644 --- a/libs/ui/src/lib/historical-market-data-editor/historical-market-data-editor.component.ts +++ b/libs/ui/src/lib/historical-market-data-editor/historical-market-data-editor.component.ts @@ -152,8 +152,11 @@ export class GfHistoricalMarketDataEditorComponent const marketDataItems = [...missingMarketData, ...this.marketData()]; - const lastDate = marketDataItems.at(-1)?.date; - if (!lastDate || !isToday(lastDate)) { + const hasMarketDataOfToday = marketDataItems.some(({ date }) => { + return isToday(date); + }); + + if (!hasMarketDataOfToday) { marketDataItems.push({ date: new Date() }); } diff --git a/libs/ui/src/lib/services/data.service.ts b/libs/ui/src/lib/services/data.service.ts index d081771ef7..3ab8d8e497 100644 --- a/libs/ui/src/lib/services/data.service.ts +++ b/libs/ui/src/lib/services/data.service.ts @@ -19,7 +19,10 @@ import { UpdateTagDto, UpdateUserSettingDto } from '@ghostfolio/common/dtos'; -import { DATE_FORMAT } from '@ghostfolio/common/helper'; +import { + DATE_FORMAT, + getLocalDateFromUtcDate +} from '@ghostfolio/common/helper'; import { AccessResponse, AccessTokenResponse, @@ -593,7 +596,7 @@ export class DataService { .pipe( map((data) => { for (const item of data.marketData) { - item.date = parseISO(item.date); + item.date = getLocalDateFromUtcDate(parseISO(item.date)); } for (const item of data.splits ?? []) {