Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

date2-format for date2, CSV import #2193

Open
PSLLSP opened this issue Apr 2, 2024 · 1 comment
Open

date2-format for date2, CSV import #2193

PSLLSP opened this issue Apr 2, 2024 · 1 comment
Labels
A-WISH Some kind of improvement request, hare-brained proposal, or plea. csv The csv file format, csv output format, or generally CSV-related.

Comments

@PSLLSP
Copy link

PSLLSP commented Apr 2, 2024

I miss a way to define date format for date2 for cases when date and date2 fields use different format, I miss command date2-format to define format of date for field date2

Example:

1;"31.12.2023 00:00:00";"Interest";;"31.12.2023";;;;;"330,52";;

date is 31.12.2023 00:00:00, date-format is %d.%m.%Y 00:00:00 or %d.%m.%Y %X (%X is time)...
date2 is 31.12.2023, date-format should be %d.%m.%Y

In this example, date and date2 are the same and date2 could be ignored but I see there is no way to import date2 field when date and date2 use different format of date...

DEMO:

$ cat test-date2.csv
1;"31.12.2023 00:00:00";"Interest";;"31.12.2023";;;;;"330,52";;
$ cat test-date2.csv.rules
separator ;

fields f1, f2, f3, f4, f5, f6, f7, f8, f9, f10, f11, f12
date-format  %d.%m.%Y 00:00:00
#date-format  %d.%m.%Y %X
#date2-format %d.%m.%Y

code         %f1
date         %f2
date2        %f5
description  %f3
amount       %f10

# debug
comment  \n%f1;%f2;%f3;%f4;%f5;%f6;%f7;%f8;%f9;%f10;%f11;%f12

# skip header
if %f1 #
  skip

hledger doesn't like format of date2:

$ hledger --version
hledger 1.32.3, linux-x86_64

$ hledger -f test-date2.csv print
hledger: Error: error: could not parse "31.12.2023" as a date using date format "%d.%m.%Y 00:00:00"
CSV record: "1","31.12.2023 00:00:00","Interest","","31.12.2023","","","","","330,52","",""
the date2 rule is:   %f5
the date-format is: %d.%m.%Y 00:00:00
you may need to change your date2 rule, change your date-format rule, or add a skip rule
for m/d/y or d/m/y dates, use date-format %-m/%-d/%Y or date-format %-d/%-m/%Y

I have found a workaround with if block - hledger 1.32+ required.
Anyway, I believe that date2-format should be added, it makes parsing easier...

WORKAROUND; note I have to define date field, it is mandatory; I see this as a bug:

$ cat test-date2w.csv
1;"31.12.2023 00:00:00";"Interest";;"31.12.2023";;;;;"330,52";;
$ cat test-date2w.csv.rules 
separator ;

fields f1, f2, f3, f4, f5, f6, f7, f8, f9, f10, f11, f12
date-format  %Y-%m-%d

code         %f1
#date         %f2
#date2        %f5
description  %f3
amount       %f10

# default date; date is mandatory; it will be rewritten or an error will be reported when parsing of date fails...
date 9999-99-99

# debug
comment  \n%f1;%f2;%f3;%f4;%f5;%f6;%f7;%f8;%f9;%f10;%f11;%f12

# skip header
if %f1 #
  skip

# parse date
if %f2 (.+)\.(.+)\.(....)
  date \3-\2-\1

# parse date2
if %f5 (.+)\.(.+)\.(....)
  date2 \3-\2-\1

Test it:

$ hledger -f test-date2w.csv print
2023-12-31=2023-12-31 (1) Interest
    ; 1;31.12.2023 00:00:00;Interest;;31.12.2023;;;;;330,52;;
    expenses:unknown          330,52
    income:unknown           -330,52
@simonmichael
Copy link
Owner

If support for multiple date formats in a record is needed, I'd prefer not to tie it to the date2 (secondary date) feature. That's both too limiting and would invest more time in a feature which shouldn't exist.

@simonmichael simonmichael added A-WISH Some kind of improvement request, hare-brained proposal, or plea. csv The csv file format, csv output format, or generally CSV-related. labels May 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-WISH Some kind of improvement request, hare-brained proposal, or plea. csv The csv file format, csv output format, or generally CSV-related.
Projects
None yet
Development

No branches or pull requests

2 participants