Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"every Nth day" periodic rule violating start date #2218

Closed
simonmichael opened this issue Aug 24, 2024 · 6 comments
Closed

"every Nth day" periodic rule violating start date #2218

simonmichael opened this issue Aug 24, 2024 · 6 comments
Labels
A-BUG Something wrong, confusing or sub-standard in the software, docs, or user experience. journal The journal file format, and its features.

Comments

@simonmichael
Copy link
Owner

Reported on the mail list by LucaBH. Here's a small repro:

~ every 31st day from 2024-07 to 2024-09
  (a)  1
$ hledger -f a.j print --forecast=2024
2024-06-30       ;  <- we don't expect this one to be generated
    (a)               1

2024-07-31
    (a)               1

2024-08-31
    (a)               1

@simonmichael simonmichael added A-BUG Something wrong, confusing or sub-standard in the software, docs, or user experience. journal The journal file format, and its features. labels Aug 24, 2024
@adept
Copy link
Collaborator

adept commented Aug 24, 2024

This is caused by

splitSpan _ (DayOfMonth dom)  ds = splitspan (nthdayofmonthcontaining dom) (addGregorianMonthsToMonthday dom) 1 ds

Namely, nthdayofmonthcontaining shifts start date back to 30th:

nthdayofmonthcontaining :: MonthDay -> Day -> Day
nthdayofmonthcontaining mdy date
  -- PARTIAL:
  | not (validDay mdy) = error' $ "nthdayofmonthcontaining: invalid day "  ++show mdy
  | nthOfSameMonth <= date = nthOfSameMonth
  | otherwise = nthOfPrevMonth
  where nthOfSameMonth = nthdayofmonth mdy s
        nthOfPrevMonth = nthdayofmonth mdy $ prevmonth s
        s = startofmonth date

Note that splitSpan explicitly calls for this behavior, but I don't know why this behavior is desired:

-- | Split a DateSpan into consecutive exact spans of the specified Interval.
-- If the first argument is true and the interval is Weeks, Months, Quarters or Years,
-- the start date will be adjusted backward if needed to nearest natural interval boundary
-- (a monday, first of month, first of quarter or first of year).

@adept
Copy link
Collaborator

adept commented Aug 24, 2024

The fix, i think, would be to filter the output of splitSpan here:

alltxnspans = splitSpan adjust ptinterval span'

and reject first returned subspans if they are from before the start of ptiterval

@adept
Copy link
Collaborator

adept commented Aug 24, 2024

Assuming that there is no need/desire to change the behavior of splitSpan, I pushed a simple fix to periodic transaction generator (that is described above)

@simonmichael
Copy link
Owner Author

Thanks! I imagine the splitSpan behaviour was to support https://hledger.org/dev/hledger.html#date-adjustments . But that shifted a bit in the last year. Your fix may be all that's needed, I'll test a bit more as well.

@simonmichael
Copy link
Owner Author

simonmichael commented Sep 4, 2024

I've pushed #2221, which fixes the start date in these cases:

  • every Nth day of month from DATE with periodic transactions
  • every M/D from DATE
  • every M/D from DATE with periodic transactions
  • every Nth WEEKDAY from DATE

and brings new docs and tests:

@simonmichael
Copy link
Owner Author

Fixed by #2221.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-BUG Something wrong, confusing or sub-standard in the software, docs, or user experience. journal The journal file format, and its features.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants