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 ?? []) {