MatzeB / pygnucash

Python code to read gnucash 2.6 sqlite3 files; features gnucash 2 ledger translator.
BSD 2-Clause "Simplified" License
34 stars 9 forks source link

gnucash 3 support (Problem with time data) #11

Closed ksalman closed 6 years ago

ksalman commented 6 years ago

I am trying to convert gnucash file. The file was originally written in gnucash 2.6 but I recently installed gnucash 3 and edited the file, so I am not sure if that makes a difference.

I am using Python 3.6.5 and I get this error

Traceback (most recent call last):
  File "pygnucash/gnucash2ledger.py", line 34, in <module>
    data = gnucash.read_file(sys.argv[1])
  File "/Users/ksalman/pygnucash/gnucash.py", line 179, in read_file
    return read_data(conn)
  File "/Users/ksalman/pygnucash/gnucash.py", line 133, in read_data
    trans.post_date = parse_time(post_date)
  File "/Users/ksalman/pygnucash/gnucash.py", line 124, in parse_time
    return datetime.strptime(time, "%Y%m%d%H%M%S")
  File "/usr/local/opt/pyenv/versions/3.6.5/lib/python3.6/_strptime.py", line 565, in _strptime_datetime
    tt, fraction = _strptime(data_string, format)
  File "/usr/local/opt/pyenv/versions/3.6.5/lib/python3.6/_strptime.py", line 362, in _strptime
    (data_string, format))
ValueError: time data '2017-11-06 10:59:00' does not match format '%Y%m%d%H%M%S'
MatzeB commented 6 years ago

I haven't tested gnucash 3 yet. Looks like they changed their format a bit. You can probably get it working by changing the strptime format string to '%Y-%m-%d %H:%M:%S'.

To fix it in a way that we can upstream here we would also need some way to differentiate between version 2 and version 3 files.

ksalman commented 6 years ago

Actually, I did try changing this line https://github.com/MatzeB/pygnucash/blob/master/gnucash.py#L124 to

return datetime.strptime(time, "%Y-%m-%d %H:%M:%S")

And now I see the following error

Traceback (most recent call last):
  File "pygnucash/gnucash2ledger.py", line 34, in <module>
    data = gnucash.read_file(sys.argv[1])
  File "/Users/ksalman/beancount/pygnucash/gnucash.py", line 179, in read_file
    return read_data(conn)
  File "/Users/ksalman/pygnucash/gnucash.py", line 133, in read_data
    trans.post_date = parse_time(post_date)
  File "/Users/ksalman/pygnucash/gnucash.py", line 125, in parse_time
    return datetime.strptime(time, "%Y-%m-%d %H:%M:%S")
  File "/usr/local/opt/pyenv/versions/3.6.5/lib/python3.6/_strptime.py", line 565, in _strptime_datetime
    tt, fraction = _strptime(data_string, format)
  File "/usr/local/opt/pyenv/versions/3.6.5/lib/python3.6/_strptime.py", line 362, in _strptime
    (data_string, format))
ValueError: time data '20140603070000' does not match format '%Y-%m-%d %H:%M:%S'
MatzeB commented 6 years ago

Ok, I installed gnucash3 and looked at it. Should be fixed with af5971ccae62034356157088ccb421fd01d2c533

ksalman commented 6 years ago

I feel like my change should've worked too, odd. Thank you for the speedy fix!

MatzeB commented 6 years ago

They don't change old records, so the code has to understand the old and the new format. Hence we couldn't just change the format but have to try both.

ksalman commented 6 years ago

Oh I see, that makes sense. Thanks again.