simonmichael / hledger

Robust, fast, intuitive plain text accounting tool with CLI, TUI and web interfaces.
https://hledger.org
GNU General Public License v3.0
2.85k stars 307 forks source link

date2-format for date2, CSV import #2193

Open PSLLSP opened 3 months ago

PSLLSP commented 3 months ago

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 commented 3 months ago

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.