Periodic transactions aligned to started date

569 views
Skip to first unread message

Scott Payne

unread,
Nov 16, 2017, 4:40:24 PM11/16/17
to Ledger
I've been doing some budgeting and want to sit down once a fortnight to review whether I'm on track or not. So I have budgeted some expenses on a fortnightly basis but there are some things that have to come out monthly and there are even some every six weeks. I do my budget on a Thursday night so I'd really like to have my fortnightly budgets start on Thursday.

I've been trying to use periodic transactions in ledger and it's not working the way I would like. I want to say e.g. "every 2 weeks from 2017-11-02" and have the budget period go from 2 Nov to 15 Nov, then 16 Nov to 29 Nov etc. But the behaviour I'm seeing in ledger is the period goes from the 2nd to the 4th, and then starts two weeks from the 5th.


$ ledger period 'every 2 weeks from 2017-11-02'
--- Period expression tokens ---
TOK_EVERY: every
TOK_INT: 2
TOK_WEEKS: weeks
TOK_SINCE: since
TOK_DATE:  year 2017 month Nov day 2
END_REACHED: <EOF>

--- Before stabilization ---
   range: from year 2017 month Nov day 2
duration: 2 weeks

--- After stabilization ---
   range: from year 2017 month Nov day 2
   start: 17-Nov-02
duration: 2 weeks

--- Sample dates in range (max. 20) ---
 1: 17-Nov-02 -- 17-Nov-04
 2: 17-Nov-05 -- 17-Nov-18
 3: 17-Nov-19 -- 17-Dec-02
 4: 17-Dec-03 -- 17-Dec-16
 5: 17-Dec-17 -- 17-Dec-30
 6: 17-Dec-31 -- 18-Jan-13
 7: 18-Jan-14 -- 18-Jan-27
 8: 18-Jan-28 -- 18-Feb-10
 9: 18-Feb-11 -- 18-Feb-24
10: 18-Feb-25 -- 18-Mar-10
11: 18-Mar-11 -- 18-Mar-24
12: 18-Mar-25 -- 18-Apr-07
13: 18-Apr-08 -- 18-Apr-21
14: 18-Apr-22 -- 18-May-05
15: 18-May-06 -- 18-May-19
16: 18-May-20 -- 18-Jun-02
17: 18-Jun-03 -- 18-Jun-16
18: 18-Jun-17 -- 18-Jun-30
19: 18-Jul-01 -- 18-Jul-14
20: 18-Jul-15 -- 18-Jul-28

I've tried "--start-of-week Thursday" but it has no effect.
 
Is it possible to achieve what I want to achieve using ledger's periodic transactions, or do I need to go another way?

John Wiegley

unread,
Nov 16, 2017, 6:05:21 PM11/16/17
to Scott Payne, Ledger
>>>>> "SP" == Scott Payne <scott...@gmail.com> writes:

SP> I've tried "--start-of-week Thursday" but it has no effect.

Does it have no effect in your report too, or just the period command?

John

Scott Payne

unread,
Nov 16, 2017, 7:21:18 PM11/16/17
to Ledger
It doesn't seem to have an effect in either the period or register commands.

Here's my test ledger file:

~ every 2 weeks from 2017/11/02
    Expenses:Biweekly       $20
    Assets

2017-11-03 Some fortnightly thing
    Expenses:Biweekly   $20
    Assets:A Thing

2017-11-16 Corner case
    Expenses:Biweekly   $20
    Assets:Another thing

Here's the output of running the register report:

$ ledger -f budget-test.ledger reg --budget --start-of-week Thursday  --no-color
2017-11-02 Budget transaction              Expenses:Biweekly                                  $-20               $-20
2017-11-02 Budget transaction              Assets                                              $20                  0
2017-11-03 Some fortnightly thing          Expenses:Biweekly                                   $20                $20
                                           Assets                                             $-20                  0
2017-11-05 Budget transaction              Expenses:Biweekly                                  $-20               $-20
2017-11-05 Budget transaction              Assets                                              $20                  0
2017-11-16 Corner case                     Expenses:Biweekly                                   $20                $20
                                           Assets                                             $-20                  0

