Browse Source

Fix date handling in time zone other than UTC

pull/7723/head
Thomas Kaul 4 days ago
parent
commit
90b43aa9a8
  1. 7
      apps/api/src/app/endpoints/sitemap/sitemap.controller.ts
  2. 3
      apps/api/src/app/symbol/symbol.controller.ts
  3. 6
      apps/api/src/app/symbol/symbol.service.ts
  4. 13
      apps/api/src/services/data-provider/data-provider.service.ts
  5. 31
      apps/api/src/services/exchange-rate-data/exchange-rate-data.service.ts
  6. 8
      libs/common/src/lib/helper.ts

7
apps/api/src/app/endpoints/sitemap/sitemap.controller.ts

@ -1,10 +1,11 @@
import { ConfigurationService } from '@ghostfolio/api/services/configuration/configuration.service'; import { ConfigurationService } from '@ghostfolio/api/services/configuration/configuration.service';
import { import {
DATE_FORMAT, DATE_FORMAT,
getYesterday, getStartOfUtcDateOfYesterday,
interpolate interpolate
} from '@ghostfolio/common/helper'; } from '@ghostfolio/common/helper';
import { utc } from '@date-fns/utc';
import { Controller, Get, Res, VERSION_NEUTRAL, Version } from '@nestjs/common'; import { Controller, Get, Res, VERSION_NEUTRAL, Version } from '@nestjs/common';
import { format } from 'date-fns'; import { format } from 'date-fns';
import { Response } from 'express'; import { Response } from 'express';
@ -32,7 +33,9 @@ export class SitemapController {
@Get() @Get()
@Version(VERSION_NEUTRAL) @Version(VERSION_NEUTRAL)
public getSitemapXml(@Res() response: Response) { public getSitemapXml(@Res() response: Response) {
const currentDate = format(getYesterday(), DATE_FORMAT); const currentDate = format(getStartOfUtcDateOfYesterday(), DATE_FORMAT, {
in: utc
});
response.setHeader('content-type', 'application/xml'); response.setHeader('content-type', 'application/xml');
response.send( response.send(

3
apps/api/src/app/symbol/symbol.controller.ts

@ -8,6 +8,7 @@ import {
} from '@ghostfolio/common/interfaces'; } from '@ghostfolio/common/interfaces';
import type { RequestWithUser } from '@ghostfolio/common/types'; import type { RequestWithUser } from '@ghostfolio/common/types';
import { utc } from '@date-fns/utc';
import { import {
Controller, Controller,
Get, Get,
@ -103,7 +104,7 @@ export class SymbolController {
@Param('dateString') dateString: string, @Param('dateString') dateString: string,
@Param('symbol') symbol: string @Param('symbol') symbol: string
): Promise<DataProviderHistoricalResponse> { ): Promise<DataProviderHistoricalResponse> {
const date = parseISO(dateString); const date = parseISO(dateString, { in: utc });
if (!isDate(date)) { if (!isDate(date)) {
throw new HttpException( throw new HttpException(

6
apps/api/src/app/symbol/symbol.service.ts

@ -19,6 +19,7 @@ import {
} from '@ghostfolio/common/interfaces'; } from '@ghostfolio/common/interfaces';
import { UserWithSettings } from '@ghostfolio/common/types'; import { UserWithSettings } from '@ghostfolio/common/types';
import { utc } from '@date-fns/utc';
import { Injectable, Logger } from '@nestjs/common'; import { Injectable, Logger } from '@nestjs/common';
import { format, subDays } from 'date-fns'; import { format, subDays } from 'date-fns';
@ -123,8 +124,9 @@ export class SymbolService {
return { return {
marketPrice: marketPrice:
historicalData?.[assetProfileIdentifier]?.[format(date, DATE_FORMAT)] historicalData?.[assetProfileIdentifier]?.[
?.marketPrice format(date, DATE_FORMAT, { in: utc })
]?.marketPrice
}; };
} }

13
apps/api/src/services/data-provider/data-provider.service.ts

@ -393,10 +393,11 @@ export class DataProviderService implements OnModuleInit {
const rangeQuery = const rangeQuery =
from && to from && to
? Prisma.sql`AND date >= ${format(from, DATE_FORMAT)}::timestamp AND date <= ${format( ? Prisma.sql`AND date >= ${format(from, DATE_FORMAT, {
to, in: utc
DATE_FORMAT })}::timestamp AND date <= ${format(to, DATE_FORMAT, {
)}::timestamp` in: utc
})}::timestamp`
: Prisma.empty; : Prisma.empty;
const dataSources = aItems.map(({ dataSource }) => { const dataSources = aItems.map(({ dataSource }) => {
@ -430,7 +431,9 @@ export class DataProviderService implements OnModuleInit {
r[assetProfileIdentifier] = {}; r[assetProfileIdentifier] = {};
} }
r[assetProfileIdentifier][format(new Date(date), DATE_FORMAT)] = { r[assetProfileIdentifier][
format(new Date(date), DATE_FORMAT, { in: utc })
] = {
marketPrice marketPrice
}; };

31
apps/api/src/services/exchange-rate-data/exchange-rate-data.service.ts

@ -12,11 +12,12 @@ import {
import { import {
DATE_FORMAT, DATE_FORMAT,
getAssetProfileIdentifier, getAssetProfileIdentifier,
getYesterday, getStartOfUtcDateOfYesterday,
resetHours resetHours
} from '@ghostfolio/common/helper'; } from '@ghostfolio/common/helper';
import { DataProviderHistoricalResponse } from '@ghostfolio/common/interfaces'; import { DataProviderHistoricalResponse } from '@ghostfolio/common/interfaces';
import { utc } from '@date-fns/utc';
import { Injectable, Logger } from '@nestjs/common'; import { Injectable, Logger } from '@nestjs/common';
import { import {
eachDayOfInterval, eachDayOfInterval,
@ -164,11 +165,19 @@ export class ExchangeRateDataService {
} }
public async loadCurrencies() { public async loadCurrencies() {
const startOfUtcDateOfYesterday = getStartOfUtcDateOfYesterday();
const dateStringOfYesterday = format(
startOfUtcDateOfYesterday,
DATE_FORMAT,
{ in: utc }
);
const historicalData = await this.dataProviderService.getHistorical( const historicalData = await this.dataProviderService.getHistorical(
this.currencyPairs, this.currencyPairs,
'day', 'day',
getYesterday(), startOfUtcDateOfYesterday,
getYesterday() startOfUtcDateOfYesterday
); );
const quotes = await this.dataProviderService.getQuotes({ const quotes = await this.dataProviderService.getQuotes({
@ -196,7 +205,7 @@ export class ExchangeRateDataService {
if (isNumber(quote?.marketPrice)) { if (isNumber(quote?.marketPrice)) {
result[symbol] = { result[symbol] = {
[format(getYesterday(), DATE_FORMAT)]: { [dateStringOfYesterday]: {
marketPrice: quote.marketPrice marketPrice: quote.marketPrice
} }
}; };
@ -219,17 +228,19 @@ export class ExchangeRateDataService {
for (const symbol of Object.keys(resultExtended)) { for (const symbol of Object.keys(resultExtended)) {
const [currency1, currency2] = symbol.match(/.{1,3}/g); const [currency1, currency2] = symbol.match(/.{1,3}/g);
const date = format(getYesterday(), DATE_FORMAT);
this.exchangeRates[symbol] = resultExtended[symbol]?.[date]?.marketPrice; this.exchangeRates[symbol] =
resultExtended[symbol]?.[dateStringOfYesterday]?.marketPrice;
if (!this.exchangeRates[symbol]) { if (!this.exchangeRates[symbol]) {
// Not found, calculate indirectly via base currency // Not found, calculate indirectly via base currency
this.exchangeRates[symbol] = this.exchangeRates[symbol] =
resultExtended[`${currency1}${DEFAULT_CURRENCY}`]?.[date] resultExtended[`${currency1}${DEFAULT_CURRENCY}`]?.[
?.marketPrice * dateStringOfYesterday
resultExtended[`${DEFAULT_CURRENCY}${currency2}`]?.[date] ]?.marketPrice *
?.marketPrice; resultExtended[`${DEFAULT_CURRENCY}${currency2}`]?.[
dateStringOfYesterday
]?.marketPrice;
// Calculate the opposite direction // Calculate the opposite direction
this.exchangeRates[`${currency2}${currency1}`] = this.exchangeRates[`${currency2}${currency1}`] =

8
libs/common/src/lib/helper.ts

@ -531,14 +531,6 @@ export function getUtc(aDateString: string) {
); );
} }
export function getYesterday() {
const year = getYear(new Date());
const month = getMonth(new Date());
const day = getDate(new Date());
return subDays(new Date(Date.UTC(year, month, day)), 1);
}
export function hasGhostfolioPrefix(aSymbol: string) { export function hasGhostfolioPrefix(aSymbol: string) {
if (!aSymbol) { if (!aSymbol) {
return false; return false;

Loading…
Cancel
Save