// Number of days between two dates
#include <iostream>
#include <ctime>
struct mydate{
int year, month, day;
mydate(int y=0, int m=0, int d=0):year(y),month(m),day(d){}
void print(){std::cout<<year<<'/'<<month<<'/'<<day;}
};
/// Solution 1 : ctime
double diffday1(const mydate& d2, const mydate& d1)
{
struct std::tm dateBegin = {0,0,0,d1.day,d1.month-1,d1.year-1900};
struct std::tm dateEnd = {0,0,0,d2.day,d2.month-1,d2.year-1900};
std::time_t timeBegin = std::mktime(&dateBegin);
std::time_t timeEnd = std::mktime(&dateEnd);
double difference = 0.0;
if ( timeBegin != (std::time_t)(-1) && timeEnd != (std::time_t)(-1) )
{
difference = std::difftime(timeEnd, timeBegin) / (60 * 60 * 24);
}
return difference;
}
/// Solution 1.1 :
// https://www.geeksforgeeks.org/find-number-of-days-between-two-given-dates/
// To store number of days in all months from January to Dec.
const int monthDays[12] = {31, 28, 31, 30, 31, 30,
31, 31, 30, 31, 30, 31};
// This function counts number of leap years before the given date
int countLeapYears(const mydate& d)
{
int years = d.year;
// Check if the current year needs to be considered
// for the count of leap years or not
if (d.month < 3)
years--;
// An year is a leap year if it is a multiple of 4,
// multiple of 400 and not a multiple of 100.
return years / 4 - years / 100 + years / 400;
}
// This function returns number of days between two given dates
int diffday11(const mydate& d2, const mydate& d1)
{
// COUNT TOTAL NUMBER OF DAYS BEFORE FIRST DATE 'd1'
// initialize count using years and day
long int n1 = d1.year*365 + d1.day;
// Add days for months in given date
for (int i=0; i<d1.month - 1; i++)
n1 += monthDays[i];
// Since every leap year is of 366 days,
// Add a day for every leap year
n1 += countLeapYears(d1);
// SIMILARLY, COUNT TOTAL NUMBER OF DAYS BEFORE 'd2'
long int n2 = d2.year*365 + d2.day;
for (int i=0; i<d2.month - 1; i++)
n2 += monthDays[i];
n2 += countLeapYears(d2);
// return difference between two counts
return (n2 - n1);
}
/// Solution 2 : Rata Die,
// an explanation of the algorithm can be found at <http://mysite.verizon.net/
// aesir_research/date/rata.htm>.
int rdn(int y, int m, int d) { /* Rata Die day one is 0001-01-01 */
if (m < 3) y--, m += 12;
return 365*y + y/4 - y/100 + y/400 + (153*m - 457)/5 + d - 306;
}
double diffday2(const mydate& d2, const mydate& d1)
{
return static_cast<double>( rdn(d2.year, d2.month, d2.day) -
rdn(d1.year, d1.month, d1.day));
}
//
/// Solution 3 : C++14
// The date algorithms that found in this paper:
// http://howardhinnant.github.io/date_algorithms.html
//
// days_from_civil()
// Returns number of days since civil 1970-01-01. Negative values indicate
// days prior to 1970-01-01.
// Preconditions: y-m-d represents a date in the civil (Gregorian) calendar
// m is in [1, 12]
// d is in [1, last_day_of_month(y, m)]
// y is "approximately" in
// [numeric_limits<Int>::min()/366, numeric_limits<Int>::max()/366]
// Exact range of validity is:
// [civil_from_days(numeric_limits<Int>::min()),
// civil_from_days(numeric_limits<Int>::max()-719468)]
#include <limits>
template <class Int>
constexpr
Int
days_from_civil(Int y, unsigned m, unsigned d) noexcept
{
static_assert(std::numeric_limits<unsigned>::digits >= 18,
"This algorithm has not been ported to a 16 bit unsigned integer");
static_assert(std::numeric_limits<Int>::digits >= 20,
"This algorithm has not been ported to a 16 bit signed integer");
y -= m <= 2;
const Int era = (y >= 0 ? y : y-399) / 400;
const unsigned yoe = static_cast<unsigned>(y - era * 400); // [0, 399]
const unsigned doy = (153*(m + (m > 2 ? -3 : 9)) + 2)/5 + d-1; // [0, 365]
const unsigned doe = yoe * 365 + yoe/4 - yoe/100 + doy; // [0, 146096]
return era * 146097 + static_cast<Int>(doe) - 719468;
}
double diffday3(const mydate& d2, const mydate& d1)
{
return static_cast<double>( days_from_civil(d2.year,d2.month,d2.day) -
days_from_civil(d1.year,d1.month,d1.day) );
}
int main()
{
mydate d1 (2019,2,1);
mydate d2 (2020,3,1);
std::cout << "diff1 = "<< diffday1(d2,d1)<<std::endl;
std::cout << "diff1.1 = "<< diffday11(d2,d1)<<std::endl;
std::cout << "diff2 = "<< diffday2(d2,d1)<<std::endl;
std::cout << "diff3 = "<< diffday2(d2,d1)<<std::endl;
return 0;
}
About Online Cpp11 Compiler
Try our Online Cpp11 Compiler (Version GNU GCC v11.3.0) to Edit, Run, and Share your Cpp11 Code directly from your browser. This online development environment provides you the latest version GNU GCC v11.3.0.
How to use Online Cpp11 Compiler?
Write and Execute Code
- Write your program (or, paste it) directly under the "Source Code" tab.
- If you want to save your program, go to the "Project" menu and save it.
- You can directly execute your program without saving it by clicking on on "Execute" button.
User Input
The latest version of Coding Ground allows to provide program input at run time from the termnial window exactly the same way as you run your program at your own computer. So simply run a program and provide your program input (if any) from the terminal window available in the right side.
Online Cpp11 Compiler: Keyboard Shortcuts
The following are the keyword shortcut of this Online Cpp11 Compiler:
Shortcut | Description |
⌘ + Enter | Run the program |
⌘ + S | Save Project (Login Required) |
⇧ + ⌘ + S | Save As Project |
⌘ + P | New Project |
⌘ + G | Share Project |
⌘ + Z | Undo Editing |
⌘ + Y | Redo Editing |
⌘ + A | Select All Text |
⌘ + X | Cut Selected Text |
⌘ + C | Copy Selected Text |
⌘ + V | Paste Copied Text |
⌘ + F | Search Text |
⌘ + ⌥ + F | Replace Text |
Shortcut | Description |
Ctrl + Enter | Run the program |
Ctrl + S | Save Project |
Shift + Ctrl + S | Save As Project |
Ctrl + G | Share Project |
Ctrl + Z | Undo Editing |
Ctrl + Y | Redo Editing |
Ctrl + A | Select All Text |
Ctrl + X | Cut Selected Text |
Ctrl + C | Copy Selected Text |
Ctrl + V | Paste Copied Text |
Ctrl + F | Search Text |
Ctrl + H | Replace Text |
Online Cpp11 Compiler: Save and Share Cpp11 Code (Project)
Save Cpp11 Project Online
You can save your Cpp11 Project with us so that you can access this project later on. To save a project you will need to create a login Id with us. So before you save a project, please create a login Id using a link given at the top right corner of this page.
Share Cpp11 Project Online
You can use this feature to share your Cpp11 Code with your teachers, classmates and colleagues. Just click Share Button and it will create a short link, which can be shared through Email, WhatsApp or even through Social Media. A shared link will be deleted if it has been passive for almost 3 months.
More Features of Online Cpp11 Compiler
- Theme – You can change the current editor's theme from the "Editor Theme" option under "Settings" menu.
- Font Size – You can change the font size of the editor /compiler from from the "Font Size" option under "Settings" menu.
- Tab Size – You can change the tab size from the "Tab Size" option under "Settings" Menu.
- Show/Hide Line Numbers – You can show/hide the line number with the code from the "Show Line Numbers" or "Hide Line Numbers" option under "Settings" Menu.
- And, many more.
Benefits of Using Online Cpp11 Compiler
There are several benefits of using the Online Cpp11 Compiler to run your Cpp11 code:
- Platform independence: You can run your code from any device without taking care of operating systems.
- Convenience: You don't need to install anything for using this.
- No setup required: There is no need for additional setup to run your code.
- Updated version: Our online compiler/editors/terminals are the latest up-to-date.