From 67599d118bc7e54d8c22ca92b745503549dd461e Mon Sep 17 00:00:00 2001 From: Ben Prew Date: Tue, 26 Mar 2024 22:17:06 -0700 Subject: [PATCH] Find dates with non-standard separators (dots in this case) The ledger spec on the format of the DATE is sparse other than it has to start with numbers. Previously reckon expected dates to be separated by / or -, but dates can also be separated by . too. --- lib/reckon/ledger_parser.rb | 2 +- spec/reckon/ledger_parser_spec.rb | 17 +++++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/lib/reckon/ledger_parser.rb b/lib/reckon/ledger_parser.rb index dd3bb26..97504d3 100644 --- a/lib/reckon/ledger_parser.rb +++ b/lib/reckon/ledger_parser.rb @@ -130,7 +130,7 @@ def parse(ledger) next if entry =~ /^\s*[#{comment_chars}]/ # (date, type, code, description), type and code are optional - if (m = entry.match(%r{^(\d+[\d/-]+)\s+([*!])?\s*(\([^)]+\))?\s*(.*)$})) + if (m = entry.match(%r{^(\d+[^\s]+)\s+([*!])?\s*(\([^)]+\))?\s*(.*)$})) add_entry(entries, new_entry) new_entry = { date: try_parse_date(m[1]), diff --git a/spec/reckon/ledger_parser_spec.rb b/spec/reckon/ledger_parser_spec.rb index 6dd648e..c6199ab 100644 --- a/spec/reckon/ledger_parser_spec.rb +++ b/spec/reckon/ledger_parser_spec.rb @@ -132,6 +132,23 @@ @entries.last[:accounts].last[:name].should == "Assets:Bank:Checking" @entries.last[:accounts].last[:amount].should == -20.24 end + + it "should parse dot-separated dates" do + ledger = <<~HERE + 2024.03.12 groceries; 11223344556; 32095205940 + assets:bank:spending 530.00 NOK + assets:bank:co:groceries + + 2024.03.13 autosave; 11223344555; 11223344556 + assets:bank:savings + assets:bank:spending -10.00 NOK + HERE + options = { ledger_date_format: '%Y.%m.%d' } + entries = Reckon::LedgerParser.new(options).parse(StringIO.new(ledger)) + expect(entries.first[:date]).to eq(Date.new(2024, 3, 12)) + expect(entries.last[:date]).to eq(Date.new(2024, 3, 13)) + expect(entries.length).to eq(2) + end end describe "balance" do