1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
|
#include <iostream>
#include <sstream>
#include <ctime>
using namespace std;
struct Date { int d, m, y; };
const int monthDays[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int countLeapYears(Date d) {
int years = d.y;
if (d.m <= 2) --years;
return years / 4 - years / 100 + years / 400;
}
int getDifference(Date dt1, Date dt2) {
long int n1 = dt1.y * 365 + dt1.d;
for (int i = 0; i < dt1.m - 1; ++i) n1 += monthDays[i];
n1 += countLeapYears(dt1);
long int n2 = dt2.y * 365 + dt2.d;
for (int i = 0; i < dt2.m - 1; ++i) n2 += monthDays[i];
n2 += countLeapYears(dt2);
return n2 - n1;
}
int to_month_num(string& mon) {
const char *months[]
{"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};
for (size_t i = 0; i < sizeof months / sizeof months[0]; ++i)
if (mon == months[i]) return i + 1;
return 0;
}
int main() {
time_t ttime = time(0);
const char* dt = ctime(&ttime);
cout << "The current local date and time is: " << dt << endl;
string month, temp;
int d, m, y;
istringstream iss(dt);
iss >> temp >> month >> d >> temp >> y;
m = to_month_num(month);
Date dt1 = {d, m, y};
Date dt2 = {31, 2, 2020};
cout << "You have " << getDifference(dt1, dt2) << " days remaining\n";
}
|