Ledger version is 3.1.1-20160111 FWIW. 

Scott Payne

unread,
Nov 16, 2017, 7:30:42 PM11/16/17
to Ledger
OK, I've just tried using the --start-of-week 4 instead of Thursday and I get the result I expect:
 
 
$ ledger -f budget-test-weekly.ledger reg --budget --start-of-week 4  --no-color 
2017-11-02 Budget transaction              Expenses:Biweekly                                  $-20               $-20
2017-11-02 Budget transaction              Assets                                              $20                  0
2017-11-03 Some fortnightly thing          Expenses:Biweekly                                   $20                $20
                                           Assets                                             $-20                  0
2017-11-16 Budget transaction              Expenses:Biweekly                                  $-20               $-20
2017-11-16 Budget transaction              Assets                                              $20                  0
2017-11-16 Corner case                     Expenses:Biweekly                                   $20                $20
                                           Assets                                             $-20                  0

I can live with that. I tried using the day name as that was suggested by the man page. Should I raise a documentation bug or is it a bug in the implementation?


Yuri Khan

unread,
Nov 17, 2017, 1:22:50 AM11/17/17
to ledge...@googlegroups.com
On Fri, Nov 17, 2017 at 7:30 AM, Scott Payne <scott...@gmail.com> wrote:

> OK, I've just tried using the --start-of-week 4 instead of Thursday and I
> get the result I expect

That does not scale well. For one, do you have to remember to pass
this option every time you run ledger? For another, what if you need
two periodic transactions with different days? Also, it may work for
week days but obviously not for month days (--start-of-month 14?)

John Wiegley

unread,
Nov 17, 2017, 12:12:40 PM11/17/17
to Scott Payne, Ledger
>>>>> "SP" == Scott Payne <scott...@gmail.com> writes:

SP> I can live with that. I tried using the day name as that was suggested by
SP> the man page. Should I raise a documentation bug or is it a bug in the
SP> implementation?

Looking at the code, I see this:

optional<date_time::weekdays> string_to_day_of_week(const std::string& str)
{
if (str == _("sun") || str == _("sunday") || str == "0")
return gregorian::Sunday;
else if (str == _("mon") || str == _("monday") || str == "1")
return gregorian::Monday;
else if (str == _("tue") || str == _("tuesday") || str == "2")
return gregorian::Tuesday;
else if (str == _("wed") || str == _("wednesday") || str == "3")
return gregorian::Wednesday;
else if (str == _("thu") || str == _("thursday") || str == "4")
return gregorian::Thursday;
else if (str == _("fri") || str == _("friday") || str == "5")
return gregorian::Friday;
else if (str == _("sat") || str == _("saturday") || str == "6")
return gregorian::Saturday;
else
return none;
}

Try lowercase?

John

Scott Payne

unread,
Nov 17, 2017, 10:00:50 PM11/17/17
to Ledger
D'oh, I should have tried that. Yes, that works. The man page I looked at does give the example "Mon" though which is probably what prompted me to try the capitalised day name.

Thanks very much John.

Scott Payne

unread,
Nov 17, 2017, 10:19:40 PM11/17/17
to Ledger
I agree with the points you've made Yuri, for now though the --start-of-week usage satisfies my use case.

WRT remembering to pass the option I'm using direnv in my setup so I can set the LEDGER_START_OF_WEEK environment variable to keep it all automated.

I'm still just playing with the periodic transactions for now so I'm not sure if I'm going to clash with the current implementation soon or not. Given the way I'm reviewing my budget your point about monthly transactions might be valid for me sooner rather than later (e.g. I might want to see which monthly expenses are due in the next fortnight) but we'll see how we go.

Yuri Khan

unread,
Nov 18, 2017, 4:26:19 AM11/18/17
to ledge...@googlegroups.com, Scott Payne
On Fri, Nov 17, 2017 at 7:38 AM, John Wiegley <jwie...@gmail.com> wrote:

