Reputation: 132
Is there standard c++ time class which has methods to subtract dates for example: I want to subtract 10 minutes from date like this(Jan 1 Mon 2014 00:05) and get date like that(Dec 31 Sun 2013 23:55)
It would be awesome if there was such function for "struct tm" type
Upvotes: 1
Views: 1264
Reputation: 136
If you can use C++11 you can substract and add dates easy:
#include <iostream>
#include <chrono>
#include <ctime>
#include <ratio>
using namespace std;
int main ()
{
chrono::system_clock::time_point today = chrono::system_clock::now();
chrono::system_clock::duration dtn (chrono::duration<int,ratio<60,1>>(10));
time_t t = chrono::system_clock::to_time_t(today-dtn);
cout << ctime(&t) << endl;
return 0;
}
Upvotes: 0
Reputation: 14392
cppreference shows enough functions (mktime, localtime) to convert struct tm
with time_t
. Calculations are obvious with time_t
because it is an integral type traditionally representing the number of seconds since newyear 1970.
Upvotes: 2