0

I've been looking for a way to calculate a future calendar date in C++ using local time, but so far no dice.

Basically I want to add 3 days (for standard delivery) OR add one day (for overnight delivery) to the CURRENT time (printed in MM-DD-YYYY format and which is retrieved by the computer). How do I do this simply [no algorithms required] in C++?

Output would look like this:

Would you like overnight delivery [Y/N]? Y

Today's Date: 03-25-2017
Your Expected Arrival Date: 03-26-2017

Would you like overnight delivery [N/Y]? N

Today's Date: 03-25-2017
Your Expected Arrival Date: 03-28-2017
  • Have a look at the facilities in `std::chrono`. – πάντα ῥεῖ Feb 19 '17 at 05:02
  • 2
    Welcome to Stack Overflow. Please take the time to read [The Tour](http://stackoverflow.com/tour) and refer to the material from the [Help Center](http://stackoverflow.com/help/asking) what and how you can ask here. – πάντα ῥεῖ Feb 19 '17 at 05:03
  • Possible duplicate of [Algorithm to add or subtract days from a date?](http://stackoverflow.com/questions/2344330/algorithm-to-add-or-subtract-days-from-a-date). Its a C++ question, and it gives you the strategy. It also provides links to the algorithms. – jww Feb 19 '17 at 07:09
  • Those answers do not retrieve the current date from the computer and then give out that date + 1 or 3 days. They just give you a general algorithm for computing the distance between two dates that you need to tell the computer what date it is. I need the computer to give me local time. And then calculate a date in the future. – polymorphism Feb 19 '17 at 08:03
  • @polymorphism: You have not read what πάντα ῥεῖ wrote. std::chrono contains all you need. – Sebastian Mach Feb 19 '17 at 08:07
  • Sebastian did you not read my answer? I clearly understood what pei said and wrote the answer down at the bottom. I'm not contesting what pei said. I'm talking to jww who says it's a duplicate. It's not a duplicate. If it was a duplicate I wouldn't have asked it in the first place. – polymorphism Feb 19 '17 at 08:09
  • @polymorphism: Then you ignored what πάντα ῥεῖ wrote in his other comment: http://stackoverflow.com/tour . As is evident, you have not taken the time to familiarize yourself with StackOverflow yet, i.e. how to reply to certain comments. – Sebastian Mach Feb 19 '17 at 08:10
  • I saw the duplicate response. It's not a duplicate. Maybe to a professional highly paid salaried person it is. But not to me. I asked a question about how to solve that and I got one. That duplicate in no way answers my question in full. I understand there are guidelines, but I saw no question that answered my question in full. pei's response was enough for me to figure it out though. jww's suggestion that this is a duplicate doesn't hold up in my opinion because it doesn't have the breadth to answer this question. – polymorphism Feb 19 '17 at 08:12
  • I dug through every link on that "duplicate" and nowhere did it show clearly and effectively in _one_ line of code how to not only invoke the current time from the machine, but to also add X number of hours to it. Nowhere. Not one link. Not one response. Not one comment. I'm actually at a loss as to how you're seeing a feasible response to my question. I'm utterly baffled. Your insistence on question guidelines aside, is there really a direct answer to my question that is not a high-falutin' theoretical approach? – polymorphism Feb 19 '17 at 08:21

1 Answers1

2

Here's how to do it in less than 8 lines of code. Use hours to specify the exact amount of time in the future you want to go. Formatting is very much gross and unappealing like C. Don't forget to specify each "now"...expedited is different than regular shipping!

chrono::system_clock::time_point now = chrono::system_clock::now();
time_t now_c = chrono::system_clock::to_time_t(now + chrono::hours(24));
cout << "Overnight Shipping Expected Arrival Date: " << put_time(localtime(&now_c), "%F") << '\n';

chrono::system_clock::time_point now2 = chrono::system_clock::now();
time_t now2_c = chrono::system_clock::to_time_t(now2 + chrono::hours(72));
cout << "Standard Shipping Expected Arrival Date: " << put_time(localtime(&now2_c), "%F") << '\n';