> if (str == _("sun") || str == _("sunday") || str == "0")
> return gregorian::Sunday;

> Try lowercase?

I also notice the "sun" and "sunday" strings are marked for
translation. A script using these (as opposed to "0") will probably
not be portable across locales.

Rob Napier

unread,
Jan 20, 2018, 11:54:22 AM1/20/18
to Ledger
On Friday, November 17, 2017 at 10:19:40 PM UTC-5, Scott Payne wrote:

I'm still just playing with the periodic transactions for now so I'm not sure if I'm going to clash with the current implementation soon or not. Given the way I'm reviewing my budget your point about monthly transactions might be valid for me sooner rather than later (e.g. I might want to see which monthly expenses are due in the next fortnight) but we'll see how we go.

How has this turned out? I've been very interested in the forecasting feature to keep my forecast checking balance between a low- and high-water mark, but the periodic implementation seems to fight this. All monthly things seem to be put on the first of the month, and yearly expenses are all but useless because I think they're all pushed to Jan 1.

Is there a way to day-by-day forecasting with ledger? What's the intent of the --forecast option? 

Thanks,
-Rob

Scott Payne

unread,
Apr 7, 2018, 7:09:13 PM4/7/18
to ledge...@googlegroups.com
Hi Rob,

Apologies for not replying earlier, got behind in my email again.

On Sun, Jan 21, 2018 at 3:54 AM Rob Napier <robn...@gmail.com> wrote:
On Friday, November 17, 2017 at 10:19:40 PM UTC-5, Scott Payne wrote:

I'm still just playing with the periodic transactions for now so I'm not sure if I'm going to clash with the current implementation soon or not. Given the way I'm reviewing my budget your point about monthly transactions might be valid for me sooner rather than later (e.g. I might want to see which monthly expenses are due in the next fortnight) but we'll see how we go.

How has this turned out? I've been very interested in the forecasting feature to keep my forecast checking balance between a low- and high-water mark, but the periodic implementation seems to fight this. All monthly things seem to be put on the first of the month, and yearly expenses are all but useless because I think they're all pushed to Jan 1.

I ended up giving up on periodic transactions, I couldn't make it work the way I needed to, for the same reason. What I've done instead is export to a CSV then import that into a Google Sheets template, with some app script to automate matching up accounts with budgeted amounts. Our household was already using Sheets for budgeting so it doing it that way fit our current workflow anyway.

If you did get something working I'd be interested to hear what you did, it kind of sucks having to maintain two tools.

Cheers,
Scott

matt.gr...@gmail.com

unread,
Aug 15, 2018, 6:40:02 AM8/15/18
to Ledger
Hi All,

I think this is bug #532 on github page https://github.com/ledger/ledger-plan/blob/master/TODO. Having said that, the bug isn't clear that the problem is that it resets the periodic entries to align to the 'start of period'. Ie Sunday for weekly or biweekly (unless you change the default first day of the week) or the first day of the month for monthly etc.

For my use case, resetting the day of the week is not a solution - I am using multiple budget line items starting on multiple different days of the week. At the moment I have all the budget transactions listed out in a different account tree (Budget:Expenses:...), using automated transactions to replicate expense postings into the budget tree. Works well, but periodic transactions would be better - a lot easier on entry (I have written scripts to do it, but it gets messy).

I haven't got much experience in Cpp programming but interested in learning. Can anyone give me a hint as to where to look to try to fix this one myself? Would love to help out.

Thanks,

Matt

John Wiegley

unread,
Aug 19, 2018, 10:24:26 PM8/19/18
to matt.gr...@gmail.com, Ledger
>>>>> "mg" == matt graham2001 <matt.gr...@gmail.com> writes:

mg> I haven't got much experience in Cpp programming but interested in
mg> learning. Can anyone give me a hint as to where to look to try to fix this
mg> one myself? Would love to help out.

I believe you can use the "ledger period <period>" command to see what Ledger
thinks the period is/means, and then check in the code where those debug
messages are coming from to discover where the calculation is going wrong.

John
Reply all
Reply to author
Forward
0 new messages