From dceb230ec2c7b4d2d598bf7b08a4ed3eac2a312e Mon Sep 17 00:00:00 2001 From: Varun Jain Date: Fri, 31 Jul 2026 22:55:19 +0530 Subject: [PATCH 1/6] Fix historical market data dates shifting to the previous day Dates fetched via fetchMarketDataBySymbol() were kept as UTC instants, so date-fns format() calls downstream (which read local time) rendered the previous calendar day for users in timezones behind UTC. Normalize to the local date matching the UTC calendar date at the fetch boundary. Closes #6177 --- CHANGELOG.md | 1 + libs/common/src/lib/helper.spec.ts | 33 ++++++++++++++++++++++++ libs/common/src/lib/helper.ts | 8 ++++++ libs/ui/src/lib/services/data.service.ts | 7 +++-- 4 files changed, 47 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1ef9636b45..81d52e20d8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Fixed +- Fixed the date handling in the historical market data editor so entries no longer shift to the previous day for users in timezones behind UTC - Fixed the date shift of the market data in the portfolio calculation in time zones behind UTC - Fixed the dates of manually entered historical market data in time zones other than UTC diff --git a/libs/common/src/lib/helper.spec.ts b/libs/common/src/lib/helper.spec.ts index 7ba92aee9a..9887c8f140 100644 --- a/libs/common/src/lib/helper.spec.ts +++ b/libs/common/src/lib/helper.spec.ts @@ -9,6 +9,7 @@ import { getCountryCodeFromCurrency, getEmojiFlag, getHoldingName, + getLocalDateFromUtcDate, getNumberFormatGroup, getStringOrNull, getStringOrUndefined, @@ -237,6 +238,38 @@ describe('Helper', () => { }); }); + describe('Get local date from UTC date', () => { + const originalTz = process.env.TZ; + + afterEach(() => { + process.env.TZ = originalTz; + }); + + it('Preserve the calendar date in a timezone behind UTC', () => { + process.env.TZ = 'America/New_York'; + + const localDate = getLocalDateFromUtcDate( + new Date('2026-01-05T00:00:00.000Z') + ); + + expect(localDate.getFullYear()).toEqual(2026); + expect(localDate.getMonth()).toEqual(0); + expect(localDate.getDate()).toEqual(5); + }); + + it('Preserve the calendar date in a timezone ahead of UTC', () => { + process.env.TZ = 'Asia/Tokyo'; + + const localDate = getLocalDateFromUtcDate( + new Date('2026-01-05T00:00:00.000Z') + ); + + expect(localDate.getFullYear()).toEqual(2026); + expect(localDate.getMonth()).toEqual(0); + expect(localDate.getDate()).toEqual(5); + }); + }); + describe('Get number format group', () => { let languageGetter: jest.SpyInstance; 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/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 ?? []) { From e92af2d64b3a15315954e81eb427daf5a084c8aa Mon Sep 17 00:00:00 2001 From: Thomas Kaul <4159106+dtslvr@users.noreply.github.com> Date: Sun, 29 Nov 2026 08:50:19 +0100 Subject: [PATCH 2/6] Improve tests --- .../lib/helper.ahead-of-utc.time-zone.spec.ts | 28 ++++++++++++++++ .../lib/helper.behind-utc.time-zone.spec.ts | 28 ++++++++++++++++ libs/common/src/lib/helper.spec.ts | 33 ------------------- 3 files changed, 56 insertions(+), 33 deletions(-) create mode 100644 libs/common/src/lib/helper.ahead-of-utc.time-zone.spec.ts create mode 100644 libs/common/src/lib/helper.behind-utc.time-zone.spec.ts 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.spec.ts b/libs/common/src/lib/helper.spec.ts index 9887c8f140..7ba92aee9a 100644 --- a/libs/common/src/lib/helper.spec.ts +++ b/libs/common/src/lib/helper.spec.ts @@ -9,7 +9,6 @@ import { getCountryCodeFromCurrency, getEmojiFlag, getHoldingName, - getLocalDateFromUtcDate, getNumberFormatGroup, getStringOrNull, getStringOrUndefined, @@ -238,38 +237,6 @@ describe('Helper', () => { }); }); - describe('Get local date from UTC date', () => { - const originalTz = process.env.TZ; - - afterEach(() => { - process.env.TZ = originalTz; - }); - - it('Preserve the calendar date in a timezone behind UTC', () => { - process.env.TZ = 'America/New_York'; - - const localDate = getLocalDateFromUtcDate( - new Date('2026-01-05T00:00:00.000Z') - ); - - expect(localDate.getFullYear()).toEqual(2026); - expect(localDate.getMonth()).toEqual(0); - expect(localDate.getDate()).toEqual(5); - }); - - it('Preserve the calendar date in a timezone ahead of UTC', () => { - process.env.TZ = 'Asia/Tokyo'; - - const localDate = getLocalDateFromUtcDate( - new Date('2026-01-05T00:00:00.000Z') - ); - - expect(localDate.getFullYear()).toEqual(2026); - expect(localDate.getMonth()).toEqual(0); - expect(localDate.getDate()).toEqual(5); - }); - }); - describe('Get number format group', () => { let languageGetter: jest.SpyInstance; From d1a5dee45dbf821b5821cfc9af7a02a0b237df76 Mon Sep 17 00:00:00 2001 From: Thomas Kaul <4159106+dtslvr@users.noreply.github.com> Date: Sun, 29 Nov 2026 09:48:22 +0100 Subject: [PATCH 3/6] Fix missing market price of today in historical market data editor --- ...rical-market-data-editor.component.spec.ts | 19 +++++++++++++++++++ ...historical-market-data-editor.component.ts | 6 +++--- 2 files changed, 22 insertions(+), 3 deletions(-) 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..e3526df8f5 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,23 @@ 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); + }); + }); }); 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..8d8aee1a93 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 @@ -37,11 +37,11 @@ import { format, isBefore, isSameDay, - isToday, isValid, min, parse, - parseISO + parseISO, + startOfToday } from 'date-fns'; import ms from 'ms'; import { DeviceDetectorService } from 'ngx-device-detector'; @@ -153,7 +153,7 @@ export class GfHistoricalMarketDataEditorComponent const marketDataItems = [...missingMarketData, ...this.marketData()]; const lastDate = marketDataItems.at(-1)?.date; - if (!lastDate || !isToday(lastDate)) { + if (!lastDate || isBefore(lastDate, startOfToday())) { marketDataItems.push({ date: new Date() }); } From 4c2ffbdfc8ccbf8c0df13df40dcc475c4d0bfc9b Mon Sep 17 00:00:00 2001 From: Thomas Kaul <4159106+dtslvr@users.noreply.github.com> Date: Sun, 29 Nov 2026 09:48:42 +0100 Subject: [PATCH 4/6] Update changelog --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 81d52e20d8..e8e0bae96e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,7 +15,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Fixed -- Fixed the date handling in the historical market data editor so entries no longer shift to the previous day for users in timezones 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 date shift of the market data in the portfolio calculation in time zones behind UTC - Fixed the dates of manually entered historical market data in time zones other than UTC From 26773987ccaebdcd838c3c35f8e9422cb97e487b Mon Sep 17 00:00:00 2001 From: Thomas Kaul <4159106+dtslvr@users.noreply.github.com> Date: Sun, 29 Nov 2026 09:53:14 +0100 Subject: [PATCH 5/6] Update changelog --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e8e0bae96e..cdac6f5b33 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,8 +15,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Fixed -- 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 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 From 3a3110457fc380be2fc8c142d5c688fa415d699c Mon Sep 17 00:00:00 2001 From: Thomas Kaul <4159106+dtslvr@users.noreply.github.com> Date: Sun, 29 Nov 2026 10:17:22 +0100 Subject: [PATCH 6/6] Fix missing today in historical market data editor --- ...torical-market-data-editor.component.spec.ts | 17 +++++++++++++++++ .../historical-market-data-editor.component.ts | 11 +++++++---- 2 files changed, 24 insertions(+), 4 deletions(-) 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 e3526df8f5..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 @@ -75,5 +75,22 @@ describe('GfHistoricalMarketDataEditorComponent', () => { 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 8d8aee1a93..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 @@ -37,11 +37,11 @@ import { format, isBefore, isSameDay, + isToday, isValid, min, parse, - parseISO, - startOfToday + parseISO } from 'date-fns'; import ms from 'ms'; import { DeviceDetectorService } from 'ngx-device-detector'; @@ -152,8 +152,11 @@ export class GfHistoricalMarketDataEditorComponent const marketDataItems = [...missingMarketData, ...this.marketData()]; - const lastDate = marketDataItems.at(-1)?.date; - if (!lastDate || isBefore(lastDate, startOfToday())) { + const hasMarketDataOfToday = marketDataItems.some(({ date }) => { + return isToday(date); + }); + + if (!hasMarketDataOfToday) { marketDataItems.push({ date: new Date() }); }