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

Start of month functions #2034

Merged
merged 4 commits into from
Jul 29, 2024
Merged

Conversation

igitur
Copy link
Contributor

@igitur igitur commented Jul 24, 2024

I noticed the comment in Luigi's blog about the lack of start-of-month functions. So here they are.

@coveralls
Copy link

coveralls commented Jul 24, 2024

Coverage Status

coverage: 72.644% (+0.003%) from 72.641%
when pulling 0d13eb2 on igitur:start-of-month-functions
into 3d45b9a on lballabio:master.

@lballabio
Copy link
Owner

Thanks—it looks like I should start dropping more hints in my posts 😄

@@ -240,6 +246,14 @@ namespace QuantLib {
return impl_->isBusinessDay(_d);
}

inline bool Calendar::isStartOfMonth(const Date& d) const {
return (d.month() != adjust(d-1, Preceding).month());
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if

        return d == startOfMonth(d);

would be more or less efficient? It would be more clear for sure, and we wouldn't have two different implementations for two very related methods. The same goes for isEndOfMonth.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could do that, but would the test at

if (!c.isStartOfMonth(som))
then really test anything?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would still test consistency of the two methods, as if we were unaware of the implementation (which may change).

@lballabio lballabio added this to the Release 1.36 milestone Jul 29, 2024
inline bool Calendar::isEndOfMonth(const Date& d) const {
return (d.month() != adjust(d+1).month());
return d == endOfMonth(d);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, no, my bad—the previous implementation returns true if a date is the last business day of its month but also if it's a holiday after the last business day of the month. The check is used with this semantics while building schedules, which is why a few tests are failing. We should revert and comment it properly.

I'm not sure what to do with isStartOfMonth. I'm tempted to leave it as it is, even though it wouldn't be consistent with isEndOfMonth. Any thoughts?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I see what you mean. But the same holds for the start of month too? E.g. both 1 May 2024 and 2 May 2024 should return true for isStartOfMonth() if you observe International Workers Day?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's what I'm not sure about. Ideally I'd return true only for the 2nd, but then it would be inconsistent with isEndOfMonth. I guess we can keep them consistent and document it.

In other news, we can keep the simpler implementation of isEndOfMonth if we write it as

return d >= endOfMonth(d);

i.e. with a >= instead of ==.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I definitely prefer them consistent. How about we add overload functions if we strictly want to test only for the first/last business day of the month?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If one wants a strict test they can write d == endOfMonth(d).

@lballabio lballabio merged commit 826075f into lballabio:master Jul 29, 2024
41 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants