Skip to content

Instantly share code, notes, and snippets.

Created October 20, 2021 04:07
  • Star 6 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save dallaslu/431d23f4174ed74874e803dc5eca7a3c to your computer and use it in GitHub Desktop.
Sort Beancount Entries by meta['time']
"""Sort Beancount Entries by meta['time']
Although beancount says that time is meaningless, there are situations
where an account that should not be negative may have a negative balance
at some point because transfers that occurred on the same day are
scheduled later and spending records are scheduled earlier.
This is not a big deal, but it is a bit odd.
2021-10-01 balance Assets:Cash 500 USD
2021-10-01 *
Assets:Cash -800 USD
2021-10-01 * "trans"
Assets:Cash 1000 USD
Then the balance of Assets:Cash after first transaction will be -300 USD.
Now you can add a meta_data named 'time' to fix it:
2021-10-01 *
time: "18:18:18"
Assets:Cash -800 USD
2021-10-01 * "trans"
time: "12:12:12"
Assets:Cash 1000 USD
Then the cash balance changes will be: 500 -> 1500 -> 700
Just active as plugin in <your-main>.bean file.
from beancount.core import data
import time
def __parse_time(time_str):
return time.strptime(time_str, '%H:%M:%S')
__original_entry_sortkey = data.entry_sortkey
__default_time_value = __parse_time('00:00:01')
def __entry_sortkey(entry):
original_result = __original_entry_sortkey(entry)
time_meta = entry.meta.get("time", None) if entry.meta is not None else None
time_value = __parse_time(time_meta) if time_meta is not None else __default_time_value
return original_result[0], original_result[1], time_value, original_result[2]
def __posting_sortkey(entry):
if isinstance(entry, data.TxnPosting):
entry = entry.txn
return __entry_sortkey(entry)
data.entry_sortkey = __entry_sortkey
data.posting_sortkey = __posting_sortkey
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment