Feature/extend supported date formats in activities import (#2362)

* Extend supported date formats in activities import

* Update changelog
pull/2376/head^2
Frane Caleta 8 months ago committed by GitHub
parent 6d9191a46f
commit eb63802d01
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added
- Added support for dates in `DD.MM.YYYY` format in the activities import
- Set up the language localization for Türkçe (`tr`)
## 2.4.0 - 2023-09-19

@ -3,8 +3,8 @@ import { Injectable } from '@angular/core';
import { CreateAccountDto } from '@ghostfolio/api/app/account/create-account.dto';
import { CreateOrderDto } from '@ghostfolio/api/app/order/create-order.dto';
import { Activity } from '@ghostfolio/api/app/order/interfaces/activities.interface';
import { parseDate as parseDateHelper } from '@ghostfolio/common/helper';
import { Account, DataSource, Type } from '@prisma/client';
import { isMatch, parse, parseISO } from 'date-fns';
import { isFinite } from 'lodash';
import { parse as csvToJson } from 'papaparse';
import { EMPTY } from 'rxjs';
@ -219,31 +219,12 @@ export class ImportActivitiesService {
item: any;
}) {
item = this.lowercaseKeys(item);
let date: string;
for (const key of ImportActivitiesService.DATE_KEYS) {
if (item[key]) {
if (isMatch(item[key], 'dd-MM-yyyy') && item[key].length === 10) {
// Check length to only match yyyy (and not yy)
date = parse(item[key], 'dd-MM-yyyy', new Date()).toISOString();
} else if (
isMatch(item[key], 'dd/MM/yyyy') &&
item[key].length === 10
) {
// Check length to only match yyyy (and not yy)
date = parse(item[key], 'dd/MM/yyyy', new Date()).toISOString();
} else if (isMatch(item[key], 'yyyyMMdd') && item[key].length === 8) {
// Check length to only match yyyy (and not yy)
date = parse(item[key], 'yyyyMMdd', new Date()).toISOString();
} else {
try {
date = parseISO(item[key]).toISOString();
} catch {}
}
if (date) {
return date;
}
try {
return parseDateHelper(item[key].toString()).toISOString();
} catch {}
}
}

@ -1,7 +1,15 @@
import * as currencies from '@dinero.js/currencies';
import { DataSource } from '@prisma/client';
import Big from 'big.js';
import { getDate, getMonth, getYear, parse, subDays } from 'date-fns';
import {
getDate,
getMonth,
getYear,
isMatch,
parse,
parseISO,
subDays
} from 'date-fns';
import { de, es, fr, it, nl, pt, tr } from 'date-fns/locale';
import { ghostfolioScraperApiSymbolPrefix, locale } from './config';
@ -284,8 +292,34 @@ export const DATE_FORMAT = 'yyyy-MM-dd';
export const DATE_FORMAT_MONTHLY = 'MMMM yyyy';
export const DATE_FORMAT_YEARLY = 'yyyy';
export function parseDate(date: string) {
return parse(date, DATE_FORMAT, new Date());
export function parseDate(date: string): Date | null {
// Transform 'yyyyMMdd' format to supported format by parse function
if (date.length === 8) {
const match = date.match(/^(\d{4})(\d{2})(\d{2})$/);
if (match) {
const [, year, month, day] = match;
date = `${year}-${month}-${day}`;
}
}
const dateFormat = [
'dd-MM-yyyy',
'dd/MM/yyyy',
'dd.MM.yyyy',
'yyyy-MM-dd',
'yyyy/MM/dd',
'yyyy.MM.dd',
'yyyyMMdd'
].find((format) => {
return isMatch(date, format) && format.length === date.length;
});
if (dateFormat) {
return parse(date, dateFormat, new Date());
}
return parseISO(date);
}
export function prettifySymbol(aSymbol: string): string {

@ -1,5 +1,5 @@
Date,Code,Currency,Price,Quantity,Action,Fee
16-09-2021,MSFT,USD,298.580,5,buy,19.00
17/11/2021,MSFT,USD,0.62,5,dividend,0.00
16/09/2021,MSFT,USD,298.580,5,buy,19.00
01/01/2022,Penthouse Apartment,USD,500000.0,1,item,0.00
06/06/2050,MSFT,USD,0.00,0,buy,0.00
01.01.2022,Penthouse Apartment,USD,500000.0,1,item,0.00
20500606,MSFT,USD,0.00,0,buy,0.00

1 Date Code Currency Price Quantity Action Fee
2 16-09-2021 MSFT USD 298.580 5 buy 19.00
3 17/11/2021 MSFT USD 0.62 5 dividend 0.00
4 16/09/2021 01.01.2022 MSFT Penthouse Apartment USD 298.580 500000.0 5 1 buy item 19.00 0.00
5 01/01/2022 20500606 Penthouse Apartment MSFT USD 500000.0 0.00 1 0 item buy 0.00
06/06/2050 MSFT USD 0.00 0 buy 0.00
Loading…
Cancel
Save