#include <iostream>
#include <ctime>
double difference_between_two_dates_in_seconds(std::tm date1, std::tm date2) {
// Convert tm structures to time_t
std::time_t time1 = std::mktime(&date1);
std::time_t time2 = std::mktime(&date2);
// Calculate the difference in seconds
return std::difftime(time2, time1);
}
int main() {
// Define two tm structures for the dates
std::tm date1 = {0};
std::tm date2 = {0};
// Set the first date (e.g., 2025-01-01 00:00:00)
date1.tm_year = 2025 - 1900; // Year since 1900
date1.tm_mon = 0; // Month (0-11, where 0 = January)
date1.tm_mday = 12; // Day of the month (1-31)
date1.tm_hour = 0;
date1.tm_min = 0;
date1.tm_sec = 0;
// Set the second date (e.g., 2025-01-13 12:00:00)
date2.tm_year = 2025 - 1900;
date2.tm_mon = 0;
date2.tm_mday = 13;
date2.tm_hour = 0;
date2.tm_min = 0;
date2.tm_sec = 0;
std::cout << "Difference in seconds: " << difference_between_two_dates_in_seconds(date1, date2);
}
/*
run:
Difference in seconds: 86400
*/