HowardHinnant / date

A date and time library based on the C++11/14/17 <chrono> header
Other
3.08k stars 669 forks source link

Plans for a last_day_of_month member function? #748

Open QuantDevHacks opened 1 year ago

QuantDevHacks commented 1 year ago

In your documentation on date algorithms, there is discussion of an efficient last_day_of_month algorithm. Are there any plans to add this as a member function on the year_month_day class? I see you have implemented as_leap as a member function, but as far as I can tell, there is no last_day_of_month function.

Thanks in advance.

HowardHinnant commented 1 year ago

It is in there, here: https://github.com/HowardHinnant/date/blob/master/include/date/date.h#L2737-L2748

The syntax for accessing it is:

auto d = (September/last/2022).day();  // 30_d

Or given a year_month_day, ymd:

auto ymd = September/5/2022;
ymd = ymd.year()/ymd.month()/last;
auto d = ymd.day();  // 30_d

I.e. anywhere you can put a "day specifier", you can substitute last to mean the last day of the month. And get the actual value out by converting it to a day.

QuantDevHacks commented 1 year ago

Thanks -- I was aware of most of that, except for the day() member function on year_month_day_last. What I was hoping to find out is if you intend to implement the last_day_of_month algorithm as described in your documentation cited in the original question. I see you have implemented is_leap() as a member function, but there is nothing like a last_day_of_month() or is_last_day_of_month() member function.

For now, I have naively implemented my own (shown as a lambda here):

auto is_eom = [](const date::year_month_day& ymd) -> bool
{
    if (static_cast<unsigned>(ymd.day()) < 28)
    {
        return false;
    }
    else
    {
        return ymd == ymd.year() / ymd.month() / date::last;
    }
};

It isn't as robust as what you have, but before I go any further, I just wanted to check if you intend to add your algorithm as a member function.

Thanks in advance.

HowardHinnant commented 1 year ago

No, I don't intend to expand the year_month_day API any more.