0

Is there a way I can make c++ wait until an intended date for when it should proceed with the rest of the code?

I don't want it to wait using something like wait or sleep, I actually want it to just check using an if statement. Extremely bad pseudocode example:

if current_date == intended_date
    cout << "Happy Birthday";
end 

I want the program to only say Happy Birthday if the condition of it being a birthday is met. I know you can grab current date using std::chrono::system_clock::now(). However, I don't know how to format the intended_date so that it can check it against the current date.

Sinpe
  • 13
  • 1
  • 4
  • 2
    `=` is *assignment*, `==` is *comparison*. Don't forget this, it's **important**. A little slip like this has created some huge bugs, so always pay careful attention. What sort of date are you using? You need to make this pseudocode actual code before we can answer. – tadman Oct 03 '18 at 18:46
  • C++20 gives you [`year_month_day`](https://en.cppreference.com/w/cpp/chrono/year_month_day) which is pretty handy. – NathanOliver Oct 03 '18 at 18:48
  • 2
    And since C++20's not not ratified yet and compiler support is at best experimental, perhaps [Howard Hinnant's date library](https://github.com/HowardHinnant/date) may help. – user4581301 Oct 03 '18 at 18:49
  • FYI, it is a poor approach **not** to sleep or wait. If you spin inside a loop, constantly checking, you'll tie up a cpu. As yet another approach, you could use crontab to do something at a particular time, at least on Unix systems. – Leonard Oct 03 '18 at 18:55
  • @tadman My apologies, forgot that. I'm using standard dd/mm/yy format. I don't know if the code would accept something like intended_date = "10/03/2018" ; if std::chrono::system_clock::now(). == intended_date – Sinpe Oct 03 '18 at 19:00
  • @Leonard I'm aware of the benefits of using sleep or wait, but in this particular circumstance, it doesn't matter too much. It's not going to run for very long or even more than once. – Sinpe Oct 03 '18 at 19:02
  • https://stackoverflow.com/questions/12835577/how-to-convert-stdchronotime-point-to-calendar-datetime-string-with-fraction gives you a hint – Chad Oct 03 '18 at 19:15
  • @Sinpe There's nothing "standard" about `dd/mm/yyyy`, as if anything that's an infuriatingly ambiguous format that conflicts with the equally popular `mm/dd/yyyy`. [ISO-8601 (`YYYY-MM-DD`)](https://en.wikipedia.org/wiki/ISO_8601) is the only actual standard. In any case you need a date parser, which is probably an external library. – tadman Oct 03 '18 at 19:19

1 Answers1

0

I think that the standard modules chrono and iomanip offer you what you're looking for:

#include <chrono>
#include <iostream>
#include <iomanip>
#include <sstream>
#include <string>
#include <list>

int main()
{
    auto currentTime = std::chrono::system_clock::to_time_t(
        std::chrono::system_clock::now()
    );

    std::stringstream timeStream;
    timeStream << std::put_time(std::localtime(&currentTime), "%m-%d");

    const std::list<std::string> birthdays = {"12-08", "10-03"};
    for (auto& date : birthdays)
    {
        if(timeStream.str() == date)
        {
            std::cout << "Happy birthday!!\n";
        }
        else
        {
            std::cout << "Try another day!!\n";
        }
    }
    return 0;
}
Paolo Irrera
  • 253
  • 1
  • 10