Skip to content

Commit

Permalink
Find dates with non-standard separators (dots in this case)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
benprew committed Mar 27, 2024
1 parent 1f149d0 commit 67599d1
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/reckon/ledger_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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]),
Expand Down
17 changes: 17 additions & 0 deletions spec/reckon/ledger_parser_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 67599d1

Please sign in to comment.