Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Get past plus future date and get all dates in said range

I am using Howard Hinnant’s awesome date library to do some date calculations.

What I want to do is get Monday’s date wherever I am and then get Sunday’s date wherever I am. Then I want to get all the dates in between said Monday and Sunday. I don’t need time info, just the dates

With this answer, I am able to get Monday’s date as per Howard Hinnant’s answer.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

What I am struggling with is going forward to calculate Sunday’s and thus I’m not sure how to get all the dates between the ranges (Monday – Sunday)

This what I have tried thus far:

#include <chrono>
#include <iostream>

#include <date/date.h>

int main()
{
    auto currentDateYMD = date::year_month_day{ date::floor<date::days>(std::chrono::system_clock::now()) };
    auto todays_date = date::floor<date::days>(std::chrono::system_clock::now());
    std::cout << "Current Date: " << date::format("%F", currentDateYMD) << " \n";

    date::year_month_day mondayDate = todays_date - (date::weekday{ todays_date } - date::Monday);
    std::cout << "Monday: " << date::format("%F", mondayDate) << " \n";

    todays_date = date::floor<date::days>(std::chrono::system_clock::now());

    todays_date += date::days{ date::Sunday.c_encoding() }; // should go forward to Sunday's date
    std::cout << "Sunday: " << date::format("%F", todays_date) << " \n"; // prints today's date

    // calculate range
    for (unsigned int i = 0; i < /*date::Sunday.c_encoding()*/ 7; i++) {
        std::cout << "Future Date: " << date::format("%F", ++todays_date) << " \n";
    }

    std::cin.get();
    return 0;
}

Which outputs this:

Current Date: 2023-09-04
Monday: 2023-09-04
Sunday: 2023-09-04
Future Date: 2023-09-05
Future Date: 2023-09-06
Future Date: 2023-09-07
Future Date: 2023-09-08
Future Date: 2023-09-09
Future Date: 2023-09-10
Future Date: 2023-09-11

I am not sure what I am missing nor am I sure how to "traverse the range" to get dates

I am using C++17 thus I am using the date library and not std::chrono.

>Solution :

todays_date += date::days{ date::Sunday.c_encoding() }; // should go forward to Sunday's date

In general, try to stay away from the member functions .c_encoding() and .iso_encoding() as these are "escape hatches" out of the type-safe chrono system. They are best used when you have to interface with some code outside of the chrono library.

Instead you could just:

todays_date += date::days{ 6 }; // go forward to Sunday's date

Or, if you would rather compute the days{6} you could:

todays_date += date::Sunday - date::Monday; // go forward to Sunday's date

because Sunday is always 6 days after Monday (no matter the encoding under the hood).

And in your loop, you could just test the weekday and exit when you want. Maybe something like:

do
{
    std::cout << "Future Date: " << date::format("%F", ++todays_date) << " \n";
} while (date::weekday{todays_date} != date::Sunday);

Though counting to 7 as you’re doing is fine too. I’m just showing a different possibility.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading