#include <iostream>
#include <chrono>
#include <iomanip>
#include <ctime>
// C++11/14/17
/**
* Adds a specified number of weeks to the current system date using legacy C-style structures.
*
* @param weeks The number of weeks to add.
* @param buffer Character array buffer to store the resulting date string.
* @param bufferSize The size of the buffer.
*/
void addWeeks(int weeks, char* buffer, size_t bufferSize)
{
// Get current system time as a time_t epoch value
auto now = std::chrono::system_clock::now();
std::time_t now_c = std::chrono::system_clock::to_time_t(now);
// Convert to local time structure
std::tm* time_info = std::localtime(&now_c);
// Add weeks by adjusting days directly (1 week = 7 days)
time_info->tm_mday += weeks * 7;
// mktime automatically normalizes the date (handles month/year overflows)
std::time_t future_c = std::mktime(time_info);
// Format output as YYYY-MM-DD
std::strftime(buffer, bufferSize, "%Y-%m-%d", std::localtime(&future_c));
}
int main()
{
// Define the number of weeks to add
int weeks_to_add = 4;
// Allocate a buffer large enough for "YYYY-MM-DD" plus the null-terminator (\0)
char date_buffer[11];
// Call the function to compute the future date
addWeeks(weeks_to_add, date_buffer, sizeof(date_buffer));
// Output the results to the console
std::cout << "Adding " << weeks_to_add << " weeks to the current date gives: "
<< date_buffer << "\n";
}
/*
run:
Adding 4 weeks to the current date gives: 2026-12-05
